1
- import { builtinModules } from 'node:module'
2
1
import { searchForWorkspaceRoot , version as viteVersion } from 'vite'
3
2
import type { DepOptimizationOptions , ResolvedConfig , UserConfig as ViteConfig } from 'vite'
4
3
import { dirname } from 'pathe'
@@ -7,10 +6,10 @@ import type { DepsOptimizationOptions, InlineConfig } from '../../types'
7
6
export function resolveOptimizerConfig ( _testOptions : DepsOptimizationOptions | undefined , viteOptions : DepOptimizationOptions | undefined , testConfig : InlineConfig ) {
8
7
const testOptions = _testOptions || { }
9
8
const newConfig : { cacheDir ?: string ; optimizeDeps : DepOptimizationOptions } = { } as any
10
- const [ major , minor ] = viteVersion . split ( '.' ) . map ( Number )
11
- const allowed = major >= 5 || ( major === 4 && minor >= 3 )
9
+ const [ major , minor , fix ] = viteVersion . split ( '.' ) . map ( Number )
10
+ const allowed = major >= 5 || ( major === 4 && minor >= 4 ) || ( major === 4 && minor === 3 && fix >= 2 )
12
11
if ( ! allowed && testOptions ?. enabled === true )
13
- console . warn ( `Vitest: "deps.optimizer" is only available in Vite >= 4.3.0 , current Vite version: ${ viteVersion } ` )
12
+ console . warn ( `Vitest: "deps.optimizer" is only available in Vite >= 4.3.2 , current Vite version: ${ viteVersion } ` )
14
13
else
15
14
// enable by default
16
15
testOptions . enabled ??= true
@@ -24,15 +23,26 @@ export function resolveOptimizerConfig(_testOptions: DepsOptimizationOptions | u
24
23
}
25
24
else {
26
25
const cacheDir = testConfig . cache !== false ? testConfig . cache ?. dir : null
26
+ const currentInclude = ( testOptions . include || viteOptions ?. include || [ ] )
27
+ const exclude = [
28
+ 'vitest' ,
29
+ // Ideally, we shouldn't optimize react in test mode, otherwise we need to optimize _every_ dependency that uses react.
30
+ 'react' ,
31
+ ...( testOptions . exclude || viteOptions ?. exclude || [ ] ) ,
32
+ ]
33
+ const runtime = currentInclude . filter ( n => n . endsWith ( 'jsx-dev-runtime' ) )
34
+ exclude . push ( ...runtime )
35
+
36
+ const include = ( testOptions . include || viteOptions ?. include || [ ] ) . filter ( ( n : string ) => ! exclude . includes ( n ) )
27
37
newConfig . cacheDir = cacheDir ?? 'node_modules/.vitest'
28
38
newConfig . optimizeDeps = {
29
39
...viteOptions ,
30
40
...testOptions ,
31
41
noDiscovery : true ,
32
42
disabled : false ,
33
43
entries : [ ] ,
34
- exclude : [ 'vitest' , ... builtinModules , ... ( testOptions . exclude || viteOptions ?. exclude || [ ] ) ] ,
35
- include : ( testOptions . include || viteOptions ?. include || [ ] ) . filter ( ( n : string ) => n !== 'vitest' ) ,
44
+ exclude,
45
+ include,
36
46
}
37
47
}
38
48
return newConfig
0 commit comments