1- import { builtinModules } from 'node:module'
21import { searchForWorkspaceRoot , version as viteVersion } from 'vite'
32import type { DepOptimizationOptions , ResolvedConfig , UserConfig as ViteConfig } from 'vite'
43import { dirname } from 'pathe'
@@ -7,10 +6,10 @@ import type { DepsOptimizationOptions, InlineConfig } from '../../types'
76export function resolveOptimizerConfig ( _testOptions : DepsOptimizationOptions | undefined , viteOptions : DepOptimizationOptions | undefined , testConfig : InlineConfig ) {
87 const testOptions = _testOptions || { }
98 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 )
1211 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 } ` )
1413 else
1514 // enable by default
1615 testOptions . enabled ??= true
@@ -24,15 +23,26 @@ export function resolveOptimizerConfig(_testOptions: DepsOptimizationOptions | u
2423 }
2524 else {
2625 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 ) )
2737 newConfig . cacheDir = cacheDir ?? 'node_modules/.vitest'
2838 newConfig . optimizeDeps = {
2939 ...viteOptions ,
3040 ...testOptions ,
3141 noDiscovery : true ,
3242 disabled : false ,
3343 entries : [ ] ,
34- exclude : [ 'vitest' , ... builtinModules , ... ( testOptions . exclude || viteOptions ?. exclude || [ ] ) ] ,
35- include : ( testOptions . include || viteOptions ?. include || [ ] ) . filter ( ( n : string ) => n !== 'vitest' ) ,
44+ exclude,
45+ include,
3646 }
3747 }
3848 return newConfig
0 commit comments