@@ -21,60 +21,86 @@ function saveResults() {
2121}
2222
2323function percentChange ( prev , current ) {
24- const change = Math . floor ( ( current - prev ) / prev * 100 ) ;
24+ return Math . floor ( ( current - prev ) / prev * 100 ) ;
25+ }
2526
27+ function percentChangeString ( change ) {
2628 if ( change > 0 ) {
2729 return chalk . red . bold ( `+${ change } %` ) ;
2830 } else if ( change <= 0 ) {
2931 return chalk . green . bold ( change + ' %' ) ;
3032 }
3133}
3234
35+ const resultsHeaders = [
36+ 'Bundle' ,
37+ 'Prev Size' ,
38+ 'Current Size' ,
39+ 'Diff' ,
40+ 'Prev Gzip' ,
41+ 'Current Gzip' ,
42+ 'Diff' ,
43+ ] ;
44+
45+ function generateResultsArray ( current , prevResults ) {
46+ return current . bundleSizes
47+ . map ( result => {
48+ const prev = prevResults . bundleSizes . filter (
49+ res =>
50+ res . filename === result . filename &&
51+ res . bundleType === result . bundleType
52+ ) [ 0 ] ;
53+ if ( result === prev ) {
54+ // We didn't rebuild this bundle.
55+ return ;
56+ }
57+
58+ const size = result . size ;
59+ const gzip = result . gzip ;
60+ let prevSize = prev ? prev . size : 0 ;
61+ let prevGzip = prev ? prev . gzip : 0 ;
62+
63+ return {
64+ filename : result . filename ,
65+ bundleType : result . bundleType ,
66+ packageName : result . packageName ,
67+ prevSize : filesize ( prevSize ) ,
68+ prevFileSize : filesize ( size ) ,
69+ prevFileSizeChange : percentChange ( prevSize , size ) ,
70+ prevGzip : filesize ( prevGzip ) ,
71+ prevGzipSize : filesize ( gzip ) ,
72+ prevGzipSizeChange : percentChange ( prevGzip , gzip ) ,
73+ } ;
74+ // Strip any nulls
75+ } )
76+ . filter ( f => f ) ;
77+ }
78+
3379function printResults ( ) {
3480 const table = new Table ( {
35- head : [
36- chalk . gray . yellow ( 'Bundle' ) ,
37- chalk . gray . yellow ( 'Prev Size' ) ,
38- chalk . gray . yellow ( 'Current Size' ) ,
39- chalk . gray . yellow ( 'Diff' ) ,
40- chalk . gray . yellow ( 'Prev Gzip' ) ,
41- chalk . gray . yellow ( 'Current Gzip' ) ,
42- chalk . gray . yellow ( 'Diff' ) ,
43- ] ,
81+ head : resultsHeaders . map ( chalk . gray . yellow ) ,
4482 } ) ;
45- currentBuildResults . bundleSizes . forEach ( result => {
46- const matches = prevBuildResults . bundleSizes . filter (
47- ( { filename, bundleType} ) =>
48- filename === result . filename && bundleType === result . bundleType
49- ) ;
50- if ( matches . length > 1 ) {
51- throw new Error ( `Ambiguous bundle size record for: ${ result . filename } ` ) ;
52- }
53- const prev = matches [ 0 ] ;
54- if ( result === prev ) {
55- // We didn't rebuild this bundle.
56- return ;
57- }
5883
59- const size = result . size ;
60- const gzip = result . gzip ;
61- let prevSize = prev ? prev . size : 0 ;
62- let prevGzip = prev ? prev . gzip : 0 ;
84+ const results = generateResultsArray ( currentBuildResults , prevBuildResults ) ;
85+ results . forEach ( result => {
6386 table . push ( [
64- chalk . white . bold ( `${ result . filename } (${ result . bundleType } )` ) ,
65- chalk . gray . bold ( filesize ( prevSize ) ) ,
66- chalk . white . bold ( filesize ( size ) ) ,
67- percentChange ( prevSize , size ) ,
68- chalk . gray . bold ( filesize ( prevGzip ) ) ,
69- chalk . white . bold ( filesize ( gzip ) ) ,
70- percentChange ( prevGzip , gzip ) ,
87+ chalk . white . bold ( `${ result . filename } (${ result . bundleType } )` ) ,
88+ chalk . gray . bold ( result . prevSize ) ,
89+ chalk . white . bold ( result . prevFileSize ) ,
90+ percentChangeString ( result . prevFileSizeChange ) ,
91+ chalk . gray . bold ( result . prevGzip ) ,
92+ chalk . white . bold ( result . prevGzipSize ) ,
93+ percentChangeString ( result . prevGzipSizeChange ) ,
7194 ] ) ;
7295 } ) ;
96+
7397 return table . toString ( ) ;
7498}
7599
76100module . exports = {
101+ currentBuildResults,
102+ generateResultsArray,
77103 printResults,
78104 saveResults,
79- currentBuildResults ,
105+ resultsHeaders ,
80106} ;
0 commit comments