Skip to content

Commit 46fd953

Browse files
winkerVSbecksZhicheng Wang
authored and
Zhicheng Wang
committed
feat(@angular/cli): add --stats-json flag to build (angular#4189)
Allows you to run ng build --json which generates dist/stats.json. This can then be used with things like the webpack-bundle-analyzer or https://webpack.github.io/analyse/ Fix angular#3179
1 parent 83fd5e1 commit 46fd953

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

docs/documentation/build.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,5 @@ or `ng serve --prod` will also make use of uglifying and tree-shaking functional
9494
`--extract-css` (`-ec`) extract css from global styles onto css files instead of js ones
9595

9696
`--output-hashing` define the output filename cache-busting hashing mode
97+
98+
`--stats-json` generates a `stats.json` file which can be analyzed using tools such as: `webpack-bundle-analyzer` or https://webpack.github.io/analyse

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ export const baseBuildCommandOptions: any = [
3131
description: 'define the output filename cache-busting hashing mode',
3232
aliases: ['oh']
3333
},
34+
{ name: 'stats-json', type: Boolean, default: false },
3435
];
3536

3637
export interface BuildTaskOptions extends BuildOptions {
3738
watch?: boolean;
39+
statsJson?: boolean;
3840
}
3941

4042
const BuildCommand = Command.extend({

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

Lines changed: 10 additions & 0 deletions
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+
const fs = require('fs');
1011

1112

1213
export default Task.extend({
@@ -36,6 +37,15 @@ export default Task.extend({
3637
return;
3738
}
3839

40+
if (!runTaskOptions.watch && runTaskOptions.statsJson) {
41+
const jsonStats = stats.toJson('verbose');
42+
43+
fs.writeFileSync(
44+
path.resolve(project.root, outputPath, 'stats.json'),
45+
JSON.stringify(jsonStats, null, 2)
46+
);
47+
}
48+
3949
if (stats.hasErrors()) {
4050
reject();
4151
} else {

tests/e2e/tests/build/json.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {ng} from '../../utils/process';
2+
import {expectFileToExist} from '../../utils/fs';
3+
import {expectGitToBeClean} from '../../utils/git';
4+
5+
6+
export default function() {
7+
return ng('build', '--stats-json')
8+
.then(() => expectFileToExist('./dist/stats.json'))
9+
.then(() => expectGitToBeClean());
10+
}

0 commit comments

Comments
 (0)