forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverRunner.ts
More file actions
56 lines (54 loc) · 1.81 KB
/
serverRunner.ts
File metadata and controls
56 lines (54 loc) · 1.81 KB
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
import type { DevEnvironment } from 'vite'
import type { ResolvedConfig } from '../types/config'
import type { VitestFetchFunction } from './fetchModule'
import { VitestModuleEvaluator } from '#module-evaluator'
import { ModuleRunner } from 'vite/module-runner'
import { normalizeResolvedIdToUrl } from './normalizeUrl'
export class ServerModuleRunner extends ModuleRunner {
constructor(
private environment: DevEnvironment,
fetcher: VitestFetchFunction,
private config: ResolvedConfig,
) {
super(
{
hmr: false,
transport: {
async invoke(event) {
if (event.type !== 'custom') {
throw new Error(`Vitest Module Runner doesn't support Vite HMR events.`)
}
const { name, data } = event.data
if (name === 'getBuiltins') {
return await environment.hot.handleInvoke(event)
}
if (name !== 'fetchModule') {
return { error: new Error(`Unknown method: ${name}. Expected "fetchModule".`) }
}
try {
const result = await fetcher(data[0], data[1], environment, false, data[2])
return { result }
}
catch (error) {
return { error }
}
},
},
},
new VitestModuleEvaluator(),
)
}
async import(rawId: string): Promise<any> {
const resolved = await this.environment.pluginContainer.resolveId(
rawId,
this.config.root,
)
if (!resolved) {
return super.import(rawId)
}
// Vite will make "@vitest/coverage-v8" into "@vitest/coverage-v8.js" url
// instead of using an actual file path-like URL, so we resolve it here first
const url = normalizeResolvedIdToUrl(this.environment, resolved.id)
return super.import(url)
}
}