Skip to content

Commit

Permalink
refactor: simplify check
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Feb 16, 2024
1 parent 9c01760 commit 0561c9a
Showing 1 changed file with 8 additions and 22 deletions.
30 changes: 8 additions & 22 deletions packages/vitest/src/node/plugins/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { searchForWorkspaceRoot, version as viteVersion } from 'vite'
import { searchForWorkspaceRoot } from 'vite'
import type { DepOptimizationOptions, ResolvedConfig, UserConfig as ViteConfig } from 'vite'
import { dirname } from 'pathe'
import type { DepsOptimizationOptions, InlineConfig } from '../../types'
Expand All @@ -8,18 +8,15 @@ import { rootDir } from '../../paths'
export function resolveOptimizerConfig(_testOptions: DepsOptimizationOptions | undefined, viteOptions: DepOptimizationOptions | undefined, testConfig: InlineConfig) {
const testOptions = _testOptions || {}
const newConfig: { cacheDir?: string; optimizeDeps: DepOptimizationOptions } = {} as any
const [major, minor, fix] = viteVersion.split('.').map(Number)
const allowed = major >= 5 || (major === 4 && minor >= 4) || (major === 4 && minor === 3 && fix >= 2)
if (!allowed && testOptions?.enabled === true)
console.warn(`Vitest: "deps.optimizer" is only available in Vite >= 4.3.2, current Vite version: ${viteVersion}`)
else
// disabled by default
testOptions.enabled ??= false
if (!allowed || testOptions?.enabled !== true) {
// disabled by default
testOptions.enabled ??= false
if (testOptions?.enabled !== true) {
newConfig.cacheDir = undefined
newConfig.optimizeDeps = {
// experimental in Vite >2.9.2, entries remains to help with older versions
disabled: true,
// noDiscovery available since vite@4.3.3
// https://github.com/vitejs/vite/discussions/13814
noDiscovery: true,
include: [],
entries: [],
}
}
Expand All @@ -44,23 +41,12 @@ export function resolveOptimizerConfig(_testOptions: DepsOptimizationOptions | u
...viteOptions,
...testOptions,
noDiscovery: true,
disabled: false,
entries: [],
exclude,
include,
}
}

// `optimizeDeps.disabled` is deprecated since v5.1.0-beta.1
// https://github.com/vitejs/vite/pull/15184
if (major >= 5 && minor >= 1) {
if (newConfig.optimizeDeps.disabled) {
newConfig.optimizeDeps.noDiscovery = true
newConfig.optimizeDeps.include = []
}
delete newConfig.optimizeDeps.disabled
}

return newConfig
}

Expand Down

0 comments on commit 0561c9a

Please sign in to comment.