Skip to content

Commit

Permalink
refactor: simplify lookupFile (#12585)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev authored Mar 26, 2023
1 parent fb3245a commit 4215e22
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 31 deletions.
3 changes: 2 additions & 1 deletion packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,8 @@ export async function loadConfigFromFile(
// check package.json for type: "module" and set `isESM` to true
try {
const pkg = lookupFile(configRoot, ['package.json'])
isESM = !!pkg && JSON.parse(pkg).type === 'module'
isESM =
!!pkg && JSON.parse(fs.readFileSync(pkg, 'utf-8')).type === 'module'
} catch (e) {}
}

Expand Down
13 changes: 6 additions & 7 deletions packages/vite/src/node/env.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import fs from 'node:fs'
import path from 'node:path'
import { parse } from 'dotenv'
import { expand } from 'dotenv-expand'
import { arraify, lookupFile } from './utils'
import { arraify, tryStatSync } from './utils'
import type { UserConfig } from './config'

export function loadEnv(
Expand All @@ -26,12 +27,10 @@ export function loadEnv(

const parsed = Object.fromEntries(
envFiles.flatMap((file) => {
const path = lookupFile(envDir, [file], {
pathOnly: true,
rootDir: envDir,
})
if (!path) return []
return Object.entries(parse(fs.readFileSync(path)))
const filePath = path.join(envDir, file)
if (!tryStatSync(filePath)?.isFile()) return []

return Object.entries(parse(fs.readFileSync(filePath)))
}),
)

Expand Down
7 changes: 2 additions & 5 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1186,13 +1186,10 @@ const lockfileFormats = [
{ name: 'pnpm-lock.yaml', checkPatches: false }, // Included in lockfile
{ name: 'bun.lockb', checkPatches: true },
]
const lockfileNames = lockfileFormats.map((l) => l.name)

export function getDepHash(config: ResolvedConfig, ssr: boolean): string {
const lockfilePath = lookupFile(
config.root,
lockfileFormats.map((l) => l.name),
{ pathOnly: true },
)
const lockfilePath = lookupFile(config.root, lockfileNames)
let content = lockfilePath ? fs.readFileSync(lockfilePath, 'utf-8') : ''
if (lockfilePath) {
const lockfileName = path.basename(lockfilePath)
Expand Down
6 changes: 5 additions & 1 deletion packages/vite/src/node/ssr/ssrExternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,11 @@ function cjsSsrCollectExternals(
seen: Set<string>,
logger: Logger,
) {
const rootPkgContent = lookupFile(root, ['package.json'])
const rootPkgPath = lookupFile(root, ['package.json'])
if (!rootPkgPath) {
return
}
const rootPkgContent = fs.readFileSync(rootPkgPath, 'utf-8')
if (!rootPkgContent) {
return
}
Expand Down
26 changes: 9 additions & 17 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,28 +390,20 @@ export function tryStatSync(file: string): fs.Stats | undefined {
// Ignore errors
}
}
interface LookupFileOptions {
pathOnly?: boolean
rootDir?: string
}

export function lookupFile(
dir: string,
formats: string[],
options?: LookupFileOptions,
fileNames: string[],
): string | undefined {
for (const format of formats) {
const fullPath = path.join(dir, format)
if (tryStatSync(fullPath)?.isFile()) {
return options?.pathOnly ? fullPath : fs.readFileSync(fullPath, 'utf-8')
while (dir) {
for (const fileName of fileNames) {
const fullPath = path.join(dir, fileName)
if (tryStatSync(fullPath)?.isFile()) return fullPath
}
}
const parentDir = path.dirname(dir)
if (
parentDir !== dir &&
(!options?.rootDir || parentDir.startsWith(options?.rootDir))
) {
return lookupFile(parentDir, formats, options)
const parentDir = path.dirname(dir)
if (parentDir === dir) return

dir = parentDir
}
}

Expand Down

0 comments on commit 4215e22

Please sign in to comment.