@@ -60,6 +60,7 @@ import {
60
60
FeatureFlags ,
61
61
Settings ,
62
62
ClassNames ,
63
+ Variant ,
63
64
} from 'tailwindcss-language-service/src/util/state'
64
65
import {
65
66
provideDiagnostics ,
@@ -1146,105 +1147,116 @@ function isAtRule(node: Node): node is AtRule {
1146
1147
return node . type === 'atrule'
1147
1148
}
1148
1149
1149
- function getVariants ( state : State ) : Record < string , string > {
1150
- if ( state . jit ) {
1151
- function escape ( className : string ) : string {
1152
- let node = state . modules . postcssSelectorParser . module . className ( )
1153
- node . value = className
1154
- return dlv ( node , 'raws.value' , node . value )
1155
- }
1150
+ function getVariants ( state : State ) : Array < Variant > {
1151
+ if ( state . jitContext ?. getVariants ) {
1152
+ return state . jitContext . getVariants ( )
1153
+ }
1156
1154
1157
- let result = { }
1155
+ if ( state . jit ) {
1156
+ let result : Array < Variant > = [ ]
1158
1157
// [name, [sort, fn]]
1159
1158
// [name, [[sort, fn]]]
1160
1159
Array . from ( state . jitContext . variantMap as Map < string , [ any , any ] > ) . forEach (
1161
1160
( [ variantName , variantFnOrFns ] ) => {
1162
- let fns = ( Array . isArray ( variantFnOrFns [ 0 ] ) ? variantFnOrFns : [ variantFnOrFns ] ) . map (
1163
- ( [ _sort , fn ] ) => fn
1164
- )
1161
+ result . push ( {
1162
+ name : variantName ,
1163
+ values : [ ] ,
1164
+ isArbitrary : false ,
1165
+ selectors : ( ) => {
1166
+ function escape ( className : string ) : string {
1167
+ let node = state . modules . postcssSelectorParser . module . className ( )
1168
+ node . value = className
1169
+ return dlv ( node , 'raws.value' , node . value )
1170
+ }
1165
1171
1166
- let placeholder = '__variant_placeholder__'
1172
+ let fns = ( Array . isArray ( variantFnOrFns [ 0 ] ) ? variantFnOrFns : [ variantFnOrFns ] ) . map (
1173
+ ( [ _sort , fn ] ) => fn
1174
+ )
1167
1175
1168
- let root = state . modules . postcss . module . root ( {
1169
- nodes : [
1170
- state . modules . postcss . module . rule ( {
1171
- selector : `.${ escape ( placeholder ) } ` ,
1172
- nodes : [ ] ,
1173
- } ) ,
1174
- ] ,
1175
- } )
1176
+ let placeholder = '__variant_placeholder__'
1176
1177
1177
- let classNameParser = state . modules . postcssSelectorParser . module ( ( selectors ) => {
1178
- return selectors . first . filter ( ( { type } ) => type === 'class' ) . pop ( ) . value
1179
- } )
1178
+ let root = state . modules . postcss . module . root ( {
1179
+ nodes : [
1180
+ state . modules . postcss . module . rule ( {
1181
+ selector : `.${ escape ( placeholder ) } ` ,
1182
+ nodes : [ ] ,
1183
+ } ) ,
1184
+ ] ,
1185
+ } )
1180
1186
1181
- function getClassNameFromSelector ( selector ) {
1182
- return classNameParser . transformSync ( selector )
1183
- }
1187
+ let classNameParser = state . modules . postcssSelectorParser . module ( ( selectors ) => {
1188
+ return selectors . first . filter ( ( { type } ) => type === 'class' ) . pop ( ) . value
1189
+ } )
1184
1190
1185
- function modifySelectors ( modifierFunction ) {
1186
- root . each ( ( rule ) => {
1187
- if ( rule . type !== 'rule' ) {
1188
- return
1191
+ function getClassNameFromSelector ( selector ) {
1192
+ return classNameParser . transformSync ( selector )
1189
1193
}
1190
1194
1191
- rule . selectors = rule . selectors . map ( ( selector ) => {
1192
- return modifierFunction ( {
1193
- get className ( ) {
1194
- return getClassNameFromSelector ( selector )
1195
+ function modifySelectors ( modifierFunction ) {
1196
+ root . each ( ( rule ) => {
1197
+ if ( rule . type !== 'rule' ) {
1198
+ return
1199
+ }
1200
+
1201
+ rule . selectors = rule . selectors . map ( ( selector ) => {
1202
+ return modifierFunction ( {
1203
+ get className ( ) {
1204
+ return getClassNameFromSelector ( selector )
1205
+ } ,
1206
+ selector,
1207
+ } )
1208
+ } )
1209
+ } )
1210
+ return root
1211
+ }
1212
+
1213
+ let definitions = [ ]
1214
+
1215
+ for ( let fn of fns ) {
1216
+ let definition : string
1217
+ let container = root . clone ( )
1218
+ let returnValue = fn ( {
1219
+ container,
1220
+ separator : state . separator ,
1221
+ modifySelectors,
1222
+ format : ( def : string ) => {
1223
+ definition = def . replace ( / : m e r g e \( ( [ ^ ) ] + ) \) / g, '$1' )
1224
+ } ,
1225
+ wrap : ( rule : Container ) => {
1226
+ if ( isAtRule ( rule ) ) {
1227
+ definition = `@${ rule . name } ${ rule . params } `
1228
+ }
1195
1229
} ,
1196
- selector,
1197
1230
} )
1198
- } )
1199
- } )
1200
- return root
1201
- }
1202
1231
1203
- let definitions = [ ]
1204
-
1205
- for ( let fn of fns ) {
1206
- let definition : string
1207
- let container = root . clone ( )
1208
- let returnValue = fn ( {
1209
- container,
1210
- separator : state . separator ,
1211
- modifySelectors,
1212
- format : ( def : string ) => {
1213
- definition = def . replace ( / : m e r g e \( ( [ ^ ) ] + ) \) / g, '$1' )
1214
- } ,
1215
- wrap : ( rule : Container ) => {
1216
- if ( isAtRule ( rule ) ) {
1217
- definition = `@${ rule . name } ${ rule . params } `
1232
+ if ( ! definition ) {
1233
+ definition = returnValue
1218
1234
}
1219
- } ,
1220
- } )
1221
-
1222
- if ( ! definition ) {
1223
- definition = returnValue
1224
- }
1225
1235
1226
- if ( definition ) {
1227
- definitions . push ( definition )
1228
- continue
1229
- }
1236
+ if ( definition ) {
1237
+ definitions . push ( definition )
1238
+ continue
1239
+ }
1230
1240
1231
- container . walkDecls ( ( decl ) => {
1232
- decl . remove ( )
1233
- } )
1241
+ container . walkDecls ( ( decl ) => {
1242
+ decl . remove ( )
1243
+ } )
1234
1244
1235
- definition = container
1236
- . toString ( )
1237
- . replace ( `.${ escape ( `${ variantName } :${ placeholder } ` ) } ` , '&' )
1238
- . replace ( / (?< ! \\ ) [ { } ] / g, '' )
1239
- . replace ( / \s * \n \s * / g, ' ' )
1240
- . trim ( )
1245
+ definition = container
1246
+ . toString ( )
1247
+ . replace ( `.${ escape ( `${ variantName } :${ placeholder } ` ) } ` , '&' )
1248
+ . replace ( / (?< ! \\ ) [ { } ] / g, '' )
1249
+ . replace ( / \s * \n \s * / g, ' ' )
1250
+ . trim ( )
1241
1251
1242
- if ( ! definition . includes ( placeholder ) ) {
1243
- definitions . push ( definition )
1244
- }
1245
- }
1252
+ if ( ! definition . includes ( placeholder ) ) {
1253
+ definitions . push ( definition )
1254
+ }
1255
+ }
1246
1256
1247
- result [ variantName ] = definitions . join ( ', ' ) || null
1257
+ return definitions
1258
+ } ,
1259
+ } )
1248
1260
}
1249
1261
)
1250
1262
@@ -1276,7 +1288,12 @@ function getVariants(state: State): Record<string, string> {
1276
1288
} )
1277
1289
} )
1278
1290
1279
- return variants . reduce ( ( obj , variant ) => ( { ...obj , [ variant ] : null } ) , { } )
1291
+ return variants . map ( ( variant ) => ( {
1292
+ name : variant ,
1293
+ values : [ ] ,
1294
+ isArbitrary : false ,
1295
+ selectors : ( ) => [ ] ,
1296
+ } ) )
1280
1297
}
1281
1298
1282
1299
async function getPlugins ( config : any ) {
0 commit comments