Skip to content

Commit fbc4b53

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 94e2ad9 commit fbc4b53

File tree

9 files changed

+71
-3
lines changed

9 files changed

+71
-3
lines changed

package-lock.json

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
"source-map": "^0.5.6",
126126
"source-map-loader": "^0.2.3",
127127
"source-map-support": "^0.5.0",
128+
"speed-measure-webpack-plugin": "^1.2.2",
128129
"stats-webpack-plugin": "^0.6.2",
129130
"style-loader": "^0.21.0",
130131
"stylus": "^0.54.5",

packages/angular_devkit/build_angular/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"sass-loader": "~6.0.7",
4343
"source-map-support": "^0.5.0",
4444
"source-map-loader": "^0.2.3",
45+
"speed-measure-webpack-plugin": "^1.2.2",
4546
"stats-webpack-plugin": "^0.6.2",
4647
"style-loader": "^0.21.0",
4748
"stylus": "^0.54.5",

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

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export interface BuildOptions {
4747
skipAppShell?: boolean;
4848
statsJson: boolean;
4949
forkTypeChecker: boolean;
50+
profile?: boolean;
5051

5152
main: string;
5253
index: string;

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';
@@ -62,6 +62,12 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
6262
entryPoints['polyfills'] = [path.resolve(root, buildOptions.polyfills)];
6363
}
6464

65+
if (buildOptions.profile) {
66+
extraPlugins.push(new debug.ProfilingPlugin({
67+
outputPath: path.resolve(root, 'chrome-profiler-events.json'),
68+
}))
69+
}
70+
6571
// determine hashing format
6672
const hashFormat = getOutputHashFormat(buildOptions.outputHashing as any);
6773

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 { addFileReplacements, normalizeAssetPatterns } 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

@@ -151,7 +152,18 @@ export class BrowserBuilder implements Builder<BrowserBuilderSchema> {
151152
webpackConfigs.push(typescriptConfigPartial);
152153
}
153154

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

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

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;

packages/angular_devkit/build_angular/src/browser/schema.json

+5
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@
237237
"$ref": "#/definitions/budget"
238238
},
239239
"default": []
240+
},
241+
"profile": {
242+
"type": "boolean",
243+
"description": "Output profile events for Chrome profiler.",
244+
"default": false
240245
}
241246
},
242247
"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+
});

0 commit comments

Comments
 (0)