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