Skip to content

Commit 8877e22

Browse files
authored
fix(vitest): use development/production conditions when resolving external modules (#4980)
1 parent 7d9f673 commit 8877e22

File tree

14 files changed

+123
-3
lines changed

14 files changed

+123
-3
lines changed

packages/vitest/src/node/pool.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,16 @@ export function createPool(ctx: Vitest): ProcessPool {
6060
return getDefaultPoolName(project, file)
6161
}
6262

63-
const conditions = ctx.server.config.resolve.conditions?.flatMap(c => ['--conditions', c]) || []
63+
// in addition to resolve.conditions Vite also adds production/development,
64+
// see: https://github.com/vitejs/vite/blob/af2aa09575229462635b7cbb6d248ca853057ba2/packages/vite/src/node/plugins/resolve.ts#L1056-L1080
65+
const potentialConditions = new Set(['production', 'development', ...ctx.server.config.resolve.conditions])
66+
const conditions = [...potentialConditions].filter((condition) => {
67+
if (condition === 'production')
68+
return ctx.server.config.isProduction
69+
if (condition === 'development')
70+
return !ctx.server.config.isProduction
71+
return true
72+
}).flatMap(c => ['--conditions', c])
6473

6574
// Instead of passing whole process.execArgv to the workers, pick allowed options.
6675
// Some options may crash worker, e.g. --prof, --title. nodejs/node#41103

pnpm-lock.yaml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ packages:
33
- packages/*
44
- examples/*
55
- test/*
6+
- test/config/fixtures/conditions-pkg
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as condition } from 'subpackage'
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "conditions",
3+
"private": true,
4+
"type": "module",
5+
"main": "index.js",
6+
"dependencies": {
7+
"subpackage": "file:../conditions-subpackage"
8+
}
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'custom'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw new Error('Should not be imported')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'development'
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "subpackage",
3+
"private": true,
4+
"type": "module",
5+
"exports": {
6+
".": {
7+
"custom": "./custom.js",
8+
"development": "./development.js",
9+
"production": "./production.js",
10+
"default": "./default.js"
11+
}
12+
}
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'production'

0 commit comments

Comments
 (0)