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(resolve)!: remove resolve.browserField #14733

Merged
merged 4 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 1 addition & 11 deletions docs/config/shared-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,10 @@ Export keys ending with "/" is deprecated by Node and may not work well. Please
## resolve.mainFields

- **Type:** `string[]`
- **Default:** `['module', 'jsnext:main', 'jsnext']`
- **Default:** `['browser', '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.

## resolve.browserField

- **Type:** `boolean`
- **Default:** `true`
- **Deprecated**

Whether to enable resolving to `browser` field.

In future, `resolve.mainFields`'s default value will be `['browser', 'module', 'jsnext:main', 'jsnext']` and this option will be removed.

## resolve.extensions

- **Type:** `string[]`
Expand Down
2 changes: 2 additions & 0 deletions docs/guide/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ Also there are other breaking changes which only affect few users.
- Renamed `ResolveWorkerOptions` type to `ResolvedWorkerOptions`
- [[#5657] fix: return 404 for resources requests outside the base path](https://github.com/vitejs/vite/pull/5657)
- 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.
- [[#14732] feat(resolve)!: remove `resolve.browserField`](https://github.com/vitejs/vite/pull/14732)
- `resolve.browserField` has been deprecated since Vite 3 in favour of an updated default of `['browser', 'module', 'jsnext:main', 'jsnext']` for `resolve.mainFields`.

## Migration from v3

Expand Down
2 changes: 0 additions & 2 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,6 @@ export async function resolveConfig(

const resolveOptions: ResolvedConfig['resolve'] = {
mainFields: config.resolve?.mainFields ?? DEFAULT_MAIN_FIELDS,
browserField: config.resolve?.browserField ?? true,
conditions: config.resolve?.conditions ?? [],
extensions: config.resolve?.extensions ?? DEFAULT_EXTENSIONS,
dedupe: config.resolve?.dedupe ?? [],
Expand Down Expand Up @@ -1060,7 +1059,6 @@ async function bundleConfigFile(
preferRelative: false,
tryIndex: true,
mainFields: [],
browserField: false,
conditions: [],
overrideConditions: ['node'],
dedupe: [],
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { version } = JSON.parse(
export const VERSION = version as string

export const DEFAULT_MAIN_FIELDS = [
'browser',
'module',
'jsnext:main', // moment still uses this...
'jsnext',
Expand Down
25 changes: 14 additions & 11 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,9 @@ const debug = createDebugger('vite:resolve-details', {

export interface ResolveOptions {
/**
* @default ['module', 'jsnext:main', 'jsnext']
* @default ['browser', 'module', 'jsnext:main', 'jsnext']
*/
mainFields?: string[]
/**
* @deprecated In future, `mainFields` should be used instead.
* @default true
*/
browserField?: boolean
conditions?: string[]
/**
* @default ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json']
Expand Down Expand Up @@ -281,7 +276,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {

if (
targetWeb &&
options.browserField &&
options.mainFields.includes('browser') &&
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved
(res = tryResolveBrowserMapping(fsPath, importer, options, true))
) {
return res
Expand Down Expand Up @@ -365,7 +360,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {

if (
targetWeb &&
options.browserField &&
options.mainFields.includes('browser') &&
(res = tryResolveBrowserMapping(
id,
importer,
Expand Down Expand Up @@ -991,7 +986,7 @@ export function resolvePackageEntry(
// fields are present, prioritize those instead.
if (
targetWeb &&
options.browserField &&
options.mainFields.includes('browser') &&
(!entryPoint || entryPoint.endsWith('.mjs'))
) {
// check browser field
Expand Down Expand Up @@ -1065,7 +1060,11 @@ export function resolvePackageEntry(
} else {
// resolve object browser field in package.json
const { browser: browserField } = data
if (targetWeb && options.browserField && isObject(browserField)) {
if (
targetWeb &&
options.mainFields.includes('browser') &&
isObject(browserField)
) {
entry = mapWithBrowserField(entry, browserField) || entry
}
}
Expand Down Expand Up @@ -1185,7 +1184,11 @@ function resolveDeepImport(
`${path.join(dir, 'package.json')}.`,
)
}
} else if (targetWeb && options.browserField && isObject(browserField)) {
} else if (
targetWeb &&
options.mainFields.includes('browser') &&
isObject(browserField)
) {
// resolve without postfix (see #7098)
const { file, postfix } = splitFileAndPostfix(relativeId)
const mapped = mapWithBrowserField(file, browserField)
Expand Down
1 change: 0 additions & 1 deletion packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ async function instantiateModule(

const resolveOptions: NodeImportResolveOptions = {
mainFields: ['main'],
browserField: true,
Comment on lines 138 to -140
Copy link
Member Author

@bluwy bluwy Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this was a mistake, the browserField was used in SSR resolving.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before #10071, Vite used browser field even with ssrModuleLoader and I wanted to preserve that behavior to avoid a breaking change. I think this change makes sense.

conditions: [],
overrideConditions: [...overrideConditions, 'production', 'development'],
extensions: ['.js', '.cjs', '.json'],
Expand Down
2 changes: 1 addition & 1 deletion playground/resolve/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const generatedContentImports = [
export default defineConfig({
resolve: {
extensions: ['.mjs', '.js', '.es', '.ts'],
mainFields: ['custom', 'module'],
mainFields: ['browser', 'custom', 'module'],
conditions: ['custom'],
},
define: {
Expand Down