From a6f5f5baca7a5d2064f5f4cb689764ad939fab4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Wed, 13 Nov 2024 19:50:29 +0900 Subject: [PATCH] feat: add `ssr.resolve.mainFields` option (#18646) --- docs/config/ssr-options.md | 7 +++++++ docs/guide/migration.md | 2 ++ packages/vite/src/node/config.ts | 1 + packages/vite/src/node/ssr/index.ts | 2 ++ 4 files changed, 12 insertions(+) diff --git a/docs/config/ssr-options.md b/docs/config/ssr-options.md index c48cb0d0d4dfb0..28a9775b8bbdc2 100644 --- a/docs/config/ssr-options.md +++ b/docs/config/ssr-options.md @@ -53,3 +53,10 @@ When using this option, make sure to run Node with [`--conditions` flag](https:/ For example, when setting `['node', 'custom']`, you should run `NODE_OPTIONS='--conditions custom' vite` in dev and `NODE_OPTIONS="--conditions custom" node ./dist/server.js` after build. ::: + +### ssr.resolve.mainFields + +- **Type:** `string[]` +- **Default:** `['module', 'jsnext:main', 'jsnext']` + +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. This setting only affect non-externalized dependencies. diff --git a/docs/guide/migration.md b/docs/guide/migration.md index 771aa2e4b75e5a..de0da8317095ac 100644 --- a/docs/guide/migration.md +++ b/docs/guide/migration.md @@ -93,6 +93,8 @@ There are other breaking changes which only affect few users. - If you are specifying a CommonJS file as an entry point, you may need additional steps. Read [the commonjs plugin documentation](https://github.com/rollup/plugins/blob/master/packages/commonjs/README.md#using-commonjs-files-as-entry-points) for more details. - [[#18243] chore(deps)!: migrate `fast-glob` to `tinyglobby`](https://github.com/vitejs/vite/pull/18243) - Range braces (`{01..03}` ⇒ `['01', '02', '03']`) and incremental braces (`{2..8..2}` ⇒ `['2', '4', '6', '8']`) are no longer supported in globs. +- [[#18395] feat(resolve)!: allow removing conditions](https://github.com/vitejs/vite/pull/18395) + - This PR not only introduces a breaking change mentioned above as "Default value for `resolve.conditions`", but also makes `resolve.mainFields` to not be used for no-externalized dependencies in SSR. If you were using `resolve.mainFields` and want to apply that to no-externalized dependencies in SSR, you can use [`ssr.resolve.mainFields`](/config/ssr-options#ssr-resolve-mainfields). - [[#18493] refactor!: remove fs.cachedChecks option](https://github.com/vitejs/vite/pull/18493) - This opt-in optimization was removed due to edge cases when writing a file in a cached folder and immediately importing it. diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index ca3fa85f0ad62a..983952f6bcbf29 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -1093,6 +1093,7 @@ export async function resolveConfig( configEnvironmentsSsr.resolve.conditions ??= config.ssr?.resolve?.conditions configEnvironmentsSsr.resolve.externalConditions ??= config.ssr?.resolve?.externalConditions + configEnvironmentsSsr.resolve.mainFields ??= config.ssr?.resolve?.mainFields configEnvironmentsSsr.resolve.external ??= config.ssr?.external configEnvironmentsSsr.resolve.noExternal ??= config.ssr?.noExternal } diff --git a/packages/vite/src/node/ssr/index.ts b/packages/vite/src/node/ssr/index.ts index 45901f069b565c..af8257444bdf3c 100644 --- a/packages/vite/src/node/ssr/index.ts +++ b/packages/vite/src/node/ssr/index.ts @@ -43,6 +43,8 @@ export interface SSROptions { * @default [] */ externalConditions?: string[] + + mainFields?: string[] } }