fix: wasm loading for sync and async webassembly

This commit is contained in:
alexander.akait 2024-11-13 22:43:12 +03:00
parent 038e51c4b5
commit 67543070b2
9 changed files with 130 additions and 16 deletions

View File

@ -242,7 +242,13 @@ const applyWebpackOptionsDefaults = (options, compilerIndex) => {
(options.experiments.outputModule), (options.experiments.outputModule),
development, development,
entry: options.entry, entry: options.entry,
futureDefaults futureDefaults,
syncWebAssembly:
/** @type {NonNullable<ExperimentsNormalized["syncWebAssembly"]>} */
(options.experiments.syncWebAssembly),
asyncWebAssembly:
/** @type {NonNullable<ExperimentsNormalized["asyncWebAssembly"]>} */
(options.experiments.asyncWebAssembly)
}); });
applyModuleDefaults(options.module, { applyModuleDefaults(options.module, {
@ -868,6 +874,8 @@ const applyModuleDefaults = (
* @param {boolean} options.development is development mode * @param {boolean} options.development is development mode
* @param {Entry} options.entry entry option * @param {Entry} options.entry entry option
* @param {boolean} options.futureDefaults is future defaults enabled * @param {boolean} options.futureDefaults is future defaults enabled
* @param {boolean} options.syncWebAssembly is syncWebAssembly enabled
* @param {boolean} options.asyncWebAssembly is asyncWebAssembly enabled
* @returns {void} * @returns {void}
*/ */
const applyOutputDefaults = ( const applyOutputDefaults = (
@ -879,7 +887,9 @@ const applyOutputDefaults = (
outputModule, outputModule,
development, development,
entry, entry,
futureDefaults futureDefaults,
syncWebAssembly,
asyncWebAssembly
} }
) => { ) => {
/** /**
@ -1171,8 +1181,14 @@ const applyOutputDefaults = (
F(output, "wasmLoading", () => { F(output, "wasmLoading", () => {
if (tp) { if (tp) {
if (tp.fetchWasm) return "fetch"; if (tp.fetchWasm) return "fetch";
if (tp.nodeBuiltins) if (tp.nodeBuiltins) {
return output.module ? "async-node-module" : "async-node"; if (asyncWebAssembly) {
return "async-node-module";
} else if (syncWebAssembly) {
return "async-node";
}
}
if (tp.nodeBuiltins === null || tp.fetchWasm === null) { if (tp.nodeBuiltins === null || tp.fetchWasm === null) {
return "universal"; return "universal";
} }

View File

@ -100,9 +100,10 @@ class EnableWasmLoadingPlugin {
case "async-node-module": { case "async-node-module": {
// @ts-expect-error typescript bug for duplicate require // @ts-expect-error typescript bug for duplicate require
const ReadFileCompileAsyncWasmPlugin = require("../node/ReadFileCompileAsyncWasmPlugin"); const ReadFileCompileAsyncWasmPlugin = require("../node/ReadFileCompileAsyncWasmPlugin");
new ReadFileCompileAsyncWasmPlugin({ type, import: true }).apply( new ReadFileCompileAsyncWasmPlugin({
compiler type,
); import: compiler.options.output.environment.dynamicImport
}).apply(compiler);
break; break;
} }
case "universal": case "universal":

View File

@ -1364,8 +1364,10 @@ describe("snapshots", () => {
- "import-scripts", - "import-scripts",
+ "require", + "require",
@@ ... @@ @@ ... @@
- "enabledWasmLoadingTypes": Array [
- "fetch", - "fetch",
+ "async-node", - ],
+ "enabledWasmLoadingTypes": Array [],
@@ ... @@ @@ ... @@
- "document": true, - "document": true,
+ "document": false, + "document": false,
@ -1377,13 +1379,13 @@ describe("snapshots", () => {
+ "publicPath": "", + "publicPath": "",
@@ ... @@ @@ ... @@
- "wasmLoading": "fetch", - "wasmLoading": "fetch",
+ "wasmLoading": "async-node", + "wasmLoading": false,
@@ ... @@ @@ ... @@
- "workerChunkLoading": "import-scripts", - "workerChunkLoading": "import-scripts",
+ "workerChunkLoading": "require", + "workerChunkLoading": "require",
@@ ... @@ @@ ... @@
- "workerWasmLoading": "fetch", - "workerWasmLoading": "fetch",
+ "workerWasmLoading": "async-node", + "workerWasmLoading": false,
@@ ... @@ @@ ... @@
- "aliasFields": Array [ - "aliasFields": Array [
- "browser", - "browser",
@ -1521,8 +1523,10 @@ describe("snapshots", () => {
- "import-scripts", - "import-scripts",
+ "require", + "require",
@@ ... @@ @@ ... @@
- "enabledWasmLoadingTypes": Array [
- "fetch", - "fetch",
+ "async-node", - ],
+ "enabledWasmLoadingTypes": Array [],
@@ ... @@ @@ ... @@
- "document": true, - "document": true,
+ "document": false, + "document": false,
@ -1534,13 +1538,13 @@ describe("snapshots", () => {
+ "publicPath": "", + "publicPath": "",
@@ ... @@ @@ ... @@
- "wasmLoading": "fetch", - "wasmLoading": "fetch",
+ "wasmLoading": "async-node", + "wasmLoading": false,
@@ ... @@ @@ ... @@
- "workerChunkLoading": "import-scripts", - "workerChunkLoading": "import-scripts",
+ "workerChunkLoading": "require", + "workerChunkLoading": "require",
@@ ... @@ @@ ... @@
- "workerWasmLoading": "fetch", - "workerWasmLoading": "fetch",
+ "workerWasmLoading": "async-node", + "workerWasmLoading": false,
@@ ... @@ @@ ... @@
- "aliasFields": Array [ - "aliasFields": Array [
- "browser", - "browser",
@ -1654,8 +1658,10 @@ describe("snapshots", () => {
- "import-scripts", - "import-scripts",
+ "require", + "require",
@@ ... @@ @@ ... @@
- "enabledWasmLoadingTypes": Array [
- "fetch", - "fetch",
+ "async-node", - ],
+ "enabledWasmLoadingTypes": Array [],
@@ ... @@ @@ ... @@
- "document": true, - "document": true,
+ "document": false, + "document": false,
@ -1667,13 +1673,13 @@ describe("snapshots", () => {
+ "publicPath": "", + "publicPath": "",
@@ ... @@ @@ ... @@
- "wasmLoading": "fetch", - "wasmLoading": "fetch",
+ "wasmLoading": "async-node", + "wasmLoading": false,
@@ ... @@ @@ ... @@
- "workerChunkLoading": "import-scripts", - "workerChunkLoading": "import-scripts",
+ "workerChunkLoading": "require", + "workerChunkLoading": "require",
@@ ... @@ @@ ... @@
- "workerWasmLoading": "fetch", - "workerWasmLoading": "fetch",
+ "workerWasmLoading": "async-node", + "workerWasmLoading": false,
@@ ... @@ @@ ... @@
- "aliasFields": Array [ - "aliasFields": Array [
- "browser", - "browser",

View File

@ -0,0 +1,6 @@
it("should work", function() {
return import("./module").then(function(module) {
const result = module.run();
expect(result).toEqual(84);
});
});

View File

@ -0,0 +1,6 @@
import { getNumber } from "./wasm.wat?1";
import { getNumber as getNumber2 } from "./wasm.wat?2";
export function run() {
return getNumber() + getNumber2();
}

View File

@ -0,0 +1,5 @@
module.exports = {
findBundle: function (i, options) {
return i === 0 ? ["bundle0.mjs"] : [`bundle${i}.js`];
}
};

View File

@ -0,0 +1,5 @@
var supportsWebAssembly = require("../../../helpers/supportsWebAssembly");
module.exports = function (config) {
return supportsWebAssembly();
};

View File

@ -0,0 +1,10 @@
(module
(type $t0 (func (param i32 i32) (result i32)))
(type $t1 (func (result i32)))
(func $add (export "add") (type $t0) (param $p0 i32) (param $p1 i32) (result i32)
(i32.add
(get_local $p0)
(get_local $p1)))
(func $getNumber (export "getNumber") (type $t1) (result i32)
(i32.const 42)))

View File

@ -0,0 +1,59 @@
/** @type {import("../../../../").Configuration[]} */
module.exports = [
{
module: {
rules: [
{
test: /\.wat$/,
loader: "wast-loader",
type: "webassembly/async"
}
]
},
output: {
module: true,
webassemblyModuleFilename: "[id].[hash].wasm"
},
experiments: {
outputModule: true,
asyncWebAssembly: true
}
},
{
target: "node",
module: {
rules: [
{
test: /\.wat$/,
loader: "wast-loader",
type: "webassembly/async"
}
]
},
output: {
webassemblyModuleFilename: "[id].[hash].wasm"
},
experiments: {
asyncWebAssembly: true
}
},
{
target: "node",
module: {
rules: [
{
test: /\.wat$/,
loader: "wast-loader",
type: "webassembly/sync"
}
]
},
output: {
module: false,
webassemblyModuleFilename: "[id].[hash].wasm"
},
experiments: {
syncWebAssembly: true
}
}
];