/
define-config.ts
90 lines (80 loc) · 2.54 KB
/
define-config.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import type {
BaseTheme,
Preset,
TwindConfig,
TwindUserConfig,
ExtractThemes,
TwindPresetConfig,
} from './types'
import { asArray } from './utils'
/**
* @group Configuration
* @param param0
* @returns
*/
export function defineConfig<Theme = BaseTheme, Presets extends Preset<any>[] = Preset[]>({
presets = [] as unknown as Presets,
...userConfig
}: TwindUserConfig<Theme, Presets>): TwindConfig<BaseTheme & ExtractThemes<Theme, Presets>> {
// most user config values go first to have precendence over preset config
// only `preflight` and `theme` are applied as last preset to override all presets
let config: TwindConfig<BaseTheme & ExtractThemes<Theme, Presets>> = {
darkMode: undefined,
darkColor: undefined,
preflight: userConfig.preflight !== false && [],
theme: {},
variants: asArray(userConfig.variants),
rules: asArray(userConfig.rules),
ignorelist: asArray(userConfig.ignorelist),
hash: undefined,
stringify: (property, value) => property + ':' + value,
finalize: [],
}
for (const preset of asArray([
...presets,
{
darkMode: userConfig.darkMode,
darkColor: userConfig.darkColor,
preflight: userConfig.preflight !== false && asArray(userConfig.preflight),
theme: userConfig.theme as TwindConfig<BaseTheme & ExtractThemes<Theme, Presets>>['theme'],
hash: userConfig.hash,
stringify: userConfig.stringify,
finalize: userConfig.finalize,
} as TwindPresetConfig<Theme>,
])) {
const {
preflight,
darkMode = config.darkMode,
darkColor = config.darkColor,
theme,
variants,
rules,
ignorelist,
hash = config.hash,
stringify = config.stringify,
finalize,
} = typeof preset == 'function' ? preset(config) : (preset as TwindPresetConfig<Theme>)
config = {
// values defined by user or previous presets take precedence
preflight: config.preflight !== false &&
preflight !== false && [...config.preflight, ...asArray(preflight)],
darkMode,
darkColor,
theme: {
...config.theme,
...theme,
extend: {
...config.theme.extend,
...theme?.extend,
},
},
variants: [...config.variants, ...asArray(variants)],
rules: [...config.rules, ...asArray(rules)],
ignorelist: [...config.ignorelist, ...asArray(ignorelist)],
hash,
stringify,
finalize: [...config.finalize, ...asArray(finalize)],
} as TwindConfig<BaseTheme & ExtractThemes<Theme, Presets>>
}
return config
}