| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-16 20:01:53 +08:00
										 |  |  | const parseJson = require("json-parse-even-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"); | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | const createSchemaValidation = require("./util/create-schema-validation"); | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | const makePathsRelative = require("./util/identifier").makePathsRelative; | 
					
						
							| 
									
										
										
										
											2017-01-06 17:02:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-15 22:24:11 +08:00
										 |  |  | /** @typedef {import("../declarations/WebpackOptions").Externals} Externals */ | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | /** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptions} DllReferencePluginOptions */ | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | /** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptionsContent} DllReferencePluginOptionsContent */ | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | /** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptionsManifest} DllReferencePluginOptionsManifest */ | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2024-03-12 00:33:52 +08:00
										 |  |  | /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */ | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | const validate = createSchemaValidation( | 
					
						
							|  |  |  | 	require("../schemas/plugins/DllReferencePlugin.check.js"), | 
					
						
							|  |  |  | 	() => require("../schemas/plugins/DllReferencePlugin.json"), | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		name: "Dll Reference Plugin", | 
					
						
							|  |  |  | 		baseDataPath: "options" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | /** @typedef {{ path: string, data: DllReferencePluginOptionsManifest | undefined, error: Error | undefined }} CompilationDataItem */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | 		validate(options); | 
					
						
							| 
									
										
										
										
											2017-01-06 17:02:30 +08:00
										 |  |  | 		this.options = options; | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 		/** @type {WeakMap<object, CompilationDataItem>} */ | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | 		this._compilationData = new WeakMap(); | 
					
						
							| 
									
										
										
										
											2017-01-06 17:02:30 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											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") { | 
					
						
							| 
									
										
										
										
											2024-03-12 00:33:52 +08:00
										 |  |  | 						/** @type {InputFileSystem} */ | 
					
						
							|  |  |  | 						(compiler.inputFileSystem).readFile(manifest, (err, result) => { | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | 							if (err) return callback(err); | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 							/** @type {CompilationDataItem} */ | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							| 
									
										
										
										
											2024-03-12 00:33:52 +08:00
										 |  |  | 								data.data = parseJson( | 
					
						
							|  |  |  | 									/** @type {Buffer} */ (result).toString("utf-8") | 
					
						
							|  |  |  | 								); | 
					
						
							| 
									
										
										
										
											2024-07-31 15:37:05 +08:00
										 |  |  | 							} catch (parseErr) { | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | 								// Store the error in the params so that it can
 | 
					
						
							|  |  |  | 								// be added as a compilation error later on.
 | 
					
						
							|  |  |  | 								const manifestPath = makePathsRelative( | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 									/** @type {string} */ (compiler.options.context), | 
					
						
							| 
									
										
										
										
											2018-09-27 13:22:19 +08:00
										 |  |  | 									manifest, | 
					
						
							|  |  |  | 									compiler.root | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | 								); | 
					
						
							| 
									
										
										
										
											2024-07-31 15:37:05 +08:00
										 |  |  | 								data.error = new DllManifestError( | 
					
						
							|  |  |  | 									manifestPath, | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 									/** @type {Error} */ (parseErr).message | 
					
						
							| 
									
										
										
										
											2024-07-31 15:37:05 +08:00
										 |  |  | 								); | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 			let resolvedContent = | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | 				"content" in this.options ? this.options.content : undefined; | 
					
						
							|  |  |  | 			if ("manifest" in this.options) { | 
					
						
							| 
									
										
										
										
											2024-07-31 04:09:42 +08:00
										 |  |  | 				const manifestParameter = this.options.manifest; | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | 				let manifest; | 
					
						
							|  |  |  | 				if (typeof manifestParameter === "string") { | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 					const data = | 
					
						
							|  |  |  | 						/** @type {CompilationDataItem} */ | 
					
						
							|  |  |  | 						(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; | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 					if (!resolvedContent) resolvedContent = manifest.content; | 
					
						
							| 
									
										
										
										
											2018-06-30 15:54:47 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-01-06 17:02:30 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-05-15 22:24:11 +08:00
										 |  |  | 			/** @type {Externals} */ | 
					
						
							| 
									
										
										
										
											2017-02-05 07:19:06 +08:00
										 |  |  | 			const externals = {}; | 
					
						
							| 
									
										
										
										
											2024-07-31 10:39:30 +08:00
										 |  |  | 			const source = `dll-reference ${name}`; | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 			externals[source] = /** @type {string} */ (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({ | 
					
						
							| 
									
										
										
										
											2024-07-31 04:09:42 +08:00
										 |  |  | 				source, | 
					
						
							| 
									
										
										
										
											2017-01-06 17:02:30 +08:00
										 |  |  | 				type: this.options.type, | 
					
						
							|  |  |  | 				scope: this.options.scope, | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 				context: | 
					
						
							|  |  |  | 					/** @type {string} */ | 
					
						
							|  |  |  | 					(this.options.context || compiler.options.context), | 
					
						
							|  |  |  | 				content: | 
					
						
							|  |  |  | 					/** @type {DllReferencePluginOptionsContent} */ | 
					
						
							|  |  |  | 					(resolvedContent), | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2024-07-31 04:09:42 +08:00
										 |  |  | 					const manifest = this.options.manifest; | 
					
						
							| 
									
										
										
										
											2018-09-21 17:00:44 +08:00
										 |  |  | 					if (typeof manifest === "string") { | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 						const data = /** @type {CompilationDataItem} */ ( | 
					
						
							|  |  |  | 							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) { | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 							compilation.errors.push( | 
					
						
							|  |  |  | 								/** @type {DllManifestError} */ (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 { | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} filename filename of the manifest | 
					
						
							|  |  |  | 	 * @param {string} message error message | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-07-02 17:58:45 +08:00
										 |  |  | 	constructor(filename, message) { | 
					
						
							|  |  |  | 		super(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		this.name = "DllManifestError"; | 
					
						
							|  |  |  | 		this.message = `Dll manifest ${filename}\n${message}`; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-06 17:02:30 +08:00
										 |  |  | module.exports = DllReferencePlugin; |