@@ -20,37 +20,35 @@ function saveResults() {
2020 ) ;
2121}
2222
23+
2324function percentChange ( prev , current ) {
24- const change = Math . floor ( ( current - prev ) / prev * 100 ) ;
25+ return Math . floor ( ( current - prev ) / prev * 100 ) ;
26+ }
2527
28+ function percentChangeString ( change ) {
2629 if ( change > 0 ) {
2730 return chalk . red . bold ( `+${ change } %` ) ;
2831 } else if ( change <= 0 ) {
2932 return chalk . green . bold ( change + ' %' ) ;
3033 }
3134}
3235
33- function printResults ( ) {
34- 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- ] ,
44- } ) ;
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 ] ;
36+ const resultsHeaders = [
37+ 'Bundle' ,
38+ 'Prev Size' ,
39+ 'Current Size' ,
40+ 'Diff' ,
41+ 'Prev Gzip' ,
42+ 'Current Gzip' ,
43+ 'Diff' ,
44+ ] ;
45+
46+ function generateResultsArray ( current , prevResults ) {
47+ currentBuildResults . bundleSizes . forEach ( index => {
48+ const result = currentBuildResults . bundleSizes [ index ] ;
49+ const prev = prevBuildResults . bundleSizes . filter (
50+ res => res . filename === result . filename
51+ ) [ 0 ] ;
5452 if ( result === prev ) {
5553 // We didn't rebuild this bundle.
5654 return ;
@@ -60,21 +58,45 @@ function printResults() {
6058 const gzip = result . gzip ;
6159 let prevSize = prev ? prev . size : 0 ;
6260 let prevGzip = prev ? prev . gzip : 0 ;
63- table . push ( [
64- chalk . white . bold ( `${ result . filename } (${ result . bundleType } )` ) ,
65- chalk . gray . bold ( filesize ( prevSize ) ) ,
66- chalk . white . bold ( filesize ( size ) ) ,
61+
62+ return [
63+ `${ result . filename } (${ result . bundleType } ` ,
64+ filesize ( prevSize ) ,
65+ filesize ( size ) ,
6766 percentChange ( prevSize , size ) ,
68- chalk . gray . bold ( filesize ( prevGzip ) ) ,
69- chalk . white . bold ( filesize ( gzip ) ) ,
67+ filesize ( prevGzip ) ,
68+ filesize ( gzip ) ,
7069 percentChange ( prevGzip , gzip ) ,
70+ ] ;
71+ // Strip any nulls
72+ } ) . filter ( f => f ) ;
73+ }
74+
75+ function printResults ( ) {
76+ const table = new Table ( {
77+ head : resultsHeaders . map ( chalk . gray . yellow ) ,
78+ } ) ;
79+
80+ const results = generateResultsArray ( currentBuildResults , prevBuildResults )
81+ results . forEach ( row => {
82+ table . push ( [
83+ chalk . white . bold ( row [ 0 ] ) ,
84+ chalk . gray . bold ( row [ 1 ] ) ,
85+ chalk . white . bold ( row [ 2 ] ) ,
86+ percentChangeString ( row [ 3 ] ) ,
87+ chalk . gray . bold ( row [ 4 ] ) ,
88+ chalk . white . bold ( row [ 5 ] ) ,
89+ percentChangeString ( row [ 6 ] ) ,
7190 ] ) ;
7291 } ) ;
92+
7393 return table . toString ( ) ;
7494}
7595
7696module . exports = {
97+ currentBuildResults,
98+ generateResultsArray,
7799 printResults,
78100 saveResults,
79- currentBuildResults ,
101+ resultsHeaders ,
80102} ;
0 commit comments