Skip to content

Commit

Permalink
fix: define default exports properties as named exports for ESM compa…
Browse files Browse the repository at this point in the history
…tibility (#1979)
  • Loading branch information
sheremet-va committed Sep 5, 2022
1 parent a98c845 commit 5b760b0
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 14 deletions.
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

0 comments on commit 5b760b0

Please sign in to comment.