-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathvitePreprocessor.ts
More file actions
123 lines (109 loc) · 3.39 KB
/
vitePreprocessor.ts
File metadata and controls
123 lines (109 loc) · 3.39 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
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
import path from 'path'
import { build, type BuildEnvironmentOptions, type InlineConfig } from 'vite'
import chokidar from 'chokidar'
import { debug, getConfig, type CypressPreprocessor } from './common'
import { maybeMap, omit } from './utils'
const watchers: Record<string, chokidar.FSWatcher> = {}
/**
* Cypress preprocessor for running e2e tests using vite.
*
* @param {InlineConfig | string} config - Vite config object, or path to user
* Vite config file for backwards compatibility
*
* @example
* import vitePreprocessor from 'cypress-vite'
* ...
* setupNodeEvents(on) {
* on(
* 'file:preprocessor',
* vitePreprocessor(path.resolve(__dirname, './vite.config.ts')),
* )
* },
*/
function vitePreprocessor(
userConfig?: InlineConfig | string,
): CypressPreprocessor {
const config: InlineConfig = getConfig(userConfig)
debug('User config path: %s', config.configFile)
return async (file) => {
const { outputPath, filePath, shouldWatch } = file
debug('Preprocessing file %s', filePath)
const fileName = path.basename(outputPath)
const filenameWithoutExtension = path.basename(
outputPath,
path.extname(outputPath),
)
if (shouldWatch && !watchers[filePath]) {
// Watch this spec file if we are not already doing so (and Cypress is
// not in headless mode)
let initial = true
watchers[filePath] = chokidar.watch(filePath)
debug('Watcher for file %s cached', filePath)
file.on('close', async () => {
await watchers[filePath].close()
delete watchers[filePath]
debug('File %s closed.', filePath)
})
watchers[filePath].on('all', () => {
// Re-run the preprocessor if the file changes
if (!initial) {
file.emit('rerun')
}
initial = false
})
}
const defaultConfig: InlineConfig = {
logLevel: 'warn',
define: {
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
},
build: {
emptyOutDir: false,
minify: false,
outDir: path.dirname(outputPath),
sourcemap: true,
write: true,
watch: null,
lib: {
entry: filePath,
fileName: () => fileName,
formats: ['umd'],
name: filenameWithoutExtension,
},
},
}
const resolvedConfig: InlineConfig & {
build?: InlineConfig['build'] & {
// `rolldownOptions` is used instead of `rollupOptions` in Vite 8.
// Just copy the type from `rollupOptions`, so we don't have to
// maintain another type or install another package.
rolldownOptions?: BuildEnvironmentOptions['rollupOptions']
}
} = {
...config,
...defaultConfig,
}
// Remove any manualChunks or advancedChunks.
for (const key of ['rollupOptions', 'rolldownOptions'] as const) {
if (resolvedConfig.build?.[key]?.output) {
resolvedConfig.build[key].output = maybeMap(
resolvedConfig.build[key].output,
(o) =>
omit(
o as typeof o & {
manualChunks?: unknown
advancedChunks?: unknown
codeSplitting?: unknown
},
'manualChunks',
'advancedChunks',
'codeSplitting',
),
)
}
}
await build(resolvedConfig)
return outputPath
}
}
export default vitePreprocessor