-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathindex.ts
75 lines (65 loc) · 2.3 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
import type { WorkspaceProject } from 'vitest/node'
import type { Plugin } from 'vitest/config'
import { createViteLogger, createViteServer } from 'vitest/node'
import c from 'tinyrainbow'
import { version } from '../../package.json'
import { setupBrowserRpc } from './rpc'
import { BrowserServer } from './server'
import BrowserPlugin from './plugin'
export type { BrowserServer } from './server'
export { createBrowserPool } from './pool'
export async function createBrowserServer(
project: WorkspaceProject,
configFile: string | undefined,
prePlugins: Plugin[] = [],
postPlugins: Plugin[] = [],
) {
if (project.ctx.version !== version) {
project.ctx.logger.warn(
c.yellow(
`Loaded ${c.inverse(c.yellow(` vitest@${project.ctx.version} `))} and ${c.inverse(c.yellow(` @vitest/browser@${version} `))}.`
+ '\nRunning mixed versions is not supported and may lead into bugs'
+ '\nUpdate your dependencies and make sure the versions match.',
),
)
}
const server = new BrowserServer(project, '/')
const configPath = typeof configFile === 'string' ? configFile : false
const logLevel = (process.env.VITEST_BROWSER_DEBUG as 'info') ?? 'info'
const logger = createViteLogger(logLevel)
const vite = await createViteServer({
...project.options, // spread project config inlined in root workspace config
base: '/',
logLevel,
customLogger: {
...logger,
info(msg, options) {
logger.info(msg, options)
if (msg.includes('optimized dependencies changed. reloading')) {
logger.warn(
[
c.yellow(`\n${c.bold('[vitest]')} Vite unexpectedly reloaded a test. This may cause tests to fail, lead to flaky behaviour or duplicated test runs.\n`),
c.yellow(`For a stable experience, please add mentioned dependencies to your config\'s ${c.bold('\`optimizeDeps.include\`')} field manually.\n\n`),
].join(''),
)
}
},
},
mode: project.config.mode,
configFile: configPath,
// watch is handled by Vitest
server: {
hmr: false,
watch: null,
},
plugins: [
...prePlugins,
...(project.options?.plugins || []),
BrowserPlugin(server),
...postPlugins,
],
})
await vite.listen()
setupBrowserRpc(server)
return server
}