1
1
var _ = require ( 'lodash' )
2
2
var flatten = require ( 'flat' )
3
3
4
+
5
+ const FLATTEN_CONFIG = { delimiter : '-' , maxDepth : 2 }
6
+ const getName = name => name . split ( '-default' ) . join ( '' )
7
+
8
+
4
9
module . exports = function ( ) {
5
10
return function ( {
6
11
addUtilities, addComponents, addBase, addVariant,
7
12
e, prefix, theme, variants, config,
8
13
} ) {
9
- const buildObjectFromTheme = themeKey => {
14
+ const buildObjectFromTheme = ( themeKey , ... fallbackKeys ) => {
10
15
const buildObject = ( [ modifier , value ] ) => [ modifier , { [ themeKey ] : value } ]
11
- const themeEntries = Object . entries ( theme ( themeKey , { } ) ) . map ( entry => buildObject ( entry ) )
16
+ const getThemeSettings = ( themeKey , fallbackKeys ) => {
17
+ return theme ( themeKey , false ) || getThemeSettings ( [ themeKey , ...fallbackKeys ] = fallbackKeys )
18
+ }
19
+ const themeEntries = Object
20
+ . entries ( flatten ( getThemeSettings ( themeKey , fallbackKeys ) , FLATTEN_CONFIG ) )
21
+ . map ( entry => buildObject ( entry ) )
22
+
12
23
return _ . fromPairs ( themeEntries )
13
24
}
14
25
@@ -25,13 +36,14 @@ module.exports = function () {
25
36
Object . entries ( pluginUtilities )
26
37
. filter ( ( [ modifier , values ] ) => ! _ . isEmpty ( values ) )
27
38
. forEach ( ( [ modifier , values ] ) => {
39
+ const className = _ . kebabCase ( modifier )
28
40
const variantName = Object . keys ( Object . entries ( values ) [ 0 ] [ 1 ] ) [ 0 ]
29
- const utilities = flatten (
30
- { [ `.${ e ( `bg-${ modifier } ` ) } ` ] : values } ,
31
- { delimiter : '-' , maxDepth : 2 } ,
32
- )
41
+ const utilities = flatten ( { [ `.${ e ( `${ className } ` ) } ` ] : values } , FLATTEN_CONFIG )
33
42
34
- addUtilities ( utilities , variants ( variantName , [ 'responsive' ] ) )
43
+ addUtilities (
44
+ _ . mapKeys ( utilities , ( value , key ) => getName ( key ) ) ,
45
+ variants ( variantName , [ 'responsive' ] )
46
+ )
35
47
} )
36
48
}
37
49
}
0 commit comments