Skip to content

feat(@angular-devkit/build-angular): add profile option to browser builder #11497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -40,6 +40,7 @@
"semver": "5.5.1",
"source-map-support": "0.5.9",
"source-map-loader": "0.2.4",
"speed-measure-webpack-plugin": "^1.2.3",
"stats-webpack-plugin": "0.7.0",
"style-loader": "0.23.0",
"stylus": "0.54.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,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 @@ -69,6 +69,12 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
];
}

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 { defaultProgress, normalizeAssetPatterns, normalizeFileReplacements } from '../utils';
import { AssetPatternObject, BrowserBuilderSchema, CurrentFileReplacement } from './schema';
const SpeedMeasurePlugin = require('speed-measure-webpack-plugin');
const webpackMerge = require('webpack-merge');


Expand Down Expand Up @@ -154,7 +155,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 @@ -236,6 +236,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);
});
});
6 changes: 6 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7082,6 +7082,12 @@ spdy@^3.4.1:
select-hose "^2.0.0"
spdy-transport "^2.0.18"

speed-measure-webpack-plugin@^1.2.3:
version "1.2.3"
resolved "https://registry.yarnpkg.com/speed-measure-webpack-plugin/-/speed-measure-webpack-plugin-1.2.3.tgz#de170b5cefbfa1c039d95e639edd3ad50cfc7c48"
dependencies:
chalk "^2.0.1"

split-string@^3.0.1, split-string@^3.0.2:
version "3.1.0"
resolved "https://registry.yarnpkg.com/split-string/-/split-string-3.1.0.tgz#7cb09dda3a86585705c64b39a6466038682e8fe2"
Expand Down