diff --git a/lib/config/defaults.js b/lib/config/defaults.js index 2beed0a72..2222a84d9 100644 --- a/lib/config/defaults.js +++ b/lib/config/defaults.js @@ -242,7 +242,13 @@ const applyWebpackOptionsDefaults = (options, compilerIndex) => { (options.experiments.outputModule), development, entry: options.entry, - futureDefaults + futureDefaults, + syncWebAssembly: + /** @type {NonNullable} */ + (options.experiments.syncWebAssembly), + asyncWebAssembly: + /** @type {NonNullable} */ + (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"; } diff --git a/lib/wasm/EnableWasmLoadingPlugin.js b/lib/wasm/EnableWasmLoadingPlugin.js index d287ce9d9..b44e120ee 100644 --- a/lib/wasm/EnableWasmLoadingPlugin.js +++ b/lib/wasm/EnableWasmLoadingPlugin.js @@ -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": diff --git a/test/Defaults.unittest.js b/test/Defaults.unittest.js index 9bcc21d43..84ce5199b 100644 --- a/test/Defaults.unittest.js +++ b/test/Defaults.unittest.js @@ -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", diff --git a/test/configCases/wasm/async-node-module/index.js b/test/configCases/wasm/async-node-module/index.js new file mode 100644 index 000000000..05e484096 --- /dev/null +++ b/test/configCases/wasm/async-node-module/index.js @@ -0,0 +1,6 @@ +it("should work", function() { + return import("./module").then(function(module) { + const result = module.run(); + expect(result).toEqual(84); + }); +}); diff --git a/test/configCases/wasm/async-node-module/module.js b/test/configCases/wasm/async-node-module/module.js new file mode 100644 index 000000000..a10de6845 --- /dev/null +++ b/test/configCases/wasm/async-node-module/module.js @@ -0,0 +1,6 @@ +import { getNumber } from "./wasm.wat?1"; +import { getNumber as getNumber2 } from "./wasm.wat?2"; + +export function run() { + return getNumber() + getNumber2(); +} diff --git a/test/configCases/wasm/async-node-module/test.config.js b/test/configCases/wasm/async-node-module/test.config.js new file mode 100644 index 000000000..f16944d36 --- /dev/null +++ b/test/configCases/wasm/async-node-module/test.config.js @@ -0,0 +1,5 @@ +module.exports = { + findBundle: function (i, options) { + return i === 0 ? ["bundle0.mjs"] : [`bundle${i}.js`]; + } +}; diff --git a/test/configCases/wasm/async-node-module/test.filter.js b/test/configCases/wasm/async-node-module/test.filter.js new file mode 100644 index 000000000..bd7f4573a --- /dev/null +++ b/test/configCases/wasm/async-node-module/test.filter.js @@ -0,0 +1,5 @@ +var supportsWebAssembly = require("../../../helpers/supportsWebAssembly"); + +module.exports = function (config) { + return supportsWebAssembly(); +}; diff --git a/test/configCases/wasm/async-node-module/wasm.wat b/test/configCases/wasm/async-node-module/wasm.wat new file mode 100644 index 000000000..3a1352710 --- /dev/null +++ b/test/configCases/wasm/async-node-module/wasm.wat @@ -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))) + diff --git a/test/configCases/wasm/async-node-module/webpack.config.js b/test/configCases/wasm/async-node-module/webpack.config.js new file mode 100644 index 000000000..d30d9f4d8 --- /dev/null +++ b/test/configCases/wasm/async-node-module/webpack.config.js @@ -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 + } + } +];