| 
									
										
										
										
											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"); | 
					
						
							| 
									
										
										
										
											2018-02-11 12:27:09 +08:00
										 |  |  | const asyncLib = require("neo-async"); | 
					
						
							| 
									
										
										
										
											2018-05-22 19:24:40 +08:00
										 |  |  | const SingleEntryDependency = require("./dependencies/SingleEntryDependency"); | 
					
						
							| 
									
										
										
										
											2017-04-05 20:39:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class LibManifestPlugin { | 
					
						
							|  |  |  | 	constructor(options) { | 
					
						
							|  |  |  | 		this.options = options; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.emit.tapAsync( | 
					
						
							|  |  |  | 			"LibManifestPlugin", | 
					
						
							|  |  |  | 			(compilation, callback) => { | 
					
						
							|  |  |  | 				asyncLib.forEach( | 
					
						
							|  |  |  | 					compilation.chunks, | 
					
						
							|  |  |  | 					(chunk, callback) => { | 
					
						
							|  |  |  | 						if (!chunk.isOnlyInitial()) { | 
					
						
							|  |  |  | 							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 | 
					
						
							| 
									
										
										
										
											2017-04-05 20:39:23 +08:00
										 |  |  | 							}); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						const manifest = { | 
					
						
							|  |  |  | 							name, | 
					
						
							|  |  |  | 							type: this.options.type, | 
					
						
							|  |  |  | 							content: Array.from(chunk.modulesIterable, module => { | 
					
						
							| 
									
										
										
										
											2018-05-22 19:24:40 +08:00
										 |  |  | 								if ( | 
					
						
							|  |  |  | 									this.options.entryOnly && | 
					
						
							|  |  |  | 									!module.reasons.some( | 
					
						
							|  |  |  | 										r => r.dependency instanceof SingleEntryDependency | 
					
						
							|  |  |  | 									) | 
					
						
							|  |  |  | 								) { | 
					
						
							|  |  |  | 									return; | 
					
						
							|  |  |  | 								} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 								if (module.libIdent) { | 
					
						
							|  |  |  | 									const ident = module.libIdent({ | 
					
						
							|  |  |  | 										context: this.options.context || compiler.options.context | 
					
						
							|  |  |  | 									}); | 
					
						
							|  |  |  | 									if (ident) { | 
					
						
							|  |  |  | 										return { | 
					
						
							|  |  |  | 											ident, | 
					
						
							|  |  |  | 											data: { | 
					
						
							|  |  |  | 												id: module.id, | 
					
						
							|  |  |  | 												buildMeta: module.buildMeta | 
					
						
							|  |  |  | 											} | 
					
						
							|  |  |  | 										}; | 
					
						
							| 
									
										
										
										
											2017-04-21 16:05:56 +08:00
										 |  |  | 									} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 								} | 
					
						
							|  |  |  | 							}) | 
					
						
							|  |  |  | 								.filter(Boolean) | 
					
						
							|  |  |  | 								.reduce((obj, item) => { | 
					
						
							|  |  |  | 									obj[item.ident] = item.data; | 
					
						
							|  |  |  | 									return obj; | 
					
						
							|  |  |  | 								}, Object.create(null)) | 
					
						
							|  |  |  | 						}; | 
					
						
							|  |  |  | 						const content = Buffer.from(JSON.stringify(manifest), "utf8"); | 
					
						
							|  |  |  | 						compiler.outputFileSystem.mkdirp(path.dirname(targetPath), err => { | 
					
						
							|  |  |  | 							if (err) return callback(err); | 
					
						
							|  |  |  | 							compiler.outputFileSystem.writeFile( | 
					
						
							|  |  |  | 								targetPath, | 
					
						
							|  |  |  | 								content, | 
					
						
							|  |  |  | 								callback | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					callback | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-04-05 20:39:23 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-05-17 00:27:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | module.exports = LibManifestPlugin; |