Skip to content

Commit

Permalink
fix(@angular/build): fix incorrect budget calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
santoshyadavdev committed Jan 7, 2025
1 parent 9b883fe commit 27710bb
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 25 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 BYTES_IN_KILOBYTE = 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
81 changes: 59 additions & 22 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,13 @@
* 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,
BYTES_IN_KILOBYTE,
} from './bundle-calculator';

describe('bundle-calculator', () => {
describe('checkBudgets()', () => {
Expand All @@ -24,11 +28,11 @@ describe('bundle-calculator', () => {
assets: [
{
name: 'foo.js',
size: 1.5 * kB,
size: 1.5 * BYTES_IN_KILOBYTE,
},
{
name: 'bar.js',
size: 0.5 * kB,
size: 0.5 * BYTES_IN_KILOBYTE,
},
],
};
Expand All @@ -55,11 +59,11 @@ describe('bundle-calculator', () => {
assets: [
{
name: 'foo.js',
size: 1.5 * kB,
size: 1.5 * BYTES_IN_KILOBYTE,
},
{
name: 'bar.js',
size: 0.5 * kB,
size: 0.5 * BYTES_IN_KILOBYTE,
},
],
};
Expand Down Expand Up @@ -93,11 +97,11 @@ describe('bundle-calculator', () => {
assets: [
{
name: 'foo.js',
size: 0.75 * kB,
size: 0.75 * BYTES_IN_KILOBYTE,
},
{
name: 'bar.js',
size: 0.75 * kB,
size: 0.75 * BYTES_IN_KILOBYTE,
},
],
};
Expand Down Expand Up @@ -131,11 +135,11 @@ describe('bundle-calculator', () => {
assets: [
{
name: 'foo.js',
size: 0.5 * kB,
size: 0.5 * BYTES_IN_KILOBYTE,
},
{
name: 'bar.js',
size: 0.75 * kB,
size: 0.75 * BYTES_IN_KILOBYTE,
},
],
};
Expand Down Expand Up @@ -169,15 +173,15 @@ describe('bundle-calculator', () => {
assets: [
{
name: 'foo.js',
size: 0.75 * kB,
size: 0.75 * BYTES_IN_KILOBYTE,
},
{
name: 'bar.js',
size: 0.75 * kB,
size: 0.75 * BYTES_IN_KILOBYTE,
},
{
name: 'baz.css',
size: 1.5 * kB,
size: 1.5 * BYTES_IN_KILOBYTE,
},
],
};
Expand Down Expand Up @@ -211,11 +215,11 @@ describe('bundle-calculator', () => {
assets: [
{
name: 'foo.js',
size: 0.75 * kB,
size: 0.75 * BYTES_IN_KILOBYTE,
},
{
name: 'bar.css',
size: 0.75 * kB,
size: 0.75 * BYTES_IN_KILOBYTE,
},
],
};
Expand Down Expand Up @@ -249,11 +253,11 @@ describe('bundle-calculator', () => {
assets: [
{
name: 'foo.css',
size: 1.5 * kB,
size: 1.5 * BYTES_IN_KILOBYTE,
},
{
name: 'bar.js',
size: 0.5 * kB,
size: 0.5 * BYTES_IN_KILOBYTE,
},
],
};
Expand Down Expand Up @@ -282,11 +286,11 @@ describe('bundle-calculator', () => {
assets: [
{
name: 'foo.js',
size: 1.5 * kB,
size: 1.5 * BYTES_IN_KILOBYTE,
},
{
name: 'bar.js',
size: 0.5 * kB,
size: 0.5 * BYTES_IN_KILOBYTE,
},
],
};
Expand Down Expand Up @@ -320,11 +324,11 @@ describe('bundle-calculator', () => {
assets: [
{
name: 'foo.ext',
size: 1.5 * kB,
size: 1.5 * BYTES_IN_KILOBYTE,
},
{
name: 'bar.ext',
size: 0.5 * kB,
size: 0.5 * BYTES_IN_KILOBYTE,
},
],
};
Expand All @@ -338,5 +342,38 @@ describe('bundle-calculator', () => {
message: jasmine.stringMatching('foo.ext exceeded maximum budget.'),
});
});

it('does not exceed the individual file budget limit', () => {
const budgets: BudgetEntry[] = [
{
type: BudgetType.bundle,
maximumError: '1000kb',
},
];
const stats = {
chunks: [
{
id: 0,
initial: true,
names: ['main'],
files: ['main.ext', 'bar.ext'],
},
],
assets: [
{
name: 'main.ext',
size: 1 * BYTES_IN_KILOBYTE,
},
{
name: 'bar.ext',
size: 0.5 * BYTES_IN_KILOBYTE,
},
],
};

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

expect(failures).toHaveSize(0);
});
});
});

0 comments on commit 27710bb

Please sign in to comment.