Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(resolve): findNearestMainPackageData instead of lookupFile #12576

Merged
merged 5 commits into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/vite/src/node/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,23 @@ export function findNearestPackageData(
return null
}

// Finds the nearest package.json with a `name` field
export function findNearestMainPackageData(
basedir: string,
packageCache?: PackageCache,
): PackageData | null {
const nearestPackage = findNearestPackageData(basedir, packageCache)
return (
nearestPackage &&
(nearestPackage.data.name
? nearestPackage
: findNearestMainPackageData(
path.dirname(nearestPackage.dir),
packageCache,
))
)
}

export function loadPackageData(pkgPath: string): PackageData {
const data = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
const pkgDir = path.dirname(pkgPath)
Expand Down
10 changes: 3 additions & 7 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
isOptimizable,
isTsRequest,
isWindows,
lookupFile,
normalizePath,
resolveFrom,
safeRealpathSync,
Expand All @@ -43,6 +42,7 @@ import type { DepsOptimizer } from '../optimizer'
import type { SSROptions } from '..'
import type { PackageCache, PackageData } from '../packages'
import {
findNearestMainPackageData,
findNearestPackageData,
loadPackageData,
resolvePackageData,
Expand Down Expand Up @@ -705,12 +705,8 @@ export function tryNodeResolve(
!id.includes('\0') &&
bareImportRE.test(id)
) {
// find package.json with `name` as main
const mainPackageJson = lookupFile(basedir, ['package.json'], {
predicate: (content) => !!JSON.parse(content).name,
})
if (mainPackageJson) {
const mainPkg = JSON.parse(mainPackageJson)
const mainPkg = findNearestMainPackageData(basedir, packageCache)?.data
if (mainPkg) {
if (
mainPkg.peerDependencies?.[id] &&
mainPkg.peerDependenciesMeta?.[id]?.optional
Expand Down
8 changes: 1 addition & 7 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,6 @@ export function tryStatSync(file: string): fs.Stats | undefined {
interface LookupFileOptions {
pathOnly?: boolean
rootDir?: string
predicate?: (file: string) => boolean
}

export function lookupFile(
Expand All @@ -400,12 +399,7 @@ export function lookupFile(
for (const format of formats) {
const fullPath = path.join(dir, format)
if (tryStatSync(fullPath)?.isFile()) {
const result = options?.pathOnly
? fullPath
: fs.readFileSync(fullPath, 'utf-8')
if (!options?.predicate || options.predicate(result)) {
return result
}
return options?.pathOnly ? fullPath : fs.readFileSync(fullPath, 'utf-8')
}
}
const parentDir = path.dirname(dir)
Expand Down