Skip to content

Commit b097cef

Browse files
authoredJun 15, 2023
fix(vite-node): fix errors caused by commonjs export circular references (#3570)
1 parent 368b825 commit b097cef

File tree

8 files changed

+251
-199
lines changed

8 files changed

+251
-199
lines changed
 

‎packages/vite-node/src/client.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ export class ViteNodeRunner {
330330
set: (_, p, value) => {
331331
// treat "module.exports =" the same as "exports.default =" to not have nested "default.default",
332332
// so "exports.default" becomes the actual module
333-
if (p === 'default' && this.shouldInterop(modulePath, { default: value })) {
333+
if (p === 'default' && this.shouldInterop(modulePath, { default: value }) && cjsExports !== value) {
334334
exportAll(cjsExports, value)
335335
exports.default = value
336336
return true

‎pnpm-lock.yaml

+201-198
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/vite-node/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
"debug": "node --inspect-brk ../../packages/vite-node/dist/cli.cjs"
1010
},
1111
"devDependencies": {
12+
"@types/inquirer": "^9.0.3",
1213
"execa": "^6.1.0",
14+
"inquirer": "^9.2.7",
1315
"vite-node": "workspace:*",
1416
"vitest": "workspace:*"
1517
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// const ansiEscapes = module.exports
2+
// module.exports.default = ansiEscapes
3+
// ansiEscapes.HelloWorld = 1
4+
// console.log(ansiEscapes.HelloWorld);
5+
6+
import inquirer from 'inquirer'
7+
8+
// eslint-disable-next-line no-console
9+
console.log(inquirer.prompt)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import ansiEscapes, { HelloWorld } from './self-export'
2+
3+
// eslint-disable-next-line no-console
4+
console.log(ansiEscapes, HelloWorld)

‎test/vite-node/src/self-export.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
declare const ansiEscapes : {
2+
HelloWorld: number
3+
}
4+
export default ansiEscapes
5+
6+
declare const HelloWorld : number
7+
export { HelloWorld }

‎test/vite-node/src/self-export.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const ansiEscapes = module.exports
2+
module.exports.default = ansiEscapes
3+
ansiEscapes.HelloWorld = 1
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { expect, it } from 'vitest'
2+
import { execa } from 'execa'
3+
import { resolve } from 'pathe'
4+
import ansiEscapes, { HelloWorld } from '../src/self-export'
5+
6+
it('should export self', () => {
7+
expect(ansiEscapes.HelloWorld).eq(HelloWorld)
8+
expect(Reflect.get(ansiEscapes, 'default').HelloWorld).eq(HelloWorld)
9+
expect(HelloWorld).eq(1)
10+
})
11+
12+
const cliPath = resolve(__dirname, '../../../packages/vite-node/src/cli.ts')
13+
14+
it('example 1', async () => {
15+
const entryPath = resolve(__dirname, '../src/self-export-example1.ts')
16+
const result = await execa('npx', ['esno', cliPath, entryPath], { reject: true })
17+
expect(result.stdout).includes('Function')
18+
}, 60_000)
19+
20+
it('example 2', async () => {
21+
const entryPath = resolve(__dirname, '../src/self-export-example2.ts')
22+
const result = await execa('npx', ['esno', cliPath, entryPath], { reject: true })
23+
expect(result.stdout).includes('HelloWorld: 1').includes('default')
24+
}, 60_000)

0 commit comments

Comments
 (0)
Please sign in to comment.