From b5ea834071497637fe8c37f5d90507e0010b46e7 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Mon, 5 Sep 2022 10:49:14 +0300 Subject: [PATCH] fix: define default exports properties as named exports for ESM compatibility --- packages/vite-node/src/client.ts | 11 ++--------- packages/vite-node/src/utils.ts | 4 ---- test/core/test/module.test.ts | 2 +- 3 files changed, 3 insertions(+), 14 deletions(-) diff --git a/packages/vite-node/src/client.ts b/packages/vite-node/src/client.ts index b9cff3267fe9..8563230b0e86 100644 --- a/packages/vite-node/src/client.ts +++ b/packages/vite-node/src/client.ts @@ -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') @@ -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) { diff --git a/packages/vite-node/src/utils.ts b/packages/vite-node/src/utils.ts index 84eb5604d09e..b5c2d933d59e 100644 --- a/packages/vite-node/src/utils.ts +++ b/packages/vite-node/src/utils.ts @@ -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, '/') } diff --git a/test/core/test/module.test.ts b/test/core/test/module.test.ts index 73425312da6a..4bc8c2dd26f7 100644 --- a/test/core/test/module.test.ts +++ b/test/core/test/module.test.ts @@ -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', () => {