Skip to content

Commit 01c4fe3

Browse files
pionxzhantfu
andauthored
fix: improve cjs proxy (#508)
Co-authored-by: spigbbbbb@gmil.com <pionxzh> Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
1 parent fc15d52 commit 01c4fe3

File tree

6 files changed

+27
-2
lines changed

6 files changed

+27
-2
lines changed

packages/vite-node/src/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { builtinModules, createRequire } from 'module'
22
import { fileURLToPath, pathToFileURL } from 'url'
33
import vm from 'vm'
44
import { dirname, resolve } from 'pathe'
5-
import { normalizeId, slash, toFilePath } from './utils'
5+
import { isPrimitive, normalizeId, slash, toFilePath } from './utils'
66
import type { ModuleCache, ViteNodeRunnerOptions } from './types'
77

88
export class ViteNodeRunner {
@@ -126,7 +126,7 @@ function hasNestedDefault(target: any) {
126126
function proxyMethod(name: 'get' | 'set' | 'has' | 'deleteProperty', tryDefault: boolean) {
127127
return function(target: any, key: string | symbol, ...args: [any?, any?]) {
128128
const result = Reflect[name](target, key, ...args)
129-
if (typeof target.default !== 'object')
129+
if (isPrimitive(target.default))
130130
return result
131131
if ((tryDefault && key === 'default') || typeof result === 'undefined')
132132
return Reflect[name](target.default, key, ...args)

packages/vite-node/src/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export function normalizeId(id: string, base?: string): string {
2020
.replace(/\?$/, '') // remove end query mark
2121
}
2222

23+
export function isPrimitive(v: any) {
24+
return v !== Object(v)
25+
}
26+
2327
export function toFilePath(id: string, root: string): string {
2428
let absolute = slash(id).startsWith('/@fs/')
2529
? id.slice(4)

pnpm-lock.yaml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cjs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"fs-extra": "^10.0.0",
1212
"givens": "1.3.9",
1313
"history": "^5.2.0",
14+
"lodash": "^4.17.21",
1415
"prettier": "^2.5.1",
1516
"temp-dir": "^2.0.0",
1617
"vitest": "workspace:*"

test/cjs/test/interpret-default.test.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ import { expect, it } from 'vitest'
22
import { format } from 'prettier'
33
import givens from 'givens'
44
import tempDir from 'temp-dir'
5+
import _, { isString } from 'lodash'
56

67
it('prettier', () => {
78
expect(format('const a : A = \'t\'', { parser: 'typescript' }).trim())
89
.toEqual('const a: A = "t";'.trim())
910
})
1011

12+
it('lodash', () => {
13+
expect(typeof _.isString).toBe('function')
14+
expect(typeof isString).toBe('function')
15+
})
16+
1117
it('has nested default', () => {
1218
expect(typeof givens).toBe('function')
1319
expect(givens.name).toBe('getGiven')

test/cjs/test/lodash.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import fs, { existsSync } from 'fs-extra'
2+
import { describe, expect, it } from 'vitest'
3+
4+
describe('fs-extra', () => {
5+
it('default export', () => {
6+
expect(fs.existsSync('test/fs-extra.test.ts')).toBe(true)
7+
})
8+
9+
it('named export', () => {
10+
expect(existsSync('test/fs-extra.test.ts')).toBe(true)
11+
})
12+
})

0 commit comments

Comments
 (0)