@@ -5,10 +5,12 @@ import isUndefined from 'lodash/isUndefined'
55import defaults from 'lodash/defaults'
66import map from 'lodash/map'
77import get from 'lodash/get'
8+ import uniq from 'lodash/uniq'
89import toPath from 'lodash/toPath'
910import negateValue from './negateValue'
1011import { corePluginList } from '../corePluginList'
1112import configurePlugins from './configurePlugins'
13+ import defaultConfig from '../../stubs/defaultConfig.stub'
1214
1315const configUtils = {
1416 negative ( scale ) {
@@ -39,31 +41,29 @@ function value(valueToResolve, ...args) {
3941 return isFunction ( valueToResolve ) ? valueToResolve ( ...args ) : valueToResolve
4042}
4143
42- function mergeThemes ( themes ) {
43- const theme = ( ( { extend : _ , ... t } ) => t ) (
44- themes . reduce ( ( merged , t ) => {
45- return defaults ( merged , t )
46- } , { } )
47- )
44+ function collectExtends ( items ) {
45+ return items . reduce ( ( merged , { extend } ) => {
46+ return mergeWith ( merged , extend , ( mergedValue , extendValue ) => {
47+ if ( isUndefined ( mergedValue ) ) {
48+ return [ extendValue ]
49+ }
4850
51+ if ( Array . isArray ( mergedValue ) ) {
52+ return [ extendValue , ...mergedValue ]
53+ }
54+
55+ return [ extendValue , mergedValue ]
56+ } )
57+ } , { } )
58+ }
59+
60+ function mergeThemes ( themes ) {
4961 return {
50- ...theme ,
62+ ...themes . reduce ( ( merged , theme ) => defaults ( merged , theme ) , { } ) ,
5163
5264 // In order to resolve n config objects, we combine all of their `extend` properties
5365 // into arrays instead of objects so they aren't overridden.
54- extend : themes . reduce ( ( merged , { extend } ) => {
55- return mergeWith ( merged , extend , ( mergedValue , extendValue ) => {
56- if ( isUndefined ( mergedValue ) ) {
57- return [ extendValue ]
58- }
59-
60- if ( Array . isArray ( mergedValue ) ) {
61- return [ extendValue , ...mergedValue ]
62- }
63-
64- return [ extendValue , mergedValue ]
65- } )
66- } , { } ) ,
66+ extend : collectExtends ( themes ) ,
6767 }
6868}
6969
@@ -130,12 +130,8 @@ function extractPluginConfigs(configs) {
130130 return allConfigs
131131}
132132
133- function resolveVariants ( [ firstConfig , ...variantConfigs ] ) {
134- if ( Array . isArray ( firstConfig ) ) {
135- return firstConfig
136- }
137-
138- return [ firstConfig , ...variantConfigs ] . reverse ( ) . reduce ( ( resolved , variants ) => {
133+ function mergeVariants ( variants ) {
134+ const mergedVariants = variants . reduce ( ( resolved , variants ) => {
139135 Object . entries ( variants || { } ) . forEach ( ( [ plugin , pluginVariants ] ) => {
140136 if ( isFunction ( pluginVariants ) ) {
141137 resolved [ plugin ] = pluginVariants ( {
@@ -187,10 +183,39 @@ function resolveVariants([firstConfig, ...variantConfigs]) {
187183
188184 return resolved
189185 } , { } )
186+
187+ return {
188+ ...mergedVariants ,
189+ extend : collectExtends ( variants ) ,
190+ }
191+ }
192+
193+ function mergeVariantExtensions ( { extend, ...variants } , variantOrder ) {
194+ return mergeWith ( variants , extend , ( variantsValue , extensions ) => {
195+ const merged = uniq ( [ ...variantsValue , ...extensions ] . flat ( ) )
196+
197+ if ( extensions . flat ( ) . length === 0 ) {
198+ return merged
199+ }
200+
201+ return merged . sort ( ( a , z ) => variantOrder . indexOf ( a ) - variantOrder . indexOf ( z ) )
202+ } )
203+ }
204+
205+ function resolveVariants ( [ firstConfig , ...variantConfigs ] , variantOrder ) {
206+ // Global variants configuration like `variants: ['hover', 'focus']`
207+ if ( Array . isArray ( firstConfig ) ) {
208+ return firstConfig
209+ }
210+
211+ return mergeVariantExtensions (
212+ mergeVariants ( [ firstConfig , ...variantConfigs ] . reverse ( ) ) ,
213+ variantOrder
214+ )
190215}
191216
192217function resolveCorePlugins ( corePluginConfigs ) {
193- const result = [ ...corePluginConfigs ] . reverse ( ) . reduce ( ( resolved , corePluginConfig ) => {
218+ const result = [ ...corePluginConfigs ] . reduceRight ( ( resolved , corePluginConfig ) => {
194219 if ( isFunction ( corePluginConfig ) ) {
195220 return corePluginConfig ( { corePlugins : resolved } )
196221 }
@@ -201,31 +226,38 @@ function resolveCorePlugins(corePluginConfigs) {
201226}
202227
203228function resolvePluginLists ( pluginLists ) {
204- const result = [ ...pluginLists ] . reverse ( ) . reduce ( ( resolved , pluginList ) => {
229+ const result = [ ...pluginLists ] . reduceRight ( ( resolved , pluginList ) => {
205230 return [ ...resolved , ...pluginList ]
206231 } , [ ] )
207232
208233 return result
209234}
210235
211236export default function resolveConfig ( configs ) {
212- const allConfigs = extractPluginConfigs ( configs )
237+ const allConfigs = [
238+ ...extractPluginConfigs ( configs ) ,
239+ {
240+ darkMode : false ,
241+ prefix : '' ,
242+ important : false ,
243+ separator : ':' ,
244+ variantOrder : defaultConfig . variantOrder ,
245+ } ,
246+ ]
247+ const { variantOrder } = allConfigs . find ( ( c ) => c . variantOrder )
213248
214249 return defaults (
215250 {
216251 theme : resolveFunctionKeys (
217252 mergeExtensions ( mergeThemes ( map ( allConfigs , ( t ) => get ( t , 'theme' , { } ) ) ) )
218253 ) ,
219- variants : resolveVariants ( allConfigs . map ( ( c ) => c . variants ) ) ,
254+ variants : resolveVariants (
255+ allConfigs . map ( ( c ) => get ( c , 'variants' , { } ) ) ,
256+ variantOrder
257+ ) ,
220258 corePlugins : resolveCorePlugins ( allConfigs . map ( ( c ) => c . corePlugins ) ) ,
221259 plugins : resolvePluginLists ( configs . map ( ( c ) => get ( c , 'plugins' , [ ] ) ) ) ,
222260 } ,
223- ...allConfigs ,
224- {
225- darkMode : false ,
226- prefix : '' ,
227- important : false ,
228- separator : ':' ,
229- }
261+ ...allConfigs
230262 )
231263}
0 commit comments