From 41dad609ccd1e52ea45aff5bef2eec792dffa604 Mon Sep 17 00:00:00 2001 From: patak Date: Sun, 26 Mar 2023 11:47:04 +0200 Subject: [PATCH 1/2] refactor: simplify lookupFile --- packages/vite/src/node/config.ts | 5 ++-- packages/vite/src/node/env.ts | 13 +++++----- packages/vite/src/node/optimizer/index.ts | 21 +++++++++++----- packages/vite/src/node/ssr/ssrExternal.ts | 6 ++++- packages/vite/src/node/utils.ts | 30 +++++++---------------- 5 files changed, 38 insertions(+), 37 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index e63877c1698f17..d742cdc4d00c67 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -927,8 +927,9 @@ export async function loadConfigFromFile( } else { // 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' + const pkg = lookupFile(configRoot, 'package.json') + 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..34ddf2830279b6 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -17,7 +17,6 @@ import { flattenId, getHash, isOptimizable, - lookupFile, normalizeId, normalizePath, removeDir, @@ -1199,13 +1198,23 @@ const lockfileFormats = [ { name: 'pnpm-lock.yaml', checkPatches: false }, // Included in lockfile { name: 'bun.lockb', checkPatches: true }, ] +const lockfileNames = lockfileFormats.map((l) => l.name) + +function findNearestLockfile(dir: string) { + while (dir) { + for (const fileName of lockfileNames) { + const fullPath = path.join(dir, fileName) + if (tryStatSync(fullPath)?.isFile()) return fullPath + } + const parentDir = path.dirname(dir) + if (parentDir === dir) return + + dir = parentDir + } +} export function getDepHash(config: ResolvedConfig, ssr: boolean): string { - const lockfilePath = lookupFile( - config.root, - lockfileFormats.map((l) => l.name), - { pathOnly: true }, - ) + const lockfilePath = findNearestLockfile(config.root) 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..c2d0b916df4345 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..e0fae6659a9567 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -387,28 +387,16 @@ 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, -): 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') - } - } - const parentDir = path.dirname(dir) - if ( - parentDir !== dir && - (!options?.rootDir || parentDir.startsWith(options?.rootDir)) - ) { - return lookupFile(parentDir, formats, options) +export function lookupFile(dir: string, fileName: string): string | undefined { + while (dir) { + const fullPath = path.join(dir, fileName) + if (tryStatSync(fullPath)?.isFile()) return fullPath + + const parentDir = path.dirname(dir) + if (parentDir === dir) return + + dir = parentDir } } From ced92d216bb114fda46ef6bd43b7e2f84e3aa201 Mon Sep 17 00:00:00 2001 From: patak Date: Sun, 26 Mar 2023 14:50:51 +0200 Subject: [PATCH 2/2] chore: back to lookupFile array support --- packages/vite/src/node/config.ts | 2 +- packages/vite/src/node/optimizer/index.ts | 16 ++-------------- packages/vite/src/node/ssr/ssrExternal.ts | 2 +- packages/vite/src/node/utils.ts | 12 ++++++++---- 4 files changed, 12 insertions(+), 20 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index d742cdc4d00c67..d42af1a6a74a3b 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -927,7 +927,7 @@ export async function loadConfigFromFile( } else { // check package.json for type: "module" and set `isESM` to true try { - const pkg = lookupFile(configRoot, 'package.json') + const pkg = lookupFile(configRoot, ['package.json']) isESM = !!pkg && JSON.parse(fs.readFileSync(pkg, 'utf-8')).type === 'module' } catch (e) {} diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 34ddf2830279b6..b5a052c75f5c77 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -17,6 +17,7 @@ import { flattenId, getHash, isOptimizable, + lookupFile, normalizeId, normalizePath, removeDir, @@ -1200,21 +1201,8 @@ const lockfileFormats = [ ] const lockfileNames = lockfileFormats.map((l) => l.name) -function findNearestLockfile(dir: string) { - while (dir) { - for (const fileName of lockfileNames) { - const fullPath = path.join(dir, fileName) - if (tryStatSync(fullPath)?.isFile()) return fullPath - } - const parentDir = path.dirname(dir) - if (parentDir === dir) return - - dir = parentDir - } -} - export function getDepHash(config: ResolvedConfig, ssr: boolean): string { - const lockfilePath = findNearestLockfile(config.root) + 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 c2d0b916df4345..3d8185dad88080 100644 --- a/packages/vite/src/node/ssr/ssrExternal.ts +++ b/packages/vite/src/node/ssr/ssrExternal.ts @@ -216,7 +216,7 @@ function cjsSsrCollectExternals( seen: Set, logger: Logger, ) { - const rootPkgPath = lookupFile(root, 'package.json') + const rootPkgPath = lookupFile(root, ['package.json']) if (!rootPkgPath) { return } diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index e0fae6659a9567..78b71a512705dd 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -388,11 +388,15 @@ export function tryStatSync(file: string): fs.Stats | undefined { } } -export function lookupFile(dir: string, fileName: string): string | undefined { +export function lookupFile( + dir: string, + fileNames: string[], +): string | undefined { while (dir) { - const fullPath = path.join(dir, fileName) - if (tryStatSync(fullPath)?.isFile()) return fullPath - + 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) return