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 | class AsyncWasmChunkLoadingRuntimeModule extends RuntimeModule {
|
---|
13 | constructor({ generateLoadBinaryCode, supportsStreaming }) {
|
---|
14 | super("wasm chunk loading", RuntimeModule.STAGE_ATTACH);
|
---|
15 | this.generateLoadBinaryCode = generateLoadBinaryCode;
|
---|
16 | this.supportsStreaming = supportsStreaming;
|
---|
17 | }
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * @returns {string} runtime code
|
---|
21 | */
|
---|
22 | generate() {
|
---|
23 | const { compilation, chunk } = this;
|
---|
24 | const { outputOptions, runtimeTemplate } = compilation;
|
---|
25 | const fn = RuntimeGlobals.instantiateWasm;
|
---|
26 | const wasmModuleSrcPath = compilation.getPath(
|
---|
27 | JSON.stringify(outputOptions.webassemblyModuleFilename),
|
---|
28 | {
|
---|
29 | hash: `" + ${RuntimeGlobals.getFullHash}() + "`,
|
---|
30 | hashWithLength: length =>
|
---|
31 | `" + ${RuntimeGlobals.getFullHash}}().slice(0, ${length}) + "`,
|
---|
32 | module: {
|
---|
33 | id: '" + wasmModuleId + "',
|
---|
34 | hash: `" + wasmModuleHash + "`,
|
---|
35 | hashWithLength(length) {
|
---|
36 | return `" + wasmModuleHash.slice(0, ${length}) + "`;
|
---|
37 | }
|
---|
38 | },
|
---|
39 | runtime: chunk.runtime
|
---|
40 | }
|
---|
41 | );
|
---|
42 | return `${fn} = ${runtimeTemplate.basicFunction(
|
---|
43 | "exports, wasmModuleId, wasmModuleHash, importsObj",
|
---|
44 | [
|
---|
45 | `var req = ${this.generateLoadBinaryCode(wasmModuleSrcPath)};`,
|
---|
46 | this.supportsStreaming
|
---|
47 | ? Template.asString([
|
---|
48 | "if (typeof WebAssembly.instantiateStreaming === 'function') {",
|
---|
49 | Template.indent([
|
---|
50 | "return WebAssembly.instantiateStreaming(req, importsObj)",
|
---|
51 | Template.indent([
|
---|
52 | `.then(${runtimeTemplate.returningFunction(
|
---|
53 | "Object.assign(exports, res.instance.exports)",
|
---|
54 | "res"
|
---|
55 | )});`
|
---|
56 | ])
|
---|
57 | ]),
|
---|
58 | "}"
|
---|
59 | ])
|
---|
60 | : "// no support for streaming compilation",
|
---|
61 | "return req",
|
---|
62 | Template.indent([
|
---|
63 | `.then(${runtimeTemplate.returningFunction("x.arrayBuffer()", "x")})`,
|
---|
64 | `.then(${runtimeTemplate.returningFunction(
|
---|
65 | "WebAssembly.instantiate(bytes, importsObj)",
|
---|
66 | "bytes"
|
---|
67 | )})`,
|
---|
68 | `.then(${runtimeTemplate.returningFunction(
|
---|
69 | "Object.assign(exports, res.instance.exports)",
|
---|
70 | "res"
|
---|
71 | )});`
|
---|
72 | ])
|
---|
73 | ]
|
---|
74 | )};`;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | module.exports = AsyncWasmChunkLoadingRuntimeModule;
|
---|