Skip to content

Commit 9e12006

Browse files
committed
Merge pull request webpack#852 from hkal/improve-size-readability-abbreviations
Alternative improve size output readability
2 parents 17d9dc1 + 62d87e2 commit 9e12006

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

lib/Stats.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,15 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
339339
newline();
340340
}
341341
}
342+
function formatSize(size) {
343+
if(size <= 0) return "0 bytes";
344+
345+
var abbreviations = ["bytes", "kB", "MB", "GB"];
346+
var index = Math.floor(Math.log(size) / Math.log(1000));
347+
348+
return +(size / Math.pow(1000, index))
349+
.toPrecision(3) + ' ' + abbreviations[index];
350+
}
342351

343352
if(obj.hash) {
344353
normal("Hash: ");
@@ -361,7 +370,7 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
361370
obj.assets.forEach(function(asset) {
362371
t.push([
363372
asset.name,
364-
asset.size,
373+
formatSize(asset.size),
365374
asset.chunks.join(", "),
366375
asset.emitted ? "[emitted]" : "",
367376
asset.chunkNames.join(", ")
@@ -424,7 +433,7 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
424433
}
425434
function processModuleAttributes(module) {
426435
normal(" ");
427-
normal(module.size);
436+
normal(formatSize(module.size));
428437
if(module.chunks) {
429438
module.chunks.forEach(function(chunk) {
430439
normal(" {");
@@ -467,7 +476,7 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
467476
normal(")");
468477
}
469478
normal(" ");
470-
normal(chunk.size);
479+
normal(formatSize(chunk.size));
471480
chunk.parents.forEach(function(id) {
472481
normal(" {");
473482
yellow(id);

0 commit comments

Comments
 (0)