@@ -12,6 +12,7 @@ import { WebpackLoggingCallback } from '@angular-devkit/build-webpack';
1212import * as path from 'path' ;
1313import * as textTable from 'text-table' ;
1414import { colors as ansiColors , removeColor } from '../../utils/color' ;
15+ import { Configuration , Stats } from 'webpack' ;
1516
1617export function formatSize ( size : number ) : string {
1718 if ( size <= 0 ) {
@@ -56,10 +57,10 @@ export function generateBundleStats(
5657 }
5758}
5859
59- export function generateBuildStatsTable ( data : BundleStats [ ] , colors : boolean ) : string {
60+ function generateBuildStatsTable ( data : BundleStats [ ] , colors : boolean ) : string {
6061 const changedEntryChunksStats : BundleStatsData [ ] = [ ] ;
6162 const changedLazyChunksStats : BundleStatsData [ ] = [ ] ;
62- for ( const { initial, stats} of data ) {
63+ for ( const { initial, stats } of data ) {
6364 if ( initial ) {
6465 changedEntryChunksStats . push ( stats ) ;
6566 } else {
@@ -89,7 +90,7 @@ export function generateBuildStatsTable(data: BundleStats[], colors: boolean): s
8990 if ( changedLazyChunksStats . length ) {
9091 bundleInfo . push (
9192 [ 'Lazy Chunk Files' , 'Names' , 'Size' ] . map ( bold ) ,
92- ...changedLazyChunksStats ,
93+ ...changedLazyChunksStats ,
9394 ) ;
9495 }
9596
@@ -99,27 +100,30 @@ export function generateBuildStatsTable(data: BundleStats[], colors: boolean): s
99100 } ) ;
100101}
101102
102- export function generateBuildStats ( hash : string , time : number , colors : boolean ) : string {
103+ function generateBuildStats ( hash : string , time : number , colors : boolean ) : string {
103104 const w = ( x : string ) => colors ? ansiColors . bold . white ( x ) : x ;
104105 return `Build at: ${ w ( new Date ( ) . toISOString ( ) ) } - Hash: ${ w ( hash ) } - Time: ${ w ( '' + time ) } ms` ;
105106}
106107
107- export function statsToString ( json : any , statsConfig : any ) {
108+ function statsToString ( json : any , statsConfig : any , bundleState ?: BundleStats [ ] ) : string {
108109 const colors = statsConfig . colors ;
109110 const rs = ( x : string ) => colors ? ansiColors . reset ( x ) : x ;
110111
111- const changedChunksStats : BundleStats [ ] = [ ] ;
112- for ( const chunk of json . chunks ) {
113- if ( ! chunk . rendered ) {
114- continue ;
115- }
112+ const changedChunksStats : BundleStats [ ] = bundleState ?? [ ] ;
113+ let unchangedChunkNumber = 0 ;
114+ if ( ! bundleState ?. length ) {
115+ for ( const chunk of json . chunks ) {
116+ if ( ! chunk . rendered ) {
117+ continue ;
118+ }
116119
117- const assets = json . assets . filter ( ( asset : any ) => chunk . files . includes ( asset . name ) ) ;
118- const summedSize = assets . filter ( ( asset : any ) => ! asset . name . endsWith ( ".map" ) ) . reduce ( ( total : number , asset : any ) => { return total + asset . size } , 0 ) ;
119- changedChunksStats . push ( generateBundleStats ( { ...chunk , size : summedSize } , colors ) ) ;
120+ const assets = json . assets . filter ( ( asset : any ) => chunk . files . includes ( asset . name ) ) ;
121+ const summedSize = assets . filter ( ( asset : any ) => ! asset . name . endsWith ( ".map" ) ) . reduce ( ( total : number , asset : any ) => { return total + asset . size } , 0 ) ;
122+ changedChunksStats . push ( generateBundleStats ( { ...chunk , size : summedSize } , colors ) ) ;
123+ }
124+ unchangedChunkNumber = json . chunks . length - changedChunksStats . length ;
120125 }
121126
122- const unchangedChunkNumber = json . chunks . length - changedChunksStats . length ;
123127 const statsTable = generateBuildStatsTable ( changedChunksStats , colors ) ;
124128
125129 // In some cases we do things outside of webpack context
@@ -182,7 +186,7 @@ export function statsWarningsToString(json: any, statsConfig: any): string {
182186 output += yb ( `Warning: ${ warning } \n\n` ) ;
183187 } else {
184188 if ( ! ERRONEOUS_WARNINGS_FILTER ( warning . message ) ) {
185- continue ;
189+ continue ;
186190 }
187191 const file = warning . file || warning . moduleName ;
188192 if ( file ) {
@@ -200,7 +204,7 @@ export function statsWarningsToString(json: any, statsConfig: any): string {
200204 }
201205
202206 if ( output ) {
203- return '\n' + output ;
207+ return '\n' + output ;
204208 }
205209
206210 return '' ;
@@ -241,7 +245,7 @@ export function statsErrorsToString(json: any, statsConfig: any): string {
241245 }
242246
243247 if ( output ) {
244- return '\n' + output ;
248+ return '\n' + output ;
245249 }
246250
247251 return '' ;
@@ -261,19 +265,26 @@ export function createWebpackLoggingCallback(
261265 logger : logging . LoggerApi ,
262266) : WebpackLoggingCallback {
263267 return ( stats , config ) => {
264- // config.stats contains our own stats settings, added during buildWebpackConfig().
265- const json = stats . toJson ( config . stats ) ;
266268 if ( verbose ) {
267269 logger . info ( stats . toString ( config . stats ) ) ;
268- } else {
269- logger . info ( statsToString ( json , config . stats ) ) ;
270270 }
271271
272- if ( statsHasWarnings ( json ) ) {
273- logger . warn ( statsWarningsToString ( json , config . stats ) ) ;
274- }
275- if ( statsHasErrors ( json ) ) {
276- logger . error ( statsErrorsToString ( json , config . stats ) ) ;
277- }
278- } ;
272+ webpackStatsLogger ( logger , stats . toJson ( config . stats ) , config ) ;
273+ }
279274}
275+
276+ export function webpackStatsLogger (
277+ logger : logging . LoggerApi ,
278+ json : Stats . ToJsonOutput ,
279+ config : Configuration ,
280+ bundleStats ?: BundleStats [ ] ,
281+ ) : void {
282+ logger . info ( statsToString ( json , config . stats , bundleStats ) ) ;
283+
284+ if ( statsHasWarnings ( json ) ) {
285+ logger . warn ( statsWarningsToString ( json , config . stats ) ) ;
286+ }
287+ if ( statsHasErrors ( json ) ) {
288+ logger . error ( statsErrorsToString ( json , config . stats ) ) ;
289+ }
290+ } ;
0 commit comments