Skip to content

Commit 4985011

Browse files
committed
feat: introduce DEV_PROD_CONDITION variable
1 parent 980ac56 commit 4985011

File tree

11 files changed

+48
-41
lines changed

11 files changed

+48
-41
lines changed

docs/config/shared-options.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ For SSR builds, deduplication does not work for ESM build outputs configured fro
117117
## resolve.conditions
118118
119119
- **Type:** `string[]`
120-
- **Default:** `['module', 'browser', 'production', 'development']`
120+
- **Default:** `['module', 'browser', DEV_PROD_CONDITION]`
121121
122122
Additional allowed conditions when resolving [Conditional Exports](https://nodejs.org/api/packages.html#packages_conditional_exports) from a package.
123123
@@ -136,7 +136,7 @@ A package with conditional exports may have the following `exports` field in its
136136

137137
Here, `import` and `require` are "conditions". Conditions can be nested and should be specified from most specific to least specific.
138138

139-
Some of the default conditions (`production`, `development`) are only applied when the requirements are met. For example, `production` is only applied when `process.env.NODE_ENV === 'production'`. The `resolve.conditions` config option allows specifying additional allowed conditions and those conditions will be applied unconditionally.
139+
`DEV_PROD_CONDITION` is a special variable that is replaced with `production` or `development` depending on the value of `process.env.NODE_ENV`. It is replaced with `production` when `process.env.NODE_ENV === 'production'` and `development` otherwise.
140140

141141
Note that `import`, `require`, `default` conditions are always applied if the requirements are met.
142142

docs/config/ssr-options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Build target for the SSR server.
3434
## ssr.resolve.conditions
3535

3636
- **Type:** `string[]`
37-
- **Default:** `['module', 'node', 'production', 'development']` (`['module', 'browser', 'production', 'development']` for `ssr.target === 'webworker'`)
37+
- **Default:** `['module', 'node', DEV_PROD_CONDITION]` (`['module', 'browser', DEV_PROD_CONDITION]` for `ssr.target === 'webworker'`)
3838
- **Related:** [Resolve Conditions](./shared-options.md#resolve-conditions)
3939

4040
These conditions are used in the plugin pipeline, and only affect non-externalized dependencies during the SSR build. Use `ssr.resolve.externalConditions` to affect externalized imports.

docs/guide/migration.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ In Vite 5, the default value for `resolve.conditions` was `[]` and some conditio
2121
From Vite 6, some of the conditions are no longer added internally and need to be included in the config values.
2222
The conditions that are no longer added internally for
2323

24-
- `resolve.conditions` are `['module', 'browser', 'production', 'development']`
25-
- `ssr.resolve.conditions` are `['module', 'node', 'production', 'development']`
24+
- `resolve.conditions` are `['module', 'browser', DEV_PROD_CONDITION]`
25+
- `ssr.resolve.conditions` are `['module', 'node', DEV_PROD_CONDITION]`
2626

27-
The default values for those options are updated to the corresponding values and `ssr.resolve.conditions` no longer uses `resolve.conditions` as the default value.
27+
The default values for those options are updated to the corresponding values and `ssr.resolve.conditions` no longer uses `resolve.conditions` as the default value. Note that `DEV_PROD_CONDITION` is a special variable that is replaced with `production` or `development` depending on the value of `process.env.NODE_ENV`.
2828

2929
If you specified a custom value for `resolve.conditions` or `ssr.resolve.conditions`, you need to update it to include the new conditions.
30-
For example, if you previously specified `['custom']` for `resolve.conditions`, you need to specify `['custom', 'module', 'browser', 'production', 'development']` instead.
30+
For example, if you previously specified `['custom']` for `resolve.conditions`, you need to specify `['custom', 'module', 'browser', DEV_PROD_CONDITION]` instead.
3131

3232
### JSON stringify
3333

packages/vite/src/node/constants.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,23 @@ export const DEFAULT_MAIN_FIELDS = [
4646
'jsnext',
4747
]
4848

49+
// using " and $ as conditions would usually avoid using $ or "
50+
// as it makes difficult to use the condition in command line or json
51+
// also avoiding a symbol so that dual package hazard won't be a problem
52+
type SpecialCondition<T extends string> = `vite$"${T}"` & { __brand: never }
53+
54+
/**
55+
* A special condition that would be replaced with production or development
56+
* depending on NODE_ENV env variable
57+
*/
58+
export const DEV_PROD_CONDITION =
59+
`vite$"dev-prod"` as SpecialCondition<'dev-prod'>
60+
4961
export const DEFAULT_CONDITIONS = [
5062
'module',
5163
'browser',
5264
'node',
53-
'production',
54-
'development',
65+
DEV_PROD_CONDITION,
5566
]
5667

5768
export const DEFAULT_EXTERNAL_CONDITIONS = ['node']

packages/vite/src/node/plugins/css.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
import {
4545
CLIENT_PUBLIC_PATH,
4646
CSS_LANGS_RE,
47+
DEV_PROD_CONDITION,
4748
ESBUILD_MODULES_TARGET,
4849
SPECIAL_QUERY_RE,
4950
} from '../constants'
@@ -1092,7 +1093,7 @@ function createCSSResolvers(config: ResolvedConfig): CSSAtImportResolvers {
10921093
return (cssResolve ??= createBackCompatIdResolver(config, {
10931094
extensions: ['.css'],
10941095
mainFields: ['style'],
1095-
conditions: ['style', 'production', 'development'],
1096+
conditions: ['style', DEV_PROD_CONDITION],
10961097
tryIndex: false,
10971098
preferRelative: true,
10981099
}))
@@ -1103,7 +1104,7 @@ function createCSSResolvers(config: ResolvedConfig): CSSAtImportResolvers {
11031104
const resolver = createBackCompatIdResolver(config, {
11041105
extensions: ['.scss', '.sass', '.css'],
11051106
mainFields: ['sass', 'style'],
1106-
conditions: ['sass', 'style', 'production', 'development'],
1107+
conditions: ['sass', 'style', DEV_PROD_CONDITION],
11071108
tryIndex: true,
11081109
tryPrefix: '_',
11091110
preferRelative: true,
@@ -1122,7 +1123,7 @@ function createCSSResolvers(config: ResolvedConfig): CSSAtImportResolvers {
11221123
return (lessResolve ??= createBackCompatIdResolver(config, {
11231124
extensions: ['.less', '.css'],
11241125
mainFields: ['less', 'style'],
1125-
conditions: ['less', 'style', 'production', 'development'],
1126+
conditions: ['less', 'style', DEV_PROD_CONDITION],
11261127
tryIndex: false,
11271128
preferRelative: true,
11281129
}))

packages/vite/src/node/plugins/resolve.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { Plugin } from '../plugin'
99
import {
1010
CLIENT_ENTRY,
1111
DEP_VERSION_RE,
12+
DEV_PROD_CONDITION,
1213
ENV_ENTRY,
1314
FS_PREFIX,
1415
OPTIMIZABLE_ENTRY_RE,
@@ -1078,21 +1079,18 @@ function resolveExportsOrImports(
10781079
options: InternalResolveOptions,
10791080
type: 'imports' | 'exports',
10801081
) {
1081-
const conditions = [...options.conditions, 'require', 'import'].filter(
1082-
(condition) => {
1083-
switch (condition) {
1084-
case 'production':
1085-
return options.isProduction
1086-
case 'development':
1087-
return !options.isProduction
1088-
case 'require':
1089-
return options.isRequire
1090-
case 'import':
1091-
return !options.isRequire
1092-
}
1093-
return true
1094-
},
1095-
)
1082+
const conditions = options.conditions.map((condition) => {
1083+
if (condition === DEV_PROD_CONDITION) {
1084+
return options.isProduction ? 'production' : 'development'
1085+
}
1086+
return condition
1087+
})
1088+
1089+
if (options.isRequire) {
1090+
conditions.push('require')
1091+
} else {
1092+
conditions.push('import')
1093+
}
10961094

10971095
const fn = type === 'imports' ? imports : exports
10981096
const result = fn(pkg, key, { conditions, unsafe: true })

packages/vite/src/node/publicUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* This file will be bundled to ESM and CJS and redirected by ../index.cjs
44
* Please control the side-effects by checking the ./dist/node-cjs/publicUtils.cjs bundle
55
*/
6-
export { VERSION as version } from './constants'
6+
export { VERSION as version, DEV_PROD_CONDITION } from './constants'
77
export { version as esbuildVersion } from 'esbuild'
88
export {
99
splitVendorChunkPlugin,

packages/vite/src/node/ssr/fetchModule.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { FetchResult } from 'vite/module-runner'
33
import type { EnvironmentModuleNode, TransformResult } from '..'
44
import { tryNodeResolve } from '../plugins/resolve'
55
import { isBuiltin, isExternalUrl, isFilePathESM } from '../utils'
6+
import { DEV_PROD_CONDITION } from '../constants'
67
import { unwrapId } from '../../shared/utils'
78
import {
89
MODULE_RUNNER_SOURCEMAPPING_SOURCE,
@@ -46,7 +47,7 @@ export async function fetchModule(
4647

4748
const resolved = tryNodeResolve(url, importer, {
4849
mainFields: ['main'],
49-
conditions: [...externalConditions, 'production', 'development'],
50+
conditions: [...externalConditions, DEV_PROD_CONDITION],
5051
externalConditions,
5152
external: [],
5253
noExternal: [],

playground/resolve/vite.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'node:path'
2-
import { defineConfig, normalizePath } from 'vite'
2+
import { DEV_PROD_CONDITION, defineConfig, normalizePath } from 'vite'
33
import { a } from './config-dep.cjs'
44

55
const virtualFile = '@virtual-file'
@@ -32,7 +32,7 @@ export default defineConfig({
3232
resolve: {
3333
extensions: ['.mjs', '.js', '.es', '.ts'],
3434
mainFields: ['browser', 'custom', 'module'],
35-
conditions: ['module', 'browser', 'production', 'development', 'custom'],
35+
conditions: ['module', 'browser', DEV_PROD_CONDITION, 'custom'],
3636
},
3737
define: {
3838
VITE_CONFIG_DEP_TEST: a,

playground/ssr-conditions/vite.config.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
import { defineConfig } from 'vite'
1+
import { DEV_PROD_CONDITION, defineConfig } from 'vite'
22

33
export default defineConfig({
44
ssr: {
55
external: ['@vitejs/test-ssr-conditions-external'],
66
noExternal: ['@vitejs/test-ssr-conditions-no-external'],
77
resolve: {
8-
conditions: [
9-
'module',
10-
'node',
11-
'production',
12-
'development',
13-
'react-server',
14-
],
8+
conditions: ['module', 'node', DEV_PROD_CONDITION, 'react-server'],
159
externalConditions: ['node', 'workerd', 'react-server'],
1610
},
1711
},

0 commit comments

Comments
 (0)