1
1
import { existsSync } from "node:fs" ;
2
2
import { readFile , rm } from "node:fs/promises" ;
3
+ import { pathToFileURL } from "node:url" ;
3
4
import { homedir } from "node:os" ;
4
5
import { resolve , extname , dirname , basename , join } from "pathe" ;
6
+ import { resolveModulePath } from "exsolve" ;
5
7
import { createJiti } from "jiti" ;
6
- import { fileURLToPath } from "mlly" ;
7
8
import * as rc9 from "rc9" ;
8
9
import { defu } from "defu" ;
9
10
import { findWorkspaceDir , readPackageJSON } from "pkg-types" ;
@@ -31,7 +32,7 @@ const ASYNC_LOADERS = {
31
32
".toml" : ( ) => import ( "confbox/toml" ) . then ( ( r ) => r . parseTOML ) ,
32
33
} as const ;
33
34
34
- export const SUPPORTED_EXTENSIONS = [
35
+ export const SUPPORTED_EXTENSIONS = Object . freeze ( [
35
36
// with jiti
36
37
".js" ,
37
38
".ts" ,
@@ -46,7 +47,7 @@ export const SUPPORTED_EXTENSIONS = [
46
47
".yaml" ,
47
48
".yml" ,
48
49
".toml" ,
49
- ] as const ;
50
+ ] ) as unknown as string [ ] ;
50
51
51
52
export async function loadConfig <
52
53
T extends UserInputConfig = UserInputConfig ,
@@ -355,15 +356,9 @@ async function resolveConfig<
355
356
source = cloned . dir ;
356
357
}
357
358
358
- // Util to try resolving a module
359
- const tryResolve = ( id : string ) => {
360
- const resolved = options . jiti ! . esmResolve ( id , { try : true } ) ;
361
- return resolved ? fileURLToPath ( resolved ) : undefined ;
362
- } ;
363
-
364
359
// Try resolving as npm package
365
360
if ( NPM_PACKAGE_RE . test ( source ) ) {
366
- source = tryResolve ( source ) || source ;
361
+ source = tryResolve ( source , options ) || source ;
367
362
}
368
363
369
364
// Import from local fs
@@ -382,9 +377,12 @@ async function resolveConfig<
382
377
} ;
383
378
384
379
res . configFile =
385
- tryResolve ( resolve ( cwd , source ) ) ||
386
- tryResolve ( resolve ( cwd , ".config" , source . replace ( / \. c o n f i g $ / , "" ) ) ) ||
387
- tryResolve ( resolve ( cwd , ".config" , source ) ) ||
380
+ tryResolve ( resolve ( cwd , source ) , options ) ||
381
+ tryResolve (
382
+ resolve ( cwd , ".config" , source . replace ( / \. c o n f i g $ / , "" ) ) ,
383
+ options ,
384
+ ) ||
385
+ tryResolve ( resolve ( cwd , ".config" , source ) , options ) ||
388
386
source ;
389
387
390
388
if ( ! existsSync ( res . configFile ! ) ) {
@@ -432,3 +430,15 @@ async function resolveConfig<
432
430
433
431
return res ;
434
432
}
433
+
434
+ // --- internal ---
435
+
436
+ function tryResolve ( id : string , options : LoadConfigOptions < any , any > ) {
437
+ return resolveModulePath ( id , {
438
+ try : true ,
439
+ from : pathToFileURL ( join ( options . cwd || "." , options . configFile || "/" ) ) ,
440
+ suffixes : [ "" , "/index" ] ,
441
+ extensions : SUPPORTED_EXTENSIONS ,
442
+ cache : false ,
443
+ } ) ;
444
+ }
0 commit comments