Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/RuntimeTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions test/configCases/defer-import/import-attributes/file.ext
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"foo": "bar",
"nested": { "foo": "bar" }
}
18 changes: 18 additions & 0 deletions test/configCases/defer-import/import-attributes/index.js
Original file line number Diff line number Diff line change
@@ -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");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";

const supportsTextDecoder = require("../../../helpers/supportsTextDecoder");

module.exports = () => supportsTextDecoder();
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

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