Skip to content

Commit 952f9d1

Browse files
committed
fix: do not modify existing default export
1 parent f82bd7e commit 952f9d1

File tree

2 files changed

+176
-107
lines changed

2 files changed

+176
-107
lines changed

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

Lines changed: 83 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -232,89 +232,112 @@ export function cjsPlugin(
232232

233233
path.pushContainer(
234234
"body",
235-
t.variableDeclaration("const", [
235+
t.variableDeclaration("let", [
236236
t.variableDeclarator(
237237
id,
238-
t.logicalExpression(
239-
"??",
240-
t.memberExpression(
241-
t.identifier("exports"),
242-
t.identifier("default"),
243-
),
244-
t.identifier("exports"),
245-
),
246238
),
247239
]),
248240
);
249241

250-
for (const [local, exported] of exportNamed.entries()) {
251-
if (exported === "default") continue;
252-
253-
path.pushContainer(
254-
"body",
255-
t.expressionStatement(
256-
t.assignmentExpression(
257-
"=",
258-
t.memberExpression(id, t.identifier(exported)),
259-
t.identifier(local),
260-
),
242+
path.pushContainer(
243+
"body",
244+
t.ifStatement(
245+
t.binaryExpression(
246+
"in",
247+
t.stringLiteral("default"),
248+
t.identifier("exports"),
261249
),
262-
);
263-
}
250+
t.blockStatement([
251+
t.expressionStatement(
252+
t.assignmentExpression(
253+
"=",
254+
id,
255+
t.memberExpression(
256+
t.identifier("exports"),
257+
t.identifier("default"),
258+
),
259+
),
260+
),
261+
]),
262+
t.blockStatement([
263+
t.expressionStatement(
264+
t.assignmentExpression("=", id, t.identifier("exports")),
265+
),
266+
...exportNamed.entries().filter(([_, exported]) =>
267+
exported != "default"
268+
).map(([local, exported]) =>
269+
t.expressionStatement(
270+
t.assignmentExpression(
271+
"=",
272+
t.memberExpression(id, t.identifier(exported)),
273+
t.identifier(local),
274+
),
275+
)
276+
),
277+
]),
278+
),
279+
);
264280

265281
for (let i = 0; i < mappedNs.length; i++) {
266282
const mapped = mappedNs[i];
267283

268284
const key = path.scope.generateUid("k");
269285
path.pushContainer(
270286
"body",
271-
t.forInStatement(
272-
t.variableDeclaration("var", [
273-
t.variableDeclarator(t.identifier(key)),
274-
]),
275-
t.identifier(mapped),
276-
t.ifStatement(
277-
t.logicalExpression(
278-
"&&",
287+
t.ifStatement(
288+
t.binaryExpression(
289+
"in",
290+
t.stringLiteral("default"),
291+
t.identifier("exports"),
292+
),
293+
t.forInStatement(
294+
t.variableDeclaration("var", [
295+
t.variableDeclarator(t.identifier(key)),
296+
]),
297+
t.identifier(mapped),
298+
t.ifStatement(
279299
t.logicalExpression(
280300
"&&",
281-
t.binaryExpression(
282-
"!==",
283-
t.identifier(key),
284-
t.stringLiteral("default"),
285-
),
286-
t.binaryExpression(
287-
"!==",
288-
t.identifier(key),
289-
t.stringLiteral("__esModule"),
301+
t.logicalExpression(
302+
"&&",
303+
t.binaryExpression(
304+
"!==",
305+
t.identifier(key),
306+
t.stringLiteral("default"),
307+
),
308+
t.binaryExpression(
309+
"!==",
310+
t.identifier(key),
311+
t.stringLiteral("__esModule"),
312+
),
290313
),
291-
),
292-
t.callExpression(
293-
t.memberExpression(
314+
t.callExpression(
294315
t.memberExpression(
295316
t.memberExpression(
296-
t.identifier("Object"),
297-
t.identifier("prototype"),
317+
t.memberExpression(
318+
t.identifier("Object"),
319+
t.identifier("prototype"),
320+
),
321+
t.identifier("hasOwnProperty"),
298322
),
299-
t.identifier("hasOwnProperty"),
323+
t.identifier("call"),
300324
),
301-
t.identifier("call"),
325+
[t.identifier(mapped), t.identifier(key)],
302326
),
303-
[t.identifier(mapped), t.identifier(key)],
304327
),
305-
),
306-
t.expressionStatement(
307-
t.assignmentExpression(
308-
"=",
309-
t.memberExpression(
310-
t.cloneNode(id, true),
311-
t.identifier(key),
312-
true,
313-
),
314-
t.memberExpression(
315-
t.identifier(mapped),
316-
t.identifier(key),
317-
true,
328+
t.expressionStatement(
329+
t.assignmentExpression(
330+
"=",
331+
t.memberExpression(
332+
t.cloneNode(id, true),
333+
t.identifier(key),
334+
true,
335+
),
336+
t.memberExpression(
337+
t.identifier(mapped),
338+
t.identifier(key),
339+
true,
340+
),
318341
),
319342
),
320343
),

0 commit comments

Comments
 (0)