| 
									
										
										
										
											2020-08-25 19:20:29 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							| 
									
										
										
										
											2020-08-25 19:42:22 +08:00
										 |  |  | const StartupChunkDependenciesPlugin = require("../runtime/StartupChunkDependenciesPlugin"); | 
					
						
							| 
									
										
										
										
											2020-08-25 19:20:29 +08:00
										 |  |  | const ImportScriptsChunkLoadingRuntimeModule = require("./ImportScriptsChunkLoadingRuntimeModule"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-26 02:31:28 +08:00
										 |  |  | /** @typedef {import("../Chunk")} Chunk */ | 
					
						
							| 
									
										
										
										
											2020-08-25 19:20:29 +08:00
										 |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2025-08-28 18:34:30 +08:00
										 |  |  | /** @typedef {import("../Module").RuntimeRequirements} RuntimeRequirements */ | 
					
						
							| 
									
										
										
										
											2020-08-25 19:20:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-23 20:03:37 +08:00
										 |  |  | const PLUGIN_NAME = "ImportScriptsChunkLoadingPlugin"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-26 03:45:56 +08:00
										 |  |  | class ImportScriptsChunkLoadingPlugin { | 
					
						
							| 
									
										
										
										
											2020-08-25 19:20:29 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2020-08-25 19:42:22 +08:00
										 |  |  | 		new StartupChunkDependenciesPlugin({ | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 			chunkLoading: "import-scripts", | 
					
						
							| 
									
										
										
										
											2020-08-25 19:42:22 +08:00
										 |  |  | 			asyncChunkLoading: true | 
					
						
							|  |  |  | 		}).apply(compiler); | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 		compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => { | 
					
						
							| 
									
										
										
										
											2025-04-23 20:03:37 +08:00
										 |  |  | 			const globalChunkLoading = compilation.outputOptions.chunkLoading; | 
					
						
							|  |  |  | 			/** | 
					
						
							|  |  |  | 			 * @param {Chunk} chunk chunk | 
					
						
							|  |  |  | 			 * @returns {boolean} true, if wasm loading is enabled for the chunk | 
					
						
							|  |  |  | 			 */ | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 			const isEnabledForChunk = (chunk) => { | 
					
						
							| 
									
										
										
										
											2025-04-23 20:03:37 +08:00
										 |  |  | 				const options = chunk.getEntryOptions(); | 
					
						
							|  |  |  | 				const chunkLoading = | 
					
						
							|  |  |  | 					options && options.chunkLoading !== undefined | 
					
						
							|  |  |  | 						? options.chunkLoading | 
					
						
							|  |  |  | 						: globalChunkLoading; | 
					
						
							|  |  |  | 				return chunkLoading === "import-scripts"; | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 			const onceForChunkSet = new WeakSet(); | 
					
						
							|  |  |  | 			/** | 
					
						
							|  |  |  | 			 * @param {Chunk} chunk chunk | 
					
						
							| 
									
										
										
										
											2025-08-28 18:34:30 +08:00
										 |  |  | 			 * @param {RuntimeRequirements} set runtime requirements | 
					
						
							| 
									
										
										
										
											2025-04-23 20:03:37 +08:00
										 |  |  | 			 */ | 
					
						
							|  |  |  | 			const handler = (chunk, set) => { | 
					
						
							|  |  |  | 				if (onceForChunkSet.has(chunk)) return; | 
					
						
							|  |  |  | 				onceForChunkSet.add(chunk); | 
					
						
							|  |  |  | 				if (!isEnabledForChunk(chunk)) return; | 
					
						
							|  |  |  | 				const withCreateScriptUrl = Boolean( | 
					
						
							|  |  |  | 					compilation.outputOptions.trustedTypes | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 				set.add(RuntimeGlobals.moduleFactoriesAddOnly); | 
					
						
							|  |  |  | 				set.add(RuntimeGlobals.hasOwnProperty); | 
					
						
							|  |  |  | 				if (withCreateScriptUrl) { | 
					
						
							|  |  |  | 					set.add(RuntimeGlobals.createScriptUrl); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				compilation.addRuntimeModule( | 
					
						
							|  |  |  | 					chunk, | 
					
						
							|  |  |  | 					new ImportScriptsChunkLoadingRuntimeModule(set, withCreateScriptUrl) | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 			compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 				.for(RuntimeGlobals.ensureChunkHandlers) | 
					
						
							|  |  |  | 				.tap(PLUGIN_NAME, handler); | 
					
						
							|  |  |  | 			compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 				.for(RuntimeGlobals.hmrDownloadUpdateHandlers) | 
					
						
							|  |  |  | 				.tap(PLUGIN_NAME, handler); | 
					
						
							|  |  |  | 			compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 				.for(RuntimeGlobals.hmrDownloadManifest) | 
					
						
							|  |  |  | 				.tap(PLUGIN_NAME, handler); | 
					
						
							|  |  |  | 			compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 				.for(RuntimeGlobals.baseURI) | 
					
						
							|  |  |  | 				.tap(PLUGIN_NAME, handler); | 
					
						
							|  |  |  | 			compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 				.for(RuntimeGlobals.onChunksLoaded) | 
					
						
							|  |  |  | 				.tap(PLUGIN_NAME, handler); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 				.for(RuntimeGlobals.ensureChunkHandlers) | 
					
						
							|  |  |  | 				.tap(PLUGIN_NAME, (chunk, set) => { | 
					
						
							|  |  |  | 					if (!isEnabledForChunk(chunk)) return; | 
					
						
							|  |  |  | 					set.add(RuntimeGlobals.publicPath); | 
					
						
							|  |  |  | 					set.add(RuntimeGlobals.getChunkScriptFilename); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 				.for(RuntimeGlobals.hmrDownloadUpdateHandlers) | 
					
						
							|  |  |  | 				.tap(PLUGIN_NAME, (chunk, set) => { | 
					
						
							| 
									
										
										
										
											2020-08-25 19:37:40 +08:00
										 |  |  | 					if (!isEnabledForChunk(chunk)) return; | 
					
						
							| 
									
										
										
										
											2025-04-23 20:03:37 +08:00
										 |  |  | 					set.add(RuntimeGlobals.publicPath); | 
					
						
							|  |  |  | 					set.add(RuntimeGlobals.getChunkUpdateScriptFilename); | 
					
						
							|  |  |  | 					set.add(RuntimeGlobals.moduleCache); | 
					
						
							|  |  |  | 					set.add(RuntimeGlobals.hmrModuleData); | 
					
						
							| 
									
										
										
										
											2020-08-25 19:20:29 +08:00
										 |  |  | 					set.add(RuntimeGlobals.moduleFactoriesAddOnly); | 
					
						
							| 
									
										
										
										
											2025-04-23 20:03:37 +08:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 				.for(RuntimeGlobals.hmrDownloadManifest) | 
					
						
							|  |  |  | 				.tap(PLUGIN_NAME, (chunk, set) => { | 
					
						
							|  |  |  | 					if (!isEnabledForChunk(chunk)) return; | 
					
						
							|  |  |  | 					set.add(RuntimeGlobals.publicPath); | 
					
						
							|  |  |  | 					set.add(RuntimeGlobals.getUpdateManifestFilename); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2020-08-25 19:20:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-26 03:45:56 +08:00
										 |  |  | module.exports = ImportScriptsChunkLoadingPlugin; |