[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Alexander Akait @alexander-akait
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const { WEBASSEMBLY_MODULE_TYPE_ASYNC } = require("../ModuleTypeConstants");
|
---|
| 9 | const RuntimeGlobals = require("../RuntimeGlobals");
|
---|
| 10 | const Template = require("../Template");
|
---|
| 11 | const AsyncWasmLoadingRuntimeModule = require("../wasm-async/AsyncWasmLoadingRuntimeModule");
|
---|
| 12 |
|
---|
| 13 | /** @typedef {import("../Chunk")} Chunk */
|
---|
| 14 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 15 |
|
---|
| 16 | const PLUGIN_NAME = "UniversalCompileAsyncWasmPlugin";
|
---|
| 17 |
|
---|
| 18 | class UniversalCompileAsyncWasmPlugin {
|
---|
| 19 | /**
|
---|
| 20 | * Apply the plugin
|
---|
| 21 | * @param {Compiler} compiler the compiler instance
|
---|
| 22 | * @returns {void}
|
---|
| 23 | */
|
---|
| 24 | apply(compiler) {
|
---|
| 25 | compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => {
|
---|
| 26 | const globalWasmLoading = compilation.outputOptions.wasmLoading;
|
---|
| 27 | /**
|
---|
| 28 | * @param {Chunk} chunk chunk
|
---|
| 29 | * @returns {boolean} true, if wasm loading is enabled for the chunk
|
---|
| 30 | */
|
---|
| 31 | const isEnabledForChunk = chunk => {
|
---|
| 32 | const options = chunk.getEntryOptions();
|
---|
| 33 | const wasmLoading =
|
---|
| 34 | options && options.wasmLoading !== undefined
|
---|
| 35 | ? options.wasmLoading
|
---|
| 36 | : globalWasmLoading;
|
---|
| 37 | return wasmLoading === "universal";
|
---|
| 38 | };
|
---|
| 39 | const generateBeforeInstantiateStreaming = () =>
|
---|
| 40 | Template.asString([
|
---|
| 41 | "if (!useFetch) {",
|
---|
| 42 | Template.indent(["return fallback();"]),
|
---|
| 43 | "}"
|
---|
| 44 | ]);
|
---|
| 45 | const generateBeforeLoadBinaryCode = path =>
|
---|
| 46 | Template.asString([
|
---|
| 47 | "var useFetch = typeof document !== 'undefined' || typeof self !== 'undefined';",
|
---|
| 48 | `var wasmUrl = ${path};`
|
---|
| 49 | ]);
|
---|
| 50 | /**
|
---|
| 51 | * @type {(path: string) => string}
|
---|
| 52 | */
|
---|
| 53 | const generateLoadBinaryCode = () =>
|
---|
| 54 | Template.asString([
|
---|
| 55 | "(useFetch",
|
---|
| 56 | Template.indent([
|
---|
| 57 | `? fetch(new URL(wasmUrl, ${compilation.outputOptions.importMetaName}.url))`
|
---|
| 58 | ]),
|
---|
| 59 | Template.indent([
|
---|
| 60 | ": Promise.all([import('fs'), import('url')]).then(([{ readFile }, { URL }]) => new Promise((resolve, reject) => {",
|
---|
| 61 | Template.indent([
|
---|
| 62 | `readFile(new URL(wasmUrl, ${compilation.outputOptions.importMetaName}.url), (err, buffer) => {`,
|
---|
| 63 | Template.indent([
|
---|
| 64 | "if (err) return reject(err);",
|
---|
| 65 | "",
|
---|
| 66 | "// Fake fetch response",
|
---|
| 67 | "resolve({",
|
---|
| 68 | Template.indent(["arrayBuffer() { return buffer; }"]),
|
---|
| 69 | "});"
|
---|
| 70 | ]),
|
---|
| 71 | "});"
|
---|
| 72 | ]),
|
---|
| 73 | "})))"
|
---|
| 74 | ])
|
---|
| 75 | ]);
|
---|
| 76 |
|
---|
| 77 | compilation.hooks.runtimeRequirementInTree
|
---|
| 78 | .for(RuntimeGlobals.instantiateWasm)
|
---|
| 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 | ) {
|
---|
| 87 | return;
|
---|
| 88 | }
|
---|
| 89 | compilation.addRuntimeModule(
|
---|
| 90 | chunk,
|
---|
| 91 | new AsyncWasmLoadingRuntimeModule({
|
---|
| 92 | generateBeforeLoadBinaryCode,
|
---|
| 93 | generateLoadBinaryCode,
|
---|
| 94 | generateBeforeInstantiateStreaming,
|
---|
| 95 | supportsStreaming: true
|
---|
| 96 | })
|
---|
| 97 | );
|
---|
| 98 | });
|
---|
| 99 | });
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | module.exports = UniversalCompileAsyncWasmPlugin;
|
---|