Skip to content

Commit

Permalink
fix(vite6): re-apply default conditions if using vite 6 or later
Browse files Browse the repository at this point in the history
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
  • Loading branch information
thebanjomatic committed Dec 11, 2024
1 parent 4e60333 commit 379f9a8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
32 changes: 32 additions & 0 deletions packages/vitest/src/node/plugins/conditions.ts
Original file line number Diff line number Diff line change
@@ -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)],
}
}
7 changes: 5 additions & 2 deletions packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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:
Expand All @@ -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,
Expand Down Expand Up @@ -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,
},
},
},
Expand Down
9 changes: 6 additions & 3 deletions packages/vitest/src/node/plugins/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -36,7 +37,7 @@ export function WorkspaceVitestPlugin(
options() {
this.meta.watchMode = false
},
config(viteConfig) {
async config(viteConfig) {
const defines: Record<string, any> = deleteDefineConfig(viteConfig)

const testConfig = viteConfig.test || {}
Expand All @@ -62,14 +63,16 @@ export function WorkspaceVitestPlugin(
}
}

const { clientConditions, serverConditions } = await getConditions()

const config: ViteConfig = {
root,
resolve: {
// 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: [],
alias: testConfig.alias,
conditions: ['node'],
conditions: clientConditions,
},
esbuild: viteConfig.esbuild === false
? false
Expand Down Expand Up @@ -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,
},
},
},
Expand Down

0 comments on commit 379f9a8

Please sign in to comment.