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: define default exports properties as named exports for ESM compatibility #1979

Merged
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
11 changes: 2 additions & 9 deletions packages/vite-node/src/client.ts
Expand Up @@ -4,7 +4,7 @@ import vm from 'vm'
import { dirname, extname, isAbsolute, resolve } from 'pathe'
import { isNodeBuiltin } from 'mlly'
import createDebug from 'debug'
import { getType, isPrimitive, mergeSlashes, normalizeModuleId, normalizeRequestId, slash, toFilePath } from './utils'
import { isPrimitive, mergeSlashes, normalizeModuleId, normalizeRequestId, slash, toFilePath } from './utils'
import type { HotContext, ModuleCache, ViteNodeRunnerOptions } from './types'

const debugExecute = createDebug('vite-node:client:execute')
Expand Down Expand Up @@ -404,14 +404,7 @@ function exportAll(exports: any, sourceModule: any) {
if (exports === sourceModule)
return

const type = getType(sourceModule)
if (type !== 'Object' && type !== 'Module')
return

const constructor = sourceModule.constructor?.name

// allow only plain objects and modules (modules don't have name)
if (constructor && constructor !== 'Object')
if (typeof sourceModule !== 'object' || Array.isArray(sourceModule) || !sourceModule)
return

for (const key in sourceModule) {
Expand Down
4 changes: 0 additions & 4 deletions packages/vite-node/src/utils.ts
Expand Up @@ -9,10 +9,6 @@ export function slash(str: string) {
return str.replace(/\\/g, '/')
}

export function getType(value: unknown): string {
return Object.prototype.toString.apply(value).slice(8, -1)
}

export function mergeSlashes(str: string) {
return str.replace(/\/\//g, '/')
}
Expand Down
2 changes: 1 addition & 1 deletion test/core/test/module.test.ts
Expand Up @@ -50,7 +50,7 @@ it('arrays-cjs', () => {
it('class-cjs', () => {
expect(classCjs.default).toEqual({ variable: 1, Test: expect.any(Function) })
expect(classCjs.default).toBeInstanceOf(classCjs.Test)
expect(classCjs).not.toHaveProperty('variable')
expect(classCjs, 'for compat with ESM it also defines props on Module').toHaveProperty('variable')
})

it('should work when using esm module', () => {
Expand Down