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 06e400a
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,37 +654,38 @@ export function tryNodeResolve(
basedir = nestedResolveFrom(nestedRoot, basedir, preserveSymlinks)
}

// 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) => {
nearestPkg = resolvePackageData(
pkgId,
basedir,
preserveSymlinks,
packageCache,
)!
return nearestPkg
})!

const rootPkg = resolvePackageData(
rootPkgId,
basedir,
preserveSymlinks,
packageCache,
)!

let pkgId: string

if (rootPkg?.data?.exports) {
pkg = rootPkg
pkgId = rootPkgId
pkg = rootPkg
} else {
// nearest package.json
let nearestPkg: PackageData | undefined
pkgId = possiblePkgIds.reverse().find((pkgId) => {
nearestPkg = resolvePackageData(
pkgId,
basedir,
preserveSymlinks,
packageCache,
)!
return nearestPkg
})!
pkg = nearestPkg
}

if (!pkg || !nearestPkg) {
if (!pkg) {
// if import can't be found, check if it's an optional peer dep.
// if so, we can resolve to a special id that errors only when imported.
if (
Expand Down

0 comments on commit 06e400a

Please sign in to comment.