Skip to content

Commit

Permalink
fix(resolve): fix package.json vs exports lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Dec 31, 2022
1 parent 4f76054 commit 7a11ec1
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,14 +654,21 @@ export function tryNodeResolve(
basedir = nestedResolveFrom(nestedRoot, basedir, preserveSymlinks)
}

let pkg: PackageData | undefined
let pkgId: string | undefined
// nearest package.json
let nearestPkg: PackageData | undefined
// nearest package.json that may have the `exports` field
let pkg: PackageData | undefined

const rootPkgId = possiblePkgIds[0]

let pkgId = possiblePkgIds.reverse().find((pkgId) => {
const rootPkg = resolvePackageData(
rootPkgId,
basedir,
preserveSymlinks,
packageCache,
)!

const nearestPkgId = [...possiblePkgIds].reverse().find((pkgId) => {
nearestPkg = resolvePackageData(
pkgId,
basedir,
Expand All @@ -671,16 +678,11 @@ export function tryNodeResolve(
return nearestPkg
})!

const rootPkg = resolvePackageData(
rootPkgId,
basedir,
preserveSymlinks,
packageCache,
)!
if (rootPkg?.data?.exports) {
pkg = rootPkg
pkgId = rootPkgId
pkg = rootPkg
} else {
pkgId = nearestPkgId
pkg = nearestPkg
}

Expand Down

0 comments on commit 7a11ec1

Please sign in to comment.