* Registers compilation hooks that attach the fetch-based synchronous wasm * runtime module to chunks containing sync WebAssembly modules. * @param {Compiler} compiler the compiler instance * @returns {void}
(compiler)
| 42 | * @returns {void} |
| 43 | */ |
| 44 | apply(compiler) { |
| 45 | compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => { |
| 46 | const globalWasmLoading = compilation.outputOptions.wasmLoading; |
| 47 | /** |
| 48 | * Determines whether the chunk should load synchronous WebAssembly |
| 49 | * binaries through the `fetch` backend. |
| 50 | * @param {Chunk} chunk chunk |
| 51 | * @returns {boolean} true, if wasm loading is enabled for the chunk |
| 52 | */ |
| 53 | const isEnabledForChunk = (chunk) => { |
| 54 | const options = chunk.getEntryOptions(); |
| 55 | const wasmLoading = |
| 56 | options && options.wasmLoading !== undefined |
| 57 | ? options.wasmLoading |
| 58 | : globalWasmLoading; |
| 59 | return wasmLoading === "fetch"; |
| 60 | }; |
| 61 | /** |
| 62 | * Generates the runtime expression that downloads the emitted wasm |
| 63 | * binary for a module. |
| 64 | * @param {string} path path to the wasm file |
| 65 | * @returns {string} code to load the wasm file |
| 66 | */ |
| 67 | const generateLoadBinaryCode = (path) => |
| 68 | `fetch(${RuntimeGlobals.publicPath} + ${path})`; |
| 69 | |
| 70 | compilation.hooks.runtimeRequirementInTree |
| 71 | .for(RuntimeGlobals.ensureChunkHandlers) |
| 72 | .tap(PLUGIN_NAME, (chunk, set, { chunkGraph }) => { |
| 73 | if (!isEnabledForChunk(chunk)) return; |
| 74 | if ( |
| 75 | !chunkGraph.hasModuleInGraph( |
| 76 | chunk, |
| 77 | (m) => m.type === WEBASSEMBLY_MODULE_TYPE_SYNC |
| 78 | ) |
| 79 | ) { |
| 80 | return; |
| 81 | } |
| 82 | set.add(RuntimeGlobals.moduleCache); |
| 83 | set.add(RuntimeGlobals.publicPath); |
| 84 | compilation.addRuntimeModule( |
| 85 | chunk, |
| 86 | new WasmChunkLoadingRuntimeModule({ |
| 87 | generateLoadBinaryCode, |
| 88 | supportsStreaming: true, |
| 89 | mangleImports: this.options.mangleImports, |
| 90 | runtimeRequirements: set |
| 91 | }) |
| 92 | ); |
| 93 | }); |
| 94 | }); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | module.exports = FetchCompileWasmPlugin; |
nothing calls this directly
no test coverage detected