1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
---|
10 | if (k2 === undefined) k2 = k;
|
---|
11 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
---|
12 | }) : (function(o, m, k, k2) {
|
---|
13 | if (k2 === undefined) k2 = k;
|
---|
14 | o[k2] = m[k];
|
---|
15 | }));
|
---|
16 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
---|
17 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
---|
18 | }) : function(o, v) {
|
---|
19 | o["default"] = v;
|
---|
20 | });
|
---|
21 | var __importStar = (this && this.__importStar) || function (mod) {
|
---|
22 | if (mod && mod.__esModule) return mod;
|
---|
23 | var result = {};
|
---|
24 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
---|
25 | __setModuleDefault(result, mod);
|
---|
26 | return result;
|
---|
27 | };
|
---|
28 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
29 | exports.AnyComponentStyleBudgetChecker = void 0;
|
---|
30 | const path = __importStar(require("path"));
|
---|
31 | const webpack_1 = require("webpack");
|
---|
32 | const schema_1 = require("../../browser/schema");
|
---|
33 | const bundle_calculator_1 = require("../../utils/bundle-calculator");
|
---|
34 | const webpack_diagnostics_1 = require("../../utils/webpack-diagnostics");
|
---|
35 | const PLUGIN_NAME = 'AnyComponentStyleBudgetChecker';
|
---|
36 | /**
|
---|
37 | * Check budget sizes for component styles by emitting a warning or error if a
|
---|
38 | * budget is exceeded by a particular component's styles.
|
---|
39 | */
|
---|
40 | class AnyComponentStyleBudgetChecker {
|
---|
41 | constructor(budgets) {
|
---|
42 | this.budgets = budgets.filter((budget) => budget.type === schema_1.Type.AnyComponentStyle);
|
---|
43 | }
|
---|
44 | apply(compiler) {
|
---|
45 | compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
|
---|
46 | compilation.hooks.processAssets.tap({
|
---|
47 | name: PLUGIN_NAME,
|
---|
48 | stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_ANALYSE,
|
---|
49 | }, () => {
|
---|
50 | // In AOT compilations component styles get processed in child compilations.
|
---|
51 | if (!compilation.compiler.parentCompilation) {
|
---|
52 | return;
|
---|
53 | }
|
---|
54 | const cssExtensions = ['.css', '.scss', '.less', '.styl', '.sass'];
|
---|
55 | const componentStyles = Object.keys(compilation.assets)
|
---|
56 | .filter((name) => cssExtensions.includes(path.extname(name)))
|
---|
57 | .map((name) => ({
|
---|
58 | size: compilation.assets[name].size(),
|
---|
59 | label: name,
|
---|
60 | }));
|
---|
61 | const thresholds = this.budgets.flatMap((budget) => [...bundle_calculator_1.calculateThresholds(budget)]);
|
---|
62 | for (const { size, label } of componentStyles) {
|
---|
63 | for (const { severity, message } of bundle_calculator_1.checkThresholds(thresholds[Symbol.iterator](), size, label)) {
|
---|
64 | switch (severity) {
|
---|
65 | case bundle_calculator_1.ThresholdSeverity.Warning:
|
---|
66 | webpack_diagnostics_1.addWarning(compilation, message);
|
---|
67 | break;
|
---|
68 | case bundle_calculator_1.ThresholdSeverity.Error:
|
---|
69 | webpack_diagnostics_1.addError(compilation, message);
|
---|
70 | break;
|
---|
71 | default:
|
---|
72 | assertNever(severity);
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 | });
|
---|
77 | });
|
---|
78 | }
|
---|
79 | }
|
---|
80 | exports.AnyComponentStyleBudgetChecker = AnyComponentStyleBudgetChecker;
|
---|
81 | function assertNever(input) {
|
---|
82 | throw new Error(`Unexpected call to assertNever() with input: ${JSON.stringify(input, null /* replacer */, 4 /* tabSize */)}`);
|
---|
83 | }
|
---|