fix: defer import mangling (#19988)

This commit is contained in:
Alexander Akait 2025-10-07 19:09:05 +03:00 committed by GitHub
parent d32f1711ac
commit f3ef1428b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 46 additions and 1 deletions

View File

@ -949,7 +949,16 @@ class RuntimeTemplate {
// when the defaultInterop is used (when a ESM imports a CJS module),
if (exportName.length > 0 && exportName[0] === "default") {
if (isDeferred && exportsType !== "namespace") {
const access = `${importVar}.a${propertyAccess(exportName, 1)}`;
const exportsInfo = moduleGraph.getExportsInfo(module);
const name = exportName.slice(1);
const used = exportsInfo.getUsedName(name, runtime);
if (!used) {
const comment = Template.toNormalComment(
`unused export ${propertyAccess(exportName)}`
);
return `${comment} undefined`;
}
const access = `${importVar}.a${propertyAccess(used)}`;
if (isCall || asiSafe === undefined) {
return access;
}

View File

@ -0,0 +1,4 @@
{
"foo": "bar",
"nested": { "foo": "bar" }
}

View File

@ -0,0 +1,18 @@
import defer * as mod1 from "./file.ext" with { type: "bytes" };
import defer * as mod2 from "./file.ext" with { type: "json" };
import * as mod3 from "./file.ext" with { type: "bytes" };
import * as mod4 from "./file.ext" with { type: "json" };
it("should work with defer and import attributes", () => {
const decoder = new TextDecoder('utf-8');
const mod1Decoded = JSON.parse(decoder.decode(mod1.default));
expect(mod1Decoded.foo).toBe("bar");
expect(mod1Decoded.nested.foo).toBe("bar");
expect(mod2.default.foo).toBe("bar");
expect(mod2.default.nested.foo).toBe("bar");
const mod2Decoded = JSON.parse(decoder.decode(mod3.default));
expect(mod2Decoded.foo).toBe("bar");
expect(mod2Decoded.nested.foo).toBe("bar");
expect(mod4.default.foo).toBe("bar");
expect(mod4.default.nested.foo).toBe("bar");
});

View File

@ -0,0 +1,5 @@
"use strict";
const supportsTextDecoder = require("../../../helpers/supportsTextDecoder");
module.exports = () => supportsTextDecoder();

View File

@ -0,0 +1,9 @@
"use strict";
/** @type {import("../../../../").Configuration} */
module.exports = {
target: [`async-node${process.versions.node.split(".").map(Number)[0]}`],
experiments: {
deferImport: true
}
};