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:
co-authored by
Bartek Iwańczuk
Claude Opus 4.6
parent
9c06f4aa8c
commit
cdc14a0c6e
@@ -210,19 +210,14 @@ export function cjsPlugin(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const exportNamed = new Map<string, string>();
|
|
||||||
|
|
||||||
const idExports: types.ExportSpecifier[] = [];
|
const idExports: types.ExportSpecifier[] = [];
|
||||||
for (const name of exported) {
|
for (const name of exported) {
|
||||||
if (name === "default") {
|
if (name === "default") {
|
||||||
exportNamed.set(name, name);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const id = path.scope.generateUidIdentifier(name);
|
const id = path.scope.generateUidIdentifier(name);
|
||||||
|
|
||||||
exportNamed.set(id.name, name);
|
|
||||||
|
|
||||||
path.pushContainer(
|
path.pushContainer(
|
||||||
"body",
|
"body",
|
||||||
t.variableDeclaration(
|
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");
|
const id = path.scope.generateUidIdentifier("__default");
|
||||||
|
|
||||||
path.pushContainer(
|
path.pushContainer(
|
||||||
"body",
|
"body",
|
||||||
t.variableDeclaration("const", [
|
t.variableDeclaration("let", [
|
||||||
t.variableDeclarator(
|
t.variableDeclarator(
|
||||||
id,
|
id,
|
||||||
|
),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
|
||||||
|
path.pushContainer(
|
||||||
|
"body",
|
||||||
|
t.ifStatement(
|
||||||
|
t.logicalExpression(
|
||||||
|
"&&",
|
||||||
t.logicalExpression(
|
t.logicalExpression(
|
||||||
"??",
|
"&&",
|
||||||
t.memberExpression(
|
t.binaryExpression(
|
||||||
t.identifier("exports"),
|
"===",
|
||||||
t.identifier("default"),
|
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.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++) {
|
for (let i = 0; i < mappedNs.length; i++) {
|
||||||
@@ -274,53 +305,79 @@ export function cjsPlugin(
|
|||||||
const key = path.scope.generateUid("k");
|
const key = path.scope.generateUid("k");
|
||||||
path.pushContainer(
|
path.pushContainer(
|
||||||
"body",
|
"body",
|
||||||
t.forInStatement(
|
t.ifStatement(
|
||||||
t.variableDeclaration("var", [
|
t.logicalExpression(
|
||||||
t.variableDeclarator(t.identifier(key)),
|
"&&",
|
||||||
]),
|
|
||||||
t.identifier(mapped),
|
|
||||||
t.ifStatement(
|
|
||||||
t.logicalExpression(
|
t.logicalExpression(
|
||||||
"&&",
|
"&&",
|
||||||
t.logicalExpression(
|
t.binaryExpression(
|
||||||
"&&",
|
"===",
|
||||||
t.binaryExpression(
|
t.unaryExpression("typeof", t.identifier("exports")),
|
||||||
"!==",
|
t.stringLiteral("object"),
|
||||||
t.identifier(key),
|
|
||||||
t.stringLiteral("default"),
|
|
||||||
),
|
|
||||||
t.binaryExpression(
|
|
||||||
"!==",
|
|
||||||
t.identifier(key),
|
|
||||||
t.stringLiteral("__esModule"),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
t.callExpression(
|
t.binaryExpression(
|
||||||
t.memberExpression(
|
"!==",
|
||||||
t.memberExpression(
|
t.identifier("exports"),
|
||||||
t.memberExpression(
|
t.nullLiteral(),
|
||||||
t.identifier("Object"),
|
|
||||||
t.identifier("prototype"),
|
|
||||||
),
|
|
||||||
t.identifier("hasOwnProperty"),
|
|
||||||
),
|
|
||||||
t.identifier("call"),
|
|
||||||
),
|
|
||||||
[t.identifier(mapped), t.identifier(key)],
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
t.expressionStatement(
|
t.unaryExpression(
|
||||||
t.assignmentExpression(
|
"!",
|
||||||
"=",
|
t.binaryExpression(
|
||||||
t.memberExpression(
|
"in",
|
||||||
t.cloneNode(id, true),
|
t.stringLiteral("default"),
|
||||||
t.identifier(key),
|
t.identifier("exports"),
|
||||||
true,
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
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.callExpression(
|
||||||
t.identifier(mapped),
|
t.memberExpression(
|
||||||
t.identifier(key),
|
t.memberExpression(
|
||||||
true,
|
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);
|
state.set(HAS_ES_MODULE, true);
|
||||||
} else if (left.isMemberExpression()) {
|
} else if (left.isMemberExpression()) {
|
||||||
if (isModuleExports(t, left.node)) {
|
if (isModuleExports(t, left.node)) {
|
||||||
|
// Should always try to create synthetic default export in this case.
|
||||||
exported.add("default");
|
exported.add("default");
|
||||||
|
|
||||||
if (t.isObjectExpression(expr.node.right)) {
|
if (t.isObjectExpression(expr.node.right)) {
|
||||||
|
|||||||
@@ -29,7 +29,13 @@ Object.defineProperty(exports, "__esModule", {
|
|||||||
value: true
|
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;
|
const DEFAULT_EXPORT_END = `export default _default;
|
||||||
export var __require = exports;`;
|
export var __require = exports;`;
|
||||||
const IMPORT_REQUIRE = `import { createRequire } from "node:module";
|
const IMPORT_REQUIRE = `import { createRequire } from "node:module";
|
||||||
@@ -137,9 +143,8 @@ Deno.test("commonjs - esModule flag only", () => {
|
|||||||
Object.defineProperty(exports, "__esModule", {
|
Object.defineProperty(exports, "__esModule", {
|
||||||
value: true
|
value: true
|
||||||
});
|
});
|
||||||
const _default = exports.default ?? exports;
|
${DEFAULT_EXPORT}
|
||||||
export default _default;
|
${DEFAULT_EXPORT_END}
|
||||||
export var __require = exports;
|
|
||||||
${EXPORT_ES_MODULE}`,
|
${EXPORT_ES_MODULE}`,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -152,9 +157,8 @@ Deno.test("commonjs - esModule flag only #2", () => {
|
|||||||
Object.defineProperty(module.exports, "__esModule", {
|
Object.defineProperty(module.exports, "__esModule", {
|
||||||
value: true
|
value: true
|
||||||
});
|
});
|
||||||
const _default = exports.default ?? exports;
|
${DEFAULT_EXPORT}
|
||||||
export default _default;
|
${DEFAULT_EXPORT_END}
|
||||||
export var __require = exports;
|
|
||||||
${EXPORT_ES_MODULE}`,
|
${EXPORT_ES_MODULE}`,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -166,9 +170,8 @@ Deno.test("commonjs - esModule flag only minified #3", () => {
|
|||||||
Object.defineProperty(exports, '__esModule', {
|
Object.defineProperty(exports, '__esModule', {
|
||||||
value: !0
|
value: !0
|
||||||
});
|
});
|
||||||
const _default = exports.default ?? exports;
|
${DEFAULT_EXPORT}
|
||||||
export default _default;
|
${DEFAULT_EXPORT_END}
|
||||||
export var __require = exports;
|
|
||||||
${EXPORT_ES_MODULE}`,
|
${EXPORT_ES_MODULE}`,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -369,7 +372,7 @@ Deno.test("commonjs - detect esbuild shims", () => {
|
|||||||
import * as _ns from "./globalThis";
|
import * as _ns from "./globalThis";
|
||||||
export * from "./globalThis";
|
export * from "./globalThis";
|
||||||
${DEFAULT_EXPORT}
|
${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}`,
|
${DEFAULT_EXPORT_END}`,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -511,7 +514,7 @@ Object.defineProperty(exports, "__esModule", {
|
|||||||
});
|
});
|
||||||
export * from "./node";
|
export * from "./node";
|
||||||
${DEFAULT_EXPORT}
|
${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}
|
${DEFAULT_EXPORT_END}
|
||||||
${EXPORT_ES_MODULE}`,
|
${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}`,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user