Skip to content

Commit

Permalink
fix: don't ignore properties, when put on a default function in CJS c…
Browse files Browse the repository at this point in the history
…ontext (#2325)

* fix: don't ignore "default" key, when put on a function in CJS context

* test: test case for default.default
  • Loading branch information
sheremet-va committed Nov 14, 2022
1 parent 30c59f5 commit aa8e662
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
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('')
})
})

0 comments on commit aa8e662

Please sign in to comment.