-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathbundle-budget.ts
46 lines (40 loc) · 1.46 KB
/
bundle-budget.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Compiler } from 'webpack';
import { Budget } from '../../browser/schema';
import { ThresholdSeverity, checkBudgets } from '../../utils/bundle-calculator';
import { ProcessBundleResult } from '../../utils/process-bundle';
import { addError, addWarning } from '../../utils/webpack-diagnostics';
export interface BundleBudgetPluginOptions {
budgets: Budget[];
}
export class BundleBudgetPlugin {
constructor(private options: BundleBudgetPluginOptions) { }
apply(compiler: Compiler): void {
const { budgets } = this.options;
if (!budgets || budgets.length === 0) {
return;
}
compiler.hooks.afterEmit.tap('BundleBudgetPlugin', (compilation) => {
// No process bundle results because this plugin is only used when differential
// builds are disabled.
const processResults: ProcessBundleResult[] = [];
const stats = compilation.getStats().toJson();
for (const { severity, message } of checkBudgets(budgets, stats, processResults)) {
switch (severity) {
case ThresholdSeverity.Warning:
addWarning(compilation, `budgets: ${message}`);
break;
case ThresholdSeverity.Error:
addError(compilation, `budgets: ${message}`);
break;
}
}
});
}
}