| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const { RawSource } = require("webpack-sources"); | 
					
						
							|  |  |  | const AsyncDependenciesBlock = require("../AsyncDependenciesBlock"); | 
					
						
							|  |  |  | const Dependency = require("../Dependency"); | 
					
						
							|  |  |  | const Module = require("../Module"); | 
					
						
							|  |  |  | const ModuleFactory = require("../ModuleFactory"); | 
					
						
							| 
									
										
										
										
											2023-05-05 07:19:11 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	WEBPACK_MODULE_TYPE_LAZY_COMPILATION_PROXY | 
					
						
							|  |  |  | } = require("../ModuleTypeConstants"); | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							|  |  |  | const Template = require("../Template"); | 
					
						
							|  |  |  | const CommonJsRequireDependency = require("../dependencies/CommonJsRequireDependency"); | 
					
						
							|  |  |  | const { registerNotSerializable } = require("../util/serialization"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("../../declarations/WebpackOptions")} WebpackOptions */ | 
					
						
							|  |  |  | /** @typedef {import("../Compilation")} Compilation */ | 
					
						
							|  |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */ | 
					
						
							|  |  |  | /** @typedef {import("../Module").BuildMeta} BuildMeta */ | 
					
						
							|  |  |  | /** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */ | 
					
						
							|  |  |  | /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */ | 
					
						
							|  |  |  | /** @typedef {import("../Module").LibIdentOptions} LibIdentOptions */ | 
					
						
							|  |  |  | /** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */ | 
					
						
							| 
									
										
										
										
											2024-02-17 01:39:12 +08:00
										 |  |  | /** @typedef {import("../Module").SourceTypes} SourceTypes */ | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | /** @typedef {import("../ModuleFactory").ModuleFactoryCreateData} ModuleFactoryCreateData */ | 
					
						
							|  |  |  | /** @typedef {import("../ModuleFactory").ModuleFactoryResult} ModuleFactoryResult */ | 
					
						
							|  |  |  | /** @typedef {import("../RequestShortener")} RequestShortener */ | 
					
						
							|  |  |  | /** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */ | 
					
						
							|  |  |  | /** @typedef {import("../WebpackError")} WebpackError */ | 
					
						
							| 
									
										
										
										
											2022-01-18 19:26:43 +08:00
										 |  |  | /** @typedef {import("../dependencies/HarmonyImportDependency")} HarmonyImportDependency */ | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | /** @typedef {import("../util/Hash")} Hash */ | 
					
						
							|  |  |  | /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-25 18:12:52 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @typedef {Object} BackendApi | 
					
						
							|  |  |  |  * @property {function(Error=): void} dispose | 
					
						
							|  |  |  |  * @property {function(Module): { client: string, data: string, active: boolean }} module | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-18 19:26:43 +08:00
										 |  |  | const HMR_DEPENDENCY_TYPES = new Set([ | 
					
						
							| 
									
										
										
										
											2021-03-22 18:59:37 +08:00
										 |  |  | 	"import.meta.webpackHot.accept", | 
					
						
							|  |  |  | 	"import.meta.webpackHot.decline", | 
					
						
							|  |  |  | 	"module.hot.accept", | 
					
						
							|  |  |  | 	"module.hot.decline" | 
					
						
							|  |  |  | ]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-02 00:12:20 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {undefined|string|RegExp|Function} test test option | 
					
						
							|  |  |  |  * @param {Module} module the module | 
					
						
							|  |  |  |  * @returns {boolean} true, if the module should be selected | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const checkTest = (test, module) => { | 
					
						
							|  |  |  | 	if (test === undefined) return true; | 
					
						
							|  |  |  | 	if (typeof test === "function") { | 
					
						
							|  |  |  | 		return test(module); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (typeof test === "string") { | 
					
						
							|  |  |  | 		const name = module.nameForCondition(); | 
					
						
							|  |  |  | 		return name && name.startsWith(test); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (test instanceof RegExp) { | 
					
						
							|  |  |  | 		const name = module.nameForCondition(); | 
					
						
							|  |  |  | 		return name && test.test(name); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | const TYPES = new Set(["javascript"]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class LazyCompilationDependency extends Dependency { | 
					
						
							| 
									
										
										
										
											2021-03-22 18:15:50 +08:00
										 |  |  | 	constructor(proxyModule) { | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 		super(); | 
					
						
							| 
									
										
										
										
											2021-03-22 18:15:50 +08:00
										 |  |  | 		this.proxyModule = proxyModule; | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	get category() { | 
					
						
							|  |  |  | 		return "esm"; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	get type() { | 
					
						
							|  |  |  | 		return "lazy import()"; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @returns {string | null} an identifier to merge equal requests | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	getResourceIdentifier() { | 
					
						
							| 
									
										
										
										
											2021-03-22 18:15:50 +08:00
										 |  |  | 		return this.proxyModule.originalModule.identifier(); | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | registerNotSerializable(LazyCompilationDependency); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class LazyCompilationProxyModule extends Module { | 
					
						
							| 
									
										
										
										
											2021-01-22 08:01:17 +08:00
										 |  |  | 	constructor(context, originalModule, request, client, data, active) { | 
					
						
							| 
									
										
										
										
											2023-05-05 07:19:11 +08:00
										 |  |  | 		super( | 
					
						
							|  |  |  | 			WEBPACK_MODULE_TYPE_LAZY_COMPILATION_PROXY, | 
					
						
							|  |  |  | 			context, | 
					
						
							|  |  |  | 			originalModule.layer | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 		this.originalModule = originalModule; | 
					
						
							|  |  |  | 		this.request = request; | 
					
						
							|  |  |  | 		this.client = client; | 
					
						
							|  |  |  | 		this.data = data; | 
					
						
							|  |  |  | 		this.active = active; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @returns {string} a unique identifier of the module | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	identifier() { | 
					
						
							| 
									
										
										
										
											2023-05-05 07:19:11 +08:00
										 |  |  | 		return `${WEBPACK_MODULE_TYPE_LAZY_COMPILATION_PROXY}|${this.originalModule.identifier()}`; | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {RequestShortener} requestShortener the request shortener | 
					
						
							|  |  |  | 	 * @returns {string} a user readable identifier of the module | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	readableIdentifier(requestShortener) { | 
					
						
							| 
									
										
										
										
											2023-05-05 07:19:11 +08:00
										 |  |  | 		return `${WEBPACK_MODULE_TYPE_LAZY_COMPILATION_PROXY} ${this.originalModule.readableIdentifier( | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 			requestShortener | 
					
						
							|  |  |  | 		)}`;
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Assuming this module is in the cache. Update the (cached) module with | 
					
						
							|  |  |  | 	 * the fresh module from the factory. Usually updates internal references | 
					
						
							|  |  |  | 	 * and properties. | 
					
						
							|  |  |  | 	 * @param {Module} module fresh module | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	updateCacheModule(module) { | 
					
						
							|  |  |  | 		super.updateCacheModule(module); | 
					
						
							|  |  |  | 		const m = /** @type {LazyCompilationProxyModule} */ (module); | 
					
						
							| 
									
										
										
										
											2021-03-22 18:15:50 +08:00
										 |  |  | 		this.originalModule = m.originalModule; | 
					
						
							|  |  |  | 		this.request = m.request; | 
					
						
							|  |  |  | 		this.client = m.client; | 
					
						
							|  |  |  | 		this.data = m.data; | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 		this.active = m.active; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {LibIdentOptions} options options | 
					
						
							|  |  |  | 	 * @returns {string | null} an identifier for library inclusion | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	libIdent(options) { | 
					
						
							| 
									
										
										
										
											2023-05-05 07:19:11 +08:00
										 |  |  | 		return `${this.originalModule.libIdent( | 
					
						
							|  |  |  | 			options | 
					
						
							|  |  |  | 		)}!${WEBPACK_MODULE_TYPE_LAZY_COMPILATION_PROXY}`;
 | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {NeedBuildContext} context context info | 
					
						
							| 
									
										
										
										
											2021-12-24 20:27:31 +08:00
										 |  |  | 	 * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	needBuild(context, callback) { | 
					
						
							|  |  |  | 		callback(null, !this.buildInfo || this.buildInfo.active !== this.active); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {WebpackOptions} options webpack options | 
					
						
							|  |  |  | 	 * @param {Compilation} compilation the compilation | 
					
						
							|  |  |  | 	 * @param {ResolverWithOptions} resolver the resolver | 
					
						
							|  |  |  | 	 * @param {InputFileSystem} fs the file system | 
					
						
							|  |  |  | 	 * @param {function(WebpackError=): void} callback callback function | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	build(options, compilation, resolver, fs, callback) { | 
					
						
							|  |  |  | 		this.buildInfo = { | 
					
						
							|  |  |  | 			active: this.active | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 		/** @type {BuildMeta} */ | 
					
						
							|  |  |  | 		this.buildMeta = {}; | 
					
						
							|  |  |  | 		this.clearDependenciesAndBlocks(); | 
					
						
							|  |  |  | 		const dep = new CommonJsRequireDependency(this.client); | 
					
						
							|  |  |  | 		this.addDependency(dep); | 
					
						
							|  |  |  | 		if (this.active) { | 
					
						
							| 
									
										
										
										
											2021-03-22 18:15:50 +08:00
										 |  |  | 			const dep = new LazyCompilationDependency(this); | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 			const block = new AsyncDependenciesBlock({}); | 
					
						
							|  |  |  | 			block.addDependency(dep); | 
					
						
							|  |  |  | 			this.addBlock(block); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		callback(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2024-02-17 01:39:12 +08:00
										 |  |  | 	 * @returns {SourceTypes} types available (do not mutate) | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	getSourceTypes() { | 
					
						
							|  |  |  | 		return TYPES; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string=} type the source type for which the size should be estimated | 
					
						
							|  |  |  | 	 * @returns {number} the estimated size of the module (must be non-zero) | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	size(type) { | 
					
						
							|  |  |  | 		return 200; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {CodeGenerationContext} context context for code generation | 
					
						
							|  |  |  | 	 * @returns {CodeGenerationResult} result | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	codeGeneration({ runtimeTemplate, chunkGraph, moduleGraph }) { | 
					
						
							|  |  |  | 		const sources = new Map(); | 
					
						
							|  |  |  | 		const runtimeRequirements = new Set(); | 
					
						
							|  |  |  | 		runtimeRequirements.add(RuntimeGlobals.module); | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 		const clientDep = /** @type {CommonJsRequireDependency} */ ( | 
					
						
							|  |  |  | 			this.dependencies[0] | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 		const clientModule = moduleGraph.getModule(clientDep); | 
					
						
							|  |  |  | 		const block = this.blocks[0]; | 
					
						
							| 
									
										
										
										
											2021-01-22 08:01:17 +08:00
										 |  |  | 		const client = Template.asString([ | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 			`var client = ${runtimeTemplate.moduleExports({ | 
					
						
							|  |  |  | 				module: clientModule, | 
					
						
							|  |  |  | 				chunkGraph, | 
					
						
							|  |  |  | 				request: clientDep.userRequest, | 
					
						
							|  |  |  | 				runtimeRequirements | 
					
						
							|  |  |  | 			})}`,
 | 
					
						
							| 
									
										
										
										
											2021-01-22 08:01:17 +08:00
										 |  |  | 			`var data = ${JSON.stringify(this.data)};` | 
					
						
							|  |  |  | 		]); | 
					
						
							|  |  |  | 		const keepActive = Template.asString([ | 
					
						
							| 
									
										
										
										
											2021-02-11 17:43:10 +08:00
										 |  |  | 			`var dispose = client.keepAlive({ data: data, active: ${JSON.stringify( | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 				!!block | 
					
						
							| 
									
										
										
										
											2021-02-11 17:43:10 +08:00
										 |  |  | 			)}, module: module, onError: onError });`
 | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 		]); | 
					
						
							|  |  |  | 		let source; | 
					
						
							|  |  |  | 		if (block) { | 
					
						
							|  |  |  | 			const dep = block.dependencies[0]; | 
					
						
							|  |  |  | 			const module = moduleGraph.getModule(dep); | 
					
						
							|  |  |  | 			source = Template.asString([ | 
					
						
							| 
									
										
										
										
											2021-01-22 08:01:17 +08:00
										 |  |  | 				client, | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 				`module.exports = ${runtimeTemplate.moduleNamespacePromise({ | 
					
						
							|  |  |  | 					chunkGraph, | 
					
						
							|  |  |  | 					block, | 
					
						
							|  |  |  | 					module, | 
					
						
							|  |  |  | 					request: this.request, | 
					
						
							|  |  |  | 					strict: false, // TODO this should be inherited from the original module
 | 
					
						
							|  |  |  | 					message: "import()", | 
					
						
							|  |  |  | 					runtimeRequirements | 
					
						
							|  |  |  | 				})};`,
 | 
					
						
							| 
									
										
										
										
											2021-01-22 08:01:17 +08:00
										 |  |  | 				"if (module.hot) {", | 
					
						
							|  |  |  | 				Template.indent([ | 
					
						
							|  |  |  | 					"module.hot.accept();", | 
					
						
							|  |  |  | 					`module.hot.accept(${JSON.stringify( | 
					
						
							|  |  |  | 						chunkGraph.getModuleId(module) | 
					
						
							|  |  |  | 					)}, function() { module.hot.invalidate(); });`,
 | 
					
						
							|  |  |  | 					"module.hot.dispose(function(data) { delete data.resolveSelf; dispose(data); });", | 
					
						
							|  |  |  | 					"if (module.hot.data && module.hot.data.resolveSelf) module.hot.data.resolveSelf(module.exports);" | 
					
						
							|  |  |  | 				]), | 
					
						
							|  |  |  | 				"}", | 
					
						
							| 
									
										
										
										
											2021-01-22 04:05:23 +08:00
										 |  |  | 				"function onError() { /* ignore */ }", | 
					
						
							|  |  |  | 				keepActive | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 			]); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			source = Template.asString([ | 
					
						
							| 
									
										
										
										
											2021-01-22 08:01:17 +08:00
										 |  |  | 				client, | 
					
						
							| 
									
										
										
										
											2021-01-22 04:05:23 +08:00
										 |  |  | 				"var resolveSelf, onError;", | 
					
						
							|  |  |  | 				`module.exports = new Promise(function(resolve, reject) { resolveSelf = resolve; onError = reject; });`, | 
					
						
							| 
									
										
										
										
											2021-01-22 08:01:17 +08:00
										 |  |  | 				"if (module.hot) {", | 
					
						
							|  |  |  | 				Template.indent([ | 
					
						
							|  |  |  | 					"module.hot.accept();", | 
					
						
							|  |  |  | 					"if (module.hot.data && module.hot.data.resolveSelf) module.hot.data.resolveSelf(module.exports);", | 
					
						
							|  |  |  | 					"module.hot.dispose(function(data) { data.resolveSelf = resolveSelf; dispose(data); });" | 
					
						
							|  |  |  | 				]), | 
					
						
							|  |  |  | 				"}", | 
					
						
							| 
									
										
										
										
											2021-01-22 04:05:23 +08:00
										 |  |  | 				keepActive | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 			]); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-01-22 04:05:23 +08:00
										 |  |  | 		sources.set("javascript", new RawSource(source)); | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 		return { | 
					
						
							|  |  |  | 			sources, | 
					
						
							|  |  |  | 			runtimeRequirements | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Hash} hash the hash used to track dependencies | 
					
						
							|  |  |  | 	 * @param {UpdateHashContext} context context | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	updateHash(hash, context) { | 
					
						
							|  |  |  | 		super.updateHash(hash, context); | 
					
						
							|  |  |  | 		hash.update(this.active ? "active" : ""); | 
					
						
							|  |  |  | 		hash.update(JSON.stringify(this.data)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | registerNotSerializable(LazyCompilationProxyModule); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class LazyCompilationDependencyFactory extends ModuleFactory { | 
					
						
							|  |  |  | 	constructor(factory) { | 
					
						
							|  |  |  | 		super(); | 
					
						
							|  |  |  | 		this._factory = factory; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {ModuleFactoryCreateData} data data object | 
					
						
							| 
									
										
										
										
											2023-05-27 01:21:35 +08:00
										 |  |  | 	 * @param {function((Error | null)=, ModuleFactoryResult=): void} callback callback | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	create(data, callback) { | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 		const dependency = /** @type {LazyCompilationDependency} */ ( | 
					
						
							|  |  |  | 			data.dependencies[0] | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 		callback(null, { | 
					
						
							| 
									
										
										
										
											2021-03-22 18:15:50 +08:00
										 |  |  | 			module: dependency.proxyModule.originalModule | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class LazyCompilationPlugin { | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Object} options options | 
					
						
							| 
									
										
										
										
											2021-10-25 18:12:52 +08:00
										 |  |  | 	 * @param {(function(Compiler, function(Error?, BackendApi?): void): void) | function(Compiler): Promise<BackendApi>} options.backend the backend | 
					
						
							| 
									
										
										
										
											2021-01-22 08:01:17 +08:00
										 |  |  | 	 * @param {boolean} options.entries true, when entries are lazy compiled | 
					
						
							| 
									
										
										
										
											2021-02-01 22:52:47 +08:00
										 |  |  | 	 * @param {boolean} options.imports true, when import() modules are lazy compiled | 
					
						
							| 
									
										
										
										
											2021-02-02 03:08:38 +08:00
										 |  |  | 	 * @param {RegExp | string | (function(Module): boolean)} options.test additional filter for lazy compiled entrypoint modules | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2021-10-25 18:12:52 +08:00
										 |  |  | 	constructor({ backend, entries, imports, test }) { | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 		this.backend = backend; | 
					
						
							| 
									
										
										
										
											2021-01-22 08:01:17 +08:00
										 |  |  | 		this.entries = entries; | 
					
						
							| 
									
										
										
										
											2021-02-01 22:52:47 +08:00
										 |  |  | 		this.imports = imports; | 
					
						
							| 
									
										
										
										
											2021-02-02 00:12:20 +08:00
										 |  |  | 		this.test = test; | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		let backend; | 
					
						
							|  |  |  | 		compiler.hooks.beforeCompile.tapAsync( | 
					
						
							|  |  |  | 			"LazyCompilationPlugin", | 
					
						
							|  |  |  | 			(params, callback) => { | 
					
						
							|  |  |  | 				if (backend !== undefined) return callback(); | 
					
						
							| 
									
										
										
										
											2021-10-25 18:12:52 +08:00
										 |  |  | 				const promise = this.backend(compiler, (err, result) => { | 
					
						
							|  |  |  | 					if (err) return callback(err); | 
					
						
							|  |  |  | 					backend = result; | 
					
						
							|  |  |  | 					callback(); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 				if (promise && promise.then) { | 
					
						
							|  |  |  | 					promise.then(b => { | 
					
						
							|  |  |  | 						backend = b; | 
					
						
							|  |  |  | 						callback(); | 
					
						
							|  |  |  | 					}, callback); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2021-01-22 08:01:17 +08:00
										 |  |  | 		compiler.hooks.thisCompilation.tap( | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 			"LazyCompilationPlugin", | 
					
						
							|  |  |  | 			(compilation, { normalModuleFactory }) => { | 
					
						
							|  |  |  | 				normalModuleFactory.hooks.module.tap( | 
					
						
							|  |  |  | 					"LazyCompilationPlugin", | 
					
						
							|  |  |  | 					(originalModule, createData, resolveData) => { | 
					
						
							|  |  |  | 						if ( | 
					
						
							| 
									
										
										
										
											2022-01-18 19:26:43 +08:00
										 |  |  | 							resolveData.dependencies.every(dep => | 
					
						
							|  |  |  | 								HMR_DEPENDENCY_TYPES.has(dep.type) | 
					
						
							|  |  |  | 							) | 
					
						
							|  |  |  | 						) { | 
					
						
							|  |  |  | 							// for HMR only resolving, try to determine if the HMR accept/decline refers to
 | 
					
						
							|  |  |  | 							// an import() or not
 | 
					
						
							|  |  |  | 							const hmrDep = resolveData.dependencies[0]; | 
					
						
							|  |  |  | 							const originModule = | 
					
						
							|  |  |  | 								compilation.moduleGraph.getParentModule(hmrDep); | 
					
						
							|  |  |  | 							const isReferringToDynamicImport = originModule.blocks.some( | 
					
						
							|  |  |  | 								block => | 
					
						
							|  |  |  | 									block.dependencies.some( | 
					
						
							|  |  |  | 										dep => | 
					
						
							|  |  |  | 											dep.type === "import()" && | 
					
						
							|  |  |  | 											/** @type {HarmonyImportDependency} */ (dep).request === | 
					
						
							|  |  |  | 												hmrDep.request | 
					
						
							|  |  |  | 									) | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 							if (!isReferringToDynamicImport) return; | 
					
						
							|  |  |  | 						} else if ( | 
					
						
							|  |  |  | 							!resolveData.dependencies.every( | 
					
						
							| 
									
										
										
										
											2021-01-22 08:01:17 +08:00
										 |  |  | 								dep => | 
					
						
							| 
									
										
										
										
											2022-01-18 19:26:43 +08:00
										 |  |  | 									HMR_DEPENDENCY_TYPES.has(dep.type) || | 
					
						
							| 
									
										
										
										
											2021-06-01 00:16:21 +08:00
										 |  |  | 									(this.imports && | 
					
						
							|  |  |  | 										(dep.type === "import()" || | 
					
						
							|  |  |  | 											dep.type === "import() context element")) || | 
					
						
							| 
									
										
										
										
											2021-01-22 08:01:17 +08:00
										 |  |  | 									(this.entries && dep.type === "entry") | 
					
						
							| 
									
										
										
										
											2022-01-18 19:26:43 +08:00
										 |  |  | 							) | 
					
						
							|  |  |  | 						) | 
					
						
							|  |  |  | 							return; | 
					
						
							|  |  |  | 						if ( | 
					
						
							| 
									
										
										
										
											2022-01-20 15:58:36 +08:00
										 |  |  | 							/webpack[/\\]hot[/\\]|webpack-dev-server[/\\]client|webpack-hot-middleware[/\\]client/.test( | 
					
						
							| 
									
										
										
										
											2021-01-22 08:01:17 +08:00
										 |  |  | 								resolveData.request | 
					
						
							| 
									
										
										
										
											2022-01-18 19:26:43 +08:00
										 |  |  | 							) || | 
					
						
							|  |  |  | 							!checkTest(this.test, originalModule) | 
					
						
							|  |  |  | 						) | 
					
						
							|  |  |  | 							return; | 
					
						
							|  |  |  | 						const moduleInfo = backend.module(originalModule); | 
					
						
							|  |  |  | 						if (!moduleInfo) return; | 
					
						
							|  |  |  | 						const { client, data, active } = moduleInfo; | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-18 19:26:43 +08:00
										 |  |  | 						return new LazyCompilationProxyModule( | 
					
						
							|  |  |  | 							compiler.context, | 
					
						
							|  |  |  | 							originalModule, | 
					
						
							|  |  |  | 							resolveData.request, | 
					
						
							|  |  |  | 							client, | 
					
						
							|  |  |  | 							data, | 
					
						
							|  |  |  | 							active | 
					
						
							|  |  |  | 						); | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 				compilation.dependencyFactories.set( | 
					
						
							|  |  |  | 					LazyCompilationDependency, | 
					
						
							|  |  |  | 					new LazyCompilationDependencyFactory() | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		compiler.hooks.shutdown.tapAsync("LazyCompilationPlugin", callback => { | 
					
						
							|  |  |  | 			backend.dispose(callback); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = LazyCompilationPlugin; |