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 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/vite/src/node/packages.ts
Expand Up @@ -143,6 +143,20 @@ 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(basedir), packageCache))
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
)
}

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
Expand Up @@ -33,7 +33,6 @@ import {
isPossibleTsOutput,
isTsRequest,
isWindows,
lookupFile,
normalizePath,
resolveFrom,
safeRealpathSync,
Expand All @@ -44,6 +43,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 @@ -704,12 +704,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
Expand Up @@ -395,7 +395,6 @@ export function isDefined<T>(value: T | undefined | null): value is T {
interface LookupFileOptions {
pathOnly?: boolean
rootDir?: string
predicate?: (file: string) => boolean
}

export function lookupFile(
Expand All @@ -406,12 +405,7 @@ export function lookupFile(
for (const format of formats) {
const fullPath = path.join(dir, format)
if (fs.existsSync(fullPath) && fs.statSync(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