@@ -82,81 +82,96 @@ function parseRules(rule, cache, options = {}) {
8282 return [ cache . get ( rule ) , options ]
8383}
8484
85- function generateRules ( tailwindConfig , candidates , context ) {
86- let { candidateRuleMap, classCache, notClassCache, postCssNodeCache } = context
87- let allRules = [ ]
85+ function resolveMatchedPlugins ( classCandidate , context ) {
86+ if ( context . candidateRuleMap . has ( classCandidate ) ) {
87+ return [ context . candidateRuleMap . get ( classCandidate ) , 'DEFAULT' ]
88+ }
8889
89- for ( let candidate of candidates ) {
90- if ( notClassCache . has ( candidate ) ) {
91- continue
92- }
90+ let candidatePrefix = classCandidate
91+ let negative = false
9392
94- if ( classCache . has ( candidate ) ) {
95- allRules . push ( classCache . get ( candidate ) )
96- continue
93+ if ( candidatePrefix [ 0 ] === '-' ) {
94+ negative = true
95+ candidatePrefix = candidatePrefix . slice ( 1 )
96+ }
97+
98+ for ( let [ prefix , modifier ] of candidatePermutations ( candidatePrefix ) ) {
99+ if ( context . candidateRuleMap . has ( prefix ) ) {
100+ return [ context . candidateRuleMap . get ( prefix ) , negative ? `-${ modifier } ` : modifier ]
97101 }
102+ }
98103
99- let [ classCandidate , ... variants ] = candidate . split ( ':' ) . reverse ( )
100- let matchedPlugins = null
104+ return null
105+ }
101106
102- if ( candidateRuleMap . has ( classCandidate ) ) {
103- matchedPlugins = [ candidateRuleMap . get ( classCandidate ) , 'DEFAULT' ]
104- } else {
105- let candidatePrefix = classCandidate
106- let negative = false
107+ function resolveMatches ( candidate , context ) {
108+ let [ classCandidate , ...variants ] = candidate . split ( ':' ) . reverse ( )
109+ let matchedPlugins = resolveMatchedPlugins ( classCandidate , context )
107110
108- if ( candidatePrefix [ 0 ] === '-' ) {
109- negative = true
110- candidatePrefix = candidatePrefix . slice ( 1 )
111- }
111+ if ( matchedPlugins === null ) {
112+ return [ ]
113+ }
114+
115+ let pluginHelpers = {
116+ candidate : classCandidate ,
117+ theme : context . tailwindConfig . theme ,
118+ }
112119
113- for ( let [ prefix , modifier ] of candidatePermutations ( candidatePrefix ) ) {
114- if ( candidateRuleMap . has ( prefix ) ) {
115- matchedPlugins = [ candidateRuleMap . get ( prefix ) , negative ? `-${ modifier } ` : modifier ]
116- break
120+ let matches = [ ]
121+ let [ plugins , modifier ] = matchedPlugins
122+
123+ for ( let [ sort , plugin ] of plugins ) {
124+ if ( typeof plugin === 'function' ) {
125+ for ( let ruleSet of [ ] . concat ( plugin ( modifier , pluginHelpers ) ) ) {
126+ let [ rules , options ] = parseRules ( ruleSet , context . postCssNodeCache )
127+ for ( let rule of rules ) {
128+ matches . push ( [ { ...sort , options } , rule ] )
117129 }
118130 }
131+ } else {
132+ let ruleSet = plugin
133+ let [ rules , options ] = parseRules ( ruleSet , context . postCssNodeCache )
134+ for ( let rule of rules ) {
135+ matches . push ( [ { ...sort , options } , rule ] )
136+ }
119137 }
138+ }
139+
140+ for ( let variant of variants ) {
141+ matches = applyVariant ( variant , matches , context )
142+ }
143+
144+ return matches
145+ }
146+
147+ function generateRules ( candidates , context ) {
148+ let allRules = [ ]
120149
121- if ( matchedPlugins === null ) {
122- notClassCache . add ( candidate )
150+ for ( let candidate of candidates ) {
151+ if ( context . notClassCache . has ( candidate ) ) {
123152 continue
124153 }
125154
126- let pluginHelpers = {
127- candidate : classCandidate ,
128- theme : tailwindConfig . theme ,
155+ if ( context . classCache . has ( candidate ) ) {
156+ allRules . push ( context . classCache . get ( candidate ) )
157+ continue
129158 }
130159
131- let matches = [ ]
132- let [ plugins , modifier ] = matchedPlugins
160+ let matches = resolveMatches ( candidate , context )
133161
134- for ( let [ sort , plugin ] of plugins ) {
135- if ( typeof plugin === 'function' ) {
136- for ( let ruleSet of [ ] . concat ( plugin ( modifier , pluginHelpers ) ) ) {
137- let [ rules , options ] = parseRules ( ruleSet , context . newPostCssNodeCache )
138- for ( let rule of rules ) {
139- matches . push ( [ { ...sort , options } , rule ] )
140- }
141- }
142- } else {
143- let ruleSet = plugin
144- let [ rules , options ] = parseRules ( ruleSet , context . newPostCssNodeCache )
145- for ( let rule of rules ) {
146- matches . push ( [ { ...sort , options } , rule ] )
147- }
148- }
149- }
150-
151- for ( let variant of variants ) {
152- matches = applyVariant ( variant , matches , context )
162+ if ( matches . length === 0 ) {
163+ context . notClassCache . add ( candidate )
164+ continue
153165 }
154166
155- classCache . set ( candidate , matches )
167+ context . classCache . set ( candidate , matches )
156168 allRules . push ( matches )
157169 }
158170
159171 return allRules . flat ( 1 ) . map ( ( [ { sort, layer } , rule ] ) => [ sort | context . layerOrder [ layer ] , rule ] )
160172}
161173
162- module . exports = generateRules
174+ module . exports = {
175+ resolveMatches,
176+ generateRules,
177+ }
0 commit comments