@@ -30,20 +30,28 @@ export function formatSize(size: number): string {
30
30
return `${ roundedSize . toFixed ( fractionDigits ) } ${ abbreviations [ index ] } ` ;
31
31
}
32
32
33
- export type BundleStatsData = [ files : string , names : string , size : number | string ] ;
33
+ export type BundleStatsData = [
34
+ files : string ,
35
+ names : string ,
36
+ rawSize : number | string ,
37
+ estimatedTransferSize : number | string ,
38
+ ] ;
34
39
export interface BundleStats {
35
40
initial : boolean ;
36
41
stats : BundleStatsData ;
37
42
}
38
43
39
44
export function generateBundleStats ( info : {
40
- size ?: number ;
45
+ rawSize ?: number ;
46
+ estimatedTransferSize ?: number ;
41
47
files ?: string [ ] ;
42
48
names ?: string [ ] ;
43
49
initial ?: boolean ;
44
50
rendered ?: boolean ;
45
51
} ) : BundleStats {
46
- const size = typeof info . size === 'number' ? info . size : '-' ;
52
+ const rawSize = typeof info . rawSize === 'number' ? info . rawSize : '-' ;
53
+ const estimatedTransferSize =
54
+ typeof info . estimatedTransferSize === 'number' ? info . estimatedTransferSize : '-' ;
47
55
const files =
48
56
info . files
49
57
?. filter ( ( f ) => ! f . endsWith ( '.map' ) )
@@ -54,14 +62,15 @@ export function generateBundleStats(info: {
54
62
55
63
return {
56
64
initial,
57
- stats : [ files , names , size ] ,
65
+ stats : [ files , names , rawSize , estimatedTransferSize ] ,
58
66
} ;
59
67
}
60
68
61
69
function generateBuildStatsTable (
62
70
data : BundleStats [ ] ,
63
71
colors : boolean ,
64
72
showTotalSize : boolean ,
73
+ showEstimatedTransferSize : boolean ,
65
74
) : string {
66
75
const g = ( x : string ) => ( colors ? ansiColors . greenBright ( x ) : x ) ;
67
76
const c = ( x : string ) => ( colors ? ansiColors . cyanBright ( x ) : x ) ;
@@ -71,36 +80,70 @@ function generateBuildStatsTable(
71
80
const changedEntryChunksStats : BundleStatsData [ ] = [ ] ;
72
81
const changedLazyChunksStats : BundleStatsData [ ] = [ ] ;
73
82
74
- let initialTotalSize = 0 ;
83
+ let initialTotalRawSize = 0 ;
84
+ let initialTotalEstimatedTransferSize ;
75
85
76
86
for ( const { initial, stats } of data ) {
77
- const [ files , names , size ] = stats ;
78
-
79
- const data : BundleStatsData = [
80
- g ( files ) ,
81
- names ,
82
- c ( typeof size === 'number' ? formatSize ( size ) : size ) ,
83
- ] ;
87
+ const [ files , names , rawSize , estimatedTransferSize ] = stats ;
88
+
89
+ let data : BundleStatsData ;
90
+
91
+ if ( showEstimatedTransferSize ) {
92
+ data = [
93
+ g ( files ) ,
94
+ names ,
95
+ c ( typeof rawSize === 'number' ? formatSize ( rawSize ) : rawSize ) ,
96
+ c (
97
+ typeof estimatedTransferSize === 'number'
98
+ ? formatSize ( estimatedTransferSize )
99
+ : estimatedTransferSize ,
100
+ ) ,
101
+ ] ;
102
+ } else {
103
+ data = [ g ( files ) , names , c ( typeof rawSize === 'number' ? formatSize ( rawSize ) : rawSize ) , '' ] ;
104
+ }
84
105
85
106
if ( initial ) {
86
107
changedEntryChunksStats . push ( data ) ;
87
- if ( typeof size === 'number' ) {
88
- initialTotalSize += size ;
108
+ if ( typeof rawSize === 'number' ) {
109
+ initialTotalRawSize += rawSize ;
110
+ }
111
+ if ( showEstimatedTransferSize && typeof estimatedTransferSize === 'number' ) {
112
+ if ( initialTotalEstimatedTransferSize === undefined ) {
113
+ initialTotalEstimatedTransferSize = 0 ;
114
+ }
115
+ initialTotalEstimatedTransferSize += estimatedTransferSize ;
89
116
}
90
117
} else {
91
118
changedLazyChunksStats . push ( data ) ;
92
119
}
93
120
}
94
121
95
122
const bundleInfo : ( string | number ) [ ] [ ] = [ ] ;
123
+ const baseTitles = [ 'Names' , 'Raw Size' ] ;
124
+ const tableAlign : ( 'l' | 'r' ) [ ] = [ 'l' , 'l' , 'r' ] ;
125
+
126
+ if ( showEstimatedTransferSize ) {
127
+ baseTitles . push ( 'Estimated Transfer Size' ) ;
128
+ tableAlign . push ( 'r' ) ;
129
+ }
96
130
97
131
// Entry chunks
98
132
if ( changedEntryChunksStats . length ) {
99
- bundleInfo . push ( [ 'Initial Chunk Files' , 'Names' , 'Size' ] . map ( bold ) , ...changedEntryChunksStats ) ;
133
+ bundleInfo . push ( [ 'Initial Chunk Files' , ... baseTitles ] . map ( bold ) , ...changedEntryChunksStats ) ;
100
134
101
135
if ( showTotalSize ) {
102
136
bundleInfo . push ( [ ] ) ;
103
- bundleInfo . push ( [ ' ' , 'Initial Total' , formatSize ( initialTotalSize ) ] . map ( bold ) ) ;
137
+
138
+ const totalSizeElements = [ ' ' , 'Initial Total' , formatSize ( initialTotalRawSize ) ] ;
139
+ if ( showEstimatedTransferSize ) {
140
+ totalSizeElements . push (
141
+ typeof initialTotalEstimatedTransferSize === 'number'
142
+ ? formatSize ( initialTotalEstimatedTransferSize )
143
+ : '-' ,
144
+ ) ;
145
+ }
146
+ bundleInfo . push ( totalSizeElements . map ( bold ) ) ;
104
147
}
105
148
}
106
149
@@ -111,13 +154,13 @@ function generateBuildStatsTable(
111
154
112
155
// Lazy chunks
113
156
if ( changedLazyChunksStats . length ) {
114
- bundleInfo . push ( [ 'Lazy Chunk Files' , 'Names' , 'Size' ] . map ( bold ) , ...changedLazyChunksStats ) ;
157
+ bundleInfo . push ( [ 'Lazy Chunk Files' , ... baseTitles ] . map ( bold ) , ...changedLazyChunksStats ) ;
115
158
}
116
159
117
160
return textTable ( bundleInfo , {
118
161
hsep : dim ( ' | ' ) ,
119
162
stringLength : ( s ) => removeColor ( s ) . length ,
120
- align : [ 'l' , 'l' , 'r' ] ,
163
+ align : tableAlign ,
121
164
} ) ;
122
165
}
123
166
@@ -148,6 +191,7 @@ function statsToString(
148
191
149
192
const changedChunksStats : BundleStats [ ] = bundleState ?? [ ] ;
150
193
let unchangedChunkNumber = 0 ;
194
+ let hasEstimatedTransferSizes = false ;
151
195
if ( ! bundleState ?. length ) {
152
196
const isFirstRun = ! runsCache . has ( json . outputPath || '' ) ;
153
197
@@ -159,10 +203,26 @@ function statsToString(
159
203
}
160
204
161
205
const assets = json . assets ?. filter ( ( asset ) => chunk . files ?. includes ( asset . name ) ) ;
162
- const summedSize = assets
163
- ?. filter ( ( asset ) => ! asset . name . endsWith ( '.map' ) )
164
- . reduce ( ( total , asset ) => total + asset . size , 0 ) ;
165
- changedChunksStats . push ( generateBundleStats ( { ...chunk , size : summedSize } ) ) ;
206
+ let rawSize = 0 ;
207
+ let estimatedTransferSize ;
208
+ if ( assets ) {
209
+ for ( const asset of assets ) {
210
+ if ( asset . name . endsWith ( '.map' ) ) {
211
+ continue ;
212
+ }
213
+
214
+ rawSize += asset . size ;
215
+
216
+ if ( typeof asset . info . estimatedTransferSize === 'number' ) {
217
+ if ( estimatedTransferSize === undefined ) {
218
+ estimatedTransferSize = 0 ;
219
+ hasEstimatedTransferSizes = true ;
220
+ }
221
+ estimatedTransferSize += asset . info . estimatedTransferSize ;
222
+ }
223
+ }
224
+ }
225
+ changedChunksStats . push ( generateBundleStats ( { ...chunk , rawSize, estimatedTransferSize } ) ) ;
166
226
}
167
227
unchangedChunkNumber = json . chunks . length - changedChunksStats . length ;
168
228
@@ -186,6 +246,7 @@ function statsToString(
186
246
changedChunksStats ,
187
247
colors ,
188
248
unchangedChunkNumber === 0 ,
249
+ hasEstimatedTransferSizes ,
189
250
) ;
190
251
191
252
// In some cases we do things outside of webpack context
0 commit comments