Skip to content

Commit 656defa

Browse files
fix(@angular-devkit/build-angular) : fix incorrect budget calculation
Fixes #29040
1 parent 9b883fe commit 656defa

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

packages/angular/build/src/utils/bundle-calculator.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { formatSize } from './format-bytes';
1212
// Re-export to avoid direct schema importing throughout code
1313
export { type BudgetEntry, BudgetType };
1414

15+
export const kB = 1000;
16+
1517
interface Size {
1618
size: number;
1719
label?: string;
@@ -306,13 +308,13 @@ function calculateBytes(input: string, baseline?: string, factor: 1 | -1 = 1): n
306308
value = (baselineBytes * value) / 100;
307309
break;
308310
case 'kb':
309-
value *= 1024;
311+
value *= kB;
310312
break;
311313
case 'mb':
312-
value *= 1024 * 1024;
314+
value *= kB * kB;
313315
break;
314316
case 'gb':
315-
value *= 1024 * 1024 * 1024;
317+
value *= kB * kB * kB;
316318
break;
317319
}
318320

packages/angular/build/src/utils/bundle-calculator_spec.ts

+34-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

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

1311
describe('bundle-calculator', () => {
1412
describe('checkBudgets()', () => {
@@ -338,5 +336,38 @@ describe('bundle-calculator', () => {
338336
message: jasmine.stringMatching('foo.ext exceeded maximum budget.'),
339337
});
340338
});
339+
340+
it('yields exceeded individual file budget - 29040', () => {
341+
const budgets: BudgetEntry[] = [
342+
{
343+
type: BudgetType.bundle,
344+
maximumError: '1000kb',
345+
},
346+
];
347+
const stats = {
348+
chunks: [
349+
{
350+
id: 0,
351+
initial: true,
352+
names: ['foo'],
353+
files: ['foo.ext', 'bar.ext'],
354+
},
355+
],
356+
assets: [
357+
{
358+
name: 'foo.ext',
359+
size: 1 * kB,
360+
},
361+
{
362+
name: 'bar.ext',
363+
size: 0.5 * kB,
364+
},
365+
],
366+
};
367+
368+
const failures = Array.from(checkBudgets(budgets, stats));
369+
370+
expect(failures.length).toBe(0);
371+
});
341372
});
342373
});

0 commit comments

Comments
 (0)