@@ -96,7 +96,12 @@ async function printFileSizes(
96
96
environmentName : string ,
97
97
) {
98
98
const logs : string [ ] = [ ] ;
99
- if ( options . detail === false && options . total === false ) {
99
+
100
+ let showTotal = options . total !== false ;
101
+ const showDetail = options . detail !== false ;
102
+ const exclude = options . exclude ?? excludeAsset ;
103
+
104
+ if ( ! showTotal && ! showDetail ) {
100
105
return logs ;
101
106
}
102
107
@@ -142,7 +147,6 @@ async function printFileSizes(
142
147
groupAssetsByEmitStatus : false ,
143
148
} ) ;
144
149
145
- const exclude = options . exclude ?? excludeAsset ;
146
150
const filteredAssets = ( origin . assets || [ ] ) . filter ( ( asset ) => {
147
151
const assetInfo : PrintFileSizeAsset = {
148
152
name : asset . name ,
@@ -176,44 +180,50 @@ async function printFileSizes(
176
180
let totalSize = 0 ;
177
181
let totalGzipSize = 0 ;
178
182
183
+ // No need to print total size if there is only one asset and detail is true
184
+ showTotal = showTotal && ! ( showDetail && assets . length === 1 ) ;
185
+
179
186
for ( const asset of assets ) {
180
187
totalSize += asset . size ;
181
188
if ( options . compressed ) {
182
189
totalGzipSize += asset . gzippedSize ?? asset . size ;
183
190
}
184
191
}
185
192
186
- const showTotalSize = options . total !== false && assets . length > 1 ;
187
- const fileHeader = `File (${ environmentName } )` ;
188
- const totalSizeLabel = showTotalSize ? 'Total:' : '' ;
189
- const totalSizeStr = showTotalSize ? calcFileSize ( totalSize ) : '' ;
190
-
191
- const maxFileLength = Math . max (
192
- ...assets . map ( ( a ) => ( a . folder + path . sep + a . name ) . length ) ,
193
- fileHeader . length ,
194
- totalSizeLabel . length ,
195
- ) ;
196
- const maxSizeLength = Math . max (
197
- ...assets . map ( ( a ) => a . sizeLabel . length ) ,
198
- totalSizeStr . length ,
199
- ) ;
200
-
201
- if ( options . detail !== false ) {
193
+ const fileHeader = showDetail ? `File (${ environmentName } )` : '' ;
194
+ const totalSizeLabel = showTotal
195
+ ? showDetail
196
+ ? 'Total:'
197
+ : `Total size (${ environmentName } ):`
198
+ : '' ;
199
+ const totalSizeStr = showTotal ? calcFileSize ( totalSize ) : '' ;
200
+
201
+ if ( showDetail ) {
202
+ const maxFileLength = Math . max (
203
+ ...assets . map ( ( a ) => ( a . folder + path . sep + a . name ) . length ) ,
204
+ showTotal ? totalSizeLabel . length : 0 ,
205
+ fileHeader . length ,
206
+ ) ;
207
+
208
+ const maxSizeLength = Math . max (
209
+ ...assets . map ( ( a ) => a . sizeLabel . length ) ,
210
+ totalSizeStr . length ,
211
+ ) ;
212
+
202
213
const showGzipHeader = Boolean (
203
214
options . compressed && assets . some ( ( item ) => item . gzippedSize !== null ) ,
204
215
) ;
216
+
205
217
logs . push (
206
218
getHeader ( maxFileLength , maxSizeLength , fileHeader , showGzipHeader ) ,
207
219
) ;
208
- }
209
220
210
- for ( const asset of assets ) {
211
- let { sizeLabel } = asset ;
212
- const { name, folder, gzipSizeLabel } = asset ;
213
- const fileNameLength = ( folder + path . sep + name ) . length ;
214
- const sizeLength = sizeLabel . length ;
221
+ for ( const asset of assets ) {
222
+ let { sizeLabel } = asset ;
223
+ const { name, folder, gzipSizeLabel } = asset ;
224
+ const fileNameLength = ( folder + path . sep + name ) . length ;
225
+ const sizeLength = sizeLabel . length ;
215
226
216
- if ( options . detail !== false ) {
217
227
if ( sizeLength < maxSizeLength ) {
218
228
const rightPadding = ' ' . repeat ( maxSizeLength - sizeLength ) ;
219
229
sizeLabel += rightPadding ;
@@ -235,21 +245,27 @@ async function printFileSizes(
235
245
236
246
logs . push ( log ) ;
237
247
}
238
- }
239
248
240
- // only print total size if there are more than one asset
241
- if ( options . total !== false && assets . length > 1 ) {
242
- logs . push ( '' ) ;
249
+ if ( showTotal ) {
250
+ logs . push ( '' ) ;
251
+ let log = ' ' ;
252
+ log += ' ' . repeat ( maxFileLength - totalSizeLabel . length ) ;
253
+ log += color . magenta ( totalSizeLabel ) ;
254
+ log += ` ${ totalSizeStr } ` ;
255
+
256
+ if ( options . compressed ) {
257
+ const colorFn = getAssetColor ( totalGzipSize / assets . length ) ;
258
+ log += ' ' . repeat ( maxSizeLength - totalSizeStr . length ) ;
259
+ log += ` ${ colorFn ( calcFileSize ( totalGzipSize ) ) } ` ;
260
+ }
243
261
244
- let log = ' ' ;
245
- log += ' ' . repeat ( maxFileLength - totalSizeLabel . length ) ;
246
- log += color . magenta ( totalSizeLabel ) ;
247
- log + = ` ${ totalSizeStr } ` ;
262
+ logs . push ( log ) ;
263
+ }
264
+ } else if ( showTotal ) {
265
+ let log = ` ${ color . magenta ( totalSizeLabel ) } ${ totalSizeStr } ` ;
248
266
249
267
if ( options . compressed ) {
250
- const colorFn = getAssetColor ( totalGzipSize / assets . length ) ;
251
- log += ' ' . repeat ( maxSizeLength - totalSizeStr . length ) ;
252
- log += ` ${ colorFn ( calcFileSize ( totalGzipSize ) ) } ` ;
268
+ log += color . green ( ` (${ calcFileSize ( totalGzipSize ) } gzipped)` ) ;
253
269
}
254
270
255
271
logs . push ( log ) ;
0 commit comments