Skip to content

Commit

Permalink
fix: re-apply default conditions if using vite 6 or later (vitest-dev…
Browse files Browse the repository at this point in the history
…#7071)

Co-authored-by: thebanjomatic <[email protected]>
Co-authored-by: Hiroshi Ogawa <[email protected]>
  • Loading branch information
3 people authored Jan 16, 2025
1 parent b526896 commit 84287fc
Show file tree
Hide file tree
Showing 13 changed files with 83 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
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions test/config/deps/test-dep-conditions/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'default'
1 change: 1 addition & 0 deletions test/config/deps/test-dep-conditions/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'module'
1 change: 1 addition & 0 deletions test/config/deps/test-dep-conditions/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'node'
12 changes: 12 additions & 0 deletions test/config/deps/test-dep-conditions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@vitest/test-dep-conditions",
"type": "module",
"private": true,
"exports": {
".": {
"module": "./module.js",
"node": "./node.js",
"default": "./default.js"
}
}
}
1 change: 1 addition & 0 deletions test/config/fixtures/conditions-subpackage/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'module'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { test, expect } from 'vitest';
import condition from '@vitest/test-dep-conditions';

test('condition is correct', () => {
expect(condition).toBe('module')
})
3 changes: 3 additions & 0 deletions test/config/fixtures/default-conditions/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({})
1 change: 1 addition & 0 deletions test/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"test": "vitest --typecheck.enabled"
},
"devDependencies": {
"@vitest/test-dep-conditions": "file:./deps/test-dep-conditions",
"tinyexec": "^0.3.2",
"vite": "latest",
"vitest": "workspace:*"
Expand Down
23 changes: 23 additions & 0 deletions test/config/test/default-conditions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { expect, test } from 'vitest'
import { runVitest } from '../../test-utils'

test('"module" condition for external dep', async () => {
const { stderr } = await runVitest({
root: 'fixtures/default-conditions',
})

expect(stderr).toBe('')
})

test('"module" condition for inline dep', async () => {
const { stderr } = await runVitest({
root: 'fixtures/default-conditions',
server: {
deps: {
inline: ['@vitest/test-dep-conditions'],
},
},
})

expect(stderr).toBe('')
})

0 comments on commit 84287fc

Please sign in to comment.