Skip to content

Commit e4dd9a3

Browse files
committed
feat(@angular/cli): rewrite stats output to properly show the asset size
This is a feature in principle; the output is changed. The stats themselves dont though.
1 parent 489a3d6 commit e4dd9a3

File tree

4 files changed

+63
-9
lines changed

4 files changed

+63
-9
lines changed

packages/@angular/cli/custom-typings.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface IWebpackDevServerConfigurationOptions {
1515
};
1616
publicPath?: string;
1717
headers?: { [key: string]: string };
18-
stats?: { [key: string]: boolean };
18+
stats?: { [key: string]: boolean } | string;
1919
inline: boolean;
2020
https?: boolean;
2121
key?: string;

packages/@angular/cli/tasks/build.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { BuildTaskOptions } from '../commands/build';
77
import { NgCliWebpackConfig } from '../models/webpack-config';
88
import { getWebpackStatsConfig } from '../models/webpack-configs/utils';
99
import { CliConfig } from '../models/config';
10+
import {statsToString} from '../utilities/stats';
1011

1112
const Task = require('../ember-cli/lib/models/task');
1213
const SilentError = require('silent-error');
@@ -39,18 +40,19 @@ export default Task.extend({
3940
return reject(err);
4041
}
4142

42-
this.ui.writeLine(stats.toString(statsConfig));
43+
const json = stats.toJson('verbose');
44+
if (runTaskOptions.verbose) {
45+
this.ui.writeLine(stats.toString(statsConfig));
46+
} else {
47+
this.ui.writeLine(statsToString(json, statsConfig));
48+
}
4349

4450
if (runTaskOptions.watch) {
4551
return;
46-
}
47-
48-
if (!runTaskOptions.watch && runTaskOptions.statsJson) {
49-
const jsonStats = stats.toJson('verbose');
50-
52+
} else if (runTaskOptions.statsJson) {
5153
fs.writeFileSync(
5254
path.resolve(this.project.root, outputPath, 'stats.json'),
53-
JSON.stringify(jsonStats, null, 2)
55+
JSON.stringify(json, null, 2)
5456
);
5557
}
5658

packages/@angular/cli/tasks/serve.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { NgCliWebpackConfig } from '../models/webpack-config';
99
import { ServeTaskOptions } from '../commands/serve';
1010
import { CliConfig } from '../models/config';
1111
import { getAppFromConfig } from '../utilities/app-utils';
12+
import {statsToString} from '../utilities/stats';
1213

1314
const WebpackDevServer = require('webpack-dev-server');
1415
const Task = require('../ember-cli/lib/models/task');
@@ -159,7 +160,7 @@ export default Task.extend({
159160
disableDotRule: true,
160161
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml']
161162
},
162-
stats: statsConfig,
163+
stats: serveTaskOptions.verbose ? statsConfig : 'none',
163164
inline: true,
164165
proxy: proxyConfig,
165166
compress: serveTaskOptions.target === 'production',
@@ -209,6 +210,12 @@ export default Task.extend({
209210
`));
210211

211212
const server = new WebpackDevServer(webpackCompiler, webpackDevServerConfiguration);
213+
if (!serveTaskOptions.verbose) {
214+
webpackCompiler.plugin('done', (stats: any) => {
215+
this.ui.writeLine(statsToString(stats.toJson(), statsConfig));
216+
});
217+
}
218+
212219
return new Promise((_resolve, reject) => {
213220
server.listen(serveTaskOptions.port, serveTaskOptions.host, (err: any, _stats: any) => {
214221
if (err) {
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { bold, green, reset, white, yellow } from 'chalk';
2+
import { stripIndents } from 'common-tags';
3+
4+
5+
function _formatSize(size: number): string {
6+
if (size <= 0) {
7+
return '0 bytes';
8+
}
9+
10+
const abbreviations = ['bytes', 'kB', 'MB', 'GB'];
11+
const index = Math.floor(Math.log(size) / Math.log(1000));
12+
13+
return `${+(size / Math.pow(1000, index)).toPrecision(3)} ${abbreviations[index]}`;
14+
}
15+
16+
17+
export function statsToString(json: any, statsConfig: any) {
18+
const colors = statsConfig.colors;
19+
const r = (x: string) => colors ? reset(x) : x;
20+
const w = (x: string) => colors ? bold(white(x)) : x;
21+
const g = (x: string) => colors ? bold(green(x)) : x;
22+
const y = (x: string) => colors ? bold(yellow(x)) : x;
23+
24+
return r(stripIndents`
25+
Date: ${w(new Date().toISOString())}
26+
Hash: ${w(json.hash)}
27+
Time: ${w('' + json.time)}ms
28+
${json.chunks.map((chunk: any) => {
29+
const asset = json.assets.filter((x: any) => x.name == chunk.files[0])[0];
30+
const size = asset ? ` ${_formatSize(asset.size)}` : '';
31+
const files = chunk.files.join(', ');
32+
const names = chunk.names ? ` (${chunk.names.join(', ')})` : '';
33+
const parents = chunk.parents.map((id: string) => ` {${y(id)}}`).join('');
34+
const initial = y(chunk.entry ? '[entry]' : chunk.initial ? '[initial]' : '');
35+
const flags = ['rendered', 'recorded']
36+
.map(f => f && chunk[f] ? g(` [${f}]`) : '')
37+
.join('');
38+
39+
return `chunk {${y(chunk.id)}} ${g(files)}${names}${size}${parents} ${initial}${flags}`;
40+
}).join('\n')}
41+
42+
${json.warnings.map((warning: any) => y(`WARNING in ${warning}`)).join('\n\n')}
43+
${json.errors.map((error: any) => y(`ERROR in ${error}`)).join('\n')}
44+
`);
45+
}

0 commit comments

Comments
 (0)