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 { 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 | /**
|
---|
17 | * @typedef {object} ReadFileCompileAsyncWasmPluginOptions
|
---|
18 | * @property {boolean} [import] use import?
|
---|
19 | */
|
---|
20 |
|
---|
21 | const PLUGIN_NAME = "ReadFileCompileAsyncWasmPlugin";
|
---|
22 |
|
---|
23 | class ReadFileCompileAsyncWasmPlugin {
|
---|
24 | /**
|
---|
25 | * @param {ReadFileCompileAsyncWasmPluginOptions} [options] options object
|
---|
26 | */
|
---|
27 | constructor({ import: useImport = false } = {}) {
|
---|
28 | this._import = useImport;
|
---|
29 | }
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Apply the plugin
|
---|
33 | * @param {Compiler} compiler the compiler instance
|
---|
34 | * @returns {void}
|
---|
35 | */
|
---|
36 | apply(compiler) {
|
---|
37 | compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => {
|
---|
38 | const globalWasmLoading = compilation.outputOptions.wasmLoading;
|
---|
39 | /**
|
---|
40 | * @param {Chunk} chunk chunk
|
---|
41 | * @returns {boolean} true, if wasm loading is enabled for the chunk
|
---|
42 | */
|
---|
43 | const isEnabledForChunk = chunk => {
|
---|
44 | const options = chunk.getEntryOptions();
|
---|
45 | const wasmLoading =
|
---|
46 | options && options.wasmLoading !== undefined
|
---|
47 | ? options.wasmLoading
|
---|
48 | : globalWasmLoading;
|
---|
49 | return wasmLoading === "async-node";
|
---|
50 | };
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * @param {string} path path to wasm file
|
---|
54 | * @returns {string} generated code to load the wasm file
|
---|
55 | */
|
---|
56 | const generateLoadBinaryCode = this._import
|
---|
57 | ? path =>
|
---|
58 | Template.asString([
|
---|
59 | "Promise.all([import('fs'), import('url')]).then(([{ readFile }, { URL }]) => new Promise((resolve, reject) => {",
|
---|
60 | Template.indent([
|
---|
61 | `readFile(new URL(${path}, ${compilation.outputOptions.importMetaName}.url), (err, buffer) => {`,
|
---|
62 | Template.indent([
|
---|
63 | "if (err) return reject(err);",
|
---|
64 | "",
|
---|
65 | "// Fake fetch response",
|
---|
66 | "resolve({",
|
---|
67 | Template.indent(["arrayBuffer() { return buffer; }"]),
|
---|
68 | "});"
|
---|
69 | ]),
|
---|
70 | "});"
|
---|
71 | ]),
|
---|
72 | "}))"
|
---|
73 | ])
|
---|
74 | : path =>
|
---|
75 | Template.asString([
|
---|
76 | "new Promise(function (resolve, reject) {",
|
---|
77 | Template.indent([
|
---|
78 | "try {",
|
---|
79 | Template.indent([
|
---|
80 | "var { readFile } = require('fs');",
|
---|
81 | "var { join } = require('path');",
|
---|
82 | "",
|
---|
83 | `readFile(join(__dirname, ${path}), function(err, buffer){`,
|
---|
84 | Template.indent([
|
---|
85 | "if (err) return reject(err);",
|
---|
86 | "",
|
---|
87 | "// Fake fetch response",
|
---|
88 | "resolve({",
|
---|
89 | Template.indent(["arrayBuffer() { return buffer; }"]),
|
---|
90 | "});"
|
---|
91 | ]),
|
---|
92 | "});"
|
---|
93 | ]),
|
---|
94 | "} catch (err) { reject(err); }"
|
---|
95 | ]),
|
---|
96 | "})"
|
---|
97 | ]);
|
---|
98 |
|
---|
99 | compilation.hooks.runtimeRequirementInTree
|
---|
100 | .for(RuntimeGlobals.instantiateWasm)
|
---|
101 | .tap(PLUGIN_NAME, (chunk, set, { chunkGraph }) => {
|
---|
102 | if (!isEnabledForChunk(chunk)) return;
|
---|
103 | if (
|
---|
104 | !chunkGraph.hasModuleInGraph(
|
---|
105 | chunk,
|
---|
106 | m => m.type === WEBASSEMBLY_MODULE_TYPE_ASYNC
|
---|
107 | )
|
---|
108 | ) {
|
---|
109 | return;
|
---|
110 | }
|
---|
111 | compilation.addRuntimeModule(
|
---|
112 | chunk,
|
---|
113 | new AsyncWasmLoadingRuntimeModule({
|
---|
114 | generateLoadBinaryCode,
|
---|
115 | supportsStreaming: false
|
---|
116 | })
|
---|
117 | );
|
---|
118 | });
|
---|
119 | });
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | module.exports = ReadFileCompileAsyncWasmPlugin;
|
---|