Skip to content

Commit 822b4b0

Browse files
committed
fix(tasty): total calculation in debug
1 parent 603247b commit 822b4b0

File tree

1 file changed

+61
-8
lines changed

1 file changed

+61
-8
lines changed

src/tasty/debug.ts

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ export const tastyDebug = {
658658
return {
659659
css: prettifyCSS(rawCSS),
660660
ruleCount,
661-
size: rawCSS.length,
661+
size: rawCSS.length, // Use raw CSS size for consistent calculations
662662
};
663663
},
664664

@@ -732,12 +732,20 @@ export const tastyDebug = {
732732
const unusedCSS = this.css('unused', { root, prettify: false });
733733
const allCSS = this.css('all', { root, prettify: false });
734734

735-
// Get CSS for each global type separately
735+
// Calculate global CSS by subtracting class-based CSS from total
736+
const classCSSSize = activeCSS.length + unusedCSS.length;
737+
const totalGlobalCSSSize = allCSS.length - classCSSSize;
738+
739+
// Get CSS for each global type separately for display purposes
736740
const globalData = this.getGlobalTypeCSS('global', { root });
737741
const rawData = this.getGlobalTypeCSS('raw', { root });
738742
const keyframesData = this.getGlobalTypeCSS('keyframes', { root });
739743
const propertyData = this.getGlobalTypeCSS('property', { root });
740744

745+
// Use the calculated sizes to avoid double-counting
746+
const globalTypesTotalSize =
747+
globalData.size + rawData.size + keyframesData.size + propertyData.size;
748+
741749
// Build cleanup summary from metrics
742750
const cleanupSummary = {
743751
enabled: !!metrics,
@@ -788,16 +796,37 @@ export const tastyDebug = {
788796
};
789797
}
790798

799+
// If individual extraction matches total, use individual sizes
800+
// Otherwise, proportionally scale the individual sizes to match the total
801+
const useIndividualSizes =
802+
Math.abs(globalTypesTotalSize - totalGlobalCSSSize) < 100;
803+
804+
let adjustedGlobalSizes;
805+
if (useIndividualSizes) {
806+
adjustedGlobalSizes = {
807+
globalCSSSize: globalData.size,
808+
rawCSSSize: rawData.size,
809+
keyframesCSSSize: keyframesData.size,
810+
propertyCSSSize: propertyData.size,
811+
};
812+
} else {
813+
// Scale proportionally to match the actual total
814+
const scaleFactor = totalGlobalCSSSize / globalTypesTotalSize;
815+
adjustedGlobalSizes = {
816+
globalCSSSize: Math.round(globalData.size * scaleFactor),
817+
rawCSSSize: Math.round(rawData.size * scaleFactor),
818+
keyframesCSSSize: Math.round(keyframesData.size * scaleFactor),
819+
propertyCSSSize: Math.round(propertyData.size * scaleFactor),
820+
};
821+
}
822+
791823
const summary: Summary = {
792824
activeClasses: cacheStatus.classes.active,
793825
unusedClasses: cacheStatus.classes.unused,
794826
totalStyledClasses: cacheStatus.classes.all,
795827
activeCSSSize: activeCSS.length,
796828
unusedCSSSize: unusedCSS.length,
797-
globalCSSSize: globalData.size,
798-
rawCSSSize: rawData.size,
799-
keyframesCSSSize: keyframesData.size,
800-
propertyCSSSize: propertyData.size,
829+
...adjustedGlobalSizes,
801830
totalCSSSize: allCSS.length,
802831
activeCSS: prettifyCSS(activeCSS),
803832
unusedCSS: prettifyCSS(unusedCSS),
@@ -858,10 +887,34 @@ export const tastyDebug = {
858887
console.log(` • Calculated Total: ${calculatedTotal} characters`);
859888
console.log(` • Actual Total: ${summary.totalCSSSize} characters`);
860889

861-
if (Math.abs(calculatedTotal - summary.totalCSSSize) > 100) {
890+
const difference = Math.abs(calculatedTotal - summary.totalCSSSize);
891+
if (difference > 100) {
862892
console.warn(
863-
` ⚠️ Size mismatch: ${Math.abs(calculatedTotal - summary.totalCSSSize)} characters difference`,
893+
` ⚠️ Size mismatch: ${difference} characters difference`,
894+
);
895+
896+
// Debug: show what might be missing
897+
console.group('🔍 Debugging size mismatch:');
898+
console.log(
899+
`Active + Unused = ${summary.activeCSSSize + summary.unusedCSSSize}`,
900+
);
901+
console.log(
902+
`All Global Types = ${summary.globalCSSSize + summary.rawCSSSize + summary.keyframesCSSSize + summary.propertyCSSSize}`,
903+
);
904+
console.log(
905+
`Class-based vs Total difference = ${summary.totalCSSSize - (summary.activeCSSSize + summary.unusedCSSSize)}`,
864906
);
907+
908+
// Show scaling information
909+
console.log(`Raw global extraction total: ${globalTypesTotalSize}`);
910+
console.log(`Calculated global size: ${totalGlobalCSSSize}`);
911+
console.log(`Used individual sizes: ${useIndividualSizes}`);
912+
if (!useIndividualSizes) {
913+
console.log(
914+
`Scale factor applied: ${(totalGlobalCSSSize / globalTypesTotalSize).toFixed(3)}`,
915+
);
916+
}
917+
console.groupEnd();
865918
}
866919

867920
if (page) {

0 commit comments

Comments
 (0)