print nice build summaries

This commit is contained in:
Rich Harris
2018-08-29 15:03:10 -04:00
parent 458be49b35
commit 6393a30b13
5 changed files with 98 additions and 16 deletions

View File

@@ -1,7 +1,10 @@
import * as fs from 'fs';
import * as path from 'path';
import colors from 'kleur';
import pb from 'pretty-bytes';
import { locations } from '../config';
import relative from 'require-relative';
import { left_pad, right_pad } from '../utils';
let r: any;
let wp: any;
@@ -20,6 +23,8 @@ export class CompileResult {
}
class RollupResult extends CompileResult {
summary: string;
constructor(duration: number, compiler: RollupCompiler) {
super();
@@ -39,10 +44,51 @@ class RollupResult extends CompileResult {
this.assetsByChunkName.main = chunk.fileName;
}
});
this.summary = compiler.chunks.map(chunk => {
const size_color = chunk.code.length > 150000 ? colors.bold.red : chunk.code.length > 50000 ? colors.bold.yellow : colors.bold.white;
const size_label = left_pad(pb(chunk.code.length), 10);
const lines = [size_color(`${size_label} ${chunk.fileName}`)];
const deps = Object.keys(chunk.modules)
.map(file => {
return {
file: path.relative(process.cwd(), file),
size: chunk.modules[file].renderedLength
};
})
.filter(dep => dep.size > 0)
.sort((a, b) => b.size - a.size);
const total_unminified = deps.reduce((t, d) => t + d.size, 0);
deps.forEach((dep, i) => {
const c = i === deps.length - 1 ? '└' : '│';
let line = ` ${c} ${dep.file}`;
if (deps.length > 1) {
const p = (100 * dep.size / total_unminified).toFixed(1);
line += ` (${p}%)`;
}
lines.push(colors.gray(line));
});
return lines.join('\n');
}).join('\n');
}
print() {
return 'TODO summarise build';
const blocks: string[] = this.warnings.map(warning => {
return warning.file
? `> ${colors.bold(warning.file)}\n${warning.message}`
: `> ${warning.message}`;
});
blocks.push(this.summary);
return blocks.join('\n\n');
}
}
@@ -148,10 +194,23 @@ export class RollupCompiler {
const start = Date.now();
const bundle = await r.rollup(config);
await bundle.write(config.output);
try {
const bundle = await r.rollup(config);
await bundle.write(config.output);
return new RollupResult(Date.now() - start, bundle);
return new RollupResult(Date.now() - start, this);
} catch (err) {
if (err.filename) {
// TODO this is a bit messy. Also, can
// Rollup emit other kinds of error?
err.message = [
`Failed to build — error in ${err.filename}: ${err.message}`,
err.frame
].filter(Boolean).join('\n');
}
throw err;
}
}
async watch(cb: (err?: Error, stats?: any) => void) {