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

View File

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

View File

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