Skip to content

Commit 81e3fe7

Browse files
fix(vite): support commonjs module.exports assignment
1 parent 9975d16 commit 81e3fe7

2 files changed

Lines changed: 67 additions & 28 deletions

File tree

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

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,45 @@ export function cjsPlugin(
5454
}
5555

5656
if (exported.size > 0 || exportedNs.size > 0) {
57+
path.unshiftContainer(
58+
"body",
59+
t.expressionStatement(
60+
t.callExpression(
61+
t.memberExpression(
62+
t.identifier("Object"),
63+
t.identifier("defineProperty"),
64+
),
65+
[
66+
t.identifier("module"),
67+
t.stringLiteral("exports"),
68+
t.objectExpression([
69+
t.objectMethod(
70+
"method",
71+
t.identifier("get"),
72+
[],
73+
t.blockStatement([
74+
t.returnStatement(t.identifier("exports")),
75+
]),
76+
),
77+
t.objectMethod(
78+
"method",
79+
t.identifier("set"),
80+
[t.identifier("value")],
81+
t.blockStatement([
82+
t.expressionStatement(
83+
t.assignmentExpression(
84+
"=",
85+
t.identifier("exports"),
86+
t.identifier("value"),
87+
),
88+
),
89+
]),
90+
),
91+
]),
92+
],
93+
),
94+
),
95+
);
5796
path.unshiftContainer(
5897
"body",
5998
t.variableDeclaration("var", [
@@ -63,14 +102,7 @@ export function cjsPlugin(
63102
),
64103
t.variableDeclarator(
65104
t.identifier("module"),
66-
t.objectExpression([
67-
t.objectProperty(
68-
t.identifier("exports"),
69-
t.identifier("exports"),
70-
false,
71-
true,
72-
),
73-
]),
105+
t.objectExpression([]),
74106
),
75107
]),
76108
);
@@ -124,24 +156,11 @@ export function cjsPlugin(
124156
id,
125157
t.logicalExpression(
126158
"??",
127-
t.logicalExpression(
128-
"??",
129-
t.memberExpression(
130-
t.memberExpression(
131-
t.identifier("module"),
132-
t.identifier("exports"),
133-
),
134-
t.identifier("default"),
135-
),
136-
t.memberExpression(
137-
t.identifier("exports"),
138-
t.identifier("default"),
139-
),
140-
),
141159
t.memberExpression(
142-
t.identifier("module"),
143160
t.identifier("exports"),
161+
t.identifier("default"),
144162
),
163+
t.identifier("exports"),
145164
),
146165
),
147166
]),

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ function runTest(options: { input: string; expected: string }) {
1414
}
1515

1616
const INIT = `var exports = {},
17-
module = {
18-
exports
19-
};`;
17+
module = {};
18+
Object.defineProperty(module, "exports", {
19+
get() {
20+
return exports;
21+
},
22+
set(value) {
23+
exports = value;
24+
}
25+
});`;
2026

21-
const DEFAULT_EXPORT =
22-
`const _default = module.exports.default ?? exports.default ?? module.exports;`;
27+
const DEFAULT_EXPORT = `const _default = exports.default ?? exports;`;
2328
const DEFAULT_EXPORT_END = `export default _default;`;
2429

2530
Deno.test("commonjs - module.exports default", () => {
@@ -451,3 +456,18 @@ Object.assign(_default, _ns);
451456
${DEFAULT_EXPORT_END}`,
452457
});
453458
});
459+
460+
Deno.test("commonjs - assign module.exports", () => {
461+
runTest({
462+
input: `module.exports = { foo: 1 };`,
463+
expected: `${INIT}
464+
module.exports = {
465+
foo: 1
466+
};
467+
var _foo = exports.foo;
468+
export { _foo as foo };
469+
${DEFAULT_EXPORT}
470+
_default.foo = _foo;
471+
${DEFAULT_EXPORT_END}`,
472+
});
473+
});

0 commit comments

Comments
 (0)