1
1
import { existsSync , promises as fsp } from 'fs'
2
2
import os from 'os'
3
3
import { resolve , extname , dirname } from 'pathe'
4
- import createJiti from 'jiti'
4
+ import createJiti , { JITI } from 'jiti'
5
5
import * as rc9 from 'rc9'
6
- import defu from 'defu'
6
+ import { defu } from 'defu'
7
+ import type { JITIOptions } from 'jiti/dist/types'
7
8
import { DotenvOptions , setupDotenv } from './dotenv'
8
9
9
10
export interface InputConfig extends Record < string , any > { }
@@ -39,6 +40,9 @@ export interface LoadConfigOptions<T extends InputConfig=InputConfig> {
39
40
40
41
resolve ?: ( id : string , opts : LoadConfigOptions ) => null | ResolvedConfig | Promise < ResolvedConfig | null >
41
42
43
+ jiti ?: JITI
44
+ jitiOptions : JITIOptions ,
45
+
42
46
extend ?: false | {
43
47
extendKey ?: string | string [ ]
44
48
}
@@ -57,6 +61,14 @@ export async function loadConfig<T extends InputConfig=InputConfig> (opts: LoadC
57
61
}
58
62
}
59
63
64
+ // Create jiti instance
65
+ opts . jiti = opts . jiti || createJiti ( null , {
66
+ interopDefault : true ,
67
+ requireCache : false ,
68
+ esmResolve : true ,
69
+ ...opts . jitiOptions
70
+ } )
71
+
60
72
// Create context
61
73
const r : ResolvedConfig < T > = {
62
74
config : { } as any ,
@@ -164,8 +176,6 @@ const GIT_PREFIXES = ['github:', 'gitlab:', 'bitbucket:', 'https://']
164
176
// https://github.com/dword-design/package-name-regex
165
177
const NPM_PACKAGE_RE = / ^ ( @ [ a - z 0 - 9 - ~ ] [ a - z 0 - 9 - ._ ~ ] * \/ ) ? [ a - z 0 - 9 - ~ ] [ a - z 0 - 9 - ._ ~ ] * $ /
166
178
167
- const jiti = createJiti ( null , { interopDefault : true , requireCache : false , esmResolve : true } )
168
-
169
179
async function resolveConfig ( source : string , opts : LoadConfigOptions ) : Promise < ResolvedConfig > {
170
180
// Custom user resolver
171
181
if ( opts . resolve ) {
@@ -191,7 +201,7 @@ async function resolveConfig (source: string, opts: LoadConfigOptions): Promise<
191
201
// Try resolving as npm package
192
202
if ( NPM_PACKAGE_RE . test ( source ) ) {
193
203
try {
194
- source = jiti . resolve ( source , { paths : [ opts . cwd ] } )
204
+ source = opts . jiti . resolve ( source , { paths : [ opts . cwd ] } )
195
205
} catch ( _err ) { }
196
206
}
197
207
@@ -201,12 +211,12 @@ async function resolveConfig (source: string, opts: LoadConfigOptions): Promise<
201
211
if ( isDir ) { source = opts . configFile }
202
212
const res : ResolvedConfig = { config : null , cwd }
203
213
try {
204
- res . configFile = jiti . resolve ( resolve ( cwd , source ) , { paths : [ cwd ] } )
214
+ res . configFile = opts . jiti . resolve ( resolve ( cwd , source ) , { paths : [ cwd ] } )
205
215
} catch ( _err ) { }
206
216
if ( ! existsSync ( res . configFile ) ) {
207
217
return res
208
218
}
209
- res . config = jiti ( res . configFile )
219
+ res . config = opts . jiti ( res . configFile )
210
220
if ( typeof res . config === 'function' ) {
211
221
res . config = await res . config ( )
212
222
}
0 commit comments