Skip to content

Commit

Permalink
fix: resolve browser mapping using bare imports
Browse files Browse the repository at this point in the history
  • Loading branch information
hershelh committed Dec 6, 2022
1 parent f24679c commit 6700910
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,13 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
if (
targetWeb &&
options.browserField &&
(res = tryResolveBrowserMapping(fsPath, importer, options, true))
(res = tryResolveBrowserMapping(
fsPath,
importer,
options,
targetWeb,
true,
))
) {
return res
}
Expand Down Expand Up @@ -323,6 +329,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
importer,
options,
false,
targetWeb,
external,
))
) {
Expand Down Expand Up @@ -1163,6 +1170,7 @@ function tryResolveBrowserMapping(
importer: string | undefined,
options: InternalResolveOptions,
isFilePath: boolean,
targetWeb: boolean,
externalize?: boolean,
) {
let res: string | undefined
Expand All @@ -1173,7 +1181,12 @@ function tryResolveBrowserMapping(
const browserMappedPath = mapWithBrowserField(mapId, pkg.data.browser)
if (browserMappedPath) {
const fsPath = path.join(pkg.dir, browserMappedPath)
if ((res = tryFsResolve(fsPath, options))) {

if (
(res = bareImportRE.test(browserMappedPath)
? tryNodeResolve(browserMappedPath, importer, options, targetWeb)?.id
: tryFsResolve(fsPath, options))
) {
isDebug &&
debug(`[browser mapped] ${colors.cyan(id)} -> ${colors.dim(res)}`)
idToPkgMap.set(res, pkg)
Expand Down

0 comments on commit 6700910

Please sign in to comment.