Skip to content

Commit

Permalink
fix: optimize dependencies in setup files (#2935)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Feb 28, 2023
1 parent bc377fe commit c169f98
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ export class Vitest {
return deepMerge<ResolvedConfig>({
...this.config,
reporters: [],
deps: {
...this.config.deps,
experimentalOptimizer: {
enabled: this.config.deps?.experimentalOptimizer?.enabled ?? false,
},
},
snapshotOptions: {
...this.config.snapshotOptions,
resolveSnapshotPath: undefined,
Expand Down
19 changes: 16 additions & 3 deletions packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { builtinModules } from 'node:module'
import type { UserConfig as ViteConfig, Plugin as VitePlugin } from 'vite'
import { relative } from 'pathe'
import { normalize, relative, resolve } from 'pathe'
import { toArray } from '@vitest/utils'
import { resolveModule } from 'local-pkg'
import { configDefaults } from '../../defaults'
import type { ResolvedConfig, UserConfig } from '../../types'
import { deepMerge, notNullish, removeUndefinedValues } from '../../utils'
Expand Down Expand Up @@ -150,14 +153,24 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t
}
}
else {
const entries = await ctx.globAllTestFiles(preOptions as ResolvedConfig, preOptions.dir || getRoot())
const root = config.root || process.cwd()
const entries = await ctx.globAllTestFiles(preOptions as ResolvedConfig, preOptions.dir || root)
if (preOptions?.setupFiles) {
const setupFiles = toArray(preOptions.setupFiles).map((file: string) =>
normalize(
resolveModule(file, { paths: [root] })
?? resolve(root, file),
),
)
entries.push(...setupFiles)
}
optimizeConfig.cacheDir = preOptions.cache?.dir ?? 'node_modules/.vitest'
optimizeConfig.optimizeDeps = {
...viteConfig.optimizeDeps,
...optimizer,
disabled: false,
entries: [...(optimizer.entries || viteConfig.optimizeDeps?.entries || []), ...entries],
exclude: ['vitest', ...(optimizer.exclude || viteConfig.optimizeDeps?.exclude || [])],
exclude: ['vitest', ...builtinModules, ...(optimizer.exclude || viteConfig.optimizeDeps?.exclude || [])],
include: (optimizer.include || viteConfig.optimizeDeps?.include || []).filter((n: string) => n !== 'vitest'),
}
// Vite throws an error that it cannot rename "deps_temp", but optimization still works
Expand Down

0 comments on commit c169f98

Please sign in to comment.