Skip to content

Commit db10af0

Browse files
santoshyadavdevjkrems
authored andcommittedJan 8, 2025·
fix(@angular/build): fix incorrect budget calculation
This PR makes change to kB value which was incorrect earlier Fixes #29040 (cherry picked from commit e76800c)
1 parent 1f2481a commit db10af0

File tree

2 files changed

+64
-25
lines changed

2 files changed

+64
-25
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 BYTES_IN_KILOBYTE = 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 *= BYTES_IN_KILOBYTE;
310312
break;
311313
case 'mb':
312-
value *= 1024 * 1024;
314+
value *= BYTES_IN_KILOBYTE * BYTES_IN_KILOBYTE;
313315
break;
314316
case 'gb':
315-
value *= 1024 * 1024 * 1024;
317+
value *= BYTES_IN_KILOBYTE * BYTES_IN_KILOBYTE * BYTES_IN_KILOBYTE;
316318
break;
317319
}
318320

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

+59-22
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
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 {
10+
BYTES_IN_KILOBYTE,
11+
BudgetEntry,
12+
BudgetType,
13+
ThresholdSeverity,
14+
checkBudgets,
15+
} from './bundle-calculator';
1216

1317
describe('bundle-calculator', () => {
1418
describe('checkBudgets()', () => {
@@ -24,11 +28,11 @@ describe('bundle-calculator', () => {
2428
assets: [
2529
{
2630
name: 'foo.js',
27-
size: 1.5 * kB,
31+
size: 1.5 * BYTES_IN_KILOBYTE,
2832
},
2933
{
3034
name: 'bar.js',
31-
size: 0.5 * kB,
35+
size: 0.5 * BYTES_IN_KILOBYTE,
3236
},
3337
],
3438
};
@@ -55,11 +59,11 @@ describe('bundle-calculator', () => {
5559
assets: [
5660
{
5761
name: 'foo.js',
58-
size: 1.5 * kB,
62+
size: 1.5 * BYTES_IN_KILOBYTE,
5963
},
6064
{
6165
name: 'bar.js',
62-
size: 0.5 * kB,
66+
size: 0.5 * BYTES_IN_KILOBYTE,
6367
},
6468
],
6569
};
@@ -93,11 +97,11 @@ describe('bundle-calculator', () => {
9397
assets: [
9498
{
9599
name: 'foo.js',
96-
size: 0.75 * kB,
100+
size: 0.75 * BYTES_IN_KILOBYTE,
97101
},
98102
{
99103
name: 'bar.js',
100-
size: 0.75 * kB,
104+
size: 0.75 * BYTES_IN_KILOBYTE,
101105
},
102106
],
103107
};
@@ -131,11 +135,11 @@ describe('bundle-calculator', () => {
131135
assets: [
132136
{
133137
name: 'foo.js',
134-
size: 0.5 * kB,
138+
size: 0.5 * BYTES_IN_KILOBYTE,
135139
},
136140
{
137141
name: 'bar.js',
138-
size: 0.75 * kB,
142+
size: 0.75 * BYTES_IN_KILOBYTE,
139143
},
140144
],
141145
};
@@ -169,15 +173,15 @@ describe('bundle-calculator', () => {
169173
assets: [
170174
{
171175
name: 'foo.js',
172-
size: 0.75 * kB,
176+
size: 0.75 * BYTES_IN_KILOBYTE,
173177
},
174178
{
175179
name: 'bar.js',
176-
size: 0.75 * kB,
180+
size: 0.75 * BYTES_IN_KILOBYTE,
177181
},
178182
{
179183
name: 'baz.css',
180-
size: 1.5 * kB,
184+
size: 1.5 * BYTES_IN_KILOBYTE,
181185
},
182186
],
183187
};
@@ -211,11 +215,11 @@ describe('bundle-calculator', () => {
211215
assets: [
212216
{
213217
name: 'foo.js',
214-
size: 0.75 * kB,
218+
size: 0.75 * BYTES_IN_KILOBYTE,
215219
},
216220
{
217221
name: 'bar.css',
218-
size: 0.75 * kB,
222+
size: 0.75 * BYTES_IN_KILOBYTE,
219223
},
220224
],
221225
};
@@ -249,11 +253,11 @@ describe('bundle-calculator', () => {
249253
assets: [
250254
{
251255
name: 'foo.css',
252-
size: 1.5 * kB,
256+
size: 1.5 * BYTES_IN_KILOBYTE,
253257
},
254258
{
255259
name: 'bar.js',
256-
size: 0.5 * kB,
260+
size: 0.5 * BYTES_IN_KILOBYTE,
257261
},
258262
],
259263
};
@@ -282,11 +286,11 @@ describe('bundle-calculator', () => {
282286
assets: [
283287
{
284288
name: 'foo.js',
285-
size: 1.5 * kB,
289+
size: 1.5 * BYTES_IN_KILOBYTE,
286290
},
287291
{
288292
name: 'bar.js',
289-
size: 0.5 * kB,
293+
size: 0.5 * BYTES_IN_KILOBYTE,
290294
},
291295
],
292296
};
@@ -320,11 +324,11 @@ describe('bundle-calculator', () => {
320324
assets: [
321325
{
322326
name: 'foo.ext',
323-
size: 1.5 * kB,
327+
size: 1.5 * BYTES_IN_KILOBYTE,
324328
},
325329
{
326330
name: 'bar.ext',
327-
size: 0.5 * kB,
331+
size: 0.5 * BYTES_IN_KILOBYTE,
328332
},
329333
],
330334
};
@@ -338,5 +342,38 @@ describe('bundle-calculator', () => {
338342
message: jasmine.stringMatching('foo.ext exceeded maximum budget.'),
339343
});
340344
});
345+
346+
it('does not exceed the individual file budget limit', () => {
347+
const budgets: BudgetEntry[] = [
348+
{
349+
type: BudgetType.Bundle,
350+
maximumError: '1000kb',
351+
},
352+
];
353+
const stats = {
354+
chunks: [
355+
{
356+
id: 0,
357+
initial: true,
358+
names: ['main'],
359+
files: ['main.ext', 'bar.ext'],
360+
},
361+
],
362+
assets: [
363+
{
364+
name: 'main.ext',
365+
size: 1 * BYTES_IN_KILOBYTE,
366+
},
367+
{
368+
name: 'bar.ext',
369+
size: 0.5 * BYTES_IN_KILOBYTE,
370+
},
371+
],
372+
};
373+
374+
const failures = Array.from(checkBudgets(budgets, stats));
375+
376+
expect(failures).toHaveSize(0);
377+
});
341378
});
342379
});

0 commit comments

Comments
 (0)
Please sign in to comment.