| 
									
										
										
										
											2015-05-17 17:06:35 +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-01-06 17:02:30 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2015-05-17 17:06:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-19 17:09:30 +08:00
										 |  |  | const parseJson = require("json-parse-better-errors"); | 
					
						
							| 
									
										
										
										
											2017-01-06 17:02:30 +08:00
										 |  |  | const DelegatedModuleFactoryPlugin = require("./DelegatedModuleFactoryPlugin"); | 
					
						
							|  |  |  | const ExternalModuleFactoryPlugin = require("./ExternalModuleFactoryPlugin"); | 
					
						
							| 
									
										
										
										
											2018-07-02 17:58:45 +08:00
										 |  |  | const WebpackError = require("./WebpackError"); | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | const DelegatedSourceDependency = require("./dependencies/DelegatedSourceDependency"); | 
					
						
							|  |  |  | const makePathsRelative = require("./util/identifier").makePathsRelative; | 
					
						
							| 
									
										
										
										
											2017-01-06 17:02:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 17:19:28 +08:00
										 |  |  | const validateOptions = require("schema-utils"); | 
					
						
							| 
									
										
										
										
											2017-11-11 20:05:55 +08:00
										 |  |  | const schema = require("../schemas/plugins/DllReferencePlugin.json"); | 
					
						
							| 
									
										
										
										
											2017-10-28 05:23:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | /** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptions} DllReferencePluginOptions */ | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | /** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptionsManifest} DllReferencePluginOptionsManifest */ | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-06 17:02:30 +08:00
										 |  |  | class DllReferencePlugin { | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {DllReferencePluginOptions} options options object | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-06 17:02:30 +08:00
										 |  |  | 	constructor(options) { | 
					
						
							| 
									
										
										
										
											2019-08-07 21:55:03 +08:00
										 |  |  | 		validateOptions(schema, options, { | 
					
						
							|  |  |  | 			name: "Dll Reference Plugin", | 
					
						
							|  |  |  | 			baseDataPath: "options" | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-01-06 17:02:30 +08:00
										 |  |  | 		this.options = options; | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | 		/** @type {WeakMap<Object, {path: string, data: DllReferencePluginOptionsManifest?, error: Error?}>} */ | 
					
						
							|  |  |  | 		this._compilationData = new WeakMap(); | 
					
						
							| 
									
										
										
										
											2017-01-06 17:02:30 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.compilation.tap( | 
					
						
							|  |  |  | 			"DllReferencePlugin", | 
					
						
							|  |  |  | 			(compilation, { normalModuleFactory }) => { | 
					
						
							|  |  |  | 				compilation.dependencyFactories.set( | 
					
						
							|  |  |  | 					DelegatedSourceDependency, | 
					
						
							|  |  |  | 					normalModuleFactory | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2016-09-14 18:04:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.beforeCompile.tapAsync( | 
					
						
							|  |  |  | 			"DllReferencePlugin", | 
					
						
							|  |  |  | 			(params, callback) => { | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | 				if ("manifest" in this.options) { | 
					
						
							|  |  |  | 					const manifest = this.options.manifest; | 
					
						
							|  |  |  | 					if (typeof manifest === "string") { | 
					
						
							|  |  |  | 						compiler.inputFileSystem.readFile(manifest, (err, result) => { | 
					
						
							|  |  |  | 							if (err) return callback(err); | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | 							const data = { | 
					
						
							|  |  |  | 								path: manifest, | 
					
						
							|  |  |  | 								data: undefined, | 
					
						
							|  |  |  | 								error: undefined | 
					
						
							|  |  |  | 							}; | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | 							// Catch errors parsing the manifest so that blank
 | 
					
						
							|  |  |  | 							// or malformed manifest files don't kill the process.
 | 
					
						
							|  |  |  | 							try { | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | 								data.data = parseJson(result.toString("utf-8")); | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | 							} catch (e) { | 
					
						
							|  |  |  | 								// Store the error in the params so that it can
 | 
					
						
							|  |  |  | 								// be added as a compilation error later on.
 | 
					
						
							|  |  |  | 								const manifestPath = makePathsRelative( | 
					
						
							|  |  |  | 									compiler.options.context, | 
					
						
							| 
									
										
										
										
											2018-09-27 13:22:19 +08:00
										 |  |  | 									manifest, | 
					
						
							|  |  |  | 									compiler.root | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | 								); | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | 								data.error = new DllManifestError(manifestPath, e.message); | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | 							this._compilationData.set(params, data); | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | 							return callback(); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 						return; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | 				return callback(); | 
					
						
							| 
									
										
										
										
											2017-01-06 17:02:30 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-01-06 17:02:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.compile.tap("DllReferencePlugin", params => { | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | 			let name = this.options.name; | 
					
						
							|  |  |  | 			let sourceType = this.options.sourceType; | 
					
						
							|  |  |  | 			let content = | 
					
						
							|  |  |  | 				"content" in this.options ? this.options.content : undefined; | 
					
						
							|  |  |  | 			if ("manifest" in this.options) { | 
					
						
							|  |  |  | 				let manifestParameter = this.options.manifest; | 
					
						
							|  |  |  | 				let manifest; | 
					
						
							|  |  |  | 				if (typeof manifestParameter === "string") { | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | 					const data = this._compilationData.get(params); | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | 					// If there was an error parsing the manifest
 | 
					
						
							|  |  |  | 					// file, exit now because the error will be added
 | 
					
						
							|  |  |  | 					// as a compilation error in the "compilation" hook.
 | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | 					if (data.error) { | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | 						return; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | 					manifest = data.data; | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					manifest = manifestParameter; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if (manifest) { | 
					
						
							|  |  |  | 					if (!name) name = manifest.name; | 
					
						
							|  |  |  | 					if (!sourceType) sourceType = manifest.type; | 
					
						
							|  |  |  | 					if (!content) content = manifest.content; | 
					
						
							| 
									
										
										
										
											2018-06-30 15:54:47 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-01-06 17:02:30 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-02-05 07:19:06 +08:00
										 |  |  | 			const externals = {}; | 
					
						
							|  |  |  | 			const source = "dll-reference " + name; | 
					
						
							| 
									
										
										
										
											2017-01-06 17:02:30 +08:00
										 |  |  | 			externals[source] = name; | 
					
						
							| 
									
										
										
										
											2017-12-20 16:53:33 +08:00
										 |  |  | 			const normalModuleFactory = params.normalModuleFactory; | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | 			new ExternalModuleFactoryPlugin(sourceType || "var", externals).apply( | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				normalModuleFactory | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2017-12-20 16:53:33 +08:00
										 |  |  | 			new DelegatedModuleFactoryPlugin({ | 
					
						
							| 
									
										
										
										
											2017-01-06 17:02:30 +08:00
										 |  |  | 				source: source, | 
					
						
							|  |  |  | 				type: this.options.type, | 
					
						
							|  |  |  | 				scope: this.options.scope, | 
					
						
							|  |  |  | 				context: this.options.context || compiler.options.context, | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | 				content, | 
					
						
							| 
									
										
										
										
											2019-01-19 19:40:00 +08:00
										 |  |  | 				extensions: this.options.extensions, | 
					
						
							|  |  |  | 				associatedObjectForCache: compiler.root | 
					
						
							| 
									
										
										
										
											2017-12-20 16:53:33 +08:00
										 |  |  | 			}).apply(normalModuleFactory); | 
					
						
							| 
									
										
										
										
											2017-01-06 17:02:30 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-06-30 15:54:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		compiler.hooks.compilation.tap( | 
					
						
							|  |  |  | 			"DllReferencePlugin", | 
					
						
							|  |  |  | 			(compilation, params) => { | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | 				if ("manifest" in this.options) { | 
					
						
							|  |  |  | 					let manifest = this.options.manifest; | 
					
						
							|  |  |  | 					if (typeof manifest === "string") { | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | 						const data = this._compilationData.get(params); | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | 						// If there was an error parsing the manifest file, add the
 | 
					
						
							|  |  |  | 						// error as a compilation error to make the compilation fail.
 | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | 						if (data.error) { | 
					
						
							|  |  |  | 							compilation.errors.push(data.error); | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | 						compilation.fileDependencies.add(manifest); | 
					
						
							| 
									
										
										
										
											2018-06-30 15:54:47 +08:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-01-06 17:02:30 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 17:58:45 +08:00
										 |  |  | class DllManifestError extends WebpackError { | 
					
						
							|  |  |  | 	constructor(filename, message) { | 
					
						
							|  |  |  | 		super(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		this.name = "DllManifestError"; | 
					
						
							|  |  |  | 		this.message = `Dll manifest ${filename}\n${message}`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Error.captureStackTrace(this, this.constructor); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-06 17:02:30 +08:00
										 |  |  | module.exports = DllReferencePlugin; |