@@ -19,6 +19,35 @@ import { toPath } from '../util/toPath'
19
19
import log from '../util/log'
20
20
import negateValue from '../util/negateValue'
21
21
22
+ function parseVariantFormatString ( input ) {
23
+ if ( input . includes ( '{' ) ) {
24
+ if ( ! isBalanced ( input ) ) throw new Error ( `Your { and } are unbalanced.` )
25
+
26
+ return input
27
+ . split ( / { ( .* ) } / gim)
28
+ . flatMap ( ( line ) => parseVariantFormatString ( line ) )
29
+ . filter ( Boolean )
30
+ }
31
+
32
+ return [ input . trim ( ) ]
33
+ }
34
+
35
+ function isBalanced ( input ) {
36
+ let count = 0
37
+
38
+ for ( let char of input ) {
39
+ if ( char === '{' ) {
40
+ count ++
41
+ } else if ( char === '}' ) {
42
+ if ( -- count < 0 ) {
43
+ return false // unbalanced
44
+ }
45
+ }
46
+ }
47
+
48
+ return count === 0
49
+ }
50
+
22
51
function insertInto ( list , value , { before = [ ] } = { } ) {
23
52
before = [ ] . concat ( before )
24
53
@@ -191,12 +220,27 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
191
220
return variantFunction
192
221
}
193
222
194
- if ( ! variantFunction . startsWith ( '@' ) ) {
195
- return ( { format } ) => format ( variantFunction )
196
- }
223
+ variantFunction = variantFunction
224
+ . replace ( / \n + / g, '' )
225
+ . replace ( / \s { 1 , } / g, ' ' )
226
+ . trim ( )
227
+
228
+ let fns = parseVariantFormatString ( variantFunction )
229
+ . map ( ( str ) => {
230
+ if ( ! str . startsWith ( '@' ) ) {
231
+ return ( { format } ) => format ( str )
232
+ }
233
+
234
+ let [ , name , params ] = / @ ( .* ?) ( \( .* \) ) / g. exec ( str )
235
+ return ( { wrap } ) => wrap ( postcss . atRule ( { name, params } ) )
236
+ } )
237
+ . reverse ( )
197
238
198
- let [ , name , params ] = / @ ( .* ?) ( \( .* \) ) / g. exec ( variantFunction )
199
- return ( { wrap } ) => wrap ( postcss . atRule ( { name, params } ) )
239
+ return ( api ) => {
240
+ for ( let fn of fns ) {
241
+ fn ( api )
242
+ }
243
+ }
200
244
} )
201
245
202
246
insertInto ( variantList , variantName , options )
0 commit comments