fix(vite): improve commonjs handling (#3357)

This commit is contained in:
Marvin Hagemeister
2025-09-08 17:06:11 +02:00
committed by GitHub
parent 2c32ffe8f3
commit 2e015cd2b9
3 changed files with 38 additions and 8 deletions
@@ -243,6 +243,17 @@ export function cjsPlugin(
}
path.pushContainer("body", t.exportDefaultDeclaration(id));
path.pushContainer(
"body",
t.exportNamedDeclaration(
t.variableDeclaration("var", [
t.variableDeclarator(
t.identifier("__require"),
t.identifier("exports"),
),
]),
),
);
}
if (body.length === 0 && state.get(HAS_ES_MODULE)) {
@@ -304,9 +315,16 @@ export function cjsPlugin(
"??",
t.memberExpression(
t.cloneNode(id, true),
t.identifier("default"),
t.identifier("__require"),
),
t.logicalExpression(
"??",
t.memberExpression(
t.cloneNode(id, true),
t.identifier("default"),
),
t.cloneNode(id, true),
),
t.cloneNode(id, true),
),
);
return;