Skip to content

Commit bfd1be5

Browse files
committed
feat: custom jiti and jitiOptions
1 parent 58a7702 commit bfd1be5

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ Specify default configuration. It has the **lowest** priority.
9999

100100
Specify override configuration. It has the **highest** priority.
101101

102+
### `jiti`
103+
104+
Custom [unjs/jiti](https://github.com/unjs/jiti) instance used to import configuration files.
105+
106+
### `jitiOptions`
107+
108+
Custom [unjs/jiti](https://github.com/unjs/jiti) options to import configuration files.
109+
102110
## Extending configuration
103111

104112
If resolved config contains a `extends` key, it will be used to extend configuration.

src/loader.ts

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { existsSync, promises as fsp } from 'fs'
22
import os from 'os'
33
import { resolve, extname, dirname } from 'pathe'
4-
import createJiti from 'jiti'
4+
import createJiti, { JITI } from 'jiti'
55
import * as rc9 from 'rc9'
6-
import defu from 'defu'
6+
import { defu } from 'defu'
7+
import type { JITIOptions } from 'jiti/dist/types'
78
import { DotenvOptions, setupDotenv } from './dotenv'
89

910
export interface InputConfig extends Record<string, any> {}
@@ -39,6 +40,9 @@ export interface LoadConfigOptions<T extends InputConfig=InputConfig> {
3940

4041
resolve?: (id: string, opts: LoadConfigOptions) => null | ResolvedConfig | Promise<ResolvedConfig | null>
4142

43+
jiti?: JITI
44+
jitiOptions: JITIOptions,
45+
4246
extend?: false | {
4347
extendKey?: string | string[]
4448
}
@@ -57,6 +61,14 @@ export async function loadConfig<T extends InputConfig=InputConfig> (opts: LoadC
5761
}
5862
}
5963

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+
6072
// Create context
6173
const r: ResolvedConfig<T> = {
6274
config: {} as any,
@@ -164,8 +176,6 @@ const GIT_PREFIXES = ['github:', 'gitlab:', 'bitbucket:', 'https://']
164176
// https://github.com/dword-design/package-name-regex
165177
const NPM_PACKAGE_RE = /^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/
166178

167-
const jiti = createJiti(null, { interopDefault: true, requireCache: false, esmResolve: true })
168-
169179
async function resolveConfig (source: string, opts: LoadConfigOptions): Promise<ResolvedConfig> {
170180
// Custom user resolver
171181
if (opts.resolve) {
@@ -191,7 +201,7 @@ async function resolveConfig (source: string, opts: LoadConfigOptions): Promise<
191201
// Try resolving as npm package
192202
if (NPM_PACKAGE_RE.test(source)) {
193203
try {
194-
source = jiti.resolve(source, { paths: [opts.cwd] })
204+
source = opts.jiti.resolve(source, { paths: [opts.cwd] })
195205
} catch (_err) {}
196206
}
197207

@@ -201,12 +211,12 @@ async function resolveConfig (source: string, opts: LoadConfigOptions): Promise<
201211
if (isDir) { source = opts.configFile }
202212
const res: ResolvedConfig = { config: null, cwd }
203213
try {
204-
res.configFile = jiti.resolve(resolve(cwd, source), { paths: [cwd] })
214+
res.configFile = opts.jiti.resolve(resolve(cwd, source), { paths: [cwd] })
205215
} catch (_err) { }
206216
if (!existsSync(res.configFile)) {
207217
return res
208218
}
209-
res.config = jiti(res.configFile)
219+
res.config = opts.jiti(res.configFile)
210220
if (typeof res.config === 'function') {
211221
res.config = await res.config()
212222
}

0 commit comments

Comments
 (0)