Skip to content

Commit c12c653

Browse files
authoredNov 14, 2024··
feat: expose default mainFields/conditions (#18648)
1 parent 49783da commit c12c653

File tree

9 files changed

+41
-26
lines changed

9 files changed

+41
-26
lines changed
 

‎docs/config/shared-options.md

+2-2
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', 'development|production']`
120+
- **Default:** `['module', 'browser', 'development|production']` (`defaultClientConditions`)
121121
122122
Additional allowed conditions when resolving [Conditional Exports](https://nodejs.org/api/packages.html#packages_conditional_exports) from a package.
123123
@@ -147,7 +147,7 @@ Export keys ending with "/" is deprecated by Node and may not work well. Please
147147
## resolve.mainFields
148148

149149
- **Type:** `string[]`
150-
- **Default:** `['browser', 'module', 'jsnext:main', 'jsnext']`
150+
- **Default:** `['browser', 'module', 'jsnext:main', 'jsnext']` (`defaultClientMainFields`)
151151

152152
List of fields in `package.json` to try when resolving a package's entry point. Note this takes lower precedence than conditional exports resolved from the `exports` field: if an entry point is successfully resolved from `exports`, the main field will be ignored.
153153

‎docs/config/ssr-options.md

+1-1
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', 'development|production']` (`['module', 'browser', 'development|production']` for `ssr.target === 'webworker'`)
37+
- **Default:** `['module', 'node', 'development|production']` (`defaultServerConditions`) (`['module', 'browser', 'development|production']` (`defaultClientConditions`) 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

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ The conditions that are no longer added internally for
2424
- `resolve.conditions` are `['module', 'browser', 'development|production']`
2525
- `ssr.resolve.conditions` are `['module', 'node', 'development|production']`
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. Note that `development|production` is a special variable that is replaced with `production` or `development` depending on the value of `process.env.NODE_ENV`.
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 `development|production` is a special variable that is replaced with `production` or `development` depending on the value of `process.env.NODE_ENV`. These default values are exported from `vite` as `defaultClientConditions` and `defaultServerConditions`.
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', 'development|production']` instead.
30+
For example, if you previously specified `['custom']` for `resolve.conditions`, you need to specify `['custom', ...defaultClientConditions]` instead.
3131

3232
### JSON stringify
3333

‎packages/vite/src/node/constants.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ const DEFAULT_MAIN_FIELDS = [
4545
'jsnext:main', // moment still uses this...
4646
'jsnext',
4747
]
48-
export const DEFAULT_CLIENT_MAIN_FIELDS = DEFAULT_MAIN_FIELDS
49-
export const DEFAULT_SERVER_MAIN_FIELDS = DEFAULT_MAIN_FIELDS.filter(
50-
(f) => f !== 'browser',
48+
export const DEFAULT_CLIENT_MAIN_FIELDS = Object.freeze(DEFAULT_MAIN_FIELDS)
49+
export const DEFAULT_SERVER_MAIN_FIELDS = Object.freeze(
50+
DEFAULT_MAIN_FIELDS.filter((f) => f !== 'browser'),
5151
)
5252

5353
/**
@@ -57,11 +57,11 @@ export const DEFAULT_SERVER_MAIN_FIELDS = DEFAULT_MAIN_FIELDS.filter(
5757
export const DEV_PROD_CONDITION = `development|production` as const
5858

5959
const DEFAULT_CONDITIONS = ['module', 'browser', 'node', DEV_PROD_CONDITION]
60-
export const DEFAULT_CLIENT_CONDITIONS = DEFAULT_CONDITIONS.filter(
61-
(c) => c !== 'node',
60+
export const DEFAULT_CLIENT_CONDITIONS = Object.freeze(
61+
DEFAULT_CONDITIONS.filter((c) => c !== 'node'),
6262
)
63-
export const DEFAULT_SERVER_CONDITIONS = DEFAULT_CONDITIONS.filter(
64-
(c) => c !== 'browser',
63+
export const DEFAULT_SERVER_CONDITIONS = Object.freeze(
64+
DEFAULT_CONDITIONS.filter((c) => c !== 'browser'),
6565
)
6666

6767
// Baseline support browserslist

‎packages/vite/src/node/publicUtils.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
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 {
7+
VERSION as version,
8+
DEFAULT_CLIENT_CONDITIONS as defaultClientConditions,
9+
DEFAULT_CLIENT_MAIN_FIELDS as defaultClientMainFields,
10+
DEFAULT_SERVER_CONDITIONS as defaultServerConditions,
11+
DEFAULT_SERVER_MAIN_FIELDS as defaultServerMainFields,
12+
} from './constants'
713
export { version as esbuildVersion } from 'esbuild'
814
export {
915
splitVendorChunkPlugin,

‎packages/vite/src/node/utils.ts

+16-7
Original file line numberDiff line numberDiff line change
@@ -1070,27 +1070,36 @@ function backwardCompatibleWorkerPlugins(plugins: any) {
10701070
return []
10711071
}
10721072

1073-
function deepClone<T>(value: T): T {
1073+
type DeepWritable<T> =
1074+
T extends ReadonlyArray<unknown>
1075+
? { -readonly [P in keyof T]: DeepWritable<T[P]> }
1076+
: T extends RegExp
1077+
? RegExp
1078+
: T[keyof T] extends Function
1079+
? T
1080+
: { -readonly [P in keyof T]: DeepWritable<T[P]> }
1081+
1082+
function deepClone<T>(value: T): DeepWritable<T> {
10741083
if (Array.isArray(value)) {
1075-
return value.map((v) => deepClone(v)) as T
1084+
return value.map((v) => deepClone(v)) as DeepWritable<T>
10761085
}
10771086
if (isObject(value)) {
10781087
const cloned: Record<string, any> = {}
10791088
for (const key in value) {
10801089
cloned[key] = deepClone(value[key])
10811090
}
1082-
return cloned as T
1091+
return cloned as DeepWritable<T>
10831092
}
10841093
if (typeof value === 'function') {
1085-
return value as T
1094+
return value as DeepWritable<T>
10861095
}
10871096
if (value instanceof RegExp) {
1088-
return structuredClone(value)
1097+
return structuredClone(value) as DeepWritable<T>
10891098
}
10901099
if (typeof value === 'object' && value != null) {
10911100
throw new Error('Cannot deep clone non-plain object')
10921101
}
1093-
return value
1102+
return value as DeepWritable<T>
10941103
}
10951104

10961105
type MaybeFallback<D, V> = undefined extends V ? Exclude<V, undefined> | D : V
@@ -1146,7 +1155,7 @@ function mergeWithDefaultsRecursively<
11461155
export function mergeWithDefaults<
11471156
D extends Record<string, any>,
11481157
V extends Record<string, any>,
1149-
>(defaults: D, values: V): MergeWithDefaultsResult<D, V> {
1158+
>(defaults: D, values: V): MergeWithDefaultsResult<DeepWritable<D>, V> {
11501159
// NOTE: we need to clone the value here to avoid mutating the defaults
11511160
const clonedDefaults = deepClone(defaults)
11521161
return mergeWithDefaultsRecursively(clonedDefaults, values)

‎playground/resolve/vite.config.js

+2-2
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 { defaultClientConditions, 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', 'development|production', 'custom'],
35+
conditions: [...defaultClientConditions, 'custom'],
3636
},
3737
define: {
3838
VITE_CONFIG_DEP_TEST: a,

‎playground/ssr-conditions/vite.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { defineConfig } from 'vite'
1+
import { defaultServerConditions, 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: ['module', 'node', 'development|production', 'react-server'],
8+
conditions: [...defaultServerConditions, 'react-server'],
99
externalConditions: ['node', 'workerd', 'react-server'],
1010
},
1111
},

‎playground/ssr-webworker/vite.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineConfig } from 'vite'
1+
import { defaultClientConditions, defineConfig } from 'vite'
22

33
export default defineConfig({
44
build: {
@@ -14,7 +14,7 @@ export default defineConfig({
1414
// in the runtime, and so we can externalize it when bundling.
1515
external: ['node:assert'],
1616
resolve: {
17-
conditions: ['module', 'browser', 'development|production', 'worker'],
17+
conditions: [...defaultClientConditions, 'worker'],
1818
},
1919
},
2020
plugins: [

0 commit comments

Comments
 (0)
Please sign in to comment.