fix: don't ignore properties, when put on a default function in CJS context (#2325)

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

* test: test case for default.default
This commit is contained in:
Vladimir 2022-11-14 13:59:21 +01:00 committed by GitHub
parent 30c59f50c0
commit aa8e66250e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 1 deletions

View File

@ -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
}

View File

@ -0,0 +1,8 @@
'use strict'
function format() {
return ''
}
module.exports = format
module.exports.default = format

View File

@ -0,0 +1,3 @@
declare function format(): string
export default format

View File

@ -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('')
})
})