| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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) { | 
					
						
							|  |  |  | 		compiler.hooks.thisCompilation.tap( | 
					
						
							|  |  |  | 			"FetchCompileAsyncWasmPlugin", | 
					
						
							|  |  |  | 			compilation => { | 
					
						
							| 
									
										
										
										
											2020-09-09 15:23:01 +08:00
										 |  |  | 				const globalWasmLoading = compilation.outputOptions.wasmLoading; | 
					
						
							|  |  |  | 				const isEnabledForChunk = chunk => { | 
					
						
							|  |  |  | 					const options = chunk.getEntryOptions(); | 
					
						
							|  |  |  | 					const wasmLoading = | 
					
						
							| 
									
										
										
										
											2021-04-15 02:21:17 +08:00
										 |  |  | 						options && options.wasmLoading !== undefined | 
					
						
							|  |  |  | 							? options.wasmLoading | 
					
						
							|  |  |  | 							: globalWasmLoading; | 
					
						
							| 
									
										
										
										
											2020-09-09 15:23:01 +08:00
										 |  |  | 					return wasmLoading === "fetch"; | 
					
						
							|  |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 				const generateLoadBinaryCode = path => | 
					
						
							|  |  |  | 					`fetch(${RuntimeGlobals.publicPath} + ${path})`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 					.for(RuntimeGlobals.instantiateWasm) | 
					
						
							|  |  |  | 					.tap("FetchCompileAsyncWasmPlugin", (chunk, set) => { | 
					
						
							| 
									
										
										
										
											2020-09-09 15:23:01 +08:00
										 |  |  | 						if (!isEnabledForChunk(chunk)) return; | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 						const chunkGraph = compilation.chunkGraph; | 
					
						
							|  |  |  | 						if ( | 
					
						
							|  |  |  | 							!chunkGraph.hasModuleInGraph( | 
					
						
							|  |  |  | 								chunk, | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | 								m => m.type === WEBASSEMBLY_MODULE_TYPE_ASYNC | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 							) | 
					
						
							|  |  |  | 						) { | 
					
						
							|  |  |  | 							return; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						set.add(RuntimeGlobals.publicPath); | 
					
						
							|  |  |  | 						compilation.addRuntimeModule( | 
					
						
							|  |  |  | 							chunk, | 
					
						
							| 
									
										
										
										
											2021-08-16 15:43:50 +08:00
										 |  |  | 							new AsyncWasmLoadingRuntimeModule({ | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 								generateLoadBinaryCode, | 
					
						
							|  |  |  | 								supportsStreaming: true | 
					
						
							|  |  |  | 							}) | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = FetchCompileAsyncWasmPlugin; |