Skip to content

Commit 3334b31

Browse files
committed
fix: remove unused namedExports logic
1 parent 3e846db commit 3334b31

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

packages/plugin-vite/src/plugins/patches/commonjs.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -189,19 +189,14 @@ export function cjsPlugin(
189189
);
190190
}
191191

192-
const exportNamed = new Map<string, string>();
193-
194192
const idExports: types.ExportSpecifier[] = [];
195193
for (const name of exported) {
196194
if (name === "default") {
197-
exportNamed.set(name, name);
198195
continue;
199196
}
200197

201198
const id = path.scope.generateUidIdentifier(name);
202199

203-
exportNamed.set(id.name, name);
204-
205200
path.pushContainer(
206201
"body",
207202
t.variableDeclaration(
@@ -227,7 +222,7 @@ export function cjsPlugin(
227222
);
228223
}
229224

230-
if (exportNamed.size > 0 || exportedNs.size > 0 || hasEsModule) {
225+
if (exported.size > 0 || exportedNs.size > 0 || hasEsModule) {
231226
const id = path.scope.generateUidIdentifier("__default");
232227

233228
path.pushContainer(
@@ -244,10 +239,18 @@ export function cjsPlugin(
244239
t.ifStatement(
245240
t.logicalExpression(
246241
"&&",
247-
t.binaryExpression(
248-
"===",
249-
t.unaryExpression("typeof", t.identifier("exports")),
250-
t.stringLiteral("object"),
242+
t.logicalExpression(
243+
"&&",
244+
t.binaryExpression(
245+
"===",
246+
t.unaryExpression("typeof", t.identifier("exports")),
247+
t.stringLiteral("object"),
248+
),
249+
t.binaryExpression(
250+
"!==",
251+
t.identifier("exports"),
252+
t.nullLiteral(),
253+
),
251254
),
252255
t.binaryExpression(
253256
"in",
@@ -284,10 +287,18 @@ export function cjsPlugin(
284287
t.ifStatement(
285288
t.logicalExpression(
286289
"&&",
287-
t.binaryExpression(
288-
"===",
289-
t.unaryExpression("typeof", t.identifier("exports")),
290-
t.stringLiteral("object"),
290+
t.logicalExpression(
291+
"&&",
292+
t.binaryExpression(
293+
"===",
294+
t.unaryExpression("typeof", t.identifier("exports")),
295+
t.stringLiteral("object"),
296+
),
297+
t.binaryExpression(
298+
"!==",
299+
t.identifier("exports"),
300+
t.nullLiteral(),
301+
),
291302
),
292303
t.unaryExpression(
293304
"!",
@@ -694,6 +705,7 @@ export function cjsPlugin(
694705
state.set(HAS_ES_MODULE, true);
695706
} else if (left.isMemberExpression()) {
696707
if (isModuleExports(t, left.node)) {
708+
// Should always try to create synthetic default export in this case.
697709
exported.add("default");
698710

699711
if (t.isObjectExpression(expr.node.right)) {

packages/plugin-vite/src/plugins/patches/commonjs_test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Object.defineProperty(module, "exports", {
2727
});`;
2828

2929
const DEFAULT_EXPORT = `let _default;
30-
if (typeof exports === "object" && "default" in exports) {
30+
if (typeof exports === "object" && exports !== null && "default" in exports) {
3131
_default = exports.default;
3232
} else {
3333
_default = exports;
@@ -366,7 +366,7 @@ Deno.test("commonjs - detect esbuild shims", () => {
366366
import * as _ns from "./globalThis";
367367
export * from "./globalThis";
368368
${DEFAULT_EXPORT}
369-
if (typeof exports === "object" && !("default" in exports)) for (var _k in _ns) if (_k !== "default" && _k !== "__esModule" && Object.prototype.hasOwnProperty.call(_ns, _k)) _default[_k] = _ns[_k];
369+
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];
370370
${DEFAULT_EXPORT_END}`,
371371
});
372372
});
@@ -508,7 +508,7 @@ Object.defineProperty(exports, "__esModule", {
508508
});
509509
export * from "./node";
510510
${DEFAULT_EXPORT}
511-
if (typeof exports === "object" && !("default" in exports)) for (var _k in _ns) if (_k !== "default" && _k !== "__esModule" && Object.prototype.hasOwnProperty.call(_ns, _k)) _default[_k] = _ns[_k];
511+
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];
512512
${DEFAULT_EXPORT_END}
513513
${EXPORT_ES_MODULE}`,
514514
});

0 commit comments

Comments
 (0)