Skip to content
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

Human readable file size in bundle analyzer report #7766

Merged
merged 7 commits into from Mar 11, 2022
11 changes: 10 additions & 1 deletion packages/reporters/bundle-analyzer/client/index.js
Expand Up @@ -53,7 +53,7 @@ let foamtree = new CarrotSearchFoamTree({
<dl>
<div>
<dt>Size</dt>
<dd>${e.group.weight} bytes</dd>
<dd>${formatSize(e.group.weight)}</dd>
</div>
</dl>
</div>
Expand Down Expand Up @@ -100,3 +100,12 @@ function debounce(fn, delay) {
function translate3d(x, y, z) {
return `translate3d(${x}px, ${y}px, ${z}px)`;
}

function formatSize(x){
const units = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
let l = 0, n = parseInt(x, 10) || 0;
while(n >= 1024 && ++l){
n /= 1024;
}
return(`${n.toFixed(n < 10 && l > 0 ? 1 : 0)} ${units[l]}` + (l ? ` (${x} bytes)` : ''));
}
solstice23 marked this conversation as resolved.
Show resolved Hide resolved