/
index.ts
67 lines (60 loc) · 1.58 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import fs from 'fs'
import { dirname, resolve } from 'pathe'
import type { UserConfig } from '@unocss/core'
import type { LoadConfigResult, LoadConfigSource } from 'unconfig'
import { createConfigLoader as createLoader } from 'unconfig'
export type { LoadConfigResult, LoadConfigSource }
export async function loadConfig<U extends UserConfig>(
cwd = process.cwd(),
configOrPath: string | U = cwd,
extraConfigSources: LoadConfigSource[] = [],
): Promise<LoadConfigResult<U>> {
let inlineConfig = {} as U
if (typeof configOrPath !== 'string') {
inlineConfig = configOrPath
if (inlineConfig.configFile === false) {
return {
config: inlineConfig as U,
sources: [],
}
}
else {
configOrPath = inlineConfig.configFile || process.cwd()
}
}
const resolved = resolve(configOrPath)
let isFile = false
if (fs.existsSync(resolved) && fs.statSync(resolved).isFile()) {
isFile = true
cwd = dirname(resolved)
}
const loader = createLoader<U>({
sources: isFile
? [
{
files: resolved,
extensions: [],
},
]
: [
{
files: [
'unocss.config',
'uno.config',
],
},
...extraConfigSources,
],
cwd,
defaults: inlineConfig,
})
const result = await loader.load()
result.config = result.config || inlineConfig
if (result.config.configDeps) {
result.sources = [
...result.sources,
...result.config.configDeps.map(i => resolve(cwd, i)),
]
}
return result
}