From 379f9a8d8a70f94f3d820417850e239c791e773c Mon Sep 17 00:00:00 2001 From: thebanjomatic Date: Wed, 11 Dec 2024 13:04:28 -0700 Subject: [PATCH] fix(vite6): re-apply default conditions if using vite 6 or later Vite 6 no longer applies default conditions when you override resolve.conditions. This PR adds them back conditionally based on the vite version. Fixes #7070 --- .../vitest/src/node/plugins/conditions.ts | 32 +++++++++++++++++++ packages/vitest/src/node/plugins/index.ts | 7 ++-- packages/vitest/src/node/plugins/workspace.ts | 9 ++++-- 3 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 packages/vitest/src/node/plugins/conditions.ts diff --git a/packages/vitest/src/node/plugins/conditions.ts b/packages/vitest/src/node/plugins/conditions.ts new file mode 100644 index 000000000000..d30afd0fd5e7 --- /dev/null +++ b/packages/vitest/src/node/plugins/conditions.ts @@ -0,0 +1,32 @@ +function normalizeConditions(conditions?: string[]) { + if (!conditions) { + return [] + } + + // We remove browser and node conditions from the default list because + // we add back "node" explicitly in both client and server conditions. + // this ensures that the conditions we pass on to vite doesn't include + // both browser and node at the same time, or node twice in the server case. + return conditions.filter(c => c !== 'browser' && c !== 'node') +} + +async function getDefaultConditions() { + type Vite6Options = typeof import('vite') & Partial<{ + defaultClientConditions?: string[] + defaultServerConditions?: string[] + }> + const vite: Vite6Options = await import('vite') + + return { + defaultClientConditions: vite.defaultClientConditions, + defaultServerConditions: vite.defaultServerConditions, + } +} + +export async function getConditions() { + const { defaultClientConditions, defaultServerConditions } = await getDefaultConditions() + return { + clientConditions: ['node', ...normalizeConditions(defaultClientConditions)], + serverConditions: ['node', ...normalizeConditions(defaultServerConditions)], + } +} diff --git a/packages/vitest/src/node/plugins/index.ts b/packages/vitest/src/node/plugins/index.ts index 66119740c772..e9e2050c87bc 100644 --- a/packages/vitest/src/node/plugins/index.ts +++ b/packages/vitest/src/node/plugins/index.ts @@ -12,6 +12,7 @@ import { generateScopedClassName } from '../../integrations/css/css-modules' import { resolveApiServerConfig } from '../config/resolveConfig' import { Vitest } from '../core' import { createViteLogger, silenceImportViteIgnoreWarning } from '../viteLogger' +import { getConditions } from './conditions' import { CoverageTransform } from './coverageTransform' import { CSSEnablerPlugin } from './cssEnabler' import { MocksPlugins } from './mocks' @@ -73,6 +74,8 @@ export async function VitestPlugin( open = testConfig.uiBase ?? '/__vitest__/' } + const { clientConditions, serverConditions } = await getConditions() + const config: ViteConfig = { root: viteConfig.test?.root || options.root, esbuild: @@ -90,7 +93,7 @@ export async function VitestPlugin( // setting this option can bypass that and fallback to cjs version mainFields: [], alias: testConfig.alias, - conditions: ['node'], + conditions: clientConditions, }, server: { ...testConfig.api, @@ -119,7 +122,7 @@ export async function VitestPlugin( // by default Vite resolves `module` field, which not always a native ESM module // setting this option can bypass that and fallback to cjs version mainFields: [], - conditions: ['node'], + conditions: serverConditions, }, }, }, diff --git a/packages/vitest/src/node/plugins/workspace.ts b/packages/vitest/src/node/plugins/workspace.ts index a3e91bdcf29e..f95bb3138a0a 100644 --- a/packages/vitest/src/node/plugins/workspace.ts +++ b/packages/vitest/src/node/plugins/workspace.ts @@ -7,6 +7,7 @@ import { basename, dirname, relative, resolve } from 'pathe' import { configDefaults } from '../../defaults' import { generateScopedClassName } from '../../integrations/css/css-modules' import { createViteLogger, silenceImportViteIgnoreWarning } from '../viteLogger' +import { getConditions } from './conditions' import { CoverageTransform } from './coverageTransform' import { CSSEnablerPlugin } from './cssEnabler' import { MocksPlugins } from './mocks' @@ -36,7 +37,7 @@ export function WorkspaceVitestPlugin( options() { this.meta.watchMode = false }, - config(viteConfig) { + async config(viteConfig) { const defines: Record = deleteDefineConfig(viteConfig) const testConfig = viteConfig.test || {} @@ -62,6 +63,8 @@ export function WorkspaceVitestPlugin( } } + const { clientConditions, serverConditions } = await getConditions() + const config: ViteConfig = { root, resolve: { @@ -69,7 +72,7 @@ export function WorkspaceVitestPlugin( // setting this option can bypass that and fallback to cjs version mainFields: [], alias: testConfig.alias, - conditions: ['node'], + conditions: clientConditions, }, esbuild: viteConfig.esbuild === false ? false @@ -104,7 +107,7 @@ export function WorkspaceVitestPlugin( // by default Vite resolves `module` field, which not always a native ESM module // setting this option can bypass that and fallback to cjs version mainFields: [], - conditions: ['node'], + conditions: serverConditions, }, }, },