@@ -12,6 +12,7 @@ import { WebpackLoggingCallback } from '@angular-devkit/build-webpack';
12
12
import * as path from 'path' ;
13
13
import * as textTable from 'text-table' ;
14
14
import { colors as ansiColors , removeColor } from '../../utils/color' ;
15
+ import { Configuration , Stats } from 'webpack' ;
15
16
16
17
export function formatSize ( size : number ) : string {
17
18
if ( size <= 0 ) {
@@ -56,10 +57,10 @@ export function generateBundleStats(
56
57
}
57
58
}
58
59
59
- export function generateBuildStatsTable ( data : BundleStats [ ] , colors : boolean ) : string {
60
+ function generateBuildStatsTable ( data : BundleStats [ ] , colors : boolean ) : string {
60
61
const changedEntryChunksStats : BundleStatsData [ ] = [ ] ;
61
62
const changedLazyChunksStats : BundleStatsData [ ] = [ ] ;
62
- for ( const { initial, stats} of data ) {
63
+ for ( const { initial, stats } of data ) {
63
64
if ( initial ) {
64
65
changedEntryChunksStats . push ( stats ) ;
65
66
} else {
@@ -89,7 +90,7 @@ export function generateBuildStatsTable(data: BundleStats[], colors: boolean): s
89
90
if ( changedLazyChunksStats . length ) {
90
91
bundleInfo . push (
91
92
[ 'Lazy Chunk Files' , 'Names' , 'Size' ] . map ( bold ) ,
92
- ...changedLazyChunksStats ,
93
+ ...changedLazyChunksStats ,
93
94
) ;
94
95
}
95
96
@@ -99,27 +100,30 @@ export function generateBuildStatsTable(data: BundleStats[], colors: boolean): s
99
100
} ) ;
100
101
}
101
102
102
- export function generateBuildStats ( hash : string , time : number , colors : boolean ) : string {
103
+ function generateBuildStats ( hash : string , time : number , colors : boolean ) : string {
103
104
const w = ( x : string ) => colors ? ansiColors . bold . white ( x ) : x ;
104
105
return `Build at: ${ w ( new Date ( ) . toISOString ( ) ) } - Hash: ${ w ( hash ) } - Time: ${ w ( '' + time ) } ms` ;
105
106
}
106
107
107
- export function statsToString ( json : any , statsConfig : any ) {
108
+ function statsToString ( json : any , statsConfig : any , bundleState ?: BundleStats [ ] ) : string {
108
109
const colors = statsConfig . colors ;
109
110
const rs = ( x : string ) => colors ? ansiColors . reset ( x ) : x ;
110
111
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
+ }
116
119
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 ;
120
125
}
121
126
122
- const unchangedChunkNumber = json . chunks . length - changedChunksStats . length ;
123
127
const statsTable = generateBuildStatsTable ( changedChunksStats , colors ) ;
124
128
125
129
// In some cases we do things outside of webpack context
@@ -182,7 +186,7 @@ export function statsWarningsToString(json: any, statsConfig: any): string {
182
186
output += yb ( `Warning: ${ warning } \n\n` ) ;
183
187
} else {
184
188
if ( ! ERRONEOUS_WARNINGS_FILTER ( warning . message ) ) {
185
- continue ;
189
+ continue ;
186
190
}
187
191
const file = warning . file || warning . moduleName ;
188
192
if ( file ) {
@@ -200,7 +204,7 @@ export function statsWarningsToString(json: any, statsConfig: any): string {
200
204
}
201
205
202
206
if ( output ) {
203
- return '\n' + output ;
207
+ return '\n' + output ;
204
208
}
205
209
206
210
return '' ;
@@ -241,7 +245,7 @@ export function statsErrorsToString(json: any, statsConfig: any): string {
241
245
}
242
246
243
247
if ( output ) {
244
- return '\n' + output ;
248
+ return '\n' + output ;
245
249
}
246
250
247
251
return '' ;
@@ -261,19 +265,26 @@ export function createWebpackLoggingCallback(
261
265
logger : logging . LoggerApi ,
262
266
) : WebpackLoggingCallback {
263
267
return ( stats , config ) => {
264
- // config.stats contains our own stats settings, added during buildWebpackConfig().
265
- const json = stats . toJson ( config . stats ) ;
266
268
if ( verbose ) {
267
269
logger . info ( stats . toString ( config . stats ) ) ;
268
- } else {
269
- logger . info ( statsToString ( json , config . stats ) ) ;
270
270
}
271
271
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
+ }
279
274
}
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