Skip to content

Commit

Permalink
chore: support workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jun 1, 2023
1 parent caf9ab8 commit b541b86
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
7 changes: 2 additions & 5 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,16 @@ Handling for dependencies resolution.
- **Version:** Since Vitest 0.29.0
- **See also:** [Dep Optimization Options](https://vitejs.dev/config/dep-optimization-options.html)

::: warning
This feature is temporarily disabled since Vitest 0.30.0.
:::

Enable dependency optimization. If you have a lot of tests, this might improve their performance.

For `jsdom` and `happy-dom` environments, when Vitest will encounter the external library, it will be bundled into a single file using esbuild and imported as a whole module. This is good for several reasons:

- Importing packages with a lot of imports is expensive. By bundling them into one file we can save a lot of time
- Importing UI libraries is expensive because they are not meant to run inside Node.js
- Your `alias` configuration is now respected inside bundled packages
- Code in your tests is running closer to how it's running in the browser

You can opt-out of this behavior for certain packages with `exclude` option. You can read more about available options in [Vite](https://vitejs.dev/config/dep-optimization-options.html) docs.
Be aware that only packages in `deps.experimentalOptimizer.include` option are bundled (some plugins populate this automatically, like Svelte). You can read more about available options in [Vite](https://vitejs.dev/config/dep-optimization-options.html) docs.

This options also inherits your `optimizeDeps` configuration. If you redefine `include`/`exclude`/`entries` option in `deps.experimentalOptimizer` it will overwrite your `optimizeDeps` when running tests.

Expand Down
3 changes: 3 additions & 0 deletions packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t
const optimizer = preOptions.deps?.experimentalOptimizer
const [major, minor] = viteVersion.split('.').map(Number)
const allowed = major >= 5 || (major === 4 && minor >= 3)
if (!allowed && optimizer?.enabled === true)
console.warn(`Vitest: "deps.experimentalOptimizer" is only available in Vite >= 4.3.0, current Vite version: ${viteVersion}`)
if (!allowed || optimizer?.enabled !== true) {
optimizeConfig.cacheDir = undefined
optimizeConfig.optimizeDeps = {
Expand All @@ -155,6 +157,7 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t
...optimizer,
noDiscovery: true,
disabled: false,
entries: [],
exclude: ['vitest', ...builtinModules, ...(optimizer.exclude || viteConfig.optimizeDeps?.exclude || [])],
include: (optimizer.include || viteConfig.optimizeDeps?.include || []).filter((n: string) => n !== 'vitest'),
}
Expand Down
31 changes: 31 additions & 0 deletions packages/vitest/src/node/plugins/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { builtinModules } from 'node:module'
import { dirname, relative } from 'pathe'
import type { UserConfig as ViteConfig, Plugin as VitePlugin } from 'vite'
import { version as viteVersion } from 'vite'
import { configDefaults } from '../../defaults'
import { generateScopedClassName } from '../../integrations/css/css-modules'
import { deepMerge } from '../../utils/base'
Expand Down Expand Up @@ -116,6 +118,35 @@ export function WorkspaceVitestPlugin(project: WorkspaceProject, options: Worksp
}
}

const optimizeConfig: Partial<ViteConfig> = {}
const optimizer = testConfig.deps?.experimentalOptimizer
const [major, minor] = viteVersion.split('.').map(Number)
const allowed = major >= 5 || (major === 4 && minor >= 3)
if (!allowed && optimizer?.enabled === true)
console.warn(`Vitest: "deps.experimentalOptimizer" is only available in Vite >= 4.3.0, current Vite version: ${viteVersion}`)
if (!allowed || optimizer?.enabled !== true) {
optimizeConfig.cacheDir = undefined
optimizeConfig.optimizeDeps = {
// experimental in Vite >2.9.2, entries remains to help with older versions
disabled: true,
entries: [],
}
}
else {
const cacheDir = testConfig.cache !== false ? testConfig.cache?.dir : null
optimizeConfig.cacheDir = cacheDir ?? 'node_modules/.vitest'
optimizeConfig.optimizeDeps = {
...viteConfig.optimizeDeps,
...optimizer,
noDiscovery: true,
disabled: false,
entries: [],
exclude: ['vitest', ...builtinModules, ...(optimizer.exclude || viteConfig.optimizeDeps?.exclude || [])],
include: (optimizer.include || viteConfig.optimizeDeps?.include || []).filter((n: string) => n !== 'vitest'),
}
}
Object.assign(config, optimizeConfig)

return config
},
async configureServer(server) {
Expand Down
7 changes: 2 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b541b86

Please sign in to comment.