@@ -5,6 +5,8 @@ import zlib from "node:zlib";
55import { exec as nodeExec } from "node:child_process" ;
66import isCleanWorkingDir from "./lib/isCleanWorkingDir.js" ;
77
8+ const VERSION = 1 ;
9+
810const gzip = promisify ( zlib . gzip ) ;
911const exec = promisify ( nodeExec ) ;
1012
@@ -13,13 +15,47 @@ async function getBranchName() {
1315 return stdout . trim ( ) ;
1416}
1517
18+ async function getCommitHash ( ) {
19+ const { stdout } = await exec ( "git rev-parse HEAD" ) ;
20+ return stdout . trim ( ) ;
21+ }
22+
23+ function getBranchHeader ( branch , commit ) {
24+ let branchHeader = branch . trim ( ) ;
25+ if ( commit ) {
26+ branchHeader = chalk . bold ( branchHeader ) + chalk . gray ( ` @${ commit } ` ) ;
27+ } else {
28+ branchHeader = chalk . italic ( branchHeader ) ;
29+ }
30+ return branchHeader ;
31+ }
32+
1633async function getCache ( loc ) {
34+ let cache ;
1735 try {
1836 const contents = await fs . promises . readFile ( loc , "utf8" ) ;
19- return JSON . parse ( contents ) ;
37+ cache = JSON . parse ( contents ) ;
2038 } catch ( err ) {
2139 return { } ;
2240 }
41+
42+ const lastRun = cache [ " last run" ] ;
43+ if ( ! lastRun || ! lastRun . meta || lastRun . meta . version !== VERSION ) {
44+ console . log ( "Compare cache version mismatch. Rewriting..." ) ;
45+ return { } ;
46+ }
47+ return cache ;
48+ }
49+
50+ function cacheResults ( results ) {
51+ const files = Object . create ( null ) ;
52+ results . forEach ( function ( result ) {
53+ files [ result . filename ] = {
54+ raw : result . raw ,
55+ gz : result . gz
56+ } ;
57+ } ) ;
58+ return files ;
2359}
2460
2561function saveCache ( loc , cache ) {
@@ -43,6 +79,7 @@ export async function compareSize( { cache = ".sizecache.json", files } = {} ) {
4379 }
4480
4581 const branch = await getBranchName ( ) ;
82+ const commit = await getCommitHash ( ) ;
4683 const sizeCache = await getCache ( cache ) ;
4784
4885 let rawPadLength = 0 ;
@@ -52,9 +89,11 @@ export async function compareSize( { cache = ".sizecache.json", files } = {} ) {
5289
5390 let contents = await fs . promises . readFile ( filename , "utf8" ) ;
5491
55- // Remove the banner for size comparisons.
56- // The version string can vary widely by short SHA.
57- contents = contents . replace ( / \/ \* \! j Q u e r y [ ^ \n ] + / , "" ) ;
92+ // Remove the short SHA and .dirty from comparisons.
93+ // The short SHA so commits can be compared against each other
94+ // and .dirty to compare with the existing branch during development.
95+ const sha = / j Q u e r y v \d + .\d + .\d + (?: - \w + ) ? \+ (?: s l i m .) ? ( [ ^ \. ] + (?: \. d i r t y ) ? ) / . exec ( contents ) [ 1 ] ;
96+ contents = contents . replace ( new RegExp ( sha , "g" ) , "" ) ;
5897
5998 const size = Buffer . byteLength ( contents , "utf8" ) ;
6099 const gzippedSize = ( await gzip ( contents ) ) . length ;
@@ -67,7 +106,7 @@ export async function compareSize( { cache = ".sizecache.json", files } = {} ) {
67106 } )
68107 ) ;
69108
70- const header = "raw" . padStart ( rawPadLength ) +
109+ const sizeHeader = "raw" . padStart ( rawPadLength ) +
71110 "gz" . padStart ( gzPadLength + 1 ) +
72111 " Filename" ;
73112
@@ -78,8 +117,12 @@ export async function compareSize( { cache = ".sizecache.json", files } = {} ) {
78117 } ) ;
79118
80119 const comparisons = Object . keys ( sizeCache ) . map ( function ( branch ) {
81- const branchSizes = Object . keys ( sizeCache [ branch ] ) . map ( function ( filename ) {
82- const branchResult = sizeCache [ branch ] [ filename ] ;
120+ const meta = sizeCache [ branch ] . meta || { } ;
121+ const commit = meta . commit ;
122+
123+ const files = sizeCache [ branch ] . files ;
124+ const branchSizes = Object . keys ( files ) . map ( function ( filename ) {
125+ const branchResult = files [ filename ] ;
83126 const compareResult = results . find ( function ( result ) {
84127 return result . filename === filename ;
85128 } ) || { } ;
@@ -91,38 +134,41 @@ export async function compareSize( { cache = ".sizecache.json", files } = {} ) {
91134
92135 return [
93136 "" , // New line before each branch
94- chalk . bold ( branch ) ,
95- header ,
137+ getBranchHeader ( branch , commit ) ,
138+ sizeHeader ,
96139 ...branchSizes
97140 ] . join ( "\n" ) ;
98141 } ) ;
99142
100143 const output = [
101144 "" , // Opening new line
102145 chalk . bold ( "Sizes" ) ,
103- header ,
146+ sizeHeader ,
104147 ...sizes ,
105148 ...comparisons ,
106149 "" // Closing new line
107150 ] . join ( "\n" ) ;
108151
109152 console . log ( output ) ;
110153
154+ // Always save the last run
155+ // Save version under last run
156+ sizeCache [ " last run" ] = {
157+ meta : { version : VERSION } ,
158+ files : cacheResults ( results )
159+ } ;
160+
111161 // Only save cache for the current branch
112162 // if the working directory is clean.
113163 if ( await isCleanWorkingDir ( ) ) {
114- sizeCache [ branch ] = { } ;
115- results . forEach ( function ( result ) {
116- sizeCache [ branch ] [ result . filename ] = {
117- raw : result . raw ,
118- gz : result . gz
119- } ;
120- } ) ;
121-
122- await saveCache ( cache , sizeCache ) ;
123-
164+ sizeCache [ branch ] = {
165+ meta : { commit } ,
166+ files : cacheResults ( results )
167+ } ;
124168 console . log ( `Saved cache for ${ branch } .` ) ;
125169 }
126170
171+ await saveCache ( cache , sizeCache ) ;
172+
127173 return results ;
128174}
0 commit comments