Do not assign named exports into existing default export (#3494)

Fixes: https://github.com/denoland/fresh/issues/3492

---------

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Lach
2026-03-25 19:40:46 +01:00
committed by GitHub
co-authored by Bartek Iwańczuk Claude Opus 4.6
parent 9c06f4aa8c
commit cdc14a0c6e
2 changed files with 145 additions and 65 deletions
@@ -210,19 +210,14 @@ export function cjsPlugin(
);
}
const exportNamed = new Map<string, string>();
const idExports: types.ExportSpecifier[] = [];
for (const name of exported) {
if (name === "default") {
exportNamed.set(name, name);
continue;
}
const id = path.scope.generateUidIdentifier(name);
exportNamed.set(id.name, name);
path.pushContainer(
"body",
t.variableDeclaration(
@@ -248,24 +243,60 @@ export function cjsPlugin(
);
}
if (exportNamed.size > 0 || exportedNs.size > 0 || hasEsModule) {
if (exported.size > 0 || exportedNs.size > 0 || hasEsModule) {
const id = path.scope.generateUidIdentifier("__default");
path.pushContainer(
"body",
t.variableDeclaration("const", [
t.variableDeclaration("let", [
t.variableDeclarator(
id,
),
]),
);
path.pushContainer(
"body",
t.ifStatement(
t.logicalExpression(
"&&",
t.logicalExpression(
"??",
t.memberExpression(
t.identifier("exports"),
t.identifier("default"),
"&&",
t.binaryExpression(
"===",
t.unaryExpression("typeof", t.identifier("exports")),
t.stringLiteral("object"),
),
t.binaryExpression(
"!==",
t.identifier("exports"),
t.nullLiteral(),
),
),
t.binaryExpression(
"in",
t.stringLiteral("default"),
t.identifier("exports"),
),
),
]),
t.blockStatement([
t.expressionStatement(
t.assignmentExpression(
"=",
id,
t.memberExpression(
t.identifier("exports"),
t.identifier("default"),
),
),
),
]),
t.blockStatement([
t.expressionStatement(
t.assignmentExpression("=", id, t.identifier("exports")),
),
]),
),
);
for (let i = 0; i < mappedNs.length; i++) {
@@ -274,53 +305,79 @@ export function cjsPlugin(
const key = path.scope.generateUid("k");
path.pushContainer(
"body",
t.forInStatement(
t.variableDeclaration("var", [
t.variableDeclarator(t.identifier(key)),
]),
t.identifier(mapped),
t.ifStatement(
t.ifStatement(
t.logicalExpression(
"&&",
t.logicalExpression(
"&&",
t.logicalExpression(
"&&",
t.binaryExpression(
"!==",
t.identifier(key),
t.stringLiteral("default"),
),
t.binaryExpression(
"!==",
t.identifier(key),
t.stringLiteral("__esModule"),
),
t.binaryExpression(
"===",
t.unaryExpression("typeof", t.identifier("exports")),
t.stringLiteral("object"),
),
t.callExpression(
t.memberExpression(
t.memberExpression(
t.memberExpression(
t.identifier("Object"),
t.identifier("prototype"),
),
t.identifier("hasOwnProperty"),
),
t.identifier("call"),
),
[t.identifier(mapped), t.identifier(key)],
t.binaryExpression(
"!==",
t.identifier("exports"),
t.nullLiteral(),
),
),
t.expressionStatement(
t.assignmentExpression(
"=",
t.memberExpression(
t.cloneNode(id, true),
t.identifier(key),
true,
t.unaryExpression(
"!",
t.binaryExpression(
"in",
t.stringLiteral("default"),
t.identifier("exports"),
),
),
),
t.forInStatement(
t.variableDeclaration("var", [
t.variableDeclarator(t.identifier(key)),
]),
t.identifier(mapped),
t.ifStatement(
t.logicalExpression(
"&&",
t.logicalExpression(
"&&",
t.binaryExpression(
"!==",
t.identifier(key),
t.stringLiteral("default"),
),
t.binaryExpression(
"!==",
t.identifier(key),
t.stringLiteral("__esModule"),
),
),
t.memberExpression(
t.identifier(mapped),
t.identifier(key),
true,
t.callExpression(
t.memberExpression(
t.memberExpression(
t.memberExpression(
t.identifier("Object"),
t.identifier("prototype"),
),
t.identifier("hasOwnProperty"),
),
t.identifier("call"),
),
[t.identifier(mapped), t.identifier(key)],
),
),
t.expressionStatement(
t.assignmentExpression(
"=",
t.memberExpression(
t.cloneNode(id, true),
t.identifier(key),
true,
),
t.memberExpression(
t.identifier(mapped),
t.identifier(key),
true,
),
),
),
),
@@ -673,6 +730,7 @@ export function cjsPlugin(
state.set(HAS_ES_MODULE, true);
} else if (left.isMemberExpression()) {
if (isModuleExports(t, left.node)) {
// Should always try to create synthetic default export in this case.
exported.add("default");
if (t.isObjectExpression(expr.node.right)) {
@@ -29,7 +29,13 @@ Object.defineProperty(exports, "__esModule", {
value: true
});`;
const DEFAULT_EXPORT = `const _default = exports.default ?? exports;`;
const DEFAULT_EXPORT = `let _default;
if (typeof exports === "object" && exports !== null && "default" in exports) {
_default = exports.default;
} else {
_default = exports;
}`;
const DEFAULT_EXPORT_END = `export default _default;
export var __require = exports;`;
const IMPORT_REQUIRE = `import { createRequire } from "node:module";
@@ -137,9 +143,8 @@ Deno.test("commonjs - esModule flag only", () => {
Object.defineProperty(exports, "__esModule", {
value: true
});
const _default = exports.default ?? exports;
export default _default;
export var __require = exports;
${DEFAULT_EXPORT}
${DEFAULT_EXPORT_END}
${EXPORT_ES_MODULE}`,
});
});
@@ -152,9 +157,8 @@ Deno.test("commonjs - esModule flag only #2", () => {
Object.defineProperty(module.exports, "__esModule", {
value: true
});
const _default = exports.default ?? exports;
export default _default;
export var __require = exports;
${DEFAULT_EXPORT}
${DEFAULT_EXPORT_END}
${EXPORT_ES_MODULE}`,
});
});
@@ -166,9 +170,8 @@ Deno.test("commonjs - esModule flag only minified #3", () => {
Object.defineProperty(exports, '__esModule', {
value: !0
});
const _default = exports.default ?? exports;
export default _default;
export var __require = exports;
${DEFAULT_EXPORT}
${DEFAULT_EXPORT_END}
${EXPORT_ES_MODULE}`,
});
});
@@ -369,7 +372,7 @@ Deno.test("commonjs - detect esbuild shims", () => {
import * as _ns from "./globalThis";
export * from "./globalThis";
${DEFAULT_EXPORT}
for (var _k in _ns) if (_k !== "default" && _k !== "__esModule" && Object.prototype.hasOwnProperty.call(_ns, _k)) _default[_k] = _ns[_k];
if (typeof exports === "object" && exports !== null && !("default" in exports)) for (var _k in _ns) if (_k !== "default" && _k !== "__esModule" && Object.prototype.hasOwnProperty.call(_ns, _k)) _default[_k] = _ns[_k];
${DEFAULT_EXPORT_END}`,
});
});
@@ -511,7 +514,7 @@ Object.defineProperty(exports, "__esModule", {
});
export * from "./node";
${DEFAULT_EXPORT}
for (var _k in _ns) if (_k !== "default" && _k !== "__esModule" && Object.prototype.hasOwnProperty.call(_ns, _k)) _default[_k] = _ns[_k];
if (typeof exports === "object" && exports !== null && !("default" in exports)) for (var _k in _ns) if (_k !== "default" && _k !== "__esModule" && Object.prototype.hasOwnProperty.call(_ns, _k)) _default[_k] = _ns[_k];
${DEFAULT_EXPORT_END}
${EXPORT_ES_MODULE}`,
});
@@ -711,3 +714,22 @@ const node_events_1 = __importDefault({
});`,
});
});
Deno.test("commonjs imitating esm - default export exists", () => {
runTest({
input: `module.exports = {
'default': 'string',
otherExport: 1
};
`,
expected: `${INIT}
module.exports = {
'default': 'string',
otherExport: 1
};
var _otherExport = exports.otherExport;
export { _otherExport as otherExport };
${DEFAULT_EXPORT}
${DEFAULT_EXPORT_END}`,
});
});