| 
									
										
										
										
											2015-05-17 00:27:59 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-05 20:39:23 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2015-05-17 00:27:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 12:27:09 +08:00
										 |  |  | const asyncLib = require("neo-async"); | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | const path = require("path"); | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | const EntryDependency = require("./dependencies/EntryDependency"); | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | const { compareModulesById } = require("./util/comparators"); | 
					
						
							| 
									
										
										
										
											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) => { | 
					
						
							| 
									
										
										
										
											2018-08-16 22:11:20 +08:00
										 |  |  | 				const moduleGraph = compilation.moduleGraph; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				asyncLib.forEach( | 
					
						
							|  |  |  | 					compilation.chunks, | 
					
						
							|  |  |  | 					(chunk, callback) => { | 
					
						
							|  |  |  | 						if (!chunk.isOnlyInitial()) { | 
					
						
							|  |  |  | 							callback(); | 
					
						
							|  |  |  | 							return; | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 						const chunkGraph = compilation.chunkGraph; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						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, | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 							content: Array.from( | 
					
						
							|  |  |  | 								chunkGraph.getOrderedChunkModulesIterable( | 
					
						
							|  |  |  | 									chunk, | 
					
						
							| 
									
										
										
										
											2018-08-16 22:11:20 +08:00
										 |  |  | 									compareModulesById(moduleGraph) | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 								), | 
					
						
							|  |  |  | 								module => { | 
					
						
							|  |  |  | 									if ( | 
					
						
							|  |  |  | 										this.options.entryOnly && | 
					
						
							|  |  |  | 										!compilation.moduleGraph | 
					
						
							|  |  |  | 											.getIncomingConnections(module) | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 											.some(c => c.dependency instanceof EntryDependency) | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 									) { | 
					
						
							|  |  |  | 										return; | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 									const ident = module.libIdent({ | 
					
						
							|  |  |  | 										context: this.options.context || compiler.options.context | 
					
						
							|  |  |  | 									}); | 
					
						
							|  |  |  | 									if (ident) { | 
					
						
							|  |  |  | 										return { | 
					
						
							|  |  |  | 											ident, | 
					
						
							|  |  |  | 											data: { | 
					
						
							|  |  |  | 												id: module.id, | 
					
						
							|  |  |  | 												buildMeta: module.buildMeta | 
					
						
							|  |  |  | 											} | 
					
						
							|  |  |  | 										}; | 
					
						
							|  |  |  | 									} | 
					
						
							| 
									
										
										
										
											2018-05-22 19:24:40 +08:00
										 |  |  | 								} | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +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; |