From c169f98089fa3db5e00bbf96fdbe45505b81cd7f Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 28 Feb 2023 16:09:12 +0100 Subject: [PATCH] fix: optimize dependencies in setup files (#2935) --- packages/vitest/src/node/core.ts | 6 ++++++ packages/vitest/src/node/plugins/index.ts | 19 ++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index 1598cf960ad1..76ee4a1d8cdd 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -136,6 +136,12 @@ export class Vitest { return deepMerge({ ...this.config, reporters: [], + deps: { + ...this.config.deps, + experimentalOptimizer: { + enabled: this.config.deps?.experimentalOptimizer?.enabled ?? false, + }, + }, snapshotOptions: { ...this.config.snapshotOptions, resolveSnapshotPath: undefined, diff --git a/packages/vitest/src/node/plugins/index.ts b/packages/vitest/src/node/plugins/index.ts index a8a4e9045c9e..885d6e4299ca 100644 --- a/packages/vitest/src/node/plugins/index.ts +++ b/packages/vitest/src/node/plugins/index.ts @@ -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' @@ -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