-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
workspace.ts
135 lines (124 loc) · 4.52 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
128
129
130
131
132
133
134
135
import { existsSync, readFileSync } from 'node:fs'
import { basename, dirname, relative, resolve } 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 { ResolvedConfig, UserWorkspaceConfig } from '../../types'
import { CoverageTransform } from './coverageTransform'
import { CSSEnablerPlugin } from './cssEnabler'
import { SsrReplacerPlugin } from './ssrReplacer'
import { MocksPlugin } from './mocks'
import { deleteDefineConfig, hijackVitePluginInject, resolveFsAllow } from './utils'
import { VitestResolver } from './vitestResolver'
import { VitestOptimizer } from './optimizer'
import { NormalizeURLPlugin } from './normalizeURL'
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 defines: 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') {
// if there is a package.json, read the name from it
const dir = options.workspacePath.endsWith('/')
? options.workspacePath.slice(0, -1)
: dirname(options.workspacePath)
const pkgJsonPath = resolve(dir, 'package.json')
if (existsSync(pkgJsonPath))
name = JSON.parse(readFileSync(pkgJsonPath, 'utf-8')).name
if (typeof name !== 'string' || !name)
name = basename(dir)
}
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'],
},
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: null,
open: false,
hmr: false,
preTransformRequests: false,
middlewareMode: true,
fs: {
allow: resolveFsAllow(
project.ctx.config.root,
project.ctx.server.config.configFile,
),
},
},
test: {
name,
},
}
;(config.test as ResolvedConfig).defines = defines
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))!
}
}
}
return config
},
configResolved(viteConfig) {
hijackVitePluginInject(viteConfig)
},
async configureServer(server) {
try {
const options = deepMerge(
{},
configDefaults,
server.config.test || {},
)
await project.setServer(options, server)
}
catch (err) {
await project.ctx.logger.printError(err, { fullStack: true })
process.exit(1)
}
await server.watcher.close()
},
},
SsrReplacerPlugin(),
...CSSEnablerPlugin(project),
CoverageTransform(project.ctx),
MocksPlugin(),
VitestResolver(project.ctx),
VitestOptimizer(),
NormalizeURLPlugin(),
]
}