Skip to content
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

feat(@angular/cli): add build-optimizer support #6520

Merged
merged 1 commit into from
Jul 19, 2017
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
10 changes: 10 additions & 0 deletions docs/documentation/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,13 @@ Note: service worker support is experimental and subject to change.
Show circular dependency warnings on builds.
</p>
</details>

<details>
<summary>build-optimizer</summary>
<p>
<code>--build-optimizer</code> (aliases: <code>-bo</code>)
</p>
<p>
(Experimental) Enables @angular-devkit/build-optimizer optimizations when using `--aot`.
</p>
</details>
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"homepage": "https://github.com/angular/angular-cli",
"dependencies": {
"@angular-devkit/build-optimizer": "0.0.3",
"autoprefixer": "^6.5.3",
"chalk": "^1.1.3",
"circular-dependency-plugin": "^3.0.0",
Expand Down
14 changes: 13 additions & 1 deletion packages/@angular/cli/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export const baseBuildCommandOptions: any = [
{
name: 'vendor-chunk',
type: Boolean,
default: true,
aliases: ['vc'],
description: 'Use a separate bundle containing only vendor libraries.'
},
Expand Down Expand Up @@ -159,6 +158,14 @@ export const baseBuildCommandOptions: any = [
aliases: ['scd'],
description: 'Show circular dependency warnings on builds.',
default: buildConfigDefaults['showCircularDependencies']
},
{
name: 'build-optimizer',
type: Boolean,
default: false,
aliases: ['bo'],
description: '(Experimental) Enables @angular-devkit/build-optimizer '
+ 'optimizations when using `--aot`.'
}
];

Expand All @@ -185,6 +192,11 @@ const BuildCommand = Command.extend({
// Check angular version.
Version.assertAngularVersionIs2_3_1OrHigher(this.project.root);

// Default vendor chunk to false when build optimizer is on.
if (commandOptions.vendorChunk === undefined) {
commandOptions.vendorChunk = !commandOptions.buildOptimizer;
}

const BuildTask = require('../tasks/build').default;

const buildTask = new BuildTask({
Expand Down
6 changes: 6 additions & 0 deletions packages/@angular/cli/commands/eject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ const EjectCommand = Command.extend({
availableOptions: baseEjectCommandOptions,

run: function (commandOptions: EjectTaskOptions) {

// Default vendor chunk to false when build optimizer is on.
if (commandOptions.vendorChunk === undefined) {
commandOptions.vendorChunk = !commandOptions.buildOptimizer;
}

const EjectTask = require('../tasks/eject').default;
const ejectTask = new EjectTask({
project: this.project,
Expand Down
6 changes: 6 additions & 0 deletions packages/@angular/cli/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ const ServeCommand = Command.extend({
const ServeTask = require('../tasks/serve').default;

Version.assertAngularVersionIs2_3_1OrHigher(this.project.root);

// Default vendor chunk to false when build optimizer is on.
if (commandOptions.vendorChunk === undefined) {
commandOptions.vendorChunk = !commandOptions.buildOptimizer;
}

return checkPort(commandOptions.port, commandOptions.host, defaultPort)
.then(port => {
commandOptions.port = port;
Expand Down
1 change: 1 addition & 0 deletions packages/@angular/cli/models/build-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ export interface BuildOptions {
preserveSymlinks?: boolean;
extractLicenses?: boolean;
showCircularDependencies?: boolean;
buildOptimizer?: boolean;
}
5 changes: 5 additions & 0 deletions packages/@angular/cli/models/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ export class NgCliWebpackConfig {
if (buildOptions.target !== 'development' && buildOptions.target !== 'production') {
throw new Error("Invalid build target. Only 'development' and 'production' are available.");
}

if (buildOptions.buildOptimizer
&& !(buildOptions.aot || buildOptions.target === 'production')) {
throw new Error('The `--build-optimizer` option cannot be used without `--aot`.');
}
}

// Fill in defaults for build targets
Expand Down
13 changes: 12 additions & 1 deletion packages/@angular/cli/models/webpack-configs/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const CircularDependencyPlugin = require('circular-dependency-plugin');
* require('json-loader')
* require('url-loader')
* require('file-loader')
* require('@angular-devkit/build-optimizer')
*/

export function getCommonConfig(wco: WebpackConfigOptions) {
Expand Down Expand Up @@ -71,6 +72,16 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
}));
}

if (buildOptions.buildOptimizer) {
extraRules.push({
test: /\.js$/,
use: [{
loader: '@angular-devkit/build-optimizer/webpack-loader',
options: { sourceMap: buildOptions.sourcemaps }
}]
});
}

return {
resolve: {
extensions: ['.ts', '.js'],
Expand Down Expand Up @@ -107,7 +118,7 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
node: {
fs: 'empty',
// `global` should be kept true, removing it resulted in a
// massive size increase with NGO on AIO.
// massive size increase with Build Optimizer on AIO.
global: true,
crypto: 'empty',
tls: 'empty',
Expand Down
15 changes: 12 additions & 3 deletions packages/@angular/cli/models/webpack-configs/production.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as webpack from 'webpack';
import * as fs from 'fs';
import * as semver from 'semver';
import { stripIndent } from 'common-tags';
import { PurifyPlugin } from '@angular-devkit/build-optimizer';
import { StaticAssetPlugin } from '../../plugins/static-asset';
import { GlobCopyWebpackPlugin } from '../../plugins/glob-copy-webpack-plugin';
import { WebpackConfigOptions } from '../webpack-config';
Expand Down Expand Up @@ -91,20 +92,28 @@ export const getProdConfig = function (wco: WebpackConfigOptions) {
}));
}

const uglifyCompressOptions: any = { screw_ie8: true, warnings: buildOptions.verbose };

if (buildOptions.buildOptimizer) {
// This plugin must be before webpack.optimize.UglifyJsPlugin.
extraPlugins.push(new PurifyPlugin());
uglifyCompressOptions.pure_getters = true;
}

return {
entry: entryPoints,
plugins: [
plugins: extraPlugins.concat([
new webpack.EnvironmentPlugin({
'NODE_ENV': 'production'
}),
new webpack.HashedModuleIdsPlugin(),
new webpack.optimize.ModuleConcatenationPlugin(),
new webpack.optimize.UglifyJsPlugin(<any>{
mangle: { screw_ie8: true },
compress: { screw_ie8: true, warnings: buildOptions.verbose },
compress: uglifyCompressOptions,
sourceMap: buildOptions.sourcemaps,
comments: false
})
].concat(extraPlugins)
])
};
};
13 changes: 10 additions & 3 deletions packages/@angular/cli/models/webpack-configs/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ function _createAotPlugin(wco: WebpackConfigOptions, options: any) {
}, options));
}


export const getNonAotConfig = function(wco: WebpackConfigOptions) {
const { appConfig, projectRoot } = wco;
const tsConfigPath = path.resolve(projectRoot, appConfig.root, appConfig.tsconfig);
Expand All @@ -86,7 +85,7 @@ export const getNonAotConfig = function(wco: WebpackConfigOptions) {
};

export const getAotConfig = function(wco: WebpackConfigOptions) {
const { projectRoot, appConfig } = wco;
const { projectRoot, buildOptions, appConfig } = wco;
const tsConfigPath = path.resolve(projectRoot, appConfig.root, appConfig.tsconfig);
const testTsConfigPath = path.resolve(projectRoot, appConfig.root, appConfig.testTsconfig);

Expand All @@ -99,8 +98,16 @@ export const getAotConfig = function(wco: WebpackConfigOptions) {
pluginOptions.exclude = exclude;
}

let boLoader: any = [];
if (buildOptions.buildOptimizer) {
boLoader = [{
loader: '@angular-devkit/build-optimizer/webpack-loader',
options: { sourceMap: buildOptions.sourcemaps }
}];
}

return {
module: { rules: [{ test: /\.ts$/, loader: webpackLoader }] },
module: { rules: [{ test: /\.ts$/, use: [...boLoader, webpackLoader] }] },
plugins: [ _createAotPlugin(wco, pluginOptions) ]
};
};
Expand Down
1 change: 1 addition & 0 deletions packages/@angular/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"homepage": "https://github.com/angular/angular-cli",
"dependencies": {
"@angular-devkit/build-optimizer": "0.0.3",
"@ngtools/json-schema": "1.1.0",
"@ngtools/webpack": "1.6.0-beta.1",
"autoprefixer": "^6.5.3",
Expand Down
10 changes: 10 additions & 0 deletions tests/e2e/tests/build/build-optimizer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ng } from '../../utils/process';
import { expectFileToMatch, expectFileToExist } from '../../utils/fs';
import { expectToFail } from '../../utils/utils';


export default function () {
return ng('build', '--aot', '--bo')
.then(() => expectToFail(() => expectFileToExist('dist/vendor.js')))
.then(() => expectToFail(() => expectFileToMatch('dist/main.js', /\.decorators =/)));
}
13 changes: 11 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
# yarn lockfile v1


"@angular-devkit/build-optimizer@0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@angular-devkit/build-optimizer/-/build-optimizer-0.0.3.tgz#092bdf732b79a779ce540f9bb99d6590dd971204"
dependencies:
loader-utils "^1.1.0"
magic-string "^0.19.1"
source-map "^0.5.6"
typescript "^2.3.3"

"@angular/compiler-cli@^4.0.0":
version "4.2.4"
resolved "https://registry.yarnpkg.com/@angular/compiler-cli/-/compiler-cli-4.2.4.tgz#cce941a28362fc1c042ab85890fcaab1e233dd57"
Expand Down Expand Up @@ -3143,7 +3152,7 @@ macaddress@^0.2.8:
version "0.2.8"
resolved "https://registry.yarnpkg.com/macaddress/-/macaddress-0.2.8.tgz#5904dc537c39ec6dbefeae902327135fa8511f12"

magic-string@^0.19.0:
magic-string@^0.19.0, magic-string@^0.19.1:
version "0.19.1"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.19.1.tgz#14d768013caf2ec8fdea16a49af82fc377e75201"
dependencies:
Expand Down Expand Up @@ -5242,7 +5251,7 @@ typedarray@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"

typescript@~2.3.1:
typescript@^2.3.3, typescript@~2.3.1:
version "2.3.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.3.4.tgz#3d38321828231e434f287514959c37a82b629f42"

Expand Down