Skip to content

Commit 8cb8f1b

Browse files
committed
fix(wasm): unwrap eager and weak source imports
1 parent b4d6769 commit 8cb8f1b

6 files changed

Lines changed: 87 additions & 2 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+
Return WebAssembly modules directly from eager and weak source imports.

‎lib/dependencies/import/ImportEagerDependency.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"use strict";
77

88
const makeSerializable = require("../../util/makeSerializable");
9+
const { ImportPhaseUtils } = require("../core/ImportPhase");
910
const ImportDependency = require("./ImportDependency");
1011

1112
/** @import { ReplaceSource } from "webpack-sources" */
@@ -59,7 +60,7 @@ ImportEagerDependency.Template = class ImportEagerDependencyTemplate extends (
5960
{ runtimeTemplate, module, moduleGraph, chunkGraph, runtimeRequirements }
6061
) {
6162
const dep = /** @type {ImportEagerDependency} */ (dependency);
62-
const content = runtimeTemplate.moduleNamespacePromise({
63+
let content = runtimeTemplate.moduleNamespacePromise({
6364
chunkGraph,
6465
module: /** @type {Module} */ (moduleGraph.getModule(dep)),
6566
request: dep.request,
@@ -68,6 +69,12 @@ ImportEagerDependency.Template = class ImportEagerDependencyTemplate extends (
6869
dependency: dep,
6970
runtimeRequirements
7071
});
72+
if (ImportPhaseUtils.isSource(dep.phase)) {
73+
content = `${content}.then(${runtimeTemplate.returningFunction(
74+
'm["default"]',
75+
"m"
76+
)})`;
77+
}
7178

7279
source.replace(dep.range[0], dep.range[1] - 1, content);
7380
}

‎lib/dependencies/import/ImportWeakDependency.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"use strict";
77

88
const makeSerializable = require("../../util/makeSerializable");
9+
const { ImportPhaseUtils } = require("../core/ImportPhase");
910
const ImportDependency = require("./ImportDependency");
1011

1112
/** @import { ReplaceSource } from "webpack-sources" */
@@ -57,7 +58,7 @@ ImportWeakDependency.Template = class ImportDependencyTemplate extends (
5758
{ runtimeTemplate, module, moduleGraph, chunkGraph, runtimeRequirements }
5859
) {
5960
const dep = /** @type {ImportWeakDependency} */ (dependency);
60-
const content = runtimeTemplate.moduleNamespacePromise({
61+
let content = runtimeTemplate.moduleNamespacePromise({
6162
chunkGraph,
6263
module: /** @type {Module} */ (moduleGraph.getModule(dep)),
6364
request: dep.request,
@@ -67,6 +68,12 @@ ImportWeakDependency.Template = class ImportDependencyTemplate extends (
6768
dependency: dep,
6869
runtimeRequirements
6970
});
71+
if (ImportPhaseUtils.isSource(dep.phase)) {
72+
content = `${content}.then(${runtimeTemplate.returningFunction(
73+
'm["default"]',
74+
"m"
75+
)})`;
76+
}
7077

7178
source.replace(dep.range[0], dep.range[1] - 1, content);
7279
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
for (const [mode, load] of [
2+
["eager", () => import.source(/* webpackMode: "eager" */ "./wasm.wat")],
3+
["weak", () => import.source(/* webpackMode: "weak" */ "./wasm.wat")],
4+
["eager comment", () => import(/* webpackMode: "eager", webpackSource: true */ "./wasm.wat")],
5+
["weak comment", () => import(/* webpackMode: "weak", webpackSource: true */ "./wasm.wat")]
6+
]) {
7+
it(`should return a WebAssembly.Module for ${mode} source imports`, async () => {
8+
const expected = await import.source("./wasm.wat");
9+
const source = await load();
10+
expect(source).toBeInstanceOf(WebAssembly.Module);
11+
expect(source).toBe(expected);
12+
const instance = await WebAssembly.instantiate(source);
13+
expect(instance.exports.get()).toBe(42);
14+
});
15+
}
16+
17+
for (const [mode, load] of [
18+
["eager", () => import(/* webpackMode: "eager" */ "./wasm.wat")],
19+
["weak", () => import(/* webpackMode: "weak" */ "./wasm.wat")]
20+
]) {
21+
it(`should keep the exports of ${mode} evaluation imports`, async () => {
22+
const expected = await import("./wasm.wat");
23+
const evaluated = await load();
24+
expect(evaluated).toBe(expected);
25+
expect(evaluated.get()).toBe(42);
26+
});
27+
}
28+
29+
it("should reject a weak source import when the module is unavailable", async () => {
30+
await expect(
31+
import.source(/* webpackMode: "weak" */ "./wasm.wat?unavailable")
32+
).rejects.toMatchObject({ code: "MODULE_NOT_FOUND" });
33+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(module
2+
(func (export "get") (result i32)
3+
(i32.const 42)))
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"use strict";
2+
3+
const supportsResponse = require("../../../helpers/supportsResponse");
4+
5+
/** @type {import("../../../../").Configuration[]} */
6+
const configurations = [
7+
{ target: "node", output: { module: false } },
8+
{ target: "node", output: { module: true } },
9+
{ target: "async-node", output: { module: false } },
10+
{ target: "universal", output: { module: true } }
11+
];
12+
13+
// The web target's streaming Wasm APIs require a native Response in the harness.
14+
if (supportsResponse()) {
15+
configurations.push({ target: "web", output: { module: false } });
16+
}
17+
18+
module.exports = configurations.map((options) => ({
19+
...options,
20+
module: {
21+
rules: [
22+
{
23+
test: /\.wat$/,
24+
loader: "wast-loader",
25+
type: "webassembly/async"
26+
}
27+
]
28+
},
29+
experiments: { asyncWebAssembly: true, sourceImport: true }
30+
}));

0 commit comments

Comments
 (0)