Skip to content

Commit f3ef142

Browse files
fix: defer import mangling (#19988)
1 parent d32f171 commit f3ef142

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

lib/RuntimeTemplate.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,16 @@ class RuntimeTemplate {
949949
// when the defaultInterop is used (when a ESM imports a CJS module),
950950
if (exportName.length > 0 && exportName[0] === "default") {
951951
if (isDeferred && exportsType !== "namespace") {
952-
const access = `${importVar}.a${propertyAccess(exportName, 1)}`;
952+
const exportsInfo = moduleGraph.getExportsInfo(module);
953+
const name = exportName.slice(1);
954+
const used = exportsInfo.getUsedName(name, runtime);
955+
if (!used) {
956+
const comment = Template.toNormalComment(
957+
`unused export ${propertyAccess(exportName)}`
958+
);
959+
return `${comment} undefined`;
960+
}
961+
const access = `${importVar}.a${propertyAccess(used)}`;
953962
if (isCall || asiSafe === undefined) {
954963
return access;
955964
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"foo": "bar",
3+
"nested": { "foo": "bar" }
4+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import defer * as mod1 from "./file.ext" with { type: "bytes" };
2+
import defer * as mod2 from "./file.ext" with { type: "json" };
3+
import * as mod3 from "./file.ext" with { type: "bytes" };
4+
import * as mod4 from "./file.ext" with { type: "json" };
5+
6+
it("should work with defer and import attributes", () => {
7+
const decoder = new TextDecoder('utf-8');
8+
const mod1Decoded = JSON.parse(decoder.decode(mod1.default));
9+
expect(mod1Decoded.foo).toBe("bar");
10+
expect(mod1Decoded.nested.foo).toBe("bar");
11+
expect(mod2.default.foo).toBe("bar");
12+
expect(mod2.default.nested.foo).toBe("bar");
13+
const mod2Decoded = JSON.parse(decoder.decode(mod3.default));
14+
expect(mod2Decoded.foo).toBe("bar");
15+
expect(mod2Decoded.nested.foo).toBe("bar");
16+
expect(mod4.default.foo).toBe("bar");
17+
expect(mod4.default.nested.foo).toBe("bar");
18+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
3+
const supportsTextDecoder = require("../../../helpers/supportsTextDecoder");
4+
5+
module.exports = () => supportsTextDecoder();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
target: [`async-node${process.versions.node.split(".").map(Number)[0]}`],
6+
experiments: {
7+
deferImport: true
8+
}
9+
};

0 commit comments

Comments
 (0)