5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
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' ;
9
10
10
11
interface Thresholds {
11
12
maximumWarning ?: number ;
@@ -34,7 +35,7 @@ export class BundleBudgetPlugin {
34
35
}
35
36
36
37
budgets . map ( budget => {
37
- const thresholds = this . calcualte ( budget ) ;
38
+ const thresholds = this . calculate ( budget ) ;
38
39
return {
39
40
budget,
40
41
thresholds,
@@ -43,54 +44,42 @@ export class BundleBudgetPlugin {
43
44
} )
44
45
. forEach ( budgetCheck => {
45
46
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 ) ;
86
55
} ) ;
87
56
88
57
} ) ;
89
58
cb ( ) ;
90
59
} ) ;
91
60
}
92
61
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 {
94
83
let thresholds : Thresholds = { } ;
95
84
if ( budget . maximumWarning ) {
96
85
thresholds . maximumWarning = calculateBytes ( budget . maximumWarning , budget . baseline , 'pos' ) ;
0 commit comments