| 
									
										
										
										
											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"); | 
					
						
							|  |  |  | const JsonpChunkLoadingRuntimeModule = require("./JsonpChunkLoadingRuntimeModule"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 = "JsonpChunkLoadingPlugin"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-25 19:20:29 +08:00
										 |  |  | class JsonpChunkLoadingPlugin { | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	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 === "jsonp"; | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 			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; | 
					
						
							|  |  |  | 				set.add(RuntimeGlobals.moduleFactoriesAddOnly); | 
					
						
							|  |  |  | 				set.add(RuntimeGlobals.hasOwnProperty); | 
					
						
							|  |  |  | 				compilation.addRuntimeModule( | 
					
						
							|  |  |  | 					chunk, | 
					
						
							|  |  |  | 					new JsonpChunkLoadingRuntimeModule(set) | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 			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.loadScript); | 
					
						
							|  |  |  | 					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.loadScript); | 
					
						
							|  |  |  | 					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
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = JsonpChunkLoadingPlugin; |