Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve compatibility with native Node CJS resolution #2226

Merged
merged 3 commits into from Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}
})