UNPKG

zoro-cli

Version:

https://github.com/vuejs/vue-cli

95 lines (84 loc) 2.18 kB
module.exports = function formatStats(stats, dir) { const fs = require('fs') const path = require('path') const zlib = require('zlib') const chalk = require('chalk') const ui = require('cliui')({ width: 80 }) const json = stats.toJson({ hash: false, modules: false, chunks: false, }) let assets = json.assets ? json.assets : json.children.reduce((acc, child) => acc.concat(child.assets), []) const seenNames = new Map() const isJS = val => /\.js$/.test(val) const isCSS = val => /\.css$/.test(val) const isHTML = val => /\.html$/.test(val) const extOrder = { js: 1, css: 2, html: 3, } assets = assets .filter(a => { if (seenNames.has(a.name)) { return false } seenNames.set(a.name, true) return isJS(a.name) || isCSS(a.name) || isHTML(a.name) }) .sort((a, b) => { const ext1 = path.extname(a.name) const ext2 = path.extname(b.name) if (ext1 === ext2) { return b.size - a.size } return extOrder[ext1] - extOrder[ext2] }) function formatSize(size) { return (size / 1024).toFixed(2) + ' kb' } function getGzippedSize(asset) { const filepath = path.resolve(dir, asset.name) const buffer = fs.readFileSync(filepath) return formatSize(zlib.gzipSync(buffer).length) } function makeRow(a, b, c) { return ` ${a}\t ${b}\t ${c}` } function genFileRow(asset) { const { name } = asset if (isJS(name)) { return chalk.green(path.join(dir, name)) } if (isCSS(name)) { return chalk.blue(path.join(dir, name)) } if (isHTML(name)) { return chalk.cyan(path.join(dir, name)) } return path.join(dir, name) } ui.div( makeRow( chalk.cyan.bold('File'), chalk.cyan.bold('Size'), chalk.cyan.bold('Gzipped') ) + '\n\n' + assets .map(asset => makeRow( genFileRow(asset), formatSize(asset.size), getGzippedSize(asset) ) ) .join('\n') ) return `${ui.toString()}\n\n ${chalk.gray( 'Images and other types of assets omitted.' )}\n` }