Skip to content

Commit b8c12b7

Browse files
cexbrayatclydin
authored andcommitted
feat(@angular/cli): better budget error messages
Improves error messages for budgets. Before: ERROR in budgets, maximum exceeded for total scripts. After: ERROR in budgets, maximum exceeded for total scripts. Budget 500 kB was exceeded by 83.9 kB.
1 parent fb38375 commit b8c12b7

File tree

2 files changed

+34
-45
lines changed

2 files changed

+34
-45
lines changed

packages/@angular/cli/plugins/bundle-budget.ts

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
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 { Budget, calculateBytes, calculateSizes } from '../utilities/bundle-calculator';
8+
import { Budget, Size, calculateBytes, calculateSizes } from '../utilities/bundle-calculator';
9+
import { formatSize } from '../utilities/stats';
910

1011
interface Thresholds {
1112
maximumWarning?: number;
@@ -34,7 +35,7 @@ export class BundleBudgetPlugin {
3435
}
3536

3637
budgets.map(budget => {
37-
const thresholds = this.calcualte(budget);
38+
const thresholds = this.calculate(budget);
3839
return {
3940
budget,
4041
thresholds,
@@ -43,54 +44,42 @@ export class BundleBudgetPlugin {
4344
})
4445
.forEach(budgetCheck => {
4546
budgetCheck.sizes.forEach(size => {
46-
if (budgetCheck.thresholds.maximumWarning) {
47-
if (budgetCheck.thresholds.maximumWarning < size.size) {
48-
compilation.warnings.push(`budgets, maximum exceeded for ${size.label}.`);
49-
}
50-
}
51-
if (budgetCheck.thresholds.maximumError) {
52-
if (budgetCheck.thresholds.maximumError < size.size) {
53-
compilation.errors.push(`budgets, maximum exceeded for ${size.label}.`);
54-
}
55-
}
56-
if (budgetCheck.thresholds.minimumWarning) {
57-
if (budgetCheck.thresholds.minimumWarning > size.size) {
58-
compilation.warnings.push(`budgets, minimum exceeded for ${size.label}.`);
59-
}
60-
}
61-
if (budgetCheck.thresholds.minimumError) {
62-
if (budgetCheck.thresholds.minimumError > size.size) {
63-
compilation.errors.push(`budgets, minimum exceeded for ${size.label}.`);
64-
}
65-
}
66-
if (budgetCheck.thresholds.warningLow) {
67-
if (budgetCheck.thresholds.warningLow > size.size) {
68-
compilation.warnings.push(`budgets, minimum exceeded for ${size.label}.`);
69-
}
70-
}
71-
if (budgetCheck.thresholds.warningHigh) {
72-
if (budgetCheck.thresholds.warningHigh < size.size) {
73-
compilation.warnings.push(`budgets, maximum exceeded for ${size.label}.`);
74-
}
75-
}
76-
if (budgetCheck.thresholds.errorLow) {
77-
if (budgetCheck.thresholds.errorLow > size.size) {
78-
compilation.errors.push(`budgets, minimum exceeded for ${size.label}.`);
79-
}
80-
}
81-
if (budgetCheck.thresholds.errorHigh) {
82-
if (budgetCheck.thresholds.errorHigh < size.size) {
83-
compilation.errors.push(`budgets, maximum exceeded for ${size.label}.`);
84-
}
85-
}
47+
this.checkMaximum(budgetCheck.thresholds.maximumWarning, size, compilation.warnings);
48+
this.checkMaximum(budgetCheck.thresholds.maximumError, size, compilation.errors);
49+
this.checkMinimum(budgetCheck.thresholds.minimumWarning, size, compilation.warnings);
50+
this.checkMinimum(budgetCheck.thresholds.minimumError, size, compilation.errors);
51+
this.checkMinimum(budgetCheck.thresholds.warningLow, size, compilation.warnings);
52+
this.checkMaximum(budgetCheck.thresholds.warningHigh, size, compilation.warnings);
53+
this.checkMinimum(budgetCheck.thresholds.errorLow, size, compilation.errors);
54+
this.checkMaximum(budgetCheck.thresholds.errorHigh, size, compilation.errors);
8655
});
8756

8857
});
8958
cb();
9059
});
9160
}
9261

93-
private calcualte(budget: Budget): Thresholds {
62+
private checkMinimum(threshold: number, size: Size, messages: any) {
63+
if (threshold) {
64+
if (threshold > size.size) {
65+
const sizeDifference = formatSize(threshold - size.size);
66+
messages.push(`budgets, minimum exceeded for ${size.label}. `
67+
+ `Budget ${formatSize(threshold)} was not reached by ${sizeDifference}.`);
68+
}
69+
}
70+
}
71+
72+
private checkMaximum(threshold: number, size: Size, messages: any) {
73+
if (threshold) {
74+
if (threshold < size.size) {
75+
const sizeDifference = formatSize(size.size - threshold);
76+
messages.push(`budgets, maximum exceeded for ${size.label}. `
77+
+ `Budget ${formatSize(threshold)} was exceeded by ${sizeDifference}.`);
78+
}
79+
}
80+
}
81+
82+
private calculate(budget: Budget): Thresholds {
9483
let thresholds: Thresholds = {};
9584
if (budget.maximumWarning) {
9685
thresholds.maximumWarning = calculateBytes(budget.maximumWarning, budget.baseline, 'pos');

packages/@angular/cli/utilities/stats.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { stripIndents } from 'common-tags';
77
const chalkCtx = new (chalk.constructor as any)(chalk.supportsColor ? {} : { level: 1 });
88
const { bold, green, red, reset, white, yellow } = chalkCtx;
99

10-
function _formatSize(size: number): string {
10+
export function formatSize(size: number): string {
1111
if (size <= 0) {
1212
return '0 bytes';
1313
}
@@ -30,7 +30,7 @@ export function statsToString(json: any, statsConfig: any) {
3030
.filter((chunk: any) => chunk.rendered)
3131
.map((chunk: any) => {
3232
const asset = json.assets.filter((x: any) => x.name == chunk.files[0])[0];
33-
const size = asset ? ` ${_formatSize(asset.size)}` : '';
33+
const size = asset ? ` ${formatSize(asset.size)}` : '';
3434
const files = chunk.files.join(', ');
3535
const names = chunk.names ? ` (${chunk.names.join(', ')})` : '';
3636
const initial = y(chunk.entry ? '[entry]' : chunk.initial ? '[initial]' : '');

0 commit comments

Comments
 (0)