/
worker.ts
104 lines (85 loc) · 2.7 KB
/
worker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { resolve } from 'pathe'
import { createBirpc } from 'birpc'
import { ModuleCacheMap } from 'vite-node/client'
import type { ResolvedConfig, WorkerContext, WorkerRPC } from '../types'
import { distDir } from '../constants'
import { getWorkerState } from '../utils'
import type { MockMap } from '../types/mocker'
import { executeInViteNode } from './execute'
import { rpc } from './rpc'
let _viteNode: {
run: (files: string[], config: ResolvedConfig) => Promise<void>
collect: (files: string[], config: ResolvedConfig) => Promise<void>
}
const moduleCache = new ModuleCacheMap()
const mockMap: MockMap = new Map()
async function startViteNode(ctx: WorkerContext) {
if (_viteNode)
return _viteNode
const processExit = process.exit
process.on('beforeExit', (code) => {
rpc().onWorkerExit(code)
})
process.exit = (code = process.exitCode || 0): never => {
rpc().onWorkerExit(code)
return processExit(code)
}
process.on('unhandledRejection', (err) => {
rpc().onUnhandledRejection(err)
})
const { config } = ctx
const { run, collect } = (await executeInViteNode({
files: [
resolve(distDir, 'entry.js'),
],
fetchModule(id) {
return rpc().fetch(id)
},
resolveId(id, importer) {
return rpc().resolveId(id, importer)
},
moduleCache,
mockMap,
interopDefault: config.deps.interopDefault ?? true,
root: config.root,
base: config.base,
}))[0]
_viteNode = { run, collect }
return _viteNode
}
function init(ctx: WorkerContext) {
// @ts-expect-error untyped global
if (typeof __vitest_worker__ !== 'undefined' && ctx.config.threads && ctx.config.isolate)
throw new Error(`worker for ${ctx.files.join(',')} already initialized by ${getWorkerState().ctx.files.join(',')}. This is probably an internal bug of Vitest.`)
process.stdout.write('\0')
const { config, port, id } = ctx
process.env.VITEST_WORKER_ID = String(id)
// @ts-expect-error I know what I am doing :P
globalThis.__vitest_worker__ = {
ctx,
moduleCache,
config,
mockMap,
rpc: createBirpc<WorkerRPC>(
{},
{
eventNames: ['onUserConsoleLog', 'onFinished', 'onCollected', 'onWorkerExit'],
post(v) { port.postMessage(v) },
on(fn) { port.addListener('message', fn) },
},
),
}
if (ctx.invalidates)
ctx.invalidates.forEach(i => moduleCache.delete(i))
ctx.files.forEach(i => moduleCache.delete(i))
}
export async function collect(ctx: WorkerContext) {
init(ctx)
const { collect } = await startViteNode(ctx)
return collect(ctx.files, ctx.config)
}
export async function run(ctx: WorkerContext) {
init(ctx)
const { run } = await startViteNode(ctx)
return run(ctx.files, ctx.config)
}