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