Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): display correct filename for bund…
Browse files Browse the repository at this point in the history
…les that are ES2016+

(cherry picked from commit 075c988)
  • Loading branch information
alan-agius4 committed Mar 12, 2021
1 parent eb3674e commit f6bca53
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ export enum ThresholdSeverity {
}

enum DifferentialBuildType {
// FIXME: this should match the actual file suffix and not hardcoded.
ORIGINAL = 'es2015',
DOWNLEVEL = 'es5',
ORIGINAL = 'original',
DOWNLEVEL = 'downlevel',
}

export function* calculateThresholds(budget: Budget): IterableIterator<Threshold> {
Expand Down Expand Up @@ -206,15 +205,17 @@ class BundleCalculator extends Calculator {
return [];
}

const buildTypeLabels = getBuildTypeLabels(this.processResults);

// The chunk may or may not have differential builds. Compute the size for
// each then check afterwards if they are all the same.
const buildSizes = Object.values(DifferentialBuildType).map((buildType) => {
const size = this.chunks
.filter(chunk => chunk.names.indexOf(budgetName) !== -1)
.map(chunk => this.calculateChunkSize(chunk, buildType))
.reduce((l, r) => l + r, 0);
.filter(chunk => chunk.names.includes(budgetName))
.map(chunk => this.calculateChunkSize(chunk, buildType))
.reduce((l, r) => l + r, 0);

return {size, label: `bundle ${this.budget.name}-${buildType}`};
return { size, label: `bundle ${this.budget.name}-${buildTypeLabels[buildType]}` };
});

// If this bundle was not actually generated by a differential build, then
Expand All @@ -232,13 +233,14 @@ class BundleCalculator extends Calculator {
*/
class InitialCalculator extends Calculator {
calculate() {
const buildTypeLabels = getBuildTypeLabels(this.processResults);
const buildSizes = Object.values(DifferentialBuildType).map((buildType) => {
return {
label: `bundle initial-${buildType}`,
label: `bundle initial-${buildTypeLabels[buildType]}`,
size: this.chunks
.filter(chunk => chunk.initial)
.map(chunk => this.calculateChunkSize(chunk, buildType))
.reduce((l, r) => l + r, 0),
.filter(chunk => chunk.initial)
.map(chunk => this.calculateChunkSize(chunk, buildType))
.reduce((l, r) => l + r, 0),
};
});

Expand Down Expand Up @@ -432,3 +434,19 @@ function mergeDifferentialBuildSizes(buildSizes: Size[], mergeLabel: string): Si
function allEquivalent<T>(items: Iterable<T>): boolean {
return new Set(items).size < 2;
}

function getBuildTypeLabels(processResults: ProcessBundleResult[]): Record<DifferentialBuildType, string> {
const fileNameSuffixRegExp = /\-(es20\d{2}|esnext)\./;
const originalFileName = processResults
.find(({ original }) => original?.filename && fileNameSuffixRegExp.test(original.filename))?.original?.filename;

let originalSuffix: string | undefined;
if (originalFileName) {
originalSuffix = fileNameSuffixRegExp.exec(originalFileName)?.[1];
}

return {
[DifferentialBuildType.DOWNLEVEL]: 'es5',
[DifferentialBuildType.ORIGINAL]: originalSuffix || 'es2015',
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ describe('bundle-calculator', () => {
name: '0',
// Individual builds are under budget, but combined they are over.
original: {
filename: 'initial-es2015.js',
filename: 'initial-es2017.js',
size: 1.25 * KB,
},
downlevel: {
Expand All @@ -255,7 +255,7 @@ describe('bundle-calculator', () => {
expect(failures.length).toBe(2);
expect(failures).toContain({
severity: ThresholdSeverity.Error,
message: jasmine.stringMatching('bundle initial-es2015 exceeded maximum budget.'),
message: jasmine.stringMatching('bundle initial-es2017 exceeded maximum budget.'),
});
expect(failures).toContain({
severity: ThresholdSeverity.Error,
Expand Down

0 comments on commit f6bca53

Please sign in to comment.