Skip to content

Commit 43cc3b9

Browse files
authored
feat(resolve)!: remove resolve.browserField (#14733)
1 parent cc9fb87 commit 43cc3b9

File tree

7 files changed

+33
-26
lines changed

7 files changed

+33
-26
lines changed

docs/config/shared-options.md

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,20 +154,10 @@ Export keys ending with "/" is deprecated by Node and may not work well. Please
154154
## resolve.mainFields
155155

156156
- **Type:** `string[]`
157-
- **Default:** `['module', 'jsnext:main', 'jsnext']`
157+
- **Default:** `['browser', 'module', 'jsnext:main', 'jsnext']`
158158

159159
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.
160160

161-
## resolve.browserField
162-
163-
- **Type:** `boolean`
164-
- **Default:** `true`
165-
- **Deprecated**
166-
167-
Whether to enable resolving to `browser` field.
168-
169-
In future, `resolve.mainFields`'s default value will be `['browser', 'module', 'jsnext:main', 'jsnext']` and this option will be removed.
170-
171161
## resolve.extensions
172162

173163
- **Type:** `string[]`

docs/guide/migration.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ Also there are other breaking changes which only affect few users.
135135
- In the past, Vite responded to requests outside the base path without `Accept: text/html`, as if they were requested with the base path. Vite no longer does that and responds with 404 instead.
136136
- [[#14723] fix(resolve)!: remove special .mjs handling](https://github.com/vitejs/vite/pull/14723)
137137
- In the past, when a library `"exports"` field maps to an `.mjs` file, Vite will still try to match the `"browser"` and `"module"` fields to fix compatibility with certain libraries. This behavior is now removed to align with the exports resolution algorithm.
138+
- [[#14733] feat(resolve)!: remove `resolve.browserField`](https://github.com/vitejs/vite/pull/14733)
139+
- `resolve.browserField` has been deprecated since Vite 3 in favour of an updated default of `['browser', 'module', 'jsnext:main', 'jsnext']` for `resolve.mainFields`.
138140

139141
## Migration from v3
140142

packages/vite/src/node/config.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,14 +530,27 @@ export async function resolveConfig(
530530

531531
const resolveOptions: ResolvedConfig['resolve'] = {
532532
mainFields: config.resolve?.mainFields ?? DEFAULT_MAIN_FIELDS,
533-
browserField: config.resolve?.browserField ?? true,
534533
conditions: config.resolve?.conditions ?? [],
535534
extensions: config.resolve?.extensions ?? DEFAULT_EXTENSIONS,
536535
dedupe: config.resolve?.dedupe ?? [],
537536
preserveSymlinks: config.resolve?.preserveSymlinks ?? false,
538537
alias: resolvedAlias,
539538
}
540539

540+
if (
541+
// @ts-expect-error removed field
542+
config.resolve?.browserField === false &&
543+
resolveOptions.mainFields.includes('browser')
544+
) {
545+
logger.warn(
546+
colors.yellow(
547+
`\`resolve.browserField\` is set to false, but the option is removed in favour of ` +
548+
`the 'browser' string in \`resolve.mainFields\`. You may want to update \`resolve.mainFields\` ` +
549+
`to remove the 'browser' string and preserve the previous browser behaviour.`,
550+
),
551+
)
552+
}
553+
541554
// load .env files
542555
const envDir = config.envDir
543556
? normalizePath(path.resolve(resolvedRoot, config.envDir))
@@ -1060,7 +1073,6 @@ async function bundleConfigFile(
10601073
preferRelative: false,
10611074
tryIndex: true,
10621075
mainFields: [],
1063-
browserField: false,
10641076
conditions: [],
10651077
overrideConditions: ['node'],
10661078
dedupe: [],

packages/vite/src/node/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const { version } = JSON.parse(
99
export const VERSION = version as string
1010

1111
export const DEFAULT_MAIN_FIELDS = [
12+
'browser',
1213
'module',
1314
'jsnext:main', // moment still uses this...
1415
'jsnext',

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,9 @@ const debug = createDebugger('vite:resolve-details', {
7171

7272
export interface ResolveOptions {
7373
/**
74-
* @default ['module', 'jsnext:main', 'jsnext']
74+
* @default ['browser', 'module', 'jsnext:main', 'jsnext']
7575
*/
7676
mainFields?: string[]
77-
/**
78-
* @deprecated In future, `mainFields` should be used instead.
79-
* @default true
80-
*/
81-
browserField?: boolean
8277
conditions?: string[]
8378
/**
8479
* @default ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json']
@@ -283,7 +278,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
283278

284279
if (
285280
targetWeb &&
286-
options.browserField &&
281+
options.mainFields.includes('browser') &&
287282
(res = tryResolveBrowserMapping(fsPath, importer, options, true))
288283
) {
289284
return res
@@ -367,7 +362,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
367362

368363
if (
369364
targetWeb &&
370-
options.browserField &&
365+
options.mainFields.includes('browser') &&
371366
(res = tryResolveBrowserMapping(
372367
id,
373368
importer,
@@ -988,7 +983,7 @@ export function resolvePackageEntry(
988983
}
989984

990985
// handle edge case with browser and module field semantics
991-
if (!entryPoint && targetWeb && options.browserField) {
986+
if (!entryPoint && targetWeb && options.mainFields.includes('browser')) {
992987
// check browser field
993988
// https://github.com/defunctzombie/package-browser-field-spec
994989
const browserEntry =
@@ -1059,7 +1054,11 @@ export function resolvePackageEntry(
10591054
} else {
10601055
// resolve object browser field in package.json
10611056
const { browser: browserField } = data
1062-
if (targetWeb && options.browserField && isObject(browserField)) {
1057+
if (
1058+
targetWeb &&
1059+
options.mainFields.includes('browser') &&
1060+
isObject(browserField)
1061+
) {
10631062
entry = mapWithBrowserField(entry, browserField) || entry
10641063
}
10651064
}
@@ -1181,7 +1180,11 @@ function resolveDeepImport(
11811180
`${path.join(dir, 'package.json')}.`,
11821181
)
11831182
}
1184-
} else if (targetWeb && options.browserField && isObject(browserField)) {
1183+
} else if (
1184+
targetWeb &&
1185+
options.mainFields.includes('browser') &&
1186+
isObject(browserField)
1187+
) {
11851188
// resolve without postfix (see #7098)
11861189
const { file, postfix } = splitFileAndPostfix(relativeId)
11871190
const mapped = mapWithBrowserField(file, browserField)

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ async function instantiateModule(
137137

138138
const resolveOptions: NodeImportResolveOptions = {
139139
mainFields: ['main'],
140-
browserField: true,
141140
conditions: [],
142141
overrideConditions: [...overrideConditions, 'production', 'development'],
143142
extensions: ['.js', '.cjs', '.json'],

playground/resolve/vite.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const generatedContentImports = [
2727
export default defineConfig({
2828
resolve: {
2929
extensions: ['.mjs', '.js', '.es', '.ts'],
30-
mainFields: ['custom', 'module'],
30+
mainFields: ['browser', 'custom', 'module'],
3131
conditions: ['custom'],
3232
},
3333
define: {

0 commit comments

Comments
 (0)