Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose default mainFields/conditions #18648

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ export interface EnvironmentResolveOptions {
/**
* @default ['browser', 'module', 'jsnext:main', 'jsnext']
*/
mainFields?: string[]
conditions?: string[]
externalConditions?: string[]
mainFields?: readonly string[]
conditions?: readonly string[]
externalConditions?: readonly string[]
/**
* @default ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json']
*/
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
4 changes: 2 additions & 2 deletions packages/vite/src/node/ssr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ export interface SSROptions {
*
* @default rootConfig.resolve.conditions
*/
conditions?: string[]
conditions?: readonly string[]
bluwy marked this conversation as resolved.
Show resolved Hide resolved

/**
* Conditions that are used during ssr import (including `ssrLoadModule`) of externalized dependencies.
*
* @default []
*/
externalConditions?: string[]
externalConditions?: readonly string[]
}
}

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