Skip to content

Commit

Permalink
Human readable file size in bundle analyzer report (#7766)
Browse files Browse the repository at this point in the history
  • Loading branch information
solstice23 committed Mar 11, 2022
1 parent 8442c62 commit ef16fad
Showing 1 changed file with 10 additions and 1 deletion.
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)`;
}

const UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
function formatSize(x) {
let l = 0, n = parseInt(x, 10) || 0;
while(n >= 1000 && ++l){
n /= 1000;
}
return(`${n.toFixed(l > 0 ? 2 : 0)} ${UNITS[l]}`);
}

0 comments on commit ef16fad

Please sign in to comment.