@@ -33,7 +33,7 @@ function createDoc(doc: string) {
33
33
}
34
34
}
35
35
36
- const fmtToTypewind = ( s : string ) => s . replace ( / - / g, '_' ) . replace ( / \@ / , '$' ) ;
36
+ const fmtToTypewind = ( s : string ) => s . replace ( / - / g, '_' ) . replace ( / ^ \@ / , '$' ) ;
37
37
38
38
const objectTemplate = (
39
39
props : [ prop : string , type : string , doc ? : string ] [ ]
@@ -62,10 +62,15 @@ const rootTypeTemplate = (
62
62
63
63
${ others . join ( '\n' ) }
64
64
65
+ type OpacityMap = {
66
+ [K in Opacity]: Property;
67
+ } & Record<string, Property>;
68
+ type Colors = {
69
+ [K in {} as \`\${_Colors}\`]: OpacityMap
70
+ }
71
+
65
72
type Typewind = ${ types . join ( ' & ' ) } & {
66
- ${ modifiers
67
- . map ( ( variant ) => `${ variant } (style: Property): Property` )
68
- . join ( ';' ) }
73
+ ${ modifiers . map ( variant => `${ variant } (style: Property): Property` ) . join ( ';' ) }
69
74
} & {
70
75
[arbitraryVariant: string]: (style: Property) => Property;
71
76
} & {
@@ -103,18 +108,40 @@ export async function generateTypes() {
103
108
s => ! s . startsWith ( '-' )
104
109
) ;
105
110
111
+ const opacityMap = ctx . tailwindConfig . theme . opacity ;
112
+
106
113
const classesWithStandardSyntax = classList . filter ( s => ! / \. | \/ / . test ( s ) ) ;
114
+ const classesWithCandidateItem = classesWithStandardSyntax . map ( s => {
115
+ return [ s , getCandidateItem ( ctx . candidateRuleMap , s ) ] as const ;
116
+ } ) ;
117
+
118
+ const colorSet = new Set < string > ( ) ;
107
119
const standard = typeTemplate (
108
120
'Standard' ,
109
- classesWithStandardSyntax . map ( s => {
110
- let { rule : rules , rest } = getCandidateItem ( ctx . candidateRuleMap , s ) ;
121
+ classesWithCandidateItem . map ( ( [ s , { rule : rules , rest } ] ) => {
111
122
let css = '' ;
112
123
113
124
if ( rules ) {
114
125
for ( const rule of rules ) {
115
- const [ _info , ruleOrFn ] = rule ;
126
+ const [ info , ruleOrFn ] = rule ;
116
127
117
128
if ( typeof ruleOrFn === 'function' ) {
129
+ const types = info . options . types ;
130
+ const isColor = types . some (
131
+ ( t : Record < string , string > ) => t . type == 'color'
132
+ ) ;
133
+
134
+ if ( isColor ) {
135
+ if ( rest ) {
136
+ const key = fmtToTypewind ( s ) + '$' ;
137
+
138
+ colorSet . add ( key ) ;
139
+ for ( const opacity in opacityMap ) {
140
+ // colorSet.add(key + opacity);
141
+ }
142
+ }
143
+ }
144
+
118
145
const [ ruleSet ] = ruleOrFn ( rest ?? 'DEFAULT' , { } ) ;
119
146
if ( ruleSet ) {
120
147
css += fmtRuleToCss ( ruleSet ) ;
@@ -129,6 +156,10 @@ export async function generateTypes() {
129
156
return [ fmtToTypewind ( s ) , 'Property' , css ] ;
130
157
} )
131
158
) ;
159
+ // const colorTemplate = typeTemplate(
160
+ // '_Colors',
161
+ // [...colorSet].map(color => [color, 'Property'])
162
+ // );
132
163
const candidates = [ ...ctx . candidateRuleMap . entries ( ) ] ;
133
164
const arbitraryStyles : [ string , string , string ?] [ ] = [ ] ;
134
165
for ( const [ name , rules ] of candidates ) {
@@ -158,18 +189,26 @@ export async function generateTypes() {
158
189
const arbitrary = typeTemplate ( 'Arbitrary' , arbitraryStyles ) ;
159
190
160
191
const variants = `type Variants = ${[ ...ctx . variantMap . keys ( ) ]
161
- . map ( ( variant ) => `'${ variant } '` )
192
+ . map ( variant => `'${ variant } '` )
162
193
. join ( ' | ' ) } ; `;
163
194
164
- const modifiers = [...ctx.variantMap.keys(), 'important'].map((s) => {
195
+ const modifiers = [...ctx.variantMap.keys(), 'important'].map(s => {
165
196
s = /^\d/.test(s) ? ` _$ { s } ` : s;
166
197
167
198
return fmtToTypewind(s);
168
199
});
169
200
170
201
const root = rootTypeTemplate(
171
- [variants, standard, arbitrary],
172
- ['Standard', 'Arbitrary'],
202
+ [
203
+ variants,
204
+ standard,
205
+ arbitrary,
206
+ ` type _Colors = $ { [ ...colorSet ] . map ( k => JSON . stringify ( k ) ) . join ( ' | ' ) } `,
207
+ ` type Opacity = $ { Object . keys ( opacityMap )
208
+ . map ( k => JSON . stringify ( k ) )
209
+ . join ( ' | ' ) } `,
210
+ ],
211
+ ['Standard', 'Arbitrary', 'Variants', 'Colors'],
173
212
modifiers
174
213
);
175
214
0 commit comments