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 Jan 15, 2025
1 parent b526896 commit c0a7cb7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
16 changes: 16 additions & 0 deletions packages/vitest/src/node/plugins/conditions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as vite from 'vite'

type Vite6Options = typeof vite & Partial<{
defaultServerConditions?: string[]
}>

/**
* In Vite 6+, providing a value for resolve.conditions overrides the defaults
* In Vite 5, passing ["node"] will be merged with the defaults
*
* @returns the appropriate conditions array depending on the vite version
*
*/
export function getDefaultServerConditions() {
return (vite as Vite6Options).defaultServerConditions ?? ['node']
}
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 { getDefaultServerConditions } 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 conditions = getDefaultServerConditions()

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,
},
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,
},
},
},
Expand Down
7 changes: 5 additions & 2 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 { getDefaultServerConditions } from './conditions'
import { CoverageTransform } from './coverageTransform'
import { CSSEnablerPlugin } from './cssEnabler'
import { MocksPlugins } from './mocks'
Expand Down Expand Up @@ -62,14 +63,16 @@ export function WorkspaceVitestPlugin(
}
}

const conditions = getDefaultServerConditions()

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,
},
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,
},
},
},
Expand Down

0 comments on commit c0a7cb7

Please sign in to comment.