From cdc14a0c6ea48f6835636872dbf47c5cdf275abf Mon Sep 17 00:00:00 2001 From: Lach Date: Wed, 25 Mar 2026 19:40:46 +0100 Subject: [PATCH] Do not assign named exports into existing default export (#3494) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: https://github.com/denoland/fresh/issues/3492 --------- Co-authored-by: Bartek IwaƄczuk Co-authored-by: Claude Opus 4.6 (1M context) --- .../src/plugins/patches/commonjs.ts | 164 ++++++++++++------ .../src/plugins/patches/commonjs_test.ts | 46 +++-- 2 files changed, 145 insertions(+), 65 deletions(-) diff --git a/packages/plugin-vite/src/plugins/patches/commonjs.ts b/packages/plugin-vite/src/plugins/patches/commonjs.ts index ce80b48b..20a19b00 100644 --- a/packages/plugin-vite/src/plugins/patches/commonjs.ts +++ b/packages/plugin-vite/src/plugins/patches/commonjs.ts @@ -210,19 +210,14 @@ export function cjsPlugin( ); } - const exportNamed = new Map(); - 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)) { diff --git a/packages/plugin-vite/src/plugins/patches/commonjs_test.ts b/packages/plugin-vite/src/plugins/patches/commonjs_test.ts index a7db2ee1..e62ec6cf 100644 --- a/packages/plugin-vite/src/plugins/patches/commonjs_test.ts +++ b/packages/plugin-vite/src/plugins/patches/commonjs_test.ts @@ -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}`, + }); +});