| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | const { WEBASSEMBLY_MODULE_TYPE_ASYNC } = require("../ModuleTypeConstants"); | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							| 
									
										
										
										
											2021-08-16 15:43:50 +08:00
										 |  |  | const AsyncWasmLoadingRuntimeModule = require("../wasm-async/AsyncWasmLoadingRuntimeModule"); | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-26 02:31:28 +08:00
										 |  |  | /** @typedef {import("../Chunk")} Chunk */ | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-14 11:17:57 +08:00
										 |  |  | const PLUGIN_NAME = "FetchCompileAsyncWasmPlugin"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | class FetchCompileAsyncWasmPlugin { | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-23 16:48:36 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 	 * @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) => { | 
					
						
							| 
									
										
										
										
											2024-11-14 11:17:57 +08:00
										 |  |  | 			const globalWasmLoading = compilation.outputOptions.wasmLoading; | 
					
						
							|  |  |  | 			/** | 
					
						
							|  |  |  | 			 * @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) => { | 
					
						
							| 
									
										
										
										
											2024-11-14 11:17:57 +08:00
										 |  |  | 				const options = chunk.getEntryOptions(); | 
					
						
							|  |  |  | 				const wasmLoading = | 
					
						
							|  |  |  | 					options && options.wasmLoading !== undefined | 
					
						
							|  |  |  | 						? options.wasmLoading | 
					
						
							|  |  |  | 						: globalWasmLoading; | 
					
						
							|  |  |  | 				return wasmLoading === "fetch"; | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 			/** | 
					
						
							|  |  |  | 			 * @param {string} path path to the wasm file | 
					
						
							|  |  |  | 			 * @returns {string} code to load the wasm file | 
					
						
							|  |  |  | 			 */ | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 			const generateLoadBinaryCode = (path) => | 
					
						
							| 
									
										
										
										
											2024-11-14 11:17:57 +08:00
										 |  |  | 				`fetch(${RuntimeGlobals.publicPath} + ${path})`; | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-14 11:17:57 +08:00
										 |  |  | 			compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 				.for(RuntimeGlobals.instantiateWasm) | 
					
						
							|  |  |  | 				.tap(PLUGIN_NAME, (chunk, set, { chunkGraph }) => { | 
					
						
							|  |  |  | 					if (!isEnabledForChunk(chunk)) return; | 
					
						
							|  |  |  | 					if ( | 
					
						
							|  |  |  | 						!chunkGraph.hasModuleInGraph( | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 							chunk, | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 							(m) => m.type === WEBASSEMBLY_MODULE_TYPE_ASYNC | 
					
						
							| 
									
										
										
										
											2024-11-14 11:17:57 +08:00
										 |  |  | 						) | 
					
						
							|  |  |  | 					) { | 
					
						
							|  |  |  | 						return; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					set.add(RuntimeGlobals.publicPath); | 
					
						
							|  |  |  | 					compilation.addRuntimeModule( | 
					
						
							|  |  |  | 						chunk, | 
					
						
							|  |  |  | 						new AsyncWasmLoadingRuntimeModule({ | 
					
						
							|  |  |  | 							generateLoadBinaryCode, | 
					
						
							|  |  |  | 							supportsStreaming: true | 
					
						
							|  |  |  | 						}) | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = FetchCompileAsyncWasmPlugin; |