Skip to content

Commit 8c452ae

Browse files
committed
refactor(@angular-devkit/build-angular): optimize analytics plugin bundle stats for Webpack 5
Newer Webpack 5 APIs allow the bundle stat generation for analytics to be optimized including less iteration of chunks and assets.
1 parent b1b6973 commit 8c452ae

File tree

1 file changed

+34
-37
lines changed
  • packages/angular_devkit/build_angular/src/webpack/plugins

1 file changed

+34
-37
lines changed

packages/angular_devkit/build_angular/src/webpack/plugins/analytics.ts

+34-37
Original file line numberDiff line numberDiff line change
@@ -183,47 +183,44 @@ export class NgBuildAnalyticsPlugin {
183183
}
184184

185185
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>();
220187
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')) {
224207
this._stats.cssSize += size;
225208
}
226209
}
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+
}
227224
}
228225

229226
/************************************************************************************************

0 commit comments

Comments
 (0)