Skip to content

Commit 0006c47

Browse files
fix: more commonjs cases
1 parent 6f81f16 commit 0006c47

2 files changed

Lines changed: 107 additions & 0 deletions

File tree

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,70 @@ export function cjsPlugin(
134134
}
135135
}
136136
}
137+
} else if (expr.isCallExpression()) {
138+
if (
139+
t.isMemberExpression(expr.node.callee) &&
140+
t.isIdentifier(expr.node.callee.object) &&
141+
expr.node.callee.object.name === "Object" &&
142+
t.isIdentifier(expr.node.callee.property) &&
143+
expr.node.callee.property.name === "defineProperty" &&
144+
expr.node.arguments.length === 3 &&
145+
t.isIdentifier(expr.node.arguments[0]) &&
146+
expr.node.arguments[0].name === "exports" &&
147+
t.isStringLiteral(expr.node.arguments[1]) &&
148+
expr.node.arguments[1].value !== "__esModule" &&
149+
t.isObjectExpression(expr.node.arguments[2])
150+
) {
151+
const named = expr.node.arguments[1].value;
152+
const obj = expr.node.arguments[2];
153+
154+
let right: types.Expression = t.nullLiteral();
155+
156+
for (let i = 0; i < obj.properties.length; i++) {
157+
const prop = obj.properties[i];
158+
159+
if (t.isObjectProperty(prop)) {
160+
if (t.isIdentifier(prop.key)) {
161+
if (prop.key.name === "get") {
162+
if (
163+
t.isFunctionExpression(prop.value) ||
164+
t.isArrowFunctionExpression(prop.value)
165+
) {
166+
right = t.callExpression(
167+
t.parenthesizedExpression(
168+
t.cloneNode(prop.value, true),
169+
),
170+
[],
171+
);
172+
}
173+
}
174+
}
175+
} else if (t.isObjectMethod(prop)) {
176+
if (t.isIdentifier(prop.key)) {
177+
if (prop.key.name === "get") {
178+
right = t.callExpression(
179+
t.parenthesizedExpression(
180+
t.functionExpression(
181+
null,
182+
[],
183+
t.cloneNode(prop.body, true),
184+
),
185+
),
186+
[],
187+
);
188+
}
189+
}
190+
}
191+
}
192+
193+
path.replaceWith(
194+
t.exportNamedDeclaration(
195+
t.variableDeclaration("let", [
196+
t.variableDeclarator(t.identifier(named), right),
197+
]),
198+
),
199+
);
200+
}
137201
}
138202
},
139203
},

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,49 @@ exports.trace = 'foo'`,
174174
});
175175
});
176176

177+
Deno.test("commonjs - cleared exports", () => {
178+
runTest({
179+
input: `Object.defineProperty(exports, "__esModule", { value: true });
180+
exports.foo = exports.bar = void 0;
181+
exports.foo = 'foo'`,
182+
expected: `export let foo = 'foo';`,
183+
});
184+
});
185+
186+
Deno.test("commonjs - define exports", () => {
187+
runTest({
188+
input: `var utils_1 = require("./bar");
189+
Object.defineProperty(exports, "foo", { enumerable: true, get: function () { return utils_1.foo; } });`,
190+
expected: `import * as _mod from "./bar";
191+
var utils_1 = _mod.default ?? _mod;
192+
export let foo = (function () {
193+
return utils_1.foo;
194+
})();`,
195+
});
196+
});
197+
198+
Deno.test("commonjs - define exports #2", () => {
199+
runTest({
200+
input: `var utils_1 = require("./bar");
201+
Object.defineProperty(exports, "foo", { enumerable: true, get() { return utils_1.foo; } });`,
202+
expected: `import * as _mod from "./bar";
203+
var utils_1 = _mod.default ?? _mod;
204+
export let foo = (function () {
205+
return utils_1.foo;
206+
})();`,
207+
});
208+
});
209+
210+
Deno.test("commonjs - define exports #3", () => {
211+
runTest({
212+
input: `Object.defineProperty(exports, "__esModule", { value: true });
213+
exports._globalThis = void 0;
214+
exports._globalThis = typeof globalThis === 'object' ? globalThis : global;`,
215+
expected:
216+
`export let _globalThis = typeof globalThis === 'object' ? globalThis : global;`,
217+
});
218+
});
219+
177220
// I've never seen this, seems rare. Skipping for now.
178221
Deno.test.ignore("commonjs - require", () => {
179222
runTest({

0 commit comments

Comments
 (0)