Skip to content

Commit

Permalink
fix: improve compatibility with native Node CJS resolution (#2226)
Browse files Browse the repository at this point in the history
* fix: improve compatibility with native Node CJS resolution

* chore: types

* test: increase stacktrace timeout
  • Loading branch information
sheremet-va committed Oct 31, 2022
1 parent a1ee96a commit fe71946
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 18 deletions.
31 changes: 14 additions & 17 deletions packages/vite-node/src/client.ts
Expand Up @@ -247,24 +247,21 @@ export class ViteNodeRunner {
return Reflect.get(exports, p, receiver)
},
set(_, p, value) {
// Node also allows access of named exports via exports.default
// https://nodejs.org/api/esm.html#commonjs-namespaces
if (p !== 'default') {
if (!Reflect.has(exports, 'default'))
exports.default = {}

// returns undefined, when accessing named exports, if default is not an object
// but is still present inside hasOwnKeys, this is Node behaviour for CJS
if (exports.default === null || typeof exports.default !== 'object') {
defineExport(exports, p, () => undefined)
return true
}

exports.default[p] = value
defineExport(exports, p, () => value)
if (!Reflect.has(exports, 'default'))
exports.default = {}

// returns undefined, when accessing named exports, if default is not an object
// but is still present inside hasOwnKeys, this is Node behaviour for CJS
if (exports.default === null || typeof exports.default !== 'object') {
defineExport(exports, p, () => undefined)
return true
}
return Reflect.set(exports, p, value)

exports.default[p] = value
if (p !== 'default')
defineExport(exports, p, () => value)

return true
},
})

Expand All @@ -274,7 +271,7 @@ export class ViteNodeRunner {
const moduleProxy = {
set exports(value) {
exportAll(cjsExports, value)
cjsExports.default = value
exports.default = value
},
get exports() {
return cjsExports
Expand Down
2 changes: 2 additions & 0 deletions test/cjs/src/other.cjs
@@ -0,0 +1,2 @@
'use strict'
exports.default = 2
5 changes: 5 additions & 0 deletions test/cjs/src/other.d.cts
@@ -0,0 +1,5 @@
declare const defaultExports: {
default: number
}

export default defaultExports
15 changes: 15 additions & 0 deletions test/cjs/test/named-default.test.ts
@@ -0,0 +1,15 @@
import { describe, expect, it } from 'vitest'

import * as other from '../src/other.cjs'
import defaultOther from '../src/other.cjs'

describe('correctly identified named default', () => {
it('default should be on default', () => {
expect(other.default).toBe(defaultOther)
})

it('default is an object with default', () => {
expect(other.default).toMatchObject({ default: 2 })
expect(defaultOther).toMatchObject({ default: 2 })
})
})
2 changes: 1 addition & 1 deletion test/stacktraces/test/runner.test.ts
Expand Up @@ -31,6 +31,6 @@ describe('stacktraces should respect sourcemaps', async () => {
const index = lines.findIndex(val => val.includes(`${file}:`))
const msg = lines.slice(index, index + 8).join('\n')
expect(msg).toMatchSnapshot(file)
}, 10000)
}, 30000)
}
})

0 comments on commit fe71946

Please sign in to comment.