-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
startServer.ts
79 lines (63 loc) · 2.68 KB
/
startServer.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
import Debug from 'debug'
import { createServer, ViteDevServer, InlineConfig, UserConfig } from 'vite'
import { dirname, resolve } from 'path'
import getPort from 'get-port'
import { makeCypressPlugin } from './makeCypressPlugin'
const debug = Debug('cypress:vite-dev-server:start')
export interface StartDevServerOptions {
/**
* the Cypress options object
*/
options: Cypress.DevServerOptions
/**
* By default, vite will use your vite.config file to
* Start the server. If you need additional plugins or
* to override some options, you can do so using this.
* @optional
*/
viteConfig?: UserConfig
}
const resolveServerConfig = async ({ viteConfig, options }: StartDevServerOptions): Promise<InlineConfig> => {
const { projectRoot, supportFile } = options.config
const requiredOptions: InlineConfig = {
base: '/__cypress/src/',
root: projectRoot,
}
const finalConfig: InlineConfig = { ...viteConfig, ...requiredOptions }
finalConfig.plugins = [...(finalConfig.plugins || []), makeCypressPlugin(projectRoot, supportFile, options.devServerEvents, options.specs)]
// This alias is necessary to avoid a "prefixIdentifiers" issue from slots mounting
// only cjs compiler-core accepts using prefixIdentifiers in slots which vue test utils use.
// Could we resolve this usage in test-utils?
try {
finalConfig.resolve = finalConfig.resolve || {}
finalConfig.resolve.alias = {
...finalConfig.resolve.alias,
'@vue/compiler-core': resolve(dirname(require.resolve('@vue/compiler-core')), 'dist', 'compiler-core.cjs.js'),
}
} catch (e) {
// Vue 3 is not installed
}
finalConfig.server = finalConfig.server || {}
finalConfig.server.port = await getPort({ port: finalConfig.server.port || 3000, host: 'localhost' }),
// Ask vite to pre-optimize all dependencies of the specs
finalConfig.optimizeDeps = finalConfig.optimizeDeps || {}
// pre-optimizea all the specs
if ((options.specs && options.specs.length)) {
finalConfig.optimizeDeps.entries = [...options.specs.map((spec) => spec.relative)]
// only optimize a supportFile is it is not false or undefined
if (supportFile) {
finalConfig.optimizeDeps.entries.push(supportFile)
}
}
debug(`the resolved server config is ${JSON.stringify(finalConfig, null, 2)}`)
return finalConfig
}
export async function start (devServerOptions: StartDevServerOptions): Promise<ViteDevServer> {
if (!devServerOptions.viteConfig) {
debug('User did not pass in any Vite dev server configuration')
devServerOptions.viteConfig = {}
}
debug('starting vite dev server')
const resolvedConfig = await resolveServerConfig(devServerOptions)
return createServer(resolvedConfig)
}