-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
worker.ts
166 lines (146 loc) · 4.05 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import v8 from 'node:v8'
import { register } from 'node:module'
import { dirname } from 'pathe'
import { parseErrorStacktrace } from '@vitest/utils/source-map'
import type { BirpcReturn } from 'birpc'
import type { File, Reporter, TaskResultPack, UserConsoleLog, Vitest } from 'vitest'
import type { BirpcEvents, BirpcMethods } from '../api/rpc'
import { createWorkerRPC } from './rpc'
import type { WorkerMeta, WorkerRunnerOptions } from './types'
class VSCodeReporter implements Reporter {
private rpc!: BirpcReturn<BirpcEvents, BirpcMethods>
private ctx!: Vitest
private id!: string
initVitest(ctx: Vitest, id: string) {
this.ctx = ctx
this.id = id
}
initRpc(rpc: BirpcReturn<BirpcEvents, BirpcMethods>) {
this.rpc = rpc
}
onUserConsoleLog(log: UserConsoleLog) {
this.rpc.onConsoleLog(this.id, log)
}
onTaskUpdate(packs: TaskResultPack[]) {
packs.forEach(([taskId, result]) => {
const project = this.ctx.getProjectByTaskId(taskId)
result?.errors?.forEach((error) => {
if (typeof error === 'object' && error) {
error.stacks = parseErrorStacktrace(error, {
getSourceMap: file => project.getBrowserSourceMapModuleById(file),
})
}
})
})
this.rpc.onTaskUpdate(this.id, packs)
}
onFinished(files?: File[], errors?: unknown[]) {
this.rpc.onFinished(this.id, files, errors)
}
onCollected(files?: File[]) {
this.rpc.onCollected(this.id, files)
}
onWatcherStart(files?: File[], errors?: unknown[]) {
this.rpc.onWatcherStart(this.id, files, errors)
}
onWatcherRerun(files: string[], trigger?: string) {
this.rpc.onWatcherRerun(this.id, files, trigger)
}
}
async function initVitest(meta: WorkerMeta) {
const vitestMode = await import(meta.vitestNodePath) as typeof import('vitest/node')
const reporter = new VSCodeReporter()
const vitest = await vitestMode.createVitest(
'test',
{
config: meta.configFile,
workspace: meta.workspaceFile,
watch: true,
api: false,
root: dirname(meta.id),
reporters: [reporter],
ui: false,
env: meta.env,
includeTaskLocation: true,
},
{
server: {
middlewareMode: true,
},
plugins: [
{
name: 'vitest:vscode-extension',
configResolved(config) {
// stub a server so Vite doesn't start a websocket connection,
// because we don't need it in the extension and it messes up Vite dev command
config.server.hmr = {
server: {
on: () => {},
off: () => {},
} as any,
}
},
},
],
},
)
reporter.initVitest(vitest, meta.id)
return {
vitest,
reporter,
}
}
const cwd = process.cwd()
process.on('message', async function init(message: any) {
if (message.type === 'init') {
process.off('message', init)
const data = message as WorkerRunnerOptions
try {
if (data.loader)
register(data.loader)
const errors = []
const vitest = []
for (const meta of data.meta) {
process.chdir(dirname(meta.id))
try {
vitest.push(await initVitest(meta))
}
catch (err: any) {
errors.push([meta.configFile, err.stack])
}
}
process.chdir(cwd)
if (!vitest.length) {
process.send!({ type: 'error', errors })
return
}
const rpc = createWorkerRPC(vitest.map(v => v.vitest), {
on(listener) {
process.on('message', listener)
},
post(message) {
process.send!(message)
},
serialize: v8.serialize,
deserialize: v => v8.deserialize(Buffer.from(v)),
})
vitest.forEach(v => v.reporter.initRpc(rpc))
process.send!({ type: 'ready', errors })
}
catch (err: any) {
error(err)
}
}
})
function error(err: any) {
process.send!({
type: 'error',
errors: ['', String(err.stack)],
})
}
function _debug(...args: any[]) {
process.send!({
type: 'debug',
args,
})
}