Skip to content

Commit

Permalink
fix(resolve): check nested directories for package.json (#5665)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson authored Nov 13, 2021
1 parent 5b79bc0 commit 022db52
Showing 1 changed file with 49 additions and 16 deletions.
65 changes: 49 additions & 16 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
isBuiltin,
bareImportRE,
createDebugger,
deepImportRE,
injectQuery,
isExternalUrl,
isObject,
Expand Down Expand Up @@ -505,13 +504,35 @@ export function tryNodeResolve(
const nestedRoot = id.substring(0, lastArrowIndex).trim()
const nestedPath = id.substring(lastArrowIndex + 1).trim()

// check for deep import, e.g. "my-lib/foo"
const deepMatch = nestedPath.match(deepImportRE)
const possiblePkgIds: string[] = []
for (let prevSlashIndex = -1; ; ) {
let slashIndex = nestedPath.indexOf('/', prevSlashIndex + 1)
if (slashIndex < 0) {
slashIndex = nestedPath.length
}

const part = nestedPath.slice(
prevSlashIndex + 1,
(prevSlashIndex = slashIndex)
)
if (!part) {
break
}

const pkgId = deepMatch ? deepMatch[1] || deepMatch[2] : nestedPath
// Assume path parts with an extension are not package roots, except for the
// first path part (since periods are sadly allowed in package names).
// At the same time, skip the first path part if it begins with "@"
// (since "@foo/bar" should be treated as the top-level path).
if (possiblePkgIds.length ? path.extname(part) : part[0] === '@') {
continue
}

const possiblePkgId = nestedPath.slice(0, slashIndex)
possiblePkgIds.push(possiblePkgId)
}

let basedir: string
if (dedupe && dedupe.includes(pkgId)) {
if (dedupe?.some((id) => possiblePkgIds.includes(id))) {
basedir = root
} else if (
importer &&
Expand All @@ -528,21 +549,33 @@ export function tryNodeResolve(
basedir = nestedResolveFrom(nestedRoot, basedir, options.preserveSymlinks)
}

const pkg = resolvePackageData(pkgId, basedir, options.preserveSymlinks)
let pkg: PackageData | undefined
const pkgId = possiblePkgIds.reverse().find((pkgId) => {
pkg = resolvePackageData(pkgId, basedir, options.preserveSymlinks)
return pkg
})!

if (!pkg) {
return
}

let resolved = deepMatch
? resolveDeepImport(
'.' + id.slice(pkgId.length),
pkg,
options,
targetWeb,
options.preserveSymlinks
)
: resolvePackageEntry(id, pkg, options, targetWeb, options.preserveSymlinks)
let resolved =
nestedPath !== pkgId
? resolveDeepImport(
'.' + nestedPath.slice(pkgId.length),
pkg,
options,
targetWeb,
options.preserveSymlinks
)
: resolvePackageEntry(
nestedPath,
pkg,
options,
targetWeb,
options.preserveSymlinks
)

if (!resolved) {
return
}
Expand Down Expand Up @@ -572,7 +605,7 @@ export function tryNodeResolve(
!isJsType ||
importer?.includes('node_modules') ||
exclude?.includes(pkgId) ||
exclude?.includes(id) ||
exclude?.includes(nestedPath) ||
SPECIAL_QUERY_RE.test(resolved) ||
ssr
) {
Expand Down

0 comments on commit 022db52

Please sign in to comment.