diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index e63877c1698f17..d42af1a6a74a3b 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -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) {} } diff --git a/packages/vite/src/node/env.ts b/packages/vite/src/node/env.ts index 338de6784497d8..374e1f049c0618 100644 --- a/packages/vite/src/node/env.ts +++ b/packages/vite/src/node/env.ts @@ -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( @@ -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))) }), ) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 496664baf8897a..b5a052c75f5c77 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -1199,13 +1199,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) diff --git a/packages/vite/src/node/ssr/ssrExternal.ts b/packages/vite/src/node/ssr/ssrExternal.ts index 6644fdd8e2e0b4..3d8185dad88080 100644 --- a/packages/vite/src/node/ssr/ssrExternal.ts +++ b/packages/vite/src/node/ssr/ssrExternal.ts @@ -216,7 +216,11 @@ function cjsSsrCollectExternals( seen: Set, 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 } diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 81d7e22bad6cf8..78b71a512705dd 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -387,28 +387,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 } }