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

fix: respect mainFields when resolving browser/module field (fixes #8659) #10071

Merged
merged 4 commits into from
Sep 23, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: use '!browser'
sapphi-red committed Sep 11, 2022
commit 50ecd526a9f715ced6bfbeb3d989b68f123172cb
4 changes: 4 additions & 0 deletions docs/config/shared-options.md
Original file line number Diff line number Diff line change
@@ -158,6 +158,10 @@ Export keys ending with "/" is deprecated by Node and may not work well. Please

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.

:::warning `browser` field
Currently Vite resolves `browser` field even if `browser` is not included in `resolve.mainFields`. In future, `resolve.mainFields`'s default value will be `['browser', 'module', 'jsnext:main', 'jsnext']`. But for now, you could disable resolving to `browser` field by including `'!browser'` in `resolve.mainFields`.
:::

## resolve.extensions

- **Type:** `string[]`
18 changes: 13 additions & 5 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
@@ -236,7 +236,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {

if (
targetWeb &&
options.mainFields.includes('browser') &&
mainFieldsContainsBrowser(options.mainFields) &&
(res = tryResolveBrowserMapping(fsPath, importer, options, true))
) {
return res
@@ -307,7 +307,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {

if (
targetWeb &&
options.mainFields.includes('browser') &&
mainFieldsContainsBrowser(options.mainFields) &&
(res = tryResolveBrowserMapping(
id,
importer,
@@ -396,6 +396,14 @@ export default new Proxy({}, {
}
}

/**
* TODO
* @deprecated In Vite 4, this should be `mainFields.includes('browser')`
*/
function mainFieldsContainsBrowser(mainFields: string[]) {
return !mainFields.includes('!browser')
}

function splitFileAndPostfix(path: string) {
let file = path
let postfix = ''
@@ -888,7 +896,7 @@ export function resolvePackageEntry(
// fields are present, prioritize those instead.
if (
targetWeb &&
options.mainFields.includes('browser') &&
mainFieldsContainsBrowser(options.mainFields) &&
(!entryPoint || entryPoint.endsWith('.mjs'))
) {
// check browser field
@@ -962,7 +970,7 @@ export function resolvePackageEntry(
const { browser: browserField } = data
if (
targetWeb &&
options.mainFields.includes('browser') &&
mainFieldsContainsBrowser(options.mainFields) &&
isObject(browserField)
) {
entry = mapWithBrowserField(entry, browserField) || entry
@@ -1059,7 +1067,7 @@ function resolveDeepImport(
}
} else if (
targetWeb &&
options.mainFields.includes('browser') &&
mainFieldsContainsBrowser(options.mainFields) &&
isObject(browserField)
) {
// resolve without postfix (see #7098)