| 
									
										
										
										
											2015-05-17 00:27:59 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-04-05 20:39:23 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2015-05-17 00:27:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-05 20:39:23 +08:00
										 |  |  | const path = require("path"); | 
					
						
							| 
									
										
										
										
											2017-04-05 21:03:47 +08:00
										 |  |  | const asyncLib = require("async"); | 
					
						
							| 
									
										
										
										
											2017-04-05 20:39:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class LibManifestPlugin { | 
					
						
							|  |  |  | 	constructor(options) { | 
					
						
							|  |  |  | 		this.options = options; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2017-12-08 15:50:14 +08:00
										 |  |  | 		compiler.hooks.emit.tapAsync("LibManifestPlugin", (compilation, callback) => { | 
					
						
							| 
									
										
										
										
											2017-04-05 21:03:47 +08:00
										 |  |  | 			asyncLib.forEach(compilation.chunks, (chunk, callback) => { | 
					
						
							| 
									
										
										
										
											2018-01-22 19:15:58 +08:00
										 |  |  | 				if(!chunk.isOnlyInitial()) { | 
					
						
							| 
									
										
										
										
											2017-04-05 20:39:23 +08:00
										 |  |  | 					callback(); | 
					
						
							|  |  |  | 					return; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				const targetPath = compilation.getPath(this.options.path, { | 
					
						
							|  |  |  | 					hash: compilation.hash, | 
					
						
							|  |  |  | 					chunk | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 				const name = this.options.name && compilation.getPath(this.options.name, { | 
					
						
							|  |  |  | 					hash: compilation.hash, | 
					
						
							|  |  |  | 					chunk | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 				const manifest = { | 
					
						
							|  |  |  | 					name, | 
					
						
							|  |  |  | 					type: this.options.type, | 
					
						
							| 
									
										
										
										
											2018-01-24 03:08:32 +08:00
										 |  |  | 					content: Array.from(chunk.modulesIterable, module => { | 
					
						
							| 
									
										
										
										
											2017-04-05 20:39:23 +08:00
										 |  |  | 						if(module.libIdent) { | 
					
						
							|  |  |  | 							const ident = module.libIdent({ | 
					
						
							|  |  |  | 								context: this.options.context || compiler.options.context | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 							if(ident) { | 
					
						
							| 
									
										
										
										
											2017-04-21 16:05:56 +08:00
										 |  |  | 								return { | 
					
						
							|  |  |  | 									ident, | 
					
						
							|  |  |  | 									data: { | 
					
						
							|  |  |  | 										id: module.id, | 
					
						
							| 
									
										
										
										
											2017-12-07 00:39:42 +08:00
										 |  |  | 										buildMeta: module.buildMeta | 
					
						
							| 
									
										
										
										
											2017-04-21 16:05:56 +08:00
										 |  |  | 									} | 
					
						
							| 
									
										
										
										
											2017-04-05 20:39:23 +08:00
										 |  |  | 								}; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2017-04-21 16:05:56 +08:00
										 |  |  | 					}).filter(Boolean).reduce((obj, item) => { | 
					
						
							|  |  |  | 						obj[item.ident] = item.data; | 
					
						
							| 
									
										
										
										
											2017-04-05 20:39:23 +08:00
										 |  |  | 						return obj; | 
					
						
							| 
									
										
										
										
											2017-05-20 20:46:21 +08:00
										 |  |  | 					}, Object.create(null)) | 
					
						
							| 
									
										
										
										
											2017-04-05 20:39:23 +08:00
										 |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2017-12-10 13:43:17 +08:00
										 |  |  | 				const content = Buffer.from(JSON.stringify(manifest), "utf8"); | 
					
						
							| 
									
										
										
										
											2017-04-05 20:39:23 +08:00
										 |  |  | 				compiler.outputFileSystem.mkdirp(path.dirname(targetPath), err => { | 
					
						
							|  |  |  | 					if(err) return callback(err); | 
					
						
							|  |  |  | 					compiler.outputFileSystem.writeFile(targetPath, content, callback); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}, callback); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-05-17 00:27:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | module.exports = LibManifestPlugin; |