forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(@angular-devkit/build-angular): move budget computations to …
…be post-build Refs angular#15792. This provides access to all the size information necessary because all build steps have already completed. This commit is roughly a no-op because it simply moves the budget checks (for different builds) to be executed post-build. The lone exception is the AnyComponentStyle budget. Component stylesheet files are not emitted after the build is completed, so there is no size information to work with. Instead, these budgets are checked during a separate plugin (exected for different builds **and** non-differential builds).
- Loading branch information
Showing
9 changed files
with
675 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
..._devkit/build_angular/src/angular-cli-files/plugins/any-component-style-budget-checker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* @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, Plugin } from 'webpack'; | ||
import { Budget, Type } from '../../../src/browser/schema'; | ||
import { ThresholdSeverity, calculateThresholds, checkThresholds } from '../utilities/bundle-calculator'; | ||
|
||
const PLUGIN_NAME = 'AnyComponentStyleBudgetChecker'; | ||
|
||
/** | ||
* Check budget sizes for component styles by emitting a warning or error if a | ||
* budget is exceeded by a particular component's styles. | ||
*/ | ||
export class AnyComponentStyleBudgetChecker implements Plugin { | ||
private readonly budgets: Budget[]; | ||
constructor(budgets: Budget[]) { | ||
this.budgets = budgets.filter((budget) => budget.type === Type.AnyComponentStyle); | ||
} | ||
|
||
apply(compiler: Compiler) { | ||
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { | ||
compilation.hooks.afterOptimizeChunkAssets.tap(PLUGIN_NAME, () => { | ||
// In AOT compilations component styles get processed in child compilations. | ||
// tslint:disable-next-line: no-any | ||
const parentCompilation = (compilation.compiler as any).parentCompilation; | ||
if (!parentCompilation) { | ||
return; | ||
} | ||
|
||
const componentStyles = Object.keys(compilation.assets) | ||
.filter((name) => name.endsWith('.css')) | ||
.map((name) => ({ | ||
size: compilation.assets[name].size(), | ||
label: name, | ||
})); | ||
const thresholds = flatMap(this.budgets, (budget) => calculateThresholds(budget)); | ||
|
||
for (const { size, label } of componentStyles) { | ||
for (const { severity, message } of checkThresholds(thresholds[Symbol.iterator](), size, label)) { | ||
switch (severity) { | ||
case ThresholdSeverity.Warning: | ||
compilation.warnings.push(message); | ||
break; | ||
case ThresholdSeverity.Error: | ||
compilation.errors.push(message); | ||
break; | ||
default: | ||
assertNever(severity); | ||
break; | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
function assertNever(input: never): never { | ||
throw new Error(`Unexpected call to assertNever() with input: ${ | ||
JSON.stringify(input, null /* replacer */, 4 /* tabSize */)}`); | ||
} | ||
|
||
function flatMap<T, R>(list: T[], mapper: (item: T, index: number, array: T[]) => IterableIterator<R>): R[] { | ||
return ([] as R[]).concat(...list.map(mapper).map((iterator) => Array.from(iterator))); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.