Skip to content

Commit b4450e9

Browse files
fix(wasm): parse loader output for .wasm as JavaScript under the asyncWebAssembly "auto" default
Keep the "auto" marker after resolving, like css/html, so AsyncWebAssemblyModulesPlugin steps aside for modules loaders were applied to. Claude-Session: https://claude.ai/code/session_01SMjCyxFJifzUnweGXwKtzy
1 parent 12b414a commit b4450e9

11 files changed

Lines changed: 145 additions & 57 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Parse loader output for `.wasm` as JavaScript when async wasm is auto-enabled.

‎lib/config/defaults.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ const applyExperimentsDefaults = (
674674

675675
// Enabled implicitly the value stays `"auto"`, and the plugins then step
676676
// aside per module for requests loaders were applied to.
677-
// TODO in the next major release: css/html default to `true`, drop the `"auto"` marker.
677+
// TODO in the next major release: css/html/asyncWebAssembly default to `true`, drop the `"auto"` marker.
678678
if (experiments.css === "auto") {
679679
experiments.css =
680680
!(
@@ -703,7 +703,8 @@ const applyExperimentsDefaults = (
703703
if (experiments.asyncWebAssembly === "auto") {
704704
experiments.asyncWebAssembly =
705705
!experiments.syncWebAssembly &&
706-
!RuleSetCompiler.hasRuleForResource(rules, "/file.wasm");
706+
!RuleSetCompiler.hasRuleForResource(rules, "/file.wasm") &&
707+
"auto";
707708
}
708709

709710
if (typeof experiments.buildHttp === "object") {

‎lib/util/implicitTypeLoaderFallback.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ const { parseResource } = require("./identifier");
1414

1515
/**
1616
* When a built-in module type is only enabled implicitly (`experiments.css` /
17-
* `experiments.html` resolved from their `"auto"` default), modules that reach
17+
* `experiments.html` / `experiments.asyncWebAssembly` resolved from their `"auto"` default), modules that reach
1818
* the factory with loaders applied — an inline request or loaders injected via
1919
* hooks (e.g. html-webpack-plugin's template loader) — keep the pre-existing
2020
* behavior: the loader result is parsed as JavaScript, not as the built-in type.
2121
* A `!=!` matchResource is an explicit re-type of the loader result and wins.
2222
*
23-
* TODO in the next major release: css/html are enabled unconditionally (`experiments.*: true`),
23+
* TODO in the next major release: css/html/asyncWebAssembly are enabled unconditionally (`experiments.*: true`),
2424
* so this implicit-only workaround can be removed together with the `"auto"`
2525
* marker in `applyExperimentsDefaults`.
2626
* @param {NormalModuleFactory} normalModuleFactory the normal module factory

‎lib/wasm/AsyncWebAssemblyModulesPlugin.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const {
1414
} = require("../module/ModuleTypeConstants");
1515
const { compareModulesByFullName } = require("../util/comparators");
1616
const createHooksRegistry = require("../util/createHooksRegistry");
17+
const implicitTypeLoaderFallback = require("../util/implicitTypeLoaderFallback");
1718
const lazyModule = require("../util/lazyModule");
1819
const preloadModuleType = require("../util/preloadModuleType");
1920
const AsyncWasmModule = require("./AsyncWasmModule");
@@ -80,12 +81,25 @@ class AsyncWebAssemblyModulesPlugin {
8081
* @returns {void}
8182
*/
8283
apply(compiler) {
84+
// `"auto"` marks implicit enablement, the one case where loaders win over
85+
// the built-in type — see `applyExperimentsDefaults`.
86+
// TODO in the next major release: asyncWebAssembly defaults to `true`, drop this implicit-only fallback.
87+
const implicitlyEnabled =
88+
compiler.options.experiments.asyncWebAssembly === "auto";
8389
// A binary's name may carry the compilation hash, which the analyzable call site
8490
// leaves as a stand-in — so whatever the chunk loading is, the pass that fills it
8591
// in has to be there. Applying it twice is a no-op.
8692
compiler.hooks.compilation.tap(
8793
PLUGIN_NAME,
8894
(compilation, { normalModuleFactory }) => {
95+
if (implicitlyEnabled) {
96+
implicitTypeLoaderFallback(
97+
normalModuleFactory,
98+
PLUGIN_NAME,
99+
/\.wasm$/i,
100+
WEBASSEMBLY_MODULE_TYPE_ASYNC
101+
);
102+
}
89103
const hooks =
90104
AsyncWebAssemblyModulesPlugin.getCompilationHooks(compilation);
91105
compilation.dependencyFactories.set(
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
it("should fall back to javascript for an inline loader request", () => {
2+
expect(require("./js-loader.js!./inline.wasm")).toBe(
3+
"LOADED_BY_CUSTOM_LOADER"
4+
);
5+
});
6+
7+
it("should fall back to javascript for a hook-injected loader", () => {
8+
expect(require("./injected.wasm")).toBe("LOADED_BY_CUSTOM_LOADER");
9+
});
10+
11+
it("should keep the built-in async wasm type for loader-free .wasm files", () =>
12+
import("./plain.wasm").then((wasm) => {
13+
expect(wasm.add(1, 2)).toBe(3);
14+
}));
41 Bytes
Binary file not shown.
41 Bytes
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
3+
// Stand-in for a loader whose result is JavaScript, not WebAssembly.
4+
/** @type {import("../../../../").LoaderDefinition} */
5+
module.exports = function () {
6+
return `module.exports = ${JSON.stringify("LOADED_BY_CUSTOM_LOADER")};`;
7+
};
41 Bytes
Binary file not shown.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"use strict";
2+
3+
const path = require("path");
4+
5+
const PLUGIN_NAME = "InjectLoaderPlugin";
6+
7+
/**
8+
* `experiments.asyncWebAssembly` stays at its "auto" default, with no `.wasm`
9+
* rule in `module.rules`, yet loaders still reach `.wasm` modules through an
10+
* inline request or a plugin hook. Those modules must fall back to javascript.
11+
* @type {import("../../../../").Configuration}
12+
*/
13+
module.exports = {
14+
plugins: [
15+
(compiler) => {
16+
compiler.hooks.normalModuleFactory.tap(PLUGIN_NAME, (nmf) => {
17+
nmf.hooks.afterResolve.tap(PLUGIN_NAME, (resolveData) => {
18+
const createData = resolveData.createData;
19+
if (/injected\.wasm$/.test(String(createData.resource))) {
20+
/** @type {NonNullable<(typeof createData)["loaders"]>} */
21+
(createData.loaders).push({
22+
loader: path.resolve(__dirname, "js-loader.js"),
23+
options: undefined
24+
});
25+
}
26+
});
27+
});
28+
}
29+
]
30+
};

0 commit comments

Comments
 (0)