File tree 5 files changed +24
-31
lines changed
5 files changed +24
-31
lines changed Original file line number Diff line number Diff line change @@ -928,7 +928,8 @@ export async function loadConfigFromFile(
928
928
// check package.json for type: "module" and set `isESM` to true
929
929
try {
930
930
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'
932
933
} catch ( e ) { }
933
934
}
934
935
Original file line number Diff line number Diff line change 1
1
import fs from 'node:fs'
2
+ import path from 'node:path'
2
3
import { parse } from 'dotenv'
3
4
import { expand } from 'dotenv-expand'
4
- import { arraify , lookupFile } from './utils'
5
+ import { arraify , tryStatSync } from './utils'
5
6
import type { UserConfig } from './config'
6
7
7
8
export function loadEnv (
@@ -26,12 +27,10 @@ export function loadEnv(
26
27
27
28
const parsed = Object . fromEntries (
28
29
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 ) ) )
35
34
} ) ,
36
35
)
37
36
Original file line number Diff line number Diff line change @@ -1186,13 +1186,10 @@ const lockfileFormats = [
1186
1186
{ name : 'pnpm-lock.yaml' , checkPatches : false } , // Included in lockfile
1187
1187
{ name : 'bun.lockb' , checkPatches : true } ,
1188
1188
]
1189
+ const lockfileNames = lockfileFormats . map ( ( l ) => l . name )
1189
1190
1190
1191
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 )
1196
1193
let content = lockfilePath ? fs . readFileSync ( lockfilePath , 'utf-8' ) : ''
1197
1194
if ( lockfilePath ) {
1198
1195
const lockfileName = path . basename ( lockfilePath )
Original file line number Diff line number Diff line change @@ -217,7 +217,11 @@ function cjsSsrCollectExternals(
217
217
seen : Set < string > ,
218
218
logger : Logger ,
219
219
) {
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' )
221
225
if ( ! rootPkgContent ) {
222
226
return
223
227
}
Original file line number Diff line number Diff line change @@ -390,28 +390,20 @@ export function tryStatSync(file: string): fs.Stats | undefined {
390
390
// Ignore errors
391
391
}
392
392
}
393
- interface LookupFileOptions {
394
- pathOnly ?: boolean
395
- rootDir ?: string
396
- }
397
393
398
394
export function lookupFile (
399
395
dir : string ,
400
- formats : string [ ] ,
401
- options ?: LookupFileOptions ,
396
+ fileNames : string [ ] ,
402
397
) : 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
407
402
}
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
415
407
}
416
408
}
417
409
You can’t perform that action at this time.
0 commit comments