Skip to content

Commit b8a21cc

Browse files
authored
fix: remove format sniffing module resolution from JS resolver (#22297)
1 parent 40a0847 commit b8a21cc

1 file changed

Lines changed: 7 additions & 54 deletions

File tree

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

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import colors from 'picocolors'
44
import type { PartialResolvedId } from 'rolldown'
55
import { viteResolvePlugin } from 'rolldown/experimental'
66
import { exports, imports } from 'resolve.exports'
7-
import { hasESMSyntax } from 'mlly'
87
import type { Plugin } from '../plugin'
98
import {
109
CLIENT_ENTRY,
@@ -932,14 +931,15 @@ export function resolvePackageEntry(
932931
// fallback to mainFields if still not resolved
933932
if (!entryPoint) {
934933
for (const field of options.mainFields) {
935-
if (field === 'browser') {
936-
entryPoint = tryResolveBrowserEntry(dir, data, options)
937-
if (entryPoint) {
938-
break
939-
}
940-
} else if (typeof data[field] === 'string') {
934+
if (typeof data[field] === 'string') {
941935
entryPoint = data[field]
942936
break
937+
} else if (field === 'browser') {
938+
const browser = data[field]
939+
if (isObject(browser) && browser['.']) {
940+
entryPoint = browser['.']
941+
break
942+
}
943943
}
944944
}
945945
}
@@ -1109,53 +1109,6 @@ function resolveDeepImport(
11091109
}
11101110
}
11111111

1112-
function tryResolveBrowserEntry(
1113-
dir: string,
1114-
data: PackageData['data'],
1115-
options: InternalResolveOptions,
1116-
) {
1117-
// handle edge case with browser and module field semantics
1118-
1119-
// check browser field
1120-
// https://github.com/defunctzombie/package-browser-field-spec
1121-
const browserEntry =
1122-
typeof data.browser === 'string'
1123-
? data.browser
1124-
: isObject(data.browser) && data.browser['.']
1125-
if (browserEntry) {
1126-
// check if the package also has a "module" field.
1127-
if (
1128-
!options.isRequire &&
1129-
options.mainFields.includes('module') &&
1130-
typeof data.module === 'string' &&
1131-
data.module !== browserEntry
1132-
) {
1133-
// if both are present, we may have a problem: some package points both
1134-
// to ESM, with "module" targeting Node.js, while some packages points
1135-
// "module" to browser ESM and "browser" to UMD/IIFE.
1136-
// the heuristics here is to actually read the browser entry when
1137-
// possible and check for hints of ESM. If it is not ESM, prefer "module"
1138-
// instead; Otherwise, assume it's ESM and use it.
1139-
const resolvedBrowserEntry = tryFsResolve(
1140-
path.join(dir, browserEntry),
1141-
options,
1142-
)
1143-
if (resolvedBrowserEntry) {
1144-
const content = fs.readFileSync(resolvedBrowserEntry, 'utf-8')
1145-
if (hasESMSyntax(content)) {
1146-
// likely ESM, prefer browser
1147-
return browserEntry
1148-
} else {
1149-
// non-ESM, UMD or IIFE or CJS(!!! e.g. firebase 7.x), prefer module
1150-
return data.module
1151-
}
1152-
}
1153-
} else {
1154-
return browserEntry
1155-
}
1156-
}
1157-
}
1158-
11591112
/**
11601113
* given a relative path in pkg dir,
11611114
* return a relative path in pkg dir,

0 commit comments

Comments
 (0)