Skip to content

Commit

Permalink
feat(@angular-devkit/build-angular): add profile option to browser bu…
Browse files Browse the repository at this point in the history
…ilder

This should help users send us profile logs for builds that take too long.
  • Loading branch information
filipesilva committed Jul 8, 2018
1 parent 94e2ad9 commit fbc4b53
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 3 deletions.
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
"source-map": "^0.5.6",
"source-map-loader": "^0.2.3",
"source-map-support": "^0.5.0",
"speed-measure-webpack-plugin": "^1.2.2",
"stats-webpack-plugin": "^0.6.2",
"style-loader": "^0.21.0",
"stylus": "^0.54.5",
Expand Down
1 change: 1 addition & 0 deletions packages/angular_devkit/build_angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"sass-loader": "~6.0.7",
"source-map-support": "^0.5.0",
"source-map-loader": "^0.2.3",
"speed-measure-webpack-plugin": "^1.2.2",
"stats-webpack-plugin": "^0.6.2",
"style-loader": "^0.21.0",
"stylus": "^0.54.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface BuildOptions {
skipAppShell?: boolean;
statsJson: boolean;
forkTypeChecker: boolean;
profile?: boolean;

main: string;
index: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// TODO: cleanup this file, it's copied as is from Angular CLI.

import * as path from 'path';
import { HashedModuleIdsPlugin } from 'webpack';
import { HashedModuleIdsPlugin, debug } from 'webpack';
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
import { getOutputHashFormat } from './utils';
import { isDirectory } from '../../utilities/is-directory';
Expand Down Expand Up @@ -62,6 +62,12 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
entryPoints['polyfills'] = [path.resolve(root, buildOptions.polyfills)];
}

if (buildOptions.profile) {
extraPlugins.push(new debug.ProfilingPlugin({
outputPath: path.resolve(root, 'chrome-profiler-events.json'),
}))
}

// determine hashing format
const hashFormat = getOutputHashFormat(buildOptions.outputHashing as any);

Expand Down
16 changes: 14 additions & 2 deletions packages/angular_devkit/build_angular/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
BuilderContext,
} from '@angular-devkit/architect';
import { LoggingCallback, WebpackBuilder } from '@angular-devkit/build-webpack';
import { Path, getSystemPath, normalize, resolve, virtualFs } from '@angular-devkit/core';
import { Path, getSystemPath, join, normalize, resolve, virtualFs } from '@angular-devkit/core';
import * as fs from 'fs';
import { Observable, concat, of, throwError } from 'rxjs';
import { concatMap, last, tap } from 'rxjs/operators';
Expand All @@ -36,6 +36,7 @@ import {
} from '../angular-cli-files/utilities/stats';
import { addFileReplacements, normalizeAssetPatterns } from '../utils';
import { AssetPatternObject, BrowserBuilderSchema, CurrentFileReplacement } from './schema';
const SpeedMeasurePlugin = require('speed-measure-webpack-plugin');
const webpackMerge = require('webpack-merge');


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

return webpackMerge(webpackConfigs);
const webpackConfig = webpackMerge(webpackConfigs);

if (options.profile) {
const smp = new SpeedMeasurePlugin({
outputFormat: 'json',
outputTarget: getSystemPath(join(root, 'speed-measure-plugin.json')),
});

return smp.wrap(webpackConfig);
}

return webpackConfig;
}

private _deleteOutputDir(root: Path, outputPath: Path, host: virtualFs.Host) {
Expand Down
5 changes: 5 additions & 0 deletions packages/angular_devkit/build_angular/src/browser/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ export interface BrowserBuilderSchema {
* Budget thresholds to ensure parts of your application stay within boundaries which you set.
*/
budgets: Budget[];

/**
* Output profile events for Chrome profiler.
*/
profile: boolean;
}

export type AssetPattern = string | AssetPatternObject;
Expand Down
5 changes: 5 additions & 0 deletions packages/angular_devkit/build_angular/src/browser/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@
"$ref": "#/definitions/budget"
},
"default": []
},
"profile": {
"type": "boolean",
"description": "Output profile events for Chrome profiler.",
"default": false
}
},
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import { runTargetSpec } from '@angular-devkit/architect/testing';
import { normalize } from '@angular-devkit/core';
import { tap } from 'rxjs/operators';
import { browserTargetSpec, host } from '../utils';


describe('Browser Builder profile', () => {
beforeEach(done => host.initialize().toPromise().then(done, done.fail));
afterEach(done => host.restore().toPromise().then(done, done.fail));

it('works', (done) => {
const overrides = { profile: true };
runTargetSpec(host, browserTargetSpec, overrides).pipe(
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
tap(() => {
expect(host.scopedSync().exists(normalize('chrome-profiler-events.json'))).toBe(true);
expect(host.scopedSync().exists(normalize('speed-measure-plugin.json'))).toBe(true);
}),
).toPromise().then(done, done.fail);
});
});

0 comments on commit fbc4b53

Please sign in to comment.