Skip to content

Commit

Permalink
fix(resolve): fix browser mapping fallback
Browse files Browse the repository at this point in the history
fix #2115
  • Loading branch information
yyx990803 committed Feb 22, 2021
1 parent 8ed67cf commit de58967
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ function resolveDeepImport(
const mapped = mapWithBrowserField(relativeId, browserField)
if (mapped) {
relativeId = mapped
} else {
} else if (mapped === false) {
return (resolvedImports[id] = browserExternalId)
}
}
Expand Down Expand Up @@ -625,7 +625,7 @@ function tryResolveBrowserMapping(
moduleSideEffects: pkg.hasSideEffects(res)
}
}
} else {
} else if (browserMappedPath === false) {
return browserExternalId
}
}
Expand All @@ -635,19 +635,20 @@ function tryResolveBrowserMapping(
* given a relative path in pkg dir,
* return a relative path in pkg dir,
* mapped with the "map" object
*
* - Returning `undefined` means there is no browser mapping for this id
* - Returning `false` means this id is explicitly externalized for browser
*/
function mapWithBrowserField(
relativePathInPkgDir: string,
map: Record<string, string | false>
) {
): string | false | undefined {
const normalized = normalize(relativePathInPkgDir)
const foundEntry = Object.entries(map).find(
([from]) => normalize(from) === normalized
)
if (!foundEntry) {
return relativePathInPkgDir
for (const key in map) {
if (normalize(key) === normalized) {
return map[key]
}
}
return foundEntry[1]
}

function normalize(file: string) {
Expand Down

0 comments on commit de58967

Please sign in to comment.