forked from tailwindlabs/tailwindcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolveConfig.js
More file actions
147 lines (125 loc) · 3.76 KB
/
resolveConfig.js
File metadata and controls
147 lines (125 loc) · 3.76 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import some from 'lodash/some'
import mergeWith from 'lodash/mergeWith'
import isFunction from 'lodash/isFunction'
import isUndefined from 'lodash/isUndefined'
import defaults from 'lodash/defaults'
import map from 'lodash/map'
import get from 'lodash/get'
import toPath from 'lodash/toPath'
import negateValue from './negateValue'
const configUtils = {
negative(scale) {
return Object.keys(scale)
.filter(key => scale[key] !== '0')
.reduce(
(negativeScale, key) => ({
...negativeScale,
[`-${key}`]: negateValue(scale[key]),
}),
{}
)
},
breakpoints(screens) {
return Object.keys(screens)
.filter(key => typeof screens[key] === 'string')
.reduce(
(breakpoints, key) => ({
...breakpoints,
[`screen-${key}`]: screens[key],
}),
{}
)
},
}
function value(valueToResolve, ...args) {
return isFunction(valueToResolve) ? valueToResolve(...args) : valueToResolve
}
function mergeThemes(themes) {
const theme = (({ extend: _, ...t }) => t)(
themes.reduce((merged, t) => {
return defaults(merged, t)
}, {})
)
return {
...theme,
// In order to resolve n config objects, we combine all of their `extend` properties
// into arrays instead of objects so they aren't overridden.
extend: themes.reduce((merged, { extend }) => {
return mergeWith(merged, extend, (mergedValue, extendValue) => {
if (isUndefined(mergedValue)) {
return [extendValue]
}
if (Array.isArray(mergedValue)) {
return [extendValue, ...mergedValue]
}
return [extendValue, mergedValue]
})
}, {}),
}
}
function mergeExtensions({ extend, ...theme }) {
return mergeWith(theme, extend, (themeValue, extensions) => {
// The `extend` property is an array, so we need to check if it contains any functions
if (!isFunction(themeValue) && !some(extensions, isFunction)) {
return {
...themeValue,
...Object.assign({}, ...extensions),
}
}
return (resolveThemePath, utils) => ({
...value(themeValue, resolveThemePath, utils),
...Object.assign({}, ...extensions.map(e => value(e, resolveThemePath, utils))),
})
})
}
function resolveFunctionKeys(object) {
const resolveThemePath = (key, defaultValue) => {
const path = toPath(key)
let index = 0
let val = object
while (val !== undefined && val !== null && index < path.length) {
val = val[path[index++]]
val = isFunction(val) ? val(resolveThemePath, configUtils) : val
}
return val === undefined ? defaultValue : val
}
return Object.keys(object).reduce((resolved, key) => {
return {
...resolved,
[key]: isFunction(object[key]) ? object[key](resolveThemePath, configUtils) : object[key],
}
}, {})
}
function extractPluginConfigs(configs) {
let allConfigs = []
configs.forEach(config => {
allConfigs = [...allConfigs, config]
const plugins = get(config, 'plugins', [])
if (plugins.length === 0) {
return
}
plugins.forEach(plugin => {
if (plugin.__isOptionsFunction) {
plugin = plugin()
}
allConfigs = [...allConfigs, ...extractPluginConfigs([get(plugin, 'config', {})])]
})
})
return allConfigs
}
export default function resolveConfig(configs) {
const allConfigs = extractPluginConfigs(configs)
return defaults(
{
theme: resolveFunctionKeys(
mergeExtensions(mergeThemes(map(allConfigs, t => get(t, 'theme', {}))))
),
variants: (firstVariants => {
return Array.isArray(firstVariants)
? firstVariants
: defaults({}, ...map(allConfigs, 'variants'))
})(defaults({}, ...map(allConfigs)).variants),
},
...allConfigs
)
}