Skip to content

Commit 047f5e3

Browse files
committed
feat(@angular-devkit/build-angular): add profile option to browser builder
This should help users send us profile logs for builds that take too long.
1 parent 91cf2d7 commit 047f5e3

File tree

8 files changed

+68
-3
lines changed

8 files changed

+68
-3
lines changed

Diff for: packages/angular_devkit/build_angular/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"semver": "5.5.1",
4141
"source-map-support": "0.5.9",
4242
"source-map-loader": "0.2.4",
43+
"speed-measure-webpack-plugin": "^1.2.3",
4344
"stats-webpack-plugin": "0.7.0",
4445
"style-loader": "0.23.0",
4546
"stylus": "0.54.5",

Diff for: packages/angular_devkit/build_angular/src/angular-cli-files/models/build-options.ts

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export interface BuildOptions {
5252
skipAppShell?: boolean;
5353
statsJson: boolean;
5454
forkTypeChecker: boolean;
55+
profile?: boolean;
5556

5657
main: string;
5758
index: string;

Diff for: packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/common.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// TODO: cleanup this file, it's copied as is from Angular CLI.
1010

1111
import * as path from 'path';
12-
import { HashedModuleIdsPlugin } from 'webpack';
12+
import { HashedModuleIdsPlugin, debug } from 'webpack';
1313
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
1414
import { getOutputHashFormat } from './utils';
1515
import { isDirectory } from '../../utilities/is-directory';
@@ -69,6 +69,12 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
6969
];
7070
}
7171

72+
if (buildOptions.profile) {
73+
extraPlugins.push(new debug.ProfilingPlugin({
74+
outputPath: path.resolve(root, 'chrome-profiler-events.json'),
75+
}))
76+
}
77+
7278
// determine hashing format
7379
const hashFormat = getOutputHashFormat(buildOptions.outputHashing as any);
7480

Diff for: packages/angular_devkit/build_angular/src/browser/index.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
BuilderContext,
1313
} from '@angular-devkit/architect';
1414
import { LoggingCallback, WebpackBuilder } from '@angular-devkit/build-webpack';
15-
import { Path, getSystemPath, normalize, resolve, virtualFs } from '@angular-devkit/core';
15+
import { Path, getSystemPath, join, normalize, resolve, virtualFs } from '@angular-devkit/core';
1616
import * as fs from 'fs';
1717
import { Observable, concat, of, throwError } from 'rxjs';
1818
import { concatMap, last, tap } from 'rxjs/operators';
@@ -36,6 +36,7 @@ import {
3636
} from '../angular-cli-files/utilities/stats';
3737
import { defaultProgress, normalizeAssetPatterns, normalizeFileReplacements } from '../utils';
3838
import { AssetPatternObject, BrowserBuilderSchema, CurrentFileReplacement } from './schema';
39+
const SpeedMeasurePlugin = require('speed-measure-webpack-plugin');
3940
const webpackMerge = require('webpack-merge');
4041

4142

@@ -154,7 +155,18 @@ export class BrowserBuilder implements Builder<BrowserBuilderSchema> {
154155
webpackConfigs.push(typescriptConfigPartial);
155156
}
156157

157-
return webpackMerge(webpackConfigs);
158+
const webpackConfig = webpackMerge(webpackConfigs);
159+
160+
if (options.profile) {
161+
const smp = new SpeedMeasurePlugin({
162+
outputFormat: 'json',
163+
outputTarget: getSystemPath(join(root, 'speed-measure-plugin.json')),
164+
});
165+
166+
return smp.wrap(webpackConfig);
167+
}
168+
169+
return webpackConfig;
158170
}
159171

160172
private _deleteOutputDir(root: Path, outputPath: Path, host: virtualFs.Host) {

Diff for: packages/angular_devkit/build_angular/src/browser/schema.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ export interface BrowserBuilderSchema {
222222
* Budget thresholds to ensure parts of your application stay within boundaries which you set.
223223
*/
224224
budgets: Budget[];
225+
226+
/**
227+
* Output profile events for Chrome profiler.
228+
*/
229+
profile: boolean;
225230
}
226231

227232
export type AssetPattern = string | AssetPatternObject;

Diff for: packages/angular_devkit/build_angular/src/browser/schema.json

+5
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,11 @@
236236
"$ref": "#/definitions/budget"
237237
},
238238
"default": []
239+
},
240+
"profile": {
241+
"type": "boolean",
242+
"description": "Output profile events for Chrome profiler.",
243+
"default": false
239244
}
240245
},
241246
"additionalProperties": false,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { runTargetSpec } from '@angular-devkit/architect/testing';
10+
import { normalize } from '@angular-devkit/core';
11+
import { tap } from 'rxjs/operators';
12+
import { browserTargetSpec, host } from '../utils';
13+
14+
15+
describe('Browser Builder profile', () => {
16+
beforeEach(done => host.initialize().toPromise().then(done, done.fail));
17+
afterEach(done => host.restore().toPromise().then(done, done.fail));
18+
19+
it('works', (done) => {
20+
const overrides = { profile: true };
21+
runTargetSpec(host, browserTargetSpec, overrides).pipe(
22+
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
23+
tap(() => {
24+
expect(host.scopedSync().exists(normalize('chrome-profiler-events.json'))).toBe(true);
25+
expect(host.scopedSync().exists(normalize('speed-measure-plugin.json'))).toBe(true);
26+
}),
27+
).toPromise().then(done, done.fail);
28+
});
29+
});

Diff for: yarn.lock

+6
Original file line numberDiff line numberDiff line change
@@ -7082,6 +7082,12 @@ spdy@^3.4.1:
70827082
select-hose "^2.0.0"
70837083
spdy-transport "^2.0.18"
70847084

7085+
speed-measure-webpack-plugin@^1.2.3:
7086+
version "1.2.3"
7087+
resolved "https://registry.yarnpkg.com/speed-measure-webpack-plugin/-/speed-measure-webpack-plugin-1.2.3.tgz#de170b5cefbfa1c039d95e639edd3ad50cfc7c48"
7088+
dependencies:
7089+
chalk "^2.0.1"
7090+
70857091
split-string@^3.0.1, split-string@^3.0.2:
70867092
version "3.1.0"
70877093
resolved "https://registry.yarnpkg.com/split-string/-/split-string-3.1.0.tgz#7cb09dda3a86585705c64b39a6466038682e8fe2"

0 commit comments

Comments
 (0)