Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 105 additions & 62 deletions packages/plugin-vite/src/plugins/patches/commonjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,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(
Expand All @@ -227,94 +222,141 @@ 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.binaryExpression(
"===",
t.unaryExpression("typeof", t.identifier("exports")),
t.stringLiteral("object"),
),
t.binaryExpression(
"!==",
t.identifier("exports"),
t.identifier("default"),
t.nullLiteral(),
),
),
t.binaryExpression(
"in",
t.stringLiteral("default"),
t.identifier("exports"),
),
),
]),
);

for (const [local, exported] of exportNamed.entries()) {
if (exported === "default") continue;

path.pushContainer(
"body",
t.expressionStatement(
t.assignmentExpression(
"=",
t.memberExpression(id, t.identifier(exported)),
t.identifier(local),
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++) {
const mapped = mappedNs[i];

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.binaryExpression(
"===",
t.unaryExpression("typeof", t.identifier("exports")),
t.stringLiteral("object"),
),
t.binaryExpression(
"!==",
t.identifier("exports"),
t.nullLiteral(),
),
),
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.binaryExpression(
"!==",
t.identifier(key),
t.stringLiteral("default"),
),
t.binaryExpression(
"!==",
t.identifier(key),
t.stringLiteral("__esModule"),
t.logicalExpression(
"&&",
t.binaryExpression(
"!==",
t.identifier(key),
t.stringLiteral("default"),
),
t.binaryExpression(
"!==",
t.identifier(key),
t.stringLiteral("__esModule"),
),
),
),
t.callExpression(
t.memberExpression(
t.callExpression(
t.memberExpression(
t.memberExpression(
t.identifier("Object"),
t.identifier("prototype"),
t.memberExpression(
t.identifier("Object"),
t.identifier("prototype"),
),
t.identifier("hasOwnProperty"),
),
t.identifier("hasOwnProperty"),
t.identifier("call"),
),
t.identifier("call"),
[t.identifier(mapped), t.identifier(key)],
),
[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,
t.expressionStatement(
t.assignmentExpression(
"=",
t.memberExpression(
t.cloneNode(id, true),
t.identifier(key),
true,
),
t.memberExpression(
t.identifier(mapped),
t.identifier(key),
true,
),
),
),
),
Expand Down Expand Up @@ -663,6 +705,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)) {
Expand Down
Loading
Loading