Skip to content

Commit bf0b3d5

Browse files
alan-agius4filipesilva
authored andcommitted
refactor(@angular-devkit/build-angular): remove custom Webpack Stats types
Webpack 5 contains improved types that we can leverage. (cherry picked from commit cb9e51f)
1 parent b7fc023 commit bf0b3d5

File tree

9 files changed

+110
-154
lines changed

9 files changed

+110
-154
lines changed

packages/angular_devkit/build_angular/src/browser/index.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ import { normalizeExtraEntryPoints } from '../webpack/utils/helpers';
6565
import {
6666
BundleStats,
6767
ChunkType,
68-
JsonChunkStats,
69-
JsonCompilationStats,
7068
generateBundleStats,
7169
statsErrorsToString,
7270
statsHasErrors,
@@ -260,7 +258,7 @@ export function buildWebpackBrowser(
260258
emittedFiles = [],
261259
outputPath: webpackOutputPath,
262260
} = buildEvent;
263-
const webpackRawStats = buildEvent.webpackStats as JsonCompilationStats;
261+
const webpackRawStats = buildEvent.webpackStats;
264262
if (!webpackRawStats) {
265263
throw new Error('Webpack stats build result is required.');
266264
}
@@ -568,7 +566,7 @@ export function buildWebpackBrowser(
568566
executor.stop();
569567
}
570568
for (const result of processResults) {
571-
const chunk = webpackStats.chunks?.find((chunk) => chunk.id.toString() === result.name);
569+
const chunk = webpackStats.chunks?.find((chunk) => chunk.id?.toString() === result.name);
572570

573571
if (result.original) {
574572
bundleInfoStats.push(generateBundleInfoStats(result.original, chunk, 'modern'));
@@ -580,10 +578,10 @@ export function buildWebpackBrowser(
580578
}
581579

582580
const unprocessedChunks = webpackStats.chunks?.filter((chunk) => !processResults
583-
.find((result) => chunk.id.toString() === result.name),
581+
.find((result) => chunk.id?.toString() === result.name),
584582
) || [];
585583
for (const chunk of unprocessedChunks) {
586-
const asset = webpackStats.assets?.find(a => a.name === chunk.files[0]);
584+
const asset = webpackStats.assets?.find(a => a.name === chunk.files?.[0]);
587585
bundleInfoStats.push(generateBundleStats({ ...chunk, size: asset?.size }));
588586
}
589587
} else {
@@ -771,15 +769,15 @@ function assertNever(input: never): never {
771769

772770
function generateBundleInfoStats(
773771
bundle: ProcessBundleFile,
774-
chunk: JsonChunkStats | undefined,
772+
chunk: webpack.StatsChunk | undefined,
775773
chunkType: ChunkType,
776774
): BundleStats {
777775
return generateBundleStats(
778776
{
779777
size: bundle.size,
780778
files: bundle.map ? [bundle.filename, bundle.map.filename] : [bundle.filename],
781779
names: chunk?.names,
782-
entry: !!chunk?.names.includes('runtime'),
780+
entry: !!chunk?.names?.includes('runtime'),
783781
initial: !!chunk?.initial,
784782
rendered: true,
785783
chunkType,

packages/angular_devkit/build_angular/src/server/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
getStylesConfig,
2929
getTypeScriptConfig,
3030
} from '../webpack/configs';
31-
import { JsonCompilationStats, webpackStatsLogger } from '../webpack/utils/stats';
31+
import { webpackStatsLogger } from '../webpack/utils/stats';
3232
import { Schema as ServerBuilderOptions } from './schema';
3333

3434
/**
@@ -119,7 +119,7 @@ export function execute(
119119
);
120120
}
121121

122-
webpackStatsLogger(context.logger, webpackStats as JsonCompilationStats, config);
122+
webpackStatsLogger(context.logger, webpackStats, config);
123123

124124
return { ...output, success };
125125
}),

packages/angular_devkit/build_angular/src/utils/bundle-calculator.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { basename } from 'path';
9+
import { StatsAsset, StatsChunk, StatsCompilation } from 'webpack';
910
import { Budget, Type } from '../browser/schema';
1011
import { ProcessBundleFile, ProcessBundleResult } from '../utils/process-bundle';
11-
import {
12-
JsonAssetStats,
13-
JsonChunkStats,
14-
JsonCompilationStats,
15-
formatSize,
16-
} from '../webpack/utils/stats';
12+
import { formatSize } from '../webpack/utils/stats';
1713

1814
interface Size {
1915
size: number;
@@ -108,7 +104,7 @@ export function* calculateThresholds(budget: Budget): IterableIterator<Threshold
108104
*/
109105
function calculateSizes(
110106
budget: Budget,
111-
stats: JsonCompilationStats,
107+
stats: StatsCompilation,
112108
processResults: ProcessBundleResult[],
113109
): Size[] {
114110
if (budget.type === Type.AnyComponentStyle) {
@@ -121,8 +117,8 @@ function calculateSizes(
121117
type CalculatorTypes = {
122118
new (
123119
budget: Budget,
124-
chunks: JsonChunkStats[],
125-
assets: JsonAssetStats[],
120+
chunks: StatsChunk[],
121+
assets: StatsAsset[],
126122
processResults: ProcessBundleResult[],
127123
): Calculator;
128124
};
@@ -152,21 +148,21 @@ function calculateSizes(
152148
abstract class Calculator {
153149
constructor (
154150
protected budget: Budget,
155-
protected chunks: JsonChunkStats[],
156-
protected assets: JsonAssetStats[],
151+
protected chunks: StatsChunk[],
152+
protected assets: StatsAsset[],
157153
protected processResults: ProcessBundleResult[],
158154
) {}
159155

160156
abstract calculate(): Size[];
161157

162158
/** Calculates the size of the given chunk for the provided build type. */
163159
protected calculateChunkSize(
164-
chunk: JsonChunkStats,
160+
chunk: StatsChunk,
165161
buildType: DifferentialBuildType,
166162
): number {
167163
// Look for a process result containing different builds for this chunk.
168164
const processResult = this.processResults
169-
.find((processResult) => processResult.name === chunk.id.toString());
165+
.find((processResult) => processResult.name === chunk.id?.toString());
170166

171167
if (processResult) {
172168
// Found a differential build, use the correct size information.
@@ -176,8 +172,11 @@ abstract class Calculator {
176172
return processResultFile && processResultFile.size || 0;
177173
} else {
178174
// No differential builds, get the chunk size by summing its assets.
179-
return chunk.files
180-
.filter(file => !file.endsWith('.map'))
175+
if (!chunk.files) {
176+
return 0;
177+
}
178+
179+
return chunk.files.filter(file => !file.endsWith('.map'))
181180
.map(file => {
182181
const asset = this.assets.find((asset) => asset.name === file);
183182
if (!asset) {
@@ -190,7 +189,7 @@ abstract class Calculator {
190189
}
191190
}
192191

193-
protected getAssetSize(asset: JsonAssetStats): number {
192+
protected getAssetSize(asset: StatsAsset): number {
194193
if (asset.name.endsWith('.js')) {
195194
const processResult = this.processResults
196195
.find((processResult) => processResult.original && basename(processResult.original.filename) === asset.name);
@@ -219,8 +218,9 @@ class BundleCalculator extends Calculator {
219218
// each then check afterwards if they are all the same.
220219
const buildSizes = Object.values(DifferentialBuildType).map((buildType) => {
221220
const size = this.chunks
222-
.filter(chunk => chunk.names.includes(budgetName))
223-
.map(chunk => this.calculateChunkSize(chunk, buildType))
221+
.filter(chunk => chunk?.names?.includes(budgetName))
222+
// tslint:disable-next-line: no-non-null-assertion
223+
.map(chunk => this.calculateChunkSize(chunk!, buildType))
224224
.reduce((l, r) => l + r, 0);
225225

226226
return { size, label: `bundle ${this.budget.name}-${buildTypeLabels[buildType]}` };
@@ -358,7 +358,7 @@ function calculateBytes(
358358

359359
export function* checkBudgets(
360360
budgets: Budget[],
361-
webpackStats: JsonCompilationStats,
361+
webpackStats: StatsCompilation,
362362
processResults: ProcessBundleResult[],
363363
): IterableIterator<{ severity: ThresholdSeverity, message: string }> {
364364
// Ignore AnyComponentStyle budgets as these are handled in `AnyComponentStyleBudgetChecker`.

packages/angular_devkit/build_angular/src/utils/bundle-calculator_spec.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8+
import { StatsCompilation } from 'webpack';
89
import { Budget, Type } from '../browser/schema';
910
import { ThresholdSeverity, checkBudgets } from './bundle-calculator';
1011
import { ProcessBundleResult } from './process-bundle';
@@ -32,7 +33,7 @@ describe('bundle-calculator', () => {
3233
size: 0.5 * KB,
3334
},
3435
],
35-
};
36+
} as unknown as StatsCompilation;
3637

3738
const failures = Array.from(checkBudgets(budgets, stats, [] /* processResults */));
3839

@@ -60,7 +61,7 @@ describe('bundle-calculator', () => {
6061
size: 0.5 * KB,
6162
},
6263
],
63-
};
64+
} as unknown as StatsCompilation;
6465

6566
const failures = Array.from(checkBudgets(budgets, stats, [] /* processResults */));
6667

@@ -95,7 +96,7 @@ describe('bundle-calculator', () => {
9596
size: 0.75 * KB,
9697
},
9798
],
98-
};
99+
} as unknown as StatsCompilation;
99100

100101
const failures = Array.from(checkBudgets(budgets, stats, [] /* processResults */));
101102

@@ -121,7 +122,8 @@ describe('bundle-calculator', () => {
121122
},
122123
],
123124
assets: [],
124-
};
125+
} as unknown as StatsCompilation;
126+
125127
const processResults: ProcessBundleResult[] = [
126128
{
127129
name: '0',
@@ -164,7 +166,7 @@ describe('bundle-calculator', () => {
164166
},
165167
],
166168
assets: [],
167-
};
169+
} as unknown as StatsCompilation;
168170
const processResults: ProcessBundleResult[] = [
169171
{
170172
name: '0',
@@ -211,7 +213,7 @@ describe('bundle-calculator', () => {
211213
size: 0.75 * KB,
212214
},
213215
],
214-
};
216+
} as unknown as StatsCompilation;
215217

216218
const failures = Array.from(checkBudgets(budgets, stats, [] /* processResults */));
217219

@@ -237,7 +239,7 @@ describe('bundle-calculator', () => {
237239
},
238240
],
239241
assets: [],
240-
};
242+
} as unknown as StatsCompilation;
241243
const processResults: ProcessBundleResult[] = [
242244
{
243245
name: '0',
@@ -281,7 +283,7 @@ describe('bundle-calculator', () => {
281283
},
282284
],
283285
assets: [],
284-
};
286+
} as unknown as StatsCompilation;
285287
const processResults: ProcessBundleResult[] = [
286288
{
287289
name: '0',
@@ -332,7 +334,7 @@ describe('bundle-calculator', () => {
332334
size: 1.5 * KB,
333335
},
334336
],
335-
};
337+
} as unknown as StatsCompilation;
336338

337339
const failures = Array.from(checkBudgets(budgets, stats, [] /* processResults */));
338340

@@ -367,7 +369,7 @@ describe('bundle-calculator', () => {
367369
size: 0.75 * KB,
368370
},
369371
],
370-
};
372+
} as unknown as StatsCompilation;
371373

372374
const failures = Array.from(checkBudgets(budgets, stats, [] /* processResults */));
373375

@@ -402,7 +404,7 @@ describe('bundle-calculator', () => {
402404
size: 0.5 * KB,
403405
},
404406
],
405-
};
407+
} as unknown as StatsCompilation;
406408

407409
const failures = Array.from(checkBudgets(budgets, stats, [] /* processResults */));
408410

@@ -433,7 +435,7 @@ describe('bundle-calculator', () => {
433435
size: 0.5 * KB,
434436
},
435437
],
436-
};
438+
} as unknown as StatsCompilation;
437439

438440
const failures = Array.from(checkBudgets(budgets, stats, [] /* processResults */));
439441

@@ -468,7 +470,7 @@ describe('bundle-calculator', () => {
468470
size: 0.5 * KB,
469471
},
470472
],
471-
};
473+
} as unknown as StatsCompilation;
472474

473475
const failures = Array.from(checkBudgets(budgets, stats, [] /* processResults */));
474476

@@ -499,7 +501,7 @@ describe('bundle-calculator', () => {
499501
size: 1.25 * KB,
500502
},
501503
],
502-
};
504+
} as unknown as StatsCompilation;
503505
const processResults: ProcessBundleResult[] = [
504506
{
505507
name: '0',
@@ -542,7 +544,7 @@ describe('bundle-calculator', () => {
542544
size: 1.25 * KB,
543545
},
544546
],
545-
};
547+
} as unknown as StatsCompilation;
546548
const processResults: ProcessBundleResult[] = [
547549
{
548550
name: '0',
@@ -585,7 +587,7 @@ describe('bundle-calculator', () => {
585587
size: 1.25 * KB,
586588
},
587589
],
588-
};
590+
} as unknown as StatsCompilation;
589591
const processResults: ProcessBundleResult[] = [
590592
{
591593
name: '0',
@@ -628,7 +630,7 @@ describe('bundle-calculator', () => {
628630
size: 1.25 * KB,
629631
},
630632
],
631-
};
633+
} as unknown as StatsCompilation;
632634
const processResults: ProcessBundleResult[] = [
633635
{
634636
name: '0',

packages/angular_devkit/build_angular/src/utils/webpack-diagnostics.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ import { Compilation, WebpackError } from 'webpack';
99

1010
export function addWarning(compilation: Compilation, message: string): void {
1111
compilation.warnings.push(new WebpackError(message));
12-
1312
}
1413

1514
export function addError(compilation: Compilation, message: string): void {
1615
compilation.errors.push(new WebpackError(message));
17-
1816
}

0 commit comments

Comments
 (0)