@@ -10,7 +10,7 @@ type CSSTarget =
1010 | 'all' // tasty CSS + tasty global CSS (createGlobalStyle)
1111 | 'global' // only tasty global CSS
1212 | 'active' // tasty CSS for classes currently in DOM
13- | 'cached ' // tasty CSS present in sheets but not in DOM
13+ | 'unused ' // tasty CSS with refCount = 0 (still in cache but not actively used)
1414 | 'page' // ALL CSS on the page across stylesheets (not only tasty)
1515 | string // 't123' tasty class or a CSS selector
1616 | string [ ] // array of tasty classes like ['t1', 't2']
@@ -33,7 +33,6 @@ interface InspectResult {
3333interface CacheMetrics {
3434 hits : number ;
3535 misses : number ;
36- unusedHits : number ;
3736 bulkCleanups : number ;
3837 totalInsertions : number ;
3938 totalUnused : number ;
@@ -45,12 +44,15 @@ interface CacheMetrics {
4544 rulesDeleted : number ;
4645 } > ;
4746 startTime : number ;
47+
48+ // Calculated metrics
49+ unusedHits ?: number ; // calculated as current unused count
4850}
4951
5052interface CacheStatus {
5153 classes : {
52- active : string [ ] ; // from DOM scan
53- cached : string [ ] ; // present in sheets but not currently in DOM
54+ active : string [ ] ; // classes with refCount > 0 and present in DOM
55+ unused : string [ ] ; // classes with refCount = 0 but still in cache
5456 all : string [ ] ; // union of both
5557 } ;
5658 metrics : CacheMetrics | null ;
@@ -88,18 +90,18 @@ interface SummaryOptions {
8890interface Summary {
8991 // Classes
9092 activeClasses : string [ ] ;
91- cachedClasses : string [ ] ;
93+ unusedClasses : string [ ] ;
9294 totalStyledClasses : string [ ] ;
9395
9496 // Tasty CSS sizes
9597 activeCSSSize : number ;
96- cachedCSSSize : number ;
97- totalCSSSize : number ; // tasty-only: active + cached + tasty global
98+ unusedCSSSize : number ;
99+ totalCSSSize : number ; // tasty-only: active + unused + tasty global
98100
99101 // Tasty CSS payloads
100102 activeCSS : string ;
101- cachedCSS : string ;
102- allCSS : string ; // tasty-only CSS (active + cached + tasty global)
103+ unusedCSS : string ;
104+ allCSS : string ; // tasty-only CSS (active + unused + tasty global)
103105
104106 // Tasty global (createGlobalStyle)
105107 globalCSS : string ;
@@ -449,13 +451,21 @@ export const tastyDebug = {
449451 } else if ( target === 'active' ) {
450452 const activeClasses = findAllTastyClasses ( root ) ;
451453 css = injector . instance . getCssTextForClasses ( activeClasses , { root } ) ;
452- } else if ( target === 'cached' ) {
453- const activeClasses = findAllTastyClasses ( root ) ;
454- const allClasses = findAllStyledClasses ( root ) ;
455- const cachedClasses = allClasses . filter (
456- ( cls ) => ! activeClasses . includes ( cls ) ,
457- ) ;
458- css = injector . instance . getCssTextForClasses ( cachedClasses , { root } ) ;
454+ } else if ( target === 'unused' ) {
455+ // Get unused classes (refCount = 0) from the registry
456+ const registry = ( injector . instance as any ) [
457+ 'sheetManager'
458+ ] ?. getRegistry ( root ) ;
459+ const unusedClasses : string [ ] = registry
460+ ? Array . from (
461+ registry . refCounts . entries ( ) as IterableIterator <
462+ [ string , number ]
463+ > ,
464+ )
465+ . filter ( ( [ , refCount ] : [ string , number ] ) => refCount === 0 )
466+ . map ( ( [ className ] : [ string , number ] ) => className )
467+ : [ ] ;
468+ css = injector . instance . getCssTextForClasses ( unusedClasses , { root } ) ;
459469 } else if ( target === 'page' ) {
460470 css = getPageCSS ( { root, includeCrossOrigin : true } ) ;
461471 } else if ( / ^ t \d + $ / . test ( target ) ) {
@@ -536,15 +546,23 @@ export const tastyDebug = {
536546 const { root = document } = opts || { } ;
537547 const activeClasses = findAllTastyClasses ( root ) ;
538548 const allClasses = findAllStyledClasses ( root ) ;
539- const cachedClasses = allClasses . filter (
540- ( cls ) => ! activeClasses . includes ( cls ) ,
549+ // Get unused classes (refCount = 0) from the registry
550+ const registry = ( injector . instance as any ) [ 'sheetManager' ] ?. getRegistry (
551+ root ,
541552 ) ;
553+ const unusedClasses : string [ ] = registry
554+ ? Array . from (
555+ registry . refCounts . entries ( ) as IterableIterator < [ string , number ] > ,
556+ )
557+ . filter ( ( [ , refCount ] : [ string , number ] ) => refCount === 0 )
558+ . map ( ( [ className ] : [ string , number ] ) => className )
559+ : [ ] ;
542560
543561 return {
544562 classes : {
545563 active : activeClasses ,
546- cached : cachedClasses ,
547- all : allClasses ,
564+ unused : unusedClasses ,
565+ all : [ ... activeClasses , ... unusedClasses ] ,
548566 } ,
549567 metrics : injector . instance . getMetrics ( { root } ) ,
550568 } ;
@@ -699,7 +717,7 @@ export const tastyDebug = {
699717 const metrics = this . metrics ( { root } ) ;
700718
701719 const activeCSS = this . css ( 'active' , { root, prettify : false } ) ;
702- const cachedCSS = this . css ( 'cached ' , { root, prettify : false } ) ;
720+ const unusedCSS = this . css ( 'unused ' , { root, prettify : false } ) ;
703721 const allCSS = this . css ( 'all' , { root, prettify : false } ) ;
704722
705723 // Build cleanup summary from metrics
@@ -754,13 +772,13 @@ export const tastyDebug = {
754772
755773 const summary : Summary = {
756774 activeClasses : cacheStatus . classes . active ,
757- cachedClasses : cacheStatus . classes . cached ,
775+ unusedClasses : cacheStatus . classes . unused ,
758776 totalStyledClasses : cacheStatus . classes . all ,
759777 activeCSSSize : activeCSS . length ,
760- cachedCSSSize : cachedCSS . length ,
778+ unusedCSSSize : unusedCSS . length ,
761779 totalCSSSize : allCSS . length ,
762780 activeCSS : prettifyCSS ( activeCSS ) ,
763- cachedCSS : prettifyCSS ( cachedCSS ) ,
781+ unusedCSS : prettifyCSS ( unusedCSS ) ,
764782 allCSS : prettifyCSS ( allCSS ) ,
765783 globalCSS : globalBreakdown . css ,
766784 globalCSSSize : globalBreakdown . totalCSSSize ,
@@ -781,14 +799,14 @@ export const tastyDebug = {
781799 ` • Active classes (in DOM): ${ summary . activeClasses . length } ` ,
782800 ) ;
783801 console . log (
784- ` • Cached classes (performance cache ): ${ summary . cachedClasses . length } ` ,
802+ ` • Unused classes (refCount = 0 ): ${ summary . unusedClasses . length } ` ,
785803 ) ;
786804 console . log (
787805 ` • Total styled classes: ${ summary . totalStyledClasses . length } ` ,
788806 ) ;
789807 console . log ( `💾 CSS Size:` ) ;
790808 console . log ( ` • Active CSS: ${ summary . activeCSSSize } characters` ) ;
791- console . log ( ` • Cached CSS: ${ summary . cachedCSSSize } characters` ) ;
809+ console . log ( ` • Unused CSS: ${ summary . unusedCSSSize } characters` ) ;
792810 console . log (
793811 ` • Global CSS: ${ summary . globalCSSSize } characters (${ summary . globalRuleCount } rules)` ,
794812 ) ;
@@ -815,7 +833,7 @@ export const tastyDebug = {
815833 const hitRate =
816834 metrics . hits + metrics . misses > 0
817835 ? (
818- ( ( metrics . hits + metrics . unusedHits ) /
836+ ( ( metrics . hits + ( metrics . unusedHits || 0 ) ) /
819837 ( metrics . hits + metrics . misses ) ) *
820838 100
821839 ) . toFixed ( 1 )
@@ -825,7 +843,7 @@ export const tastyDebug = {
825843
826844 console . log ( '🔍 Details:' ) ;
827845 console . log ( ' • Active classes:' , summary . activeClasses ) ;
828- console . log ( ' • Cached classes:' , summary . cachedClasses ) ;
846+ console . log ( ' • Unused classes:' , summary . unusedClasses ) ;
829847 console . groupEnd ( ) ;
830848 }
831849
@@ -984,7 +1002,7 @@ export const tastyDebug = {
9841002 console . log ( '📖 Common targets for css()/log():' ) ;
9851003 console . log ( ' • "all" - all tasty CSS + global CSS' ) ;
9861004 console . log ( ' • "active" - CSS for classes in DOM' ) ;
987- console . log ( ' • "cached " - CSS for unused classes' ) ;
1005+ console . log ( ' • "unused " - CSS for classes with refCount = 0 ' ) ;
9881006 console . log ( ' • "global" - only global CSS (createGlobalStyle)' ) ;
9891007 console . log ( ' • "page" - ALL page CSS (including non-tasty)' ) ;
9901008 console . log ( ' • "t123" - specific tasty class' ) ;
0 commit comments