/
index.ts
138 lines (126 loc) · 4.96 KB
/
index.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
import type { Plugin as VitePlugin } from 'vite'
import { configDefaults } from '../../defaults'
import type { ResolvedConfig, UserConfig } from '../../types'
import { deepMerge, ensurePackageInstalled, notNullish } from '../../utils'
import { resolveApiConfig } from '../config'
import { Vitest } from '../core'
import { GlobalSetupPlugin } from './globalSetup'
import { MocksPlugin } from './mock'
export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest()): Promise<VitePlugin[]> {
let haveStarted = false
async function UIPlugin() {
await ensurePackageInstalled('@vitest/ui')
return (await import('@vitest/ui')).default(options.uiBase)
}
return [
<VitePlugin>{
name: 'vitest',
enforce: 'pre',
config(viteConfig: any) {
// preliminary merge of options to be able to create server options for vite
// however to allow vitest plugins to modify vitest config values
// this is repeated in configResolved where the config is final
const preOptions = deepMerge({}, options, viteConfig.test ?? {})
preOptions.api = resolveApiConfig(preOptions)
// store defines for globalThis to make them
// reassignable when running in worker in src/runtime/setup.ts
const defines: Record<string, any> = {}
for (const key in viteConfig.define) {
const val = viteConfig.define[key]
let replacement: any
try {
replacement = typeof val === 'string' ? JSON.parse(val) : val
}
catch {
// probably means it contains reference to some variable,
// like this: "__VAR__": "process.env.VAR"
continue
}
if (key.startsWith('import.meta.env.')) {
const envKey = key.slice('import.meta.env.'.length)
process.env[envKey] = replacement
delete viteConfig.define[key]
}
else if (key.startsWith('process.env.')) {
const envKey = key.slice('process.env.'.length)
process.env[envKey] = replacement
delete viteConfig.define[key]
}
else if (!key.includes('.')) {
defines[key] = replacement
delete viteConfig.define[key]
}
}
(options as ResolvedConfig).defines = defines
return {
// we are setting NODE_ENV when running CLI to 'test',
// but it can be overridden
mode: viteConfig.mode || process.env.NODE_ENV || 'test',
clearScreen: false,
resolve: {
// by default Vite resolves `module` field, which not always a native ESM module
// setting this option can bypass that and fallback to cjs version
mainFields: [],
},
define: {
'process.env.NODE_ENV': 'process.env.NODE_ENV',
'global.process.env.NODE_ENV': 'global.process.env.NODE_ENV',
'globalThis.process.env.NODE_ENV': 'globalThis.process.env.NODE_ENV',
// so people can reassign envs at runtime
// import.meta.env.VITE_NAME = 'app' -> process.env.VITE_NAME = 'app'
'import.meta.env': 'process.env',
},
server: {
...preOptions.api,
open: preOptions.ui && preOptions.open
? preOptions.uiBase ?? '/__vitest__/'
: undefined,
preTransformRequests: false,
},
// disable deps optimization
cacheDir: undefined,
}
},
async configResolved(viteConfig) {
const viteConfigTest = (viteConfig.test as any) || {}
if (viteConfigTest.watch === false)
viteConfigTest.run = true
// viteConfig.test is final now, merge it for real
options = deepMerge(
{},
configDefaults,
viteConfigTest,
options,
)
options.api = resolveApiConfig(options)
// we replace every "import.meta.env" with "process.env"
// to allow reassigning, so we need to put all envs on process.env
const { PROD, DEV, ...envs } = viteConfig.env
// process.env can have only string values and will cast string on it if we pass other type,
// so we are making them truthy
process.env.PROD ??= PROD ? '1' : ''
process.env.DEV ??= DEV ? '1' : ''
process.env.SSR ??= '1'
for (const name in envs)
process.env[name] ??= envs[name]
},
async configureServer(server) {
if (haveStarted)
await ctx.report('onServerRestart')
await ctx.setServer(options, server)
haveStarted = true
if (options.api)
(await import('../../api/setup')).setup(ctx)
// #415, in run mode we don't need the watcher, close it would improve the performance
if (!options.watch)
await server.watcher.close()
},
},
MocksPlugin(),
GlobalSetupPlugin(ctx),
options.ui
? await UIPlugin()
: null,
]
.filter(notNullish)
}