| 
									
										
										
										
											2018-11-23 16:37:33 +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_SYNC } = require("../ModuleTypeConstants"); | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							| 
									
										
										
										
											2020-09-09 15:23:01 +08:00
										 |  |  | const WasmChunkLoadingRuntimeModule = require("../wasm-sync/WasmChunkLoadingRuntimeModule"); | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-09 15:23:01 +08:00
										 |  |  | // TODO webpack 6 remove
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | const PLUGIN_NAME = "FetchCompileWasmPlugin"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | class FetchCompileWasmPlugin { | 
					
						
							|  |  |  | 	constructor(options) { | 
					
						
							|  |  |  | 		this.options = options || {}; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-23 16:48:36 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | 		compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => { | 
					
						
							|  |  |  | 			const globalWasmLoading = compilation.outputOptions.wasmLoading; | 
					
						
							|  |  |  | 			const isEnabledForChunk = chunk => { | 
					
						
							|  |  |  | 				const options = chunk.getEntryOptions(); | 
					
						
							|  |  |  | 				const wasmLoading = | 
					
						
							|  |  |  | 					options && options.wasmLoading !== undefined | 
					
						
							|  |  |  | 						? options.wasmLoading | 
					
						
							|  |  |  | 						: globalWasmLoading; | 
					
						
							|  |  |  | 				return wasmLoading === "fetch"; | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 			const generateLoadBinaryCode = path => | 
					
						
							|  |  |  | 				`fetch(${RuntimeGlobals.publicPath} + ${path})`; | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | 			compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 				.for(RuntimeGlobals.ensureChunkHandlers) | 
					
						
							|  |  |  | 				.tap(PLUGIN_NAME, (chunk, set) => { | 
					
						
							|  |  |  | 					if (!isEnabledForChunk(chunk)) return; | 
					
						
							|  |  |  | 					const chunkGraph = compilation.chunkGraph; | 
					
						
							|  |  |  | 					if ( | 
					
						
							|  |  |  | 						!chunkGraph.hasModuleInGraph( | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 							chunk, | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | 							m => m.type === WEBASSEMBLY_MODULE_TYPE_SYNC | 
					
						
							|  |  |  | 						) | 
					
						
							|  |  |  | 					) { | 
					
						
							|  |  |  | 						return; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					set.add(RuntimeGlobals.moduleCache); | 
					
						
							|  |  |  | 					set.add(RuntimeGlobals.publicPath); | 
					
						
							|  |  |  | 					compilation.addRuntimeModule( | 
					
						
							|  |  |  | 						chunk, | 
					
						
							|  |  |  | 						new WasmChunkLoadingRuntimeModule({ | 
					
						
							|  |  |  | 							generateLoadBinaryCode, | 
					
						
							|  |  |  | 							supportsStreaming: true, | 
					
						
							|  |  |  | 							mangleImports: this.options.mangleImports, | 
					
						
							|  |  |  | 							runtimeRequirements: set | 
					
						
							|  |  |  | 						}) | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = FetchCompileWasmPlugin; |