Skip to content

fix(@angular/build): format sizes using decimal byte units consistently #27602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2024
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
40 changes: 20 additions & 20 deletions packages/angular/build/src/utils/bundle-calculator_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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

const KB = 1024;
const kB = 1000;

describe('bundle-calculator', () => {
describe('checkBudgets()', () => {
Expand All @@ -24,11 +24,11 @@ describe('bundle-calculator', () => {
assets: [
{
name: 'foo.js',
size: 1.5 * KB,
size: 1.5 * kB,
},
{
name: 'bar.js',
size: 0.5 * KB,
size: 0.5 * kB,
},
],
};
Expand All @@ -55,11 +55,11 @@ describe('bundle-calculator', () => {
assets: [
{
name: 'foo.js',
size: 1.5 * KB,
size: 1.5 * kB,
},
{
name: 'bar.js',
size: 0.5 * KB,
size: 0.5 * kB,
},
],
};
Expand Down Expand Up @@ -93,11 +93,11 @@ describe('bundle-calculator', () => {
assets: [
{
name: 'foo.js',
size: 0.75 * KB,
size: 0.75 * kB,
},
{
name: 'bar.js',
size: 0.75 * KB,
size: 0.75 * kB,
},
],
};
Expand Down Expand Up @@ -131,11 +131,11 @@ describe('bundle-calculator', () => {
assets: [
{
name: 'foo.js',
size: 0.5 * KB,
size: 0.5 * kB,
},
{
name: 'bar.js',
size: 0.75 * KB,
size: 0.75 * kB,
},
],
};
Expand Down Expand Up @@ -169,15 +169,15 @@ describe('bundle-calculator', () => {
assets: [
{
name: 'foo.js',
size: 0.75 * KB,
size: 0.75 * kB,
},
{
name: 'bar.js',
size: 0.75 * KB,
size: 0.75 * kB,
},
{
name: 'baz.css',
size: 1.5 * KB,
size: 1.5 * kB,
},
],
};
Expand Down Expand Up @@ -211,11 +211,11 @@ describe('bundle-calculator', () => {
assets: [
{
name: 'foo.js',
size: 0.75 * KB,
size: 0.75 * kB,
},
{
name: 'bar.css',
size: 0.75 * KB,
size: 0.75 * kB,
},
],
};
Expand Down Expand Up @@ -249,11 +249,11 @@ describe('bundle-calculator', () => {
assets: [
{
name: 'foo.css',
size: 1.5 * KB,
size: 1.5 * kB,
},
{
name: 'bar.js',
size: 0.5 * KB,
size: 0.5 * kB,
},
],
};
Expand Down Expand Up @@ -282,11 +282,11 @@ describe('bundle-calculator', () => {
assets: [
{
name: 'foo.js',
size: 1.5 * KB,
size: 1.5 * kB,
},
{
name: 'bar.js',
size: 0.5 * KB,
size: 0.5 * kB,
},
],
};
Expand Down Expand Up @@ -320,11 +320,11 @@ describe('bundle-calculator', () => {
assets: [
{
name: 'foo.ext',
size: 1.5 * KB,
size: 1.5 * kB,
},
{
name: 'bar.ext',
size: 0.5 * KB,
size: 0.5 * kB,
},
],
};
Expand Down
4 changes: 2 additions & 2 deletions packages/angular/build/src/utils/format-bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export function formatSize(size: number): string {
}

const abbreviations = ['bytes', 'kB', 'MB', 'GB'];
const index = Math.floor(Math.log(size) / Math.log(1024));
const roundedSize = size / Math.pow(1024, index);
const index = Math.floor(Math.log(size) / Math.log(1000));
const roundedSize = size / Math.pow(1000, index);
// bytes don't have a fraction
const fractionDigits = index === 0 ? 0 : 2;

Expand Down
27 changes: 27 additions & 0 deletions packages/angular/build/src/utils/format-bytes_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import { formatSize } from './format-bytes';

describe('formatSize', () => {
it('1000 bytes to be 1kB', () => {
expect(formatSize(1000)).toBe('1.00 kB');
});

it('1_000_000 bytes to be 1MB', () => {
expect(formatSize(1_000_000)).toBe('1.00 MB');
});

it('1_500_000 bytes to be 1.5MB', () => {
expect(formatSize(1_500_000)).toBe('1.50 MB');
});

it('1_000_000_000 bytes to be 1GB', () => {
expect(formatSize(1_000_000_000)).toBe('1.00 GB');
});
});
18 changes: 9 additions & 9 deletions packages/schematics/angular/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,31 +194,31 @@ function addAppToWorkspaceFile(
}

const sourceRoot = join(normalize(projectRoot), 'src');
let budgets = [];
let budgets: { type: string; maximumWarning: string; maximumError: string }[] = [];
if (options.strict) {
budgets = [
{
type: 'initial',
maximumWarning: '500kb',
maximumError: '1mb',
maximumWarning: '500kB',
maximumError: '1MB',
},
{
type: 'anyComponentStyle',
maximumWarning: '2kb',
maximumError: '4kb',
maximumWarning: '2kB',
maximumError: '4kB',
},
];
} else {
budgets = [
{
type: 'initial',
maximumWarning: '2mb',
maximumError: '5mb',
maximumWarning: '2MB',
maximumError: '5MB',
},
{
type: 'anyComponentStyle',
maximumWarning: '6kb',
maximumError: '10kb',
maximumWarning: '6kB',
maximumError: '10kB',
},
];
}
Expand Down