* Registers compilation hooks that attach the async fetch-based wasm runtime * to chunks containing async WebAssembly modules. * @param {Compiler} compiler the compiler instance * @returns {void}
(compiler)
| 27 | * @returns {void} |
| 28 | */ |
| 29 | apply(compiler) { |
| 30 | compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => { |
| 31 | const globalWasmLoading = compilation.outputOptions.wasmLoading; |
| 32 | /** |
| 33 | * Determines whether the chunk should load async WebAssembly binaries |
| 34 | * through the `fetch` backend. |
| 35 | * @param {Chunk} chunk chunk |
| 36 | * @returns {boolean} true, if wasm loading is enabled for the chunk |
| 37 | */ |
| 38 | const isEnabledForChunk = (chunk) => { |
| 39 | const options = chunk.getEntryOptions(); |
| 40 | const wasmLoading = |
| 41 | options && options.wasmLoading !== undefined |
| 42 | ? options.wasmLoading |
| 43 | : globalWasmLoading; |
| 44 | return wasmLoading === "fetch"; |
| 45 | }; |
| 46 | /** |
| 47 | * Generates the runtime expression that downloads the emitted wasm |
| 48 | * binary for an async WebAssembly module. |
| 49 | * @param {string} path path to the wasm file |
| 50 | * @returns {string} code to load the wasm file |
| 51 | */ |
| 52 | const generateLoadBinaryCode = (path) => |
| 53 | `fetch(${RuntimeGlobals.publicPath} + ${path})`; |
| 54 | |
| 55 | compilation.hooks.runtimeRequirementInTree |
| 56 | .for(RuntimeGlobals.instantiateWasm) |
| 57 | .tap(PLUGIN_NAME, (chunk, set, { chunkGraph }) => { |
| 58 | if (!isEnabledForChunk(chunk)) return; |
| 59 | if ( |
| 60 | !chunkGraph.hasModuleInGraph( |
| 61 | chunk, |
| 62 | (m) => m.type === WEBASSEMBLY_MODULE_TYPE_ASYNC |
| 63 | ) |
| 64 | ) { |
| 65 | return; |
| 66 | } |
| 67 | set.add(RuntimeGlobals.publicPath); |
| 68 | compilation.addRuntimeModule( |
| 69 | chunk, |
| 70 | new AsyncWasmLoadingRuntimeModule({ |
| 71 | generateLoadBinaryCode, |
| 72 | supportsStreaming: true |
| 73 | }) |
| 74 | ); |
| 75 | }); |
| 76 | |
| 77 | compilation.hooks.runtimeRequirementInTree |
| 78 | .for(RuntimeGlobals.compileWasm) |
| 79 | .tap(PLUGIN_NAME, (chunk, set, { chunkGraph }) => { |
| 80 | if (!isEnabledForChunk(chunk)) return; |
| 81 | if ( |
| 82 | !chunkGraph.hasModuleInGraph( |
| 83 | chunk, |
| 84 | (m) => m.type === WEBASSEMBLY_MODULE_TYPE_ASYNC |
| 85 | ) |
| 86 | ) { |
nothing calls this directly
no test coverage detected