@@ -183,47 +183,44 @@ export class NgBuildAnalyticsPlugin {
183
183
}
184
184
185
185
protected _collectBundleStats ( compilation : compilation . Compilation ) {
186
- // `compilation.chunks` is a Set in Webpack 5
187
- const chunks = Array . from ( compilation . chunks ) ;
188
-
189
- chunks
190
- . filter ( ( chunk : { rendered ?: boolean } ) => chunk . rendered )
191
- . forEach ( ( chunk : { files : string [ ] ; canBeInitial ( ) : boolean } ) => {
192
- const asset = compilation . assets [ chunk . files [ 0 ] ] ;
193
- const size = asset ? asset . size ( ) : 0 ;
194
-
195
- if ( chunk . canBeInitial ( ) ) {
196
- this . _stats . initialChunkSize += size ;
197
- } else {
198
- this . _stats . lazyChunkCount ++ ;
199
- this . _stats . lazyChunkSize += size ;
200
- }
201
- this . _stats . totalChunkCount ++ ;
202
- this . _stats . totalChunkSize += size ;
203
- } ) ;
204
-
205
- Object . entries < { size ( ) : number } > ( compilation . assets )
206
- // Filter out chunks. We only count assets that are not JS.
207
- . filter ( ( [ name ] ) => {
208
- return chunks . every ( ( chunk : { files : string [ ] } ) => chunk . files [ 0 ] != name ) ;
209
- } )
210
- . forEach ( ( [ , asset ] ) => {
211
- this . _stats . assetSize += asset . size ( ) ;
212
- this . _stats . assetCount ++ ;
213
- } ) ;
214
-
215
- for ( const [ name , asset ] of Object . entries < { size ( ) : number } > ( compilation . assets ) ) {
216
- if ( name == 'polyfill' ) {
217
- this . _stats . polyfillSize += asset . size ( ) ;
218
- }
219
- }
186
+ const chunkAssets = new Set < string > ( ) ;
220
187
for ( const chunk of compilation . chunks ) {
221
- if ( chunk . files [ 0 ] && chunk . files [ 0 ] . endsWith ( '.css' ) ) {
222
- const asset = compilation . assets [ chunk . files [ 0 ] ] ;
223
- const size = asset ? asset . size ( ) : 0 ;
188
+ if ( ! chunk . rendered ) {
189
+ continue ;
190
+ }
191
+
192
+ const firstFile = Array . from ( chunk . files ) [ 0 ] ;
193
+ const size = compilation . getAsset ( firstFile ) ?. source . size ( ) ?? 0 ;
194
+ chunkAssets . add ( firstFile ) ;
195
+
196
+ if ( chunk . canBeInitial ( ) ) {
197
+ this . _stats . initialChunkSize += size ;
198
+ } else {
199
+ this . _stats . lazyChunkCount ++ ;
200
+ this . _stats . lazyChunkSize += size ;
201
+ }
202
+
203
+ this . _stats . totalChunkCount ++ ;
204
+ this . _stats . totalChunkSize += size ;
205
+
206
+ if ( firstFile . endsWith ( '.css' ) ) {
224
207
this . _stats . cssSize += size ;
225
208
}
226
209
}
210
+
211
+ for ( const asset of compilation . getAssets ( ) ) {
212
+ // Only count non-JavaScript related files
213
+ if ( chunkAssets . has ( asset . name ) ) {
214
+ continue ;
215
+ }
216
+
217
+ this . _stats . assetSize += asset . source . size ( ) ;
218
+ this . _stats . assetCount ++ ;
219
+
220
+ if ( asset . name == 'polyfill' ) {
221
+ this . _stats . polyfillSize += asset . source . size ( ) ;
222
+ }
223
+ }
227
224
}
228
225
229
226
/************************************************************************************************
0 commit comments