Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@angular-devkit/build-angular): display correct filename for bundles that are ES2016+ #20242

Merged
merged 1 commit into from Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -37,9 +37,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 @@ -214,15 +213,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 @@ -240,13 +241,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 @@ -440,3 +442,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',
};
}
Expand Up @@ -243,7 +243,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 @@ -258,7 +258,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