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: don't ignore properties, when put on a default function in CJS context #2325

Merged
merged 2 commits into from Nov 14, 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
2 changes: 1 addition & 1 deletion packages/vite-node/src/client.ts
Expand Up @@ -273,7 +273,7 @@ export class ViteNodeRunner {

// 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') {
if (isPrimitive(exports.default)) {
defineExport(exports, p, () => undefined)
return true
}
Expand Down
8 changes: 8 additions & 0 deletions test/cjs/src/default-function.cjs
@@ -0,0 +1,8 @@
'use strict'

function format() {
return ''
}

module.exports = format
module.exports.default = format
3 changes: 3 additions & 0 deletions test/cjs/src/default-function.d.cts
@@ -0,0 +1,3 @@
declare function format(): string

export default format
13 changes: 13 additions & 0 deletions test/cjs/test/function-default.test.ts
@@ -0,0 +1,13 @@
import { describe, expect, it } from 'vitest'
import format from '../src/default-function.cjs'

describe('correctly puts default on default', () => {
it('works on default function', () => {
expect(format()).toBe('')
})

it('works on nested default function', () => {
// @ts-expect-error types defined only default
expect(format.default()).toBe('')
})
})