Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular) : fix incorrect budget calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
santoshyadavdev committed Dec 28, 2024
1 parent 9b883fe commit 656defa
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
8 changes: 5 additions & 3 deletions packages/angular/build/src/utils/bundle-calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { formatSize } from './format-bytes';
// Re-export to avoid direct schema importing throughout code
export { type BudgetEntry, BudgetType };

export const kB = 1000;

interface Size {
size: number;
label?: string;
Expand Down Expand Up @@ -306,13 +308,13 @@ function calculateBytes(input: string, baseline?: string, factor: 1 | -1 = 1): n
value = (baselineBytes * value) / 100;
break;
case 'kb':
value *= 1024;
value *= kB;
break;
case 'mb':
value *= 1024 * 1024;
value *= kB * kB;
break;
case 'gb':
value *= 1024 * 1024 * 1024;
value *= kB * kB * kB;
break;
}

Expand Down
37 changes: 34 additions & 3 deletions packages/angular/build/src/utils/bundle-calculator_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
* found in the LICENSE file at https://angular.dev/license
*/

import { BudgetEntry, BudgetType, ThresholdSeverity, checkBudgets } from './bundle-calculator';

const kB = 1000;
import { BudgetEntry, BudgetType, ThresholdSeverity, checkBudgets, kB } from './bundle-calculator';

describe('bundle-calculator', () => {
describe('checkBudgets()', () => {
Expand Down Expand Up @@ -338,5 +336,38 @@ describe('bundle-calculator', () => {
message: jasmine.stringMatching('foo.ext exceeded maximum budget.'),
});
});

it('yields exceeded individual file budget - 29040', () => {
const budgets: BudgetEntry[] = [
{
type: BudgetType.bundle,
maximumError: '1000kb',
},
];
const stats = {
chunks: [
{
id: 0,
initial: true,
names: ['foo'],
files: ['foo.ext', 'bar.ext'],
},
],
assets: [
{
name: 'foo.ext',
size: 1 * kB,
},
{
name: 'bar.ext',
size: 0.5 * kB,
},
],
};

const failures = Array.from(checkBudgets(budgets, stats));

expect(failures.length).toBe(0);
});
});
});

0 comments on commit 656defa

Please sign in to comment.