Skip to content

Commit

Permalink
fix: address code review comment
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Oct 12, 2022
1 parent 8902b09 commit 6ce0484
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -648,28 +648,31 @@ export function tryNodeResolve(
basedir = nestedResolveFrom(nestedRoot, basedir, preserveSymlinks)
}

let nearestPkg: PackageData | undefined
let pkg: PackageData | undefined
let pkgId: string

const rootPkg =
possiblePkgIds.length &&
resolvePackageData(
possiblePkgIds[0],
let pkgId: string = possiblePkgIds.reverse().find((pkgId) => {
nearestPkg = resolvePackageData(
pkgId,
basedir,
preserveSymlinks,
packageCache
)!
return nearestPkg
})!

const rootPkgId = possiblePkgIds[0]
const rootPkg =
possiblePkgIds.length &&
resolvePackageData(rootPkgId, basedir, preserveSymlinks, packageCache)!
if (rootPkg && rootPkg?.data?.exports) {
pkg = rootPkg
pkgId = possiblePkgIds[0]
pkgId = rootPkgId
} else {
pkgId = possiblePkgIds.reverse().find((pkgId) => {
pkg = resolvePackageData(pkgId, basedir, preserveSymlinks, packageCache)!
return pkg
})!
pkg = nearestPkg
}

if (!pkg) {
if (!pkg || !nearestPkg) {
// 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 Expand Up @@ -768,7 +771,8 @@ export function tryNodeResolve(
}

const ext = path.extname(resolved)
const isCJS = ext === '.cjs' || (ext === '.js' && pkg.data.type !== 'module')
const isCJS =
ext === '.cjs' || (ext === '.js' && nearestPkg.data.type !== 'module')

if (
!options.ssrOptimizeCheck &&
Expand Down

0 comments on commit 6ce0484

Please sign in to comment.