Skip to content

Commit 9a764ae

Browse files
fix: vite resolution + cjs fixes (#3208)
1 parent c97e822 commit 9a764ae

5 files changed

Lines changed: 145 additions & 8 deletions

File tree

packages/plugin-vite/src/mod.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
3838
"react-dom": "preact/compat",
3939
react: "preact/compat",
4040
},
41-
noExternal: true,
4241
},
4342
optimizeDeps: {
4443
include: [

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,28 @@ export function cjsPlugin(
3333

3434
if (body.length === 0 && state.get(HAS_ES_MODULE)) {
3535
path.pushContainer("body", t.exportNamedDeclaration(null));
36+
} else {
37+
const seen = new Set<string>();
38+
39+
const children = path.get("body");
40+
for (let i = children.length - 1; i >= 0; i--) {
41+
const child = children[i];
42+
if (child.isExportNamedDeclaration()) {
43+
if (
44+
t.isVariableDeclaration(child.node.declaration) &&
45+
child.node.declaration.declarations.length > 0 &&
46+
t.isIdentifier(child.node.declaration.declarations[0].id)
47+
) {
48+
const name = child.node.declaration.declarations[0].id.name;
49+
50+
if (seen.has(name)) {
51+
child.remove();
52+
} else {
53+
seen.add(name);
54+
}
55+
}
56+
}
57+
}
3658
}
3759
},
3860
},
@@ -112,6 +134,70 @@ export function cjsPlugin(
112134
}
113135
}
114136
}
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+
}
115201
}
116202
},
117203
},

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,58 @@ var a = _mod.default ?? _mod,
165165
});
166166
});
167167

168+
Deno.test("commonjs - duplicate exports", () => {
169+
runTest({
170+
input: `Object.defineProperty(exports, "__esModule", { value: true });
171+
exports.trace = void 0;
172+
exports.trace = 'foo'`,
173+
expected: `export let trace = 'foo';`,
174+
});
175+
});
176+
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+
168220
// I've never seen this, seems rare. Skipping for now.
169221
Deno.test.ignore("commonjs - require", () => {
170222
runTest({

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ export function deno(): Plugin {
2424
return true;
2525
},
2626
async resolveId(id, importer, options) {
27+
// Workaround until upstream PR is merged and released,
28+
// see: https://github.com/vitejs/vite/pull/20558
29+
if (id.startsWith("deno-npm:")) {
30+
id = id.slice("deno-".length);
31+
}
32+
2733
importer = isDenoSpecifier(importer)
2834
? parseDenoSpecifier(importer).specifier
2935
: importer;

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Plugin } from "vite";
22
import * as babel from "@babel/core";
3-
import { cjsPlugin } from "./commonjs.ts";
43
import { npmWorkaround } from "./npm_workaround.ts";
54

65
export function patches(): Plugin {
@@ -9,16 +8,11 @@ export function patches(): Plugin {
98
applyToEnvironment() {
109
return true;
1110
},
12-
resolveId(id) {
13-
if (id.startsWith("deno-npm:")) {
14-
return id.slice("deno-".length);
15-
}
16-
},
1711
transform(code, id) {
1812
const res = babel.transformSync(code, {
1913
filename: id,
2014
babelrc: false,
21-
plugins: [npmWorkaround, cjsPlugin],
15+
plugins: [npmWorkaround],
2216
});
2317

2418
if (res?.code) {

0 commit comments

Comments
 (0)