From db22c6772519f62a6435a9cac21017d4b56441aa Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Tue, 4 Jul 2023 11:32:03 +0200 Subject: [PATCH] fix(vite-node): allow importing node:test --- packages/vite-node/src/externalize.ts | 4 +-- packages/vite-node/src/utils.ts | 43 ++++++++++++++++----------- test/core/src/cjs/promise-export.js | 1 + test/core/test/builtin.test.ts | 8 +++++ 4 files changed, 37 insertions(+), 19 deletions(-) create mode 100644 test/core/src/cjs/promise-export.js create mode 100644 test/core/test/builtin.test.ts diff --git a/packages/vite-node/src/externalize.ts b/packages/vite-node/src/externalize.ts index 91abd815a2ab..ba7bc1c124f0 100644 --- a/packages/vite-node/src/externalize.ts +++ b/packages/vite-node/src/externalize.ts @@ -1,8 +1,8 @@ import { existsSync } from 'node:fs' -import { isNodeBuiltin, isValidNodeImport } from 'mlly' +import { isValidNodeImport } from 'mlly' import { join } from 'pathe' import type { DepsHandlingOptions } from './types' -import { slash } from './utils' +import { isNodeBuiltin, slash } from './utils' const KNOWN_ASSET_TYPES = [ // images diff --git a/packages/vite-node/src/utils.ts b/packages/vite-node/src/utils.ts index db409988d912..78acb5a4b887 100644 --- a/packages/vite-node/src/utils.ts +++ b/packages/vite-node/src/utils.ts @@ -50,7 +50,31 @@ export function isInternalRequest(id: string): boolean { return internalRequestRegexp.test(id) } +const prefixedBuiltins = new Set([ + 'node:test', +]) + +const builtins = new Set([ + ...builtinModules, + 'assert/strict', + 'diagnostics_channel', + 'dns/promises', + 'fs/promises', + 'path/posix', + 'path/win32', + 'readline/promises', + 'stream/consumers', + 'stream/promises', + 'stream/web', + 'timers/promises', + 'util/types', + 'wasi', +]) + export function normalizeModuleId(id: string) { + // unique id that is not available as "test" + if (prefixedBuiltins.has(id)) + return id return id .replace(/\\/g, '/') .replace(/^\/@fs\//, isWindows ? '' : '/') @@ -91,25 +115,10 @@ export function toFilePath(id: string, root: string): { path: string; exists: bo } } -const builtins = new Set([ - ...builtinModules, - 'assert/strict', - 'diagnostics_channel', - 'dns/promises', - 'fs/promises', - 'path/posix', - 'path/win32', - 'readline/promises', - 'stream/consumers', - 'stream/promises', - 'stream/web', - 'timers/promises', - 'util/types', - 'wasi', -]) - const NODE_BUILTIN_NAMESPACE = 'node:' export function isNodeBuiltin(id: string): boolean { + if (prefixedBuiltins.has(id)) + return true return builtins.has( id.startsWith(NODE_BUILTIN_NAMESPACE) ? id.slice(NODE_BUILTIN_NAMESPACE.length) diff --git a/test/core/src/cjs/promise-export.js b/test/core/src/cjs/promise-export.js new file mode 100644 index 000000000000..85d9732c58cf --- /dev/null +++ b/test/core/src/cjs/promise-export.js @@ -0,0 +1 @@ +module.exports = Promise.resolve({ value: 42 }) diff --git a/test/core/test/builtin.test.ts b/test/core/test/builtin.test.ts new file mode 100644 index 000000000000..7cbbd2fdf4fb --- /dev/null +++ b/test/core/test/builtin.test.ts @@ -0,0 +1,8 @@ +import testModule, { run } from 'node:test' +import { expect, it } from 'vitest' + +it('node:test works correctly', () => { + expect(run).toBeTypeOf('function') + expect(testModule).toBeTypeOf('function') + expect(testModule.run).toBeTypeOf('function') +})