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
1011interface 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' ) ;
0 commit comments