-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathresolveConfig.ts
128 lines (112 loc) · 4.33 KB
/
resolveConfig.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
/**
* The logic inside of this file is heavily reused from
* Vitest's own config resolution logic.
* You can find it here https://github.com/vitest-dev/vitest/blob/main/packages/vitest/src/node/create.ts
*/
import debugFn from 'debug'
import type { InlineConfig } from 'vite'
import path from 'path'
import { configFiles } from './constants'
import type { ViteDevServerConfig } from './devServer'
import { Cypress, CypressSourcemap } from './plugins/index'
import type { Vite } from './getVite'
import { dynamicImport } from './dynamic-import'
const debug = debugFn('cypress:vite-dev-server:resolve-config')
export const createViteDevServerConfig = async (config: ViteDevServerConfig, vite: Vite): Promise<InlineConfig> => {
const { viteConfig: inlineViteConfig, cypressConfig: { projectRoot } } = config
let resolvedOverrides: InlineConfig = {}
if (inlineViteConfig) {
debug(`Received a custom viteConfig`, inlineViteConfig)
if (typeof inlineViteConfig === 'function') {
resolvedOverrides = await inlineViteConfig()
} else if (typeof inlineViteConfig === 'object') {
resolvedOverrides = inlineViteConfig
}
// Set "configFile: false" to disable auto resolution of <project-root>/vite.config.js
resolvedOverrides = { configFile: false, ...resolvedOverrides }
} else {
const { findUp } = await dynamicImport<typeof import('find-up')>('find-up')
const configFile = await findUp(configFiles, { cwd: projectRoot })
if (!configFile) {
if (config.onConfigNotFound) {
config.onConfigNotFound('vite', projectRoot, configFiles)
// The config process will be killed from the parent, but we want to early exit so we don't get
// any additional errors related to not having a config
process.exit(0)
} else {
throw new Error(`Your component devServer config for vite is missing a required viteConfig property, since we could not automatically detect one.\n Please add one to your ${config.cypressConfig.configFile}`)
}
}
debug('Resolved config file at', configFile, 'using root', projectRoot)
resolvedOverrides = { configFile }
}
const finalConfig = vite.mergeConfig(
resolvedOverrides,
makeCypressViteConfig(config, vite),
)
debug('The resolved server config is', JSON.stringify(finalConfig, null, 2))
return finalConfig
}
function makeCypressViteConfig (config: ViteDevServerConfig, vite: Vite): InlineConfig {
const {
cypressConfig: {
projectRoot,
devServerPublicPathRoute,
supportFile,
cypressBinaryRoot,
isTextTerminal,
},
specs,
} = config
// Vite caches its output in the .vite directory in the node_modules where vite lives.
// So we want to find that node_modules path and ensure it's added to the "allow" list
const vitePathNodeModules = path.dirname(path.dirname(require.resolve(`vite/package.json`, {
paths: [projectRoot],
})))
return {
root: projectRoot,
base: `${devServerPublicPathRoute}/`,
optimizeDeps: {
esbuildOptions: {
incremental: true,
plugins: [
{
name: 'cypress-esbuild-plugin',
setup (build) {
build.onEnd(function (result) {
// We don't want to completely fail the build here on errors so we treat the errors as warnings
// which will handle things more gracefully. Vite will 500 on files that have errors when they
// are requested later and Cypress will display an error message.
// See: https://github.com/cypress-io/cypress/pull/21599
result.warnings = [...result.warnings, ...result.errors]
result.errors = []
})
},
},
],
},
entries: [
...specs.map((s) => path.relative(projectRoot, s.relative)),
...(supportFile ? [path.resolve(projectRoot, supportFile)] : []),
].filter((v) => v != null),
},
server: {
fs: {
allow: [
projectRoot,
vitePathNodeModules,
cypressBinaryRoot,
],
},
host: '127.0.0.1',
// Disable file watching and HMR when executing tests in `run` mode
...(isTextTerminal
? { watch: { ignored: '**/*' }, hmr: false }
: {}),
},
plugins: [
Cypress(config, vite),
CypressSourcemap(config, vite),
],
}
}