Skip to content

Commit

Permalink
Add some stats to the 'yarn build' command
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Jun 11, 2019
1 parent 35c68d4 commit 4fab7bd
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions resources/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

'use strict';

const fs = require('fs');
const path = require('path');
const assert = require('assert');
const babel = require('@babel/core');
Expand Down Expand Up @@ -35,6 +36,53 @@ if (require.main === module) {
);

writeFile('./dist/package.json', JSON.stringify(packageJSON, null, 2));
showStats();
}

function showStats() {
const fileTypes = {};
let totalSize = 0;

for (const filepath of readdirRecursive('./dist')) {
const name = filepath.split(path.sep).pop();
let [base, ...ext] = name.split('.');
ext = ext.join('.');

const filetype = ext ? '*.' + ext : base;
fileTypes[filetype] = fileTypes[filetype] || { filepaths: [], size: 0 };

const { size } = fs.lstatSync(path.join('./dist', filepath));
totalSize += size;
fileTypes[filetype].size += size;
fileTypes[filetype].filepaths.push(filepath);
}

let stats = [];
for (const [filetype, typeStats] of Object.entries(fileTypes)) {
const numFiles = typeStats.filepaths.length;

if (numFiles > 1) {
stats.push([filetype + ' x' + numFiles, typeStats.size]);
} else {
stats.push([typeStats.filepaths[0], typeStats.size]);
}
}
stats.sort((a, b) => b[1] - a[1]);
stats = stats.map(([type, size]) => [type, (size / 1024).toFixed(2) + ' KB']);

const typeMaxLength = Math.max(...stats.map(x => x[0].length));
const sizeMaxLength = Math.max(...stats.map(x => x[1].length));
for (const [type, size] of stats) {
console.log(
type.padStart(typeMaxLength) + ' | ' + size.padStart(sizeMaxLength),
);
}

console.log('-'.repeat(typeMaxLength + 3 + sizeMaxLength));
const totalMB = (totalSize / 1024 / 1024).toFixed(2) + ' MB';
console.log(
'Total'.padStart(typeMaxLength) + ' | ' + totalMB.padStart(sizeMaxLength),
);
}

function babelBuild(srcPath, envName) {
Expand Down

0 comments on commit 4fab7bd

Please sign in to comment.