source: imaps-frontend/node_modules/webpack/lib/wasm-async/AsyncWasmLoadingRuntimeModule.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 4.3 KB
RevLine 
[79a0317]1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const RuntimeGlobals = require("../RuntimeGlobals");
9const RuntimeModule = require("../RuntimeModule");
10const Template = require("../Template");
11
12/** @typedef {import("../Chunk")} Chunk */
13/** @typedef {import("../Compilation")} Compilation */
14
15/**
16 * @typedef {object} AsyncWasmLoadingRuntimeModuleOptions
17 * @property {(function(string): string)=} generateBeforeLoadBinaryCode
18 * @property {function(string): string} generateLoadBinaryCode
19 * @property {(function(): string)=} generateBeforeInstantiateStreaming
20 * @property {boolean} supportsStreaming
21 */
22
23class AsyncWasmLoadingRuntimeModule extends RuntimeModule {
24 /**
25 * @param {AsyncWasmLoadingRuntimeModuleOptions} options options
26 */
27 constructor({
28 generateLoadBinaryCode,
29 generateBeforeLoadBinaryCode,
30 generateBeforeInstantiateStreaming,
31 supportsStreaming
32 }) {
33 super("wasm loading", RuntimeModule.STAGE_NORMAL);
34 this.generateLoadBinaryCode = generateLoadBinaryCode;
35 this.generateBeforeLoadBinaryCode = generateBeforeLoadBinaryCode;
36 this.generateBeforeInstantiateStreaming =
37 generateBeforeInstantiateStreaming;
38 this.supportsStreaming = supportsStreaming;
39 }
40
41 /**
42 * @returns {string | null} runtime code
43 */
44 generate() {
45 const compilation = /** @type {Compilation} */ (this.compilation);
46 const chunk = /** @type {Chunk} */ (this.chunk);
47 const { outputOptions, runtimeTemplate } = compilation;
48 const fn = RuntimeGlobals.instantiateWasm;
49 const wasmModuleSrcPath = compilation.getPath(
50 JSON.stringify(outputOptions.webassemblyModuleFilename),
51 {
52 hash: `" + ${RuntimeGlobals.getFullHash}() + "`,
53 hashWithLength: length =>
54 `" + ${RuntimeGlobals.getFullHash}}().slice(0, ${length}) + "`,
55 module: {
56 id: '" + wasmModuleId + "',
57 hash: '" + wasmModuleHash + "',
58 hashWithLength(length) {
59 return `" + wasmModuleHash.slice(0, ${length}) + "`;
60 }
61 },
62 runtime: chunk.runtime
63 }
64 );
65
66 const loader = this.generateLoadBinaryCode(wasmModuleSrcPath);
67 const fallback = [
68 `.then(${runtimeTemplate.returningFunction("x.arrayBuffer()", "x")})`,
69 `.then(${runtimeTemplate.returningFunction(
70 "WebAssembly.instantiate(bytes, importsObj)",
71 "bytes"
72 )})`,
73 `.then(${runtimeTemplate.returningFunction(
74 "Object.assign(exports, res.instance.exports)",
75 "res"
76 )})`
77 ];
78 const getStreaming = () => {
79 const concat = (/** @type {string[]} */ ...text) => text.join("");
80 return [
81 this.generateBeforeLoadBinaryCode
82 ? this.generateBeforeLoadBinaryCode(wasmModuleSrcPath)
83 : "",
84 `var req = ${loader};`,
85 `var fallback = ${runtimeTemplate.returningFunction(
86 Template.asString(["req", Template.indent(fallback)])
87 )};`,
88 concat(
89 "return req.then(",
90 runtimeTemplate.basicFunction("res", [
91 'if (typeof WebAssembly.instantiateStreaming === "function") {',
92 Template.indent(
93 this.generateBeforeInstantiateStreaming
94 ? this.generateBeforeInstantiateStreaming()
95 : ""
96 ),
97 Template.indent([
98 "return WebAssembly.instantiateStreaming(res, importsObj)",
99 Template.indent([
100 ".then(",
101 Template.indent([
102 `${runtimeTemplate.returningFunction(
103 "Object.assign(exports, res.instance.exports)",
104 "res"
105 )},`,
106 runtimeTemplate.basicFunction("e", [
107 'if(res.headers.get("Content-Type") !== "application/wasm") {',
108 Template.indent([
109 '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);',
110 "return fallback();"
111 ]),
112 "}",
113 "throw e;"
114 ])
115 ]),
116 ");"
117 ])
118 ]),
119 "}",
120 "return fallback();"
121 ]),
122 ");"
123 )
124 ];
125 };
126
127 return `${fn} = ${runtimeTemplate.basicFunction(
128 "exports, wasmModuleId, wasmModuleHash, importsObj",
129 this.supportsStreaming
130 ? getStreaming()
131 : [
132 this.generateBeforeLoadBinaryCode
133 ? this.generateBeforeLoadBinaryCode(wasmModuleSrcPath)
134 : "",
135 `return ${loader}`,
136 `${Template.indent(fallback)};`
137 ]
138 )};`;
139 }
140}
141
142module.exports = AsyncWasmLoadingRuntimeModule;
Note: See TracBrowser for help on using the repository browser.