@@ -1598,6 +1598,33 @@ function processConfig(config: CrosswindConfig): ProcessedConfig {
15981598/**
15991599 * Generates CSS rules from parsed utility classes
16001600*/
1601+ /**
1602+ * Selector → cascade rank.
1603+ *
1604+ * 0 — shorthand (`m-*`, `p-*`, `border`, `rounded`, `inset-*`)
1605+ * 1 — axis (`mx-*`, `my-*`, `px-*`, `py-*`, `inset-x-*`, corner radii)
1606+ * 2 — side (`mt-*`, `mr-*`, `pt-*`, `top-*`, `border-t-*`, …)
1607+ */
1608+ const SELECTOR_CLASS_RE = / \. (?: \\ ! ) ? (?: [ a - z A - Z 0 - 9 _ - ] * \\ ? : ) * (?: \\ ! ) ? ( [ a - z A - Z 0 - 9 _ - ] + ) /
1609+ const AXIS_UTILITY_RE = / ^ (?: m x | m y | p x | p y | i n s e t - x | i n s e t - y | b o r d e r - x | b o r d e r - y | s c r o l l - m x | s c r o l l - m y | s c r o l l - p x | s c r o l l - p y | s p a c e - x | s p a c e - y | g a p - x | g a p - y ) - /
1610+ const AXIS_RADIUS_RE = / ^ r o u n d e d - [ t l b r ] (?: - | $ ) /
1611+ const SIDE_UTILITY_RE = / ^ (?: m t | m r | m b | m l | p t | p r | p b | p l | t o p | r i g h t | b o t t o m | l e f t | b o r d e r - t | b o r d e r - r | b o r d e r - b | b o r d e r - l | s c r o l l - m t | s c r o l l - m r | s c r o l l - m b | s c r o l l - m l | s c r o l l - p t | s c r o l l - p r | s c r o l l - p b | s c r o l l - p l ) - /
1612+ const SIDE_RADIUS_RE = / ^ r o u n d e d - (?: t l | t r | b l | b r ) (?: - | $ ) /
1613+
1614+ function computeUtilityRank ( selector : string ) : number {
1615+ // The escaped important marker (`.\!m-0`) must be skipped or the class
1616+ // match fails and every !-utility ranked 0, letting `!m-0` emit after
1617+ // (and defeat) `!mx-auto`.
1618+ const m = SELECTOR_CLASS_RE . exec ( selector )
1619+ if ( ! m ) return 0
1620+ const cls = m [ 1 ]
1621+ if ( AXIS_UTILITY_RE . test ( cls ) || AXIS_RADIUS_RE . test ( cls ) )
1622+ return 1
1623+ if ( SIDE_UTILITY_RE . test ( cls ) || SIDE_RADIUS_RE . test ( cls ) )
1624+ return 2
1625+ return 0
1626+ }
1627+
16011628export class CSSGenerator {
16021629 private rules : Map < string , CSSRule [ ] > = new Map ( )
16031630 private classCache : Set < string > = new Set ( )
@@ -1609,6 +1636,7 @@ export class CSSGenerator {
16091636 private noMatchCache : Set < string > = new Set ( )
16101637 private usedKeyframes : Set < string > = new Set ( )
16111638 private variantHandledCache : Map < string , boolean > = new Map ( )
1639+ private utilityRankCache : Map < string , number > = new Map ( )
16121640 private compiledGenerated : Set < string > = new Set ( )
16131641 private darkStrategy : 'class' | 'media'
16141642 // Fast-path lookup tables, re-based on the user's theme when it diverges
@@ -2797,26 +2825,18 @@ export class CSSGenerator {
27972825 * combinations like `m-0 mx-auto` silently break (the shorthand resets
27982826 * the auto margins). Matches Tailwind's own stylesheet ordering.
27992827 *
2800- * 0 — shorthand (`m-*`, `p-*`, `border`, `rounded`, `inset-*`)
2801- * 1 — axis (`mx-*`, `my-*`, `px-*`, `py-*`, `inset-x-*`, corner radii)
2802- * 2 — side (`mt-*`, `mr-*`, `pt-*`, `top-*`, `border-t-*`, …)
2828+ * See computeUtilityRank for the ranks themselves.
28032829 */
28042830 private getUtilityRank ( selector : string ) : number {
2805- // The escaped important marker (`.\!m-0`) must be skipped or the class
2806- // match fails and every !-utility ranked 0, letting `!m-0` emit after
2807- // (and defeat) `!mx-auto`.
2808- const m = selector . match ( / \. (?: \\ ! ) ? (?: [ a - z A - Z 0 - 9 _ - ] * \\ ? : ) * (?: \\ ! ) ? ( [ a - z A - Z 0 - 9 _ - ] + ) / )
2809- if ( ! m ) return 0
2810- const cls = m [ 1 ]
2811- if ( / ^ (?: m x | m y | p x | p y | i n s e t - x | i n s e t - y | b o r d e r - x | b o r d e r - y | s c r o l l - m x | s c r o l l - m y | s c r o l l - p x | s c r o l l - p y | s p a c e - x | s p a c e - y | g a p - x | g a p - y ) - / . test ( cls )
2812- || / ^ r o u n d e d - (?: [ t l b r ] ) (?: - | $ ) / . test ( cls ) ) {
2813- return 1
2814- }
2815- if ( / ^ (?: m t | m r | m b | m l | p t | p r | p b | p l | t o p | r i g h t | b o t t o m | l e f t | b o r d e r - t | b o r d e r - r | b o r d e r - b | b o r d e r - l | s c r o l l - m t | s c r o l l - m r | s c r o l l - m b | s c r o l l - m l | s c r o l l - p t | s c r o l l - p r | s c r o l l - p b | s c r o l l - p l ) - / . test ( cls )
2816- || / ^ r o u n d e d - (?: t l | t r | b l | b r ) (?: - | $ ) / . test ( cls ) ) {
2817- return 2
2818- }
2819- return 0
2831+ // Ranking is a pure function of the selector, and toCSS re-ranks every
2832+ // rule on every call — a watching build pays for the same three regexes
2833+ // over the same selectors on each rebuild. Memoize per generator.
2834+ const cached = this . utilityRankCache . get ( selector )
2835+ if ( cached !== undefined )
2836+ return cached
2837+ const rank = computeUtilityRank ( selector )
2838+ this . utilityRankCache . set ( selector , rank )
2839+ return rank
28202840 }
28212841
28222842 /**
@@ -2937,6 +2957,9 @@ export class CSSGenerator {
29372957 this . noMatchCache . clear ( )
29382958 this . usedKeyframes . clear ( )
29392959 this . compiledGenerated . clear ( )
2960+ // Keyed by selector, so it tracks the same set of classes selectorCache
2961+ // does and would otherwise accumulate across a watch session's rebuilds.
2962+ this . utilityRankCache . clear ( )
29402963 // variantHandledCache is config-derived, not generation state — it
29412964 // stays valid across resets.
29422965 }
0 commit comments