Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 *= BYTES_IN_KILOBYTE;
break;
case 'mb':
value *= 1024 * 1024;
value *= BYTES_IN_KILOBYTE * BYTES_IN_KILOBYTE;
break;
case 'gb':
value *= 1024 * 1024 * 1024;
value *= BYTES_IN_KILOBYTE * BYTES_IN_KILOBYTE * BYTES_IN_KILOBYTE;
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 {
BYTES_IN_KILOBYTE,
BudgetEntry,
BudgetType,
ThresholdSeverity,
checkBudgets,
} 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);
});
});
});
Loading