Skip to content

Commit

Permalink
feat: expose default mainFields/conditions (vitejs#18648)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Nov 14, 2024
1 parent 49783da commit c12c653
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 26 deletions.
4 changes: 2 additions & 2 deletions docs/config/shared-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ For SSR builds, deduplication does not work for ESM build outputs configured fro
## resolve.conditions
- **Type:** `string[]`
- **Default:** `['module', 'browser', 'development|production']`
- **Default:** `['module', 'browser', 'development|production']` (`defaultClientConditions`)
Additional allowed conditions when resolving [Conditional Exports](https://nodejs.org/api/packages.html#packages_conditional_exports) from a package.
Expand Down Expand Up @@ -147,7 +147,7 @@ Export keys ending with "/" is deprecated by Node and may not work well. Please
## resolve.mainFields

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

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.

Expand Down
2 changes: 1 addition & 1 deletion docs/config/ssr-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Build target for the SSR server.
## ssr.resolve.conditions

- **Type:** `string[]`
- **Default:** `['module', 'node', 'development|production']` (`['module', 'browser', 'development|production']` for `ssr.target === 'webworker'`)
- **Default:** `['module', 'node', 'development|production']` (`defaultServerConditions`) (`['module', 'browser', 'development|production']` (`defaultClientConditions`) for `ssr.target === 'webworker'`)
- **Related:** [Resolve Conditions](./shared-options.md#resolve-conditions)

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.
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ The conditions that are no longer added internally for
- `resolve.conditions` are `['module', 'browser', 'development|production']`
- `ssr.resolve.conditions` are `['module', 'node', 'development|production']`

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`.
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`.

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

### JSON stringify

Expand Down
14 changes: 7 additions & 7 deletions packages/vite/src/node/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ const DEFAULT_MAIN_FIELDS = [
'jsnext:main', // moment still uses this...
'jsnext',
]
export const DEFAULT_CLIENT_MAIN_FIELDS = DEFAULT_MAIN_FIELDS
export const DEFAULT_SERVER_MAIN_FIELDS = DEFAULT_MAIN_FIELDS.filter(
(f) => f !== 'browser',
export const DEFAULT_CLIENT_MAIN_FIELDS = Object.freeze(DEFAULT_MAIN_FIELDS)
export const DEFAULT_SERVER_MAIN_FIELDS = Object.freeze(
DEFAULT_MAIN_FIELDS.filter((f) => f !== 'browser'),
)

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

const DEFAULT_CONDITIONS = ['module', 'browser', 'node', DEV_PROD_CONDITION]
export const DEFAULT_CLIENT_CONDITIONS = DEFAULT_CONDITIONS.filter(
(c) => c !== 'node',
export const DEFAULT_CLIENT_CONDITIONS = Object.freeze(
DEFAULT_CONDITIONS.filter((c) => c !== 'node'),
)
export const DEFAULT_SERVER_CONDITIONS = DEFAULT_CONDITIONS.filter(
(c) => c !== 'browser',
export const DEFAULT_SERVER_CONDITIONS = Object.freeze(
DEFAULT_CONDITIONS.filter((c) => c !== 'browser'),
)

// Baseline support browserslist
Expand Down
8 changes: 7 additions & 1 deletion packages/vite/src/node/publicUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
* This file will be bundled to ESM and CJS and redirected by ../index.cjs
* Please control the side-effects by checking the ./dist/node-cjs/publicUtils.cjs bundle
*/
export { VERSION as version } from './constants'
export {
VERSION as version,
DEFAULT_CLIENT_CONDITIONS as defaultClientConditions,
DEFAULT_CLIENT_MAIN_FIELDS as defaultClientMainFields,
DEFAULT_SERVER_CONDITIONS as defaultServerConditions,
DEFAULT_SERVER_MAIN_FIELDS as defaultServerMainFields,
} from './constants'
export { version as esbuildVersion } from 'esbuild'
export {
splitVendorChunkPlugin,
Expand Down
23 changes: 16 additions & 7 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1070,27 +1070,36 @@ function backwardCompatibleWorkerPlugins(plugins: any) {
return []
}

function deepClone<T>(value: T): T {
type DeepWritable<T> =
T extends ReadonlyArray<unknown>
? { -readonly [P in keyof T]: DeepWritable<T[P]> }
: T extends RegExp
? RegExp
: T[keyof T] extends Function
? T
: { -readonly [P in keyof T]: DeepWritable<T[P]> }

function deepClone<T>(value: T): DeepWritable<T> {
if (Array.isArray(value)) {
return value.map((v) => deepClone(v)) as T
return value.map((v) => deepClone(v)) as DeepWritable<T>
}
if (isObject(value)) {
const cloned: Record<string, any> = {}
for (const key in value) {
cloned[key] = deepClone(value[key])
}
return cloned as T
return cloned as DeepWritable<T>
}
if (typeof value === 'function') {
return value as T
return value as DeepWritable<T>
}
if (value instanceof RegExp) {
return structuredClone(value)
return structuredClone(value) as DeepWritable<T>
}
if (typeof value === 'object' && value != null) {
throw new Error('Cannot deep clone non-plain object')
}
return value
return value as DeepWritable<T>
}

type MaybeFallback<D, V> = undefined extends V ? Exclude<V, undefined> | D : V
Expand Down Expand Up @@ -1146,7 +1155,7 @@ function mergeWithDefaultsRecursively<
export function mergeWithDefaults<
D extends Record<string, any>,
V extends Record<string, any>,
>(defaults: D, values: V): MergeWithDefaultsResult<D, V> {
>(defaults: D, values: V): MergeWithDefaultsResult<DeepWritable<D>, V> {
// NOTE: we need to clone the value here to avoid mutating the defaults
const clonedDefaults = deepClone(defaults)
return mergeWithDefaultsRecursively(clonedDefaults, values)
Expand Down
4 changes: 2 additions & 2 deletions playground/resolve/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'node:path'
import { defineConfig, normalizePath } from 'vite'
import { defaultClientConditions, defineConfig, normalizePath } from 'vite'
import { a } from './config-dep.cjs'

const virtualFile = '@virtual-file'
Expand Down Expand Up @@ -32,7 +32,7 @@ export default defineConfig({
resolve: {
extensions: ['.mjs', '.js', '.es', '.ts'],
mainFields: ['browser', 'custom', 'module'],
conditions: ['module', 'browser', 'development|production', 'custom'],
conditions: [...defaultClientConditions, 'custom'],
},
define: {
VITE_CONFIG_DEP_TEST: a,
Expand Down
4 changes: 2 additions & 2 deletions playground/ssr-conditions/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { defineConfig } from 'vite'
import { defaultServerConditions, defineConfig } from 'vite'

export default defineConfig({
ssr: {
external: ['@vitejs/test-ssr-conditions-external'],
noExternal: ['@vitejs/test-ssr-conditions-no-external'],
resolve: {
conditions: ['module', 'node', 'development|production', 'react-server'],
conditions: [...defaultServerConditions, 'react-server'],
externalConditions: ['node', 'workerd', 'react-server'],
},
},
Expand Down
4 changes: 2 additions & 2 deletions playground/ssr-webworker/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineConfig } from 'vite'
import { defaultClientConditions, defineConfig } from 'vite'

export default defineConfig({
build: {
Expand All @@ -14,7 +14,7 @@ export default defineConfig({
// in the runtime, and so we can externalize it when bundling.
external: ['node:assert'],
resolve: {
conditions: ['module', 'browser', 'development|production', 'worker'],
conditions: [...defaultClientConditions, 'worker'],
},
},
plugins: [
Expand Down

0 comments on commit c12c653

Please sign in to comment.