/
workspace.ts
127 lines (115 loc) · 4.38 KB
/
workspace.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
import { dirname, relative } from 'pathe'
import type { UserConfig as ViteConfig, Plugin as VitePlugin } from 'vite'
import { configDefaults } from '../../defaults'
import { generateScopedClassName } from '../../integrations/css/css-modules'
import { deepMerge } from '../../utils/base'
import type { WorkspaceProject } from '../workspace'
import type { UserWorkspaceConfig } from '../../types'
import { CoverageTransform } from './coverageTransform'
import { CSSEnablerPlugin } from './cssEnabler'
import { SsrReplacerPlugin } from './ssrReplacer'
import { GlobalSetupPlugin } from './globalSetup'
import { MocksPlugin } from './mocks'
import { deleteDefineConfig, resolveOptimizerConfig } from './utils'
import { VitestResolver } from './vitestResolver'
interface WorkspaceOptions extends UserWorkspaceConfig {
root?: string
workspacePath: string | number
}
export function WorkspaceVitestPlugin(project: WorkspaceProject, options: WorkspaceOptions) {
return <VitePlugin[]>[
{
name: 'vitest:project',
enforce: 'pre',
options() {
this.meta.watchMode = false
},
config(viteConfig) {
const env: Record<string, any> = deleteDefineConfig(viteConfig)
const testConfig = viteConfig.test || {}
const root = testConfig.root || viteConfig.root || options.root
let name = testConfig.name
if (!name) {
if (typeof options.workspacePath === 'string')
name = dirname(options.workspacePath).split('/').pop()
else
name = options.workspacePath.toString()
}
const config: ViteConfig = {
root,
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: [],
alias: testConfig.alias,
conditions: ['node'],
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
// @ts-ignore we support Vite ^3.0, but browserField is available in Vite ^3.2
browserField: false,
},
esbuild: {
sourcemap: 'external',
// Enables using ignore hint for coverage providers with @preserve keyword
legalComments: 'inline',
},
server: {
// disable watch mode in workspaces,
// because it is handled by the top-level watcher
watch: {
ignored: ['**/*'],
depth: 0,
persistent: false,
},
open: false,
hmr: false,
preTransformRequests: false,
},
test: {
env,
name,
},
}
const classNameStrategy = (typeof testConfig.css !== 'boolean' && testConfig.css?.modules?.classNameStrategy) || 'stable'
if (classNameStrategy !== 'scoped') {
config.css ??= {}
config.css.modules ??= {}
if (config.css.modules) {
config.css.modules.generateScopedName = (name: string, filename: string) => {
const root = project.config.root
return generateScopedClassName(classNameStrategy, name, relative(root, filename))!
}
}
}
const webOptimizer = resolveOptimizerConfig(testConfig.deps?.experimentalOptimizer?.web, viteConfig.optimizeDeps, testConfig)
const ssrOptimizer = resolveOptimizerConfig(testConfig.deps?.experimentalOptimizer?.ssr, viteConfig.ssr?.optimizeDeps, testConfig)
config.cacheDir = webOptimizer.cacheDir || ssrOptimizer.cacheDir || config.cacheDir
config.optimizeDeps = webOptimizer.optimizeDeps
config.ssr = {
optimizeDeps: ssrOptimizer.optimizeDeps,
}
return config
},
async configureServer(server) {
try {
const options = deepMerge(
{},
configDefaults,
server.config.test || {},
)
await project.setServer(options, server)
}
catch (err) {
await project.ctx.logger.printError(err, true)
process.exit(1)
}
await server.watcher.close()
},
},
SsrReplacerPlugin(),
...CSSEnablerPlugin(project),
CoverageTransform(project.ctx),
GlobalSetupPlugin(project, project.ctx.logger),
MocksPlugin(),
VitestResolver(project.ctx),
]
}