1
1
import { pathToFileURL } from 'node:url'
2
2
import vm from 'node:vm'
3
3
import { DEFAULT_REQUEST_STUBS , ModuleCacheMap , ViteNodeRunner } from 'vite-node/client'
4
- import { isInternalRequest , isNodeBuiltin , isPrimitive } from 'vite-node/utils'
4
+ import { isInternalRequest , isNodeBuiltin , isPrimitive , toFilePath } from 'vite-node/utils'
5
5
import type { ViteNodeRunnerOptions } from 'vite-node'
6
6
import { normalize , relative , resolve } from 'pathe'
7
7
import { processError } from '@vitest/utils/error'
@@ -41,6 +41,7 @@ export const packageCache = new Map<string, any>()
41
41
export const moduleCache = new ModuleCacheMap ( )
42
42
export const mockMap : MockMap = new Map ( )
43
43
export const fileMap = new FileMap ( )
44
+ const externalizeMap = new Map < string , string > ( )
44
45
45
46
export async function startViteNode ( options : ContextExecutorOptions ) {
46
47
if ( _viteNode )
@@ -64,7 +65,7 @@ export interface ContextExecutorOptions {
64
65
65
66
export async function startVitestExecutor ( options : ContextExecutorOptions ) {
66
67
// @ts -expect-error injected untyped global
67
- const state = ( ) => globalThis . __vitest_worker__ || options . state
68
+ const state = ( ) : WorkerGlobalState => globalThis . __vitest_worker__ || options . state
68
69
const rpc = ( ) => state ( ) . rpc
69
70
70
71
const processExit = process . exit
@@ -97,7 +98,18 @@ export async function startVitestExecutor(options: ContextExecutorOptions) {
97
98
}
98
99
99
100
return await createVitestExecutor ( {
100
- fetchModule ( id ) {
101
+ async fetchModule ( id ) {
102
+ if ( externalizeMap . has ( id ) )
103
+ return { externalize : externalizeMap . get ( id ) ! }
104
+ // always externalize Vitest because we import from there before running tests
105
+ // so we already have it cached by Node.js
106
+ if ( id . includes ( distDir ) ) {
107
+ const { path } = toFilePath ( id , state ( ) . config . root )
108
+ const externalize = pathToFileURL ( path ) . toString ( )
109
+ externalizeMap . set ( id , externalize )
110
+ return { externalize }
111
+ }
112
+
101
113
return rpc ( ) . fetch ( id , getTransformMode ( ) )
102
114
} ,
103
115
resolveId ( id , importer ) {
0 commit comments