Skip to content

Commit 4215e22

Browse files
authoredMar 26, 2023
refactor: simplify lookupFile (#12585)
1 parent fb3245a commit 4215e22

File tree

5 files changed

+24
-31
lines changed

5 files changed

+24
-31
lines changed
 

‎packages/vite/src/node/config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,8 @@ export async function loadConfigFromFile(
928928
// check package.json for type: "module" and set `isESM` to true
929929
try {
930930
const pkg = lookupFile(configRoot, ['package.json'])
931-
isESM = !!pkg && JSON.parse(pkg).type === 'module'
931+
isESM =
932+
!!pkg && JSON.parse(fs.readFileSync(pkg, 'utf-8')).type === 'module'
932933
} catch (e) {}
933934
}
934935

‎packages/vite/src/node/env.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import fs from 'node:fs'
2+
import path from 'node:path'
23
import { parse } from 'dotenv'
34
import { expand } from 'dotenv-expand'
4-
import { arraify, lookupFile } from './utils'
5+
import { arraify, tryStatSync } from './utils'
56
import type { UserConfig } from './config'
67

78
export function loadEnv(
@@ -26,12 +27,10 @@ export function loadEnv(
2627

2728
const parsed = Object.fromEntries(
2829
envFiles.flatMap((file) => {
29-
const path = lookupFile(envDir, [file], {
30-
pathOnly: true,
31-
rootDir: envDir,
32-
})
33-
if (!path) return []
34-
return Object.entries(parse(fs.readFileSync(path)))
30+
const filePath = path.join(envDir, file)
31+
if (!tryStatSync(filePath)?.isFile()) return []
32+
33+
return Object.entries(parse(fs.readFileSync(filePath)))
3534
}),
3635
)
3736

‎packages/vite/src/node/optimizer/index.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1186,13 +1186,10 @@ const lockfileFormats = [
11861186
{ name: 'pnpm-lock.yaml', checkPatches: false }, // Included in lockfile
11871187
{ name: 'bun.lockb', checkPatches: true },
11881188
]
1189+
const lockfileNames = lockfileFormats.map((l) => l.name)
11891190

11901191
export function getDepHash(config: ResolvedConfig, ssr: boolean): string {
1191-
const lockfilePath = lookupFile(
1192-
config.root,
1193-
lockfileFormats.map((l) => l.name),
1194-
{ pathOnly: true },
1195-
)
1192+
const lockfilePath = lookupFile(config.root, lockfileNames)
11961193
let content = lockfilePath ? fs.readFileSync(lockfilePath, 'utf-8') : ''
11971194
if (lockfilePath) {
11981195
const lockfileName = path.basename(lockfilePath)

‎packages/vite/src/node/ssr/ssrExternal.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,11 @@ function cjsSsrCollectExternals(
217217
seen: Set<string>,
218218
logger: Logger,
219219
) {
220-
const rootPkgContent = lookupFile(root, ['package.json'])
220+
const rootPkgPath = lookupFile(root, ['package.json'])
221+
if (!rootPkgPath) {
222+
return
223+
}
224+
const rootPkgContent = fs.readFileSync(rootPkgPath, 'utf-8')
221225
if (!rootPkgContent) {
222226
return
223227
}

‎packages/vite/src/node/utils.ts

+9-17
Original file line numberDiff line numberDiff line change
@@ -390,28 +390,20 @@ export function tryStatSync(file: string): fs.Stats | undefined {
390390
// Ignore errors
391391
}
392392
}
393-
interface LookupFileOptions {
394-
pathOnly?: boolean
395-
rootDir?: string
396-
}
397393

398394
export function lookupFile(
399395
dir: string,
400-
formats: string[],
401-
options?: LookupFileOptions,
396+
fileNames: string[],
402397
): string | undefined {
403-
for (const format of formats) {
404-
const fullPath = path.join(dir, format)
405-
if (tryStatSync(fullPath)?.isFile()) {
406-
return options?.pathOnly ? fullPath : fs.readFileSync(fullPath, 'utf-8')
398+
while (dir) {
399+
for (const fileName of fileNames) {
400+
const fullPath = path.join(dir, fileName)
401+
if (tryStatSync(fullPath)?.isFile()) return fullPath
407402
}
408-
}
409-
const parentDir = path.dirname(dir)
410-
if (
411-
parentDir !== dir &&
412-
(!options?.rootDir || parentDir.startsWith(options?.rootDir))
413-
) {
414-
return lookupFile(parentDir, formats, options)
403+
const parentDir = path.dirname(dir)
404+
if (parentDir === dir) return
405+
406+
dir = parentDir
415407
}
416408
}
417409

0 commit comments

Comments
 (0)
Please sign in to comment.