[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const { WEBASSEMBLY_MODULE_TYPE_SYNC } = require("../ModuleTypeConstants");
|
---|
| 9 | const RuntimeGlobals = require("../RuntimeGlobals");
|
---|
| 10 | const WasmChunkLoadingRuntimeModule = require("../wasm-sync/WasmChunkLoadingRuntimeModule");
|
---|
| 11 |
|
---|
| 12 | /** @typedef {import("../Chunk")} Chunk */
|
---|
| 13 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 14 |
|
---|
| 15 | /**
|
---|
| 16 | * @typedef {object} FetchCompileWasmPluginOptions
|
---|
| 17 | * @property {boolean} [mangleImports] mangle imports
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 | // TODO webpack 6 remove
|
---|
| 21 |
|
---|
| 22 | const PLUGIN_NAME = "FetchCompileWasmPlugin";
|
---|
| 23 |
|
---|
| 24 | class FetchCompileWasmPlugin {
|
---|
| 25 | /**
|
---|
| 26 | * @param {FetchCompileWasmPluginOptions} [options] options
|
---|
| 27 | */
|
---|
| 28 | constructor(options = {}) {
|
---|
| 29 | this.options = options;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * Apply the plugin
|
---|
| 34 | * @param {Compiler} compiler the compiler instance
|
---|
| 35 | * @returns {void}
|
---|
| 36 | */
|
---|
| 37 | apply(compiler) {
|
---|
| 38 | compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => {
|
---|
| 39 | const globalWasmLoading = compilation.outputOptions.wasmLoading;
|
---|
| 40 | /**
|
---|
| 41 | * @param {Chunk} chunk chunk
|
---|
| 42 | * @returns {boolean} true, if wasm loading is enabled for the chunk
|
---|
| 43 | */
|
---|
| 44 | const isEnabledForChunk = chunk => {
|
---|
| 45 | const options = chunk.getEntryOptions();
|
---|
| 46 | const wasmLoading =
|
---|
| 47 | options && options.wasmLoading !== undefined
|
---|
| 48 | ? options.wasmLoading
|
---|
| 49 | : globalWasmLoading;
|
---|
| 50 | return wasmLoading === "fetch";
|
---|
| 51 | };
|
---|
| 52 | /**
|
---|
| 53 | * @param {string} path path to the wasm file
|
---|
| 54 | * @returns {string} code to load the wasm file
|
---|
| 55 | */
|
---|
| 56 | const generateLoadBinaryCode = path =>
|
---|
| 57 | `fetch(${RuntimeGlobals.publicPath} + ${path})`;
|
---|
| 58 |
|
---|
| 59 | compilation.hooks.runtimeRequirementInTree
|
---|
| 60 | .for(RuntimeGlobals.ensureChunkHandlers)
|
---|
| 61 | .tap(PLUGIN_NAME, (chunk, set, { chunkGraph }) => {
|
---|
| 62 | if (!isEnabledForChunk(chunk)) return;
|
---|
| 63 | if (
|
---|
| 64 | !chunkGraph.hasModuleInGraph(
|
---|
| 65 | chunk,
|
---|
| 66 | m => m.type === WEBASSEMBLY_MODULE_TYPE_SYNC
|
---|
| 67 | )
|
---|
| 68 | ) {
|
---|
| 69 | return;
|
---|
| 70 | }
|
---|
| 71 | set.add(RuntimeGlobals.moduleCache);
|
---|
| 72 | set.add(RuntimeGlobals.publicPath);
|
---|
| 73 | compilation.addRuntimeModule(
|
---|
| 74 | chunk,
|
---|
| 75 | new WasmChunkLoadingRuntimeModule({
|
---|
| 76 | generateLoadBinaryCode,
|
---|
| 77 | supportsStreaming: true,
|
---|
| 78 | mangleImports: this.options.mangleImports,
|
---|
| 79 | runtimeRequirements: set
|
---|
| 80 | })
|
---|
| 81 | );
|
---|
| 82 | });
|
---|
| 83 | });
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | module.exports = FetchCompileWasmPlugin;
|
---|