|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import { Budget, calculateBytes, calculateSizes } from '../utilities/bundle-calculator'; |
| 9 | + |
| 10 | +interface Thresholds { |
| 11 | + maximumWarning?: number; |
| 12 | + maximumError?: number; |
| 13 | + minimumWarning?: number; |
| 14 | + minimumError?: number; |
| 15 | + warningLow?: number; |
| 16 | + warningHigh?: number; |
| 17 | + errorLow?: number; |
| 18 | + errorHigh?: number; |
| 19 | +} |
| 20 | + |
| 21 | +export interface BundleBudgetPluginOptions { |
| 22 | + budgets: Budget[]; |
| 23 | +} |
| 24 | + |
| 25 | +export class BundleBudgetPlugin { |
| 26 | + constructor(private options: BundleBudgetPluginOptions) {} |
| 27 | + |
| 28 | + apply(compiler: any): void { |
| 29 | + const { budgets } = this.options; |
| 30 | + compiler.plugin('after-emit', (compilation: any, cb: Function) => { |
| 31 | + if (!budgets || budgets.length === 0) { |
| 32 | + cb(); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + budgets.map(budget => { |
| 37 | + const thresholds = this.calcualteThresholds(budget); |
| 38 | + return { |
| 39 | + budget, |
| 40 | + thresholds, |
| 41 | + sizes: calculateSizes(budget, compilation) |
| 42 | + }; |
| 43 | + }) |
| 44 | + .forEach(budgetCheck => { |
| 45 | + 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 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + }); |
| 89 | + cb(); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + private calcualteThresholds(budget: Budget): Thresholds { |
| 94 | + let thresholds: Thresholds = {}; |
| 95 | + if (budget.maximumWarning) { |
| 96 | + thresholds = { ...thresholds, |
| 97 | + maximumWarning: calculateBytes(budget.maximumWarning, budget.baseline, 'pos') |
| 98 | + }; |
| 99 | + } |
| 100 | + if (budget.maximumError) { |
| 101 | + thresholds = { ...thresholds, |
| 102 | + maximumError: calculateBytes(budget.maximumError, budget.baseline, 'pos') |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + if (budget.minimumWarning) { |
| 107 | + thresholds = { ...thresholds, |
| 108 | + minimumWarning: calculateBytes(budget.minimumWarning, budget.baseline, 'neg') |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + if (budget.minimumError) { |
| 113 | + thresholds = { ...thresholds, |
| 114 | + minimumError: calculateBytes(budget.minimumError, budget.baseline, 'neg') |
| 115 | + }; |
| 116 | + } |
| 117 | + |
| 118 | + if (budget.warning) { |
| 119 | + thresholds = { ...thresholds, |
| 120 | + warningLow: calculateBytes(budget.warning, budget.baseline, 'neg') |
| 121 | + }; |
| 122 | + } |
| 123 | + |
| 124 | + if (budget.warning) { |
| 125 | + thresholds = { ...thresholds, |
| 126 | + warningHigh: calculateBytes(budget.warning, budget.baseline, 'pos') |
| 127 | + }; |
| 128 | + } |
| 129 | + |
| 130 | + if (budget.error) { |
| 131 | + thresholds = { ...thresholds, |
| 132 | + errorLow: calculateBytes(budget.error, budget.baseline, 'neg') |
| 133 | + }; |
| 134 | + } |
| 135 | + |
| 136 | + if (budget.error) { |
| 137 | + thresholds = { ...thresholds, |
| 138 | + errorHigh: calculateBytes(budget.error, budget.baseline, 'pos') |
| 139 | + }; |
| 140 | + } |
| 141 | + |
| 142 | + return thresholds; |
| 143 | + } |
| 144 | +} |
0 commit comments