Skip to content

Commit

Permalink
fix(vite-node): allow importing node:test
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jul 4, 2023
1 parent 0819275 commit db22c67
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
4 changes: 2 additions & 2 deletions 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
Expand Down
43 changes: 26 additions & 17 deletions packages/vite-node/src/utils.ts
Expand Up @@ -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 ? '' : '/')
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/core/src/cjs/promise-export.js
@@ -0,0 +1 @@
module.exports = Promise.resolve({ value: 42 })
8 changes: 8 additions & 0 deletions 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')
})

0 comments on commit db22c67

Please sign in to comment.