| [9af201e] | 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 RuntimeGlobals = require("../RuntimeGlobals");
|
|---|
| 9 | const RuntimeModule = require("../RuntimeModule");
|
|---|
| 10 | const Template = require("../Template");
|
|---|
| 11 |
|
|---|
| 12 | /** @typedef {import("../Chunk")} Chunk */
|
|---|
| 13 | /** @typedef {import("../Compilation")} Compilation */
|
|---|
| 14 |
|
|---|
| 15 | /** @typedef {(wasmModuleSrcPath: string) => string} GenerateBeforeLoadBinaryCode */
|
|---|
| 16 | /** @typedef {(wasmModuleSrcPath: string) => string} GenerateLoadBinaryCode */
|
|---|
| 17 | /** @typedef {() => string} GenerateBeforeInstantiateStreaming */
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * @typedef {object} AsyncWasmLoadingRuntimeModuleOptions
|
|---|
| 21 | * @property {GenerateLoadBinaryCode} generateLoadBinaryCode
|
|---|
| 22 | * @property {GenerateBeforeLoadBinaryCode=} generateBeforeLoadBinaryCode
|
|---|
| 23 | * @property {GenerateBeforeInstantiateStreaming=} generateBeforeInstantiateStreaming
|
|---|
| 24 | * @property {boolean} supportsStreaming
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 | class AsyncWasmLoadingRuntimeModule extends RuntimeModule {
|
|---|
| 28 | /**
|
|---|
| 29 | * @param {AsyncWasmLoadingRuntimeModuleOptions} options options
|
|---|
| 30 | */
|
|---|
| 31 | constructor({
|
|---|
| 32 | generateLoadBinaryCode,
|
|---|
| 33 | generateBeforeLoadBinaryCode,
|
|---|
| 34 | generateBeforeInstantiateStreaming,
|
|---|
| 35 | supportsStreaming
|
|---|
| 36 | }) {
|
|---|
| 37 | super("wasm loading", RuntimeModule.STAGE_NORMAL);
|
|---|
| 38 | /** @type {GenerateLoadBinaryCode} */
|
|---|
| 39 | this.generateLoadBinaryCode = generateLoadBinaryCode;
|
|---|
| 40 | /** @type {generateBeforeLoadBinaryCode | undefined} */
|
|---|
| 41 | this.generateBeforeLoadBinaryCode = generateBeforeLoadBinaryCode;
|
|---|
| 42 | /** @type {generateBeforeInstantiateStreaming | undefined} */
|
|---|
| 43 | this.generateBeforeInstantiateStreaming =
|
|---|
| 44 | generateBeforeInstantiateStreaming;
|
|---|
| 45 | /** @type {boolean} */
|
|---|
| 46 | this.supportsStreaming = supportsStreaming;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | /**
|
|---|
| 50 | * Generates runtime code for this runtime module.
|
|---|
| 51 | * @returns {string | null} runtime code
|
|---|
| 52 | */
|
|---|
| 53 | generate() {
|
|---|
| 54 | const compilation = /** @type {Compilation} */ (this.compilation);
|
|---|
| 55 | const chunk = /** @type {Chunk} */ (this.chunk);
|
|---|
| 56 | const { outputOptions, runtimeTemplate } = compilation;
|
|---|
| 57 | const fn = RuntimeGlobals.instantiateWasm;
|
|---|
| 58 | const wasmModuleSrcPath = compilation.getPath(
|
|---|
| 59 | JSON.stringify(outputOptions.webassemblyModuleFilename),
|
|---|
| 60 | {
|
|---|
| 61 | hash: `" + ${RuntimeGlobals.getFullHash}() + "`,
|
|---|
| 62 | hashWithLength: (length) =>
|
|---|
| 63 | `" + ${RuntimeGlobals.getFullHash}}().slice(0, ${length}) + "`,
|
|---|
| 64 | module: {
|
|---|
| 65 | id: '" + wasmModuleId + "',
|
|---|
| 66 | hash: '" + wasmModuleHash + "',
|
|---|
| 67 | hashWithLength(length) {
|
|---|
| 68 | return `" + wasmModuleHash.slice(0, ${length}) + "`;
|
|---|
| 69 | }
|
|---|
| 70 | },
|
|---|
| 71 | runtime: chunk.runtime
|
|---|
| 72 | }
|
|---|
| 73 | );
|
|---|
| 74 |
|
|---|
| 75 | const loader = this.generateLoadBinaryCode(wasmModuleSrcPath);
|
|---|
| 76 | const fallback = [
|
|---|
| 77 | `.then(${runtimeTemplate.returningFunction("x.arrayBuffer()", "x")})`,
|
|---|
| 78 | `.then(${runtimeTemplate.returningFunction(
|
|---|
| 79 | "WebAssembly.instantiate(bytes, importsObj)",
|
|---|
| 80 | "bytes"
|
|---|
| 81 | )})`,
|
|---|
| 82 | `.then(${runtimeTemplate.returningFunction(
|
|---|
| 83 | "Object.assign(exports, res.instance.exports)",
|
|---|
| 84 | "res"
|
|---|
| 85 | )})`
|
|---|
| 86 | ];
|
|---|
| 87 | const getStreaming = () => {
|
|---|
| 88 | /**
|
|---|
| 89 | * @param {string[]} text text
|
|---|
| 90 | * @returns {string} merged text
|
|---|
| 91 | */
|
|---|
| 92 | const concat = (...text) => text.join("");
|
|---|
| 93 | return [
|
|---|
| 94 | this.generateBeforeLoadBinaryCode
|
|---|
| 95 | ? this.generateBeforeLoadBinaryCode(wasmModuleSrcPath)
|
|---|
| 96 | : "",
|
|---|
| 97 | `var req = ${loader};`,
|
|---|
| 98 | `var fallback = ${runtimeTemplate.returningFunction(
|
|---|
| 99 | Template.asString(["req", Template.indent(fallback)])
|
|---|
| 100 | )};`,
|
|---|
| 101 | concat(
|
|---|
| 102 | "return req.then(",
|
|---|
| 103 | runtimeTemplate.basicFunction("res", [
|
|---|
| 104 | 'if (typeof WebAssembly.instantiateStreaming === "function") {',
|
|---|
| 105 | Template.indent(
|
|---|
| 106 | this.generateBeforeInstantiateStreaming
|
|---|
| 107 | ? this.generateBeforeInstantiateStreaming()
|
|---|
| 108 | : ""
|
|---|
| 109 | ),
|
|---|
| 110 | Template.indent([
|
|---|
| 111 | "return WebAssembly.instantiateStreaming(res, importsObj)",
|
|---|
| 112 | Template.indent([
|
|---|
| 113 | ".then(",
|
|---|
| 114 | Template.indent([
|
|---|
| 115 | `${runtimeTemplate.returningFunction(
|
|---|
| 116 | "Object.assign(exports, res.instance.exports)",
|
|---|
| 117 | "res"
|
|---|
| 118 | )},`,
|
|---|
| 119 | runtimeTemplate.basicFunction("e", [
|
|---|
| 120 | 'if(res.headers.get("Content-Type") !== "application/wasm") {',
|
|---|
| 121 | Template.indent([
|
|---|
| 122 | 'console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\\n", e);',
|
|---|
| 123 | "return fallback();"
|
|---|
| 124 | ]),
|
|---|
| 125 | "}",
|
|---|
| 126 | "throw e;"
|
|---|
| 127 | ])
|
|---|
| 128 | ]),
|
|---|
| 129 | ");"
|
|---|
| 130 | ])
|
|---|
| 131 | ]),
|
|---|
| 132 | "}",
|
|---|
| 133 | "return fallback();"
|
|---|
| 134 | ]),
|
|---|
| 135 | ");"
|
|---|
| 136 | )
|
|---|
| 137 | ];
|
|---|
| 138 | };
|
|---|
| 139 |
|
|---|
| 140 | return `${fn} = ${runtimeTemplate.basicFunction(
|
|---|
| 141 | "exports, wasmModuleId, wasmModuleHash, importsObj",
|
|---|
| 142 | this.supportsStreaming
|
|---|
| 143 | ? getStreaming()
|
|---|
| 144 | : [
|
|---|
| 145 | this.generateBeforeLoadBinaryCode
|
|---|
| 146 | ? this.generateBeforeLoadBinaryCode(wasmModuleSrcPath)
|
|---|
| 147 | : "",
|
|---|
| 148 | `return ${loader}`,
|
|---|
| 149 | `${Template.indent(fallback)};`
|
|---|
| 150 | ]
|
|---|
| 151 | )};`;
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | module.exports = AsyncWasmLoadingRuntimeModule;
|
|---|