Skip to content

Commit

Permalink
feat(@angular/cli): add ngo support
Browse files Browse the repository at this point in the history
  • Loading branch information
filipesilva committed Jul 2, 2017
1 parent 64e6b94 commit 0ad9430
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 19 deletions.
10 changes: 10 additions & 0 deletions docs/documentation/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,13 @@ Note: service worker support is experimental and subject to change.
Show circular dependency warnings on builds.
</p>
</details>

<details>
<summary>ngo</summary>
<p>
<code>--ngo</code>
</p>
<p>
Enables NGO 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 @@ -74,6 +74,7 @@
"magic-string": "^0.19.0",
"memory-fs": "^0.4.1",
"minimatch": "^3.0.3",
"ngo-loader": "github:angular/ngo",
"node-modules-path": "^1.0.0",
"nopt": "^4.0.1",
"opn": "4.0.2",
Expand Down
29 changes: 22 additions & 7 deletions packages/@angular/cli/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const baseBuildCommandOptions: any = [
{
name: 'environment',
type: String,
aliases: ['e'] ,
aliases: ['e'],
description: 'Defines the build environment.'
},
{
Expand All @@ -43,7 +43,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 @@ -144,6 +143,12 @@ export const baseBuildCommandOptions: any = [
type: Boolean,
aliases: ['scd'],
description: 'Show circular dependency warnings on builds.'
},
{
name: 'ngo',
type: Boolean,
default: false,
description: 'Enables NGO optimizations when using `--aot`.'
}
];

Expand All @@ -158,15 +163,25 @@ const BuildCommand = Command.extend({

availableOptions: baseBuildCommandOptions.concat([
{
name: 'stats-json',
type: Boolean,
default: false,
description: oneLine`Generates a \`stats.json\` file which can be analyzed using tools
name: 'stats-json',
type: Boolean,
default: false,
description: oneLine`Generates a \`stats.json\` file which can be analyzed using tools
such as: \`webpack-bundle-analyzer\` or https://webpack.github.io/analyse.`
}
}
]),

run: function (commandOptions: BuildTaskOptions) {

// Remove vendor chunk if undefined and --ngo if on.
if (commandOptions.vendorChunk === undefined) {
if (commandOptions.ngo) {
commandOptions.vendorChunk = false;
} else {
commandOptions.vendorChunk = true;
}
}

// Check angular version.
Version.assertAngularVersionIs2_3_1OrHigher(this.project.root);

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 @@ -21,4 +21,5 @@ export interface BuildOptions {
preserveSymlinks?: boolean;
extractLicenses?: boolean;
showCircularDependencies?: boolean;
ngo?: boolean;
}
4 changes: 4 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,10 @@ export class NgCliWebpackConfig {
if (buildOptions.target !== 'development' && buildOptions.target !== 'production') {
throw new Error("Invalid build target. Only 'development' and 'production' are available.");
}

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

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

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

if (buildOptions.ngo) {
extraRules.push({
test: /\.js$/,
use: [{
loader: 'ngo-loader',
options: { sourceMap: buildOptions.sourcemaps }
}]
});
}

return {
resolve: {
extensions: ['.ts', '.js'],
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 @@ -7,6 +7,7 @@ import { StaticAssetPlugin } from '../../plugins/static-asset';
import { GlobCopyWebpackPlugin } from '../../plugins/glob-copy-webpack-plugin';
import { WebpackConfigOptions } from '../webpack-config';

const PurifyPlugin = require('ngo-loader').PurifyPlugin;
const licensePlugin = require('license-webpack-plugin');

export const getProdConfig = function (wco: WebpackConfigOptions) {
Expand Down Expand Up @@ -91,19 +92,27 @@ export const getProdConfig = function (wco: WebpackConfigOptions) {
}));
}

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

if (buildOptions.ngo) {
// 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 (<any>webpack).HashedModuleIdsPlugin(),
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 ngoLoader: any = [];
if (buildOptions.ngo) {
ngoLoader = [{
loader: 'ngo-loader',
options: { sourceMap: buildOptions.sourcemaps }
}];
}

return {
module: { rules: [{ test: /\.ts$/, loader: webpackLoader }] },
module: { rules: [{ test: /\.ts$/, use: [...ngoLoader, 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 @@ -59,6 +59,7 @@
"lodash": "^4.11.1",
"memory-fs": "^0.4.1",
"minimatch": "^3.0.3",
"ngo-loader": "github:angular/ngo",
"node-modules-path": "^1.0.0",
"nopt": "^4.0.1",
"opn": "4.0.2",
Expand Down
9 changes: 9 additions & 0 deletions tests/e2e/tests/build/ngo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ng } from '../../utils/process';
import { expectFileToMatch } from '../../utils/fs';
import { expectToFail } from '../../utils/utils';


export default function () {
return ng('build', '--aot', '--ngo')
.then(() => expectToFail(() => expectFileToMatch('dist/vendor.js', /\.decorators =/)));
}
25 changes: 19 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3053,7 +3053,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 @@ -3239,6 +3239,15 @@ negotiator@0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.1.tgz#2b327184e8992101177b28563fb5e7102acd0ca9"

"ngo-loader@github:angular/ngo":
version "0.0.10"
resolved "https://codeload.github.com/angular/ngo/tar.gz/13e7ef35e3cc5a08bbde39ae228bee8d7bf544c9"
dependencies:
loader-utils "^1.1.0"
magic-string "^0.19.1"
source-map "^0.5.6"
typescript "^2.4.0-dev.20170608"

no-case@^2.2.0:
version "2.3.1"
resolved "https://registry.yarnpkg.com/no-case/-/no-case-2.3.1.tgz#7aeba1c73a52184265554b7dc03baf720df80081"
Expand Down Expand Up @@ -4355,16 +4364,16 @@ right-align@^0.1.1:
dependencies:
align-text "^0.1.1"

rimraf@2, rimraf@^2.2.8, rimraf@^2.5.1, rimraf@^2.6.1:
rimraf@2, rimraf@^2.2.8, rimraf@~2.2.6:
version "2.2.8"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.2.8.tgz#e439be2aaee327321952730f99a8929e4fc50582"

rimraf@^2.5.1, rimraf@^2.6.1:
version "2.6.1"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.1.tgz#c2338ec643df7a1b7fe5c54fa86f57428a55f33d"
dependencies:
glob "^7.0.5"

rimraf@~2.2.6:
version "2.2.8"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.2.8.tgz#e439be2aaee327321952730f99a8929e4fc50582"

ripemd160@^2.0.0, ripemd160@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/ripemd160/-/ripemd160-2.0.1.tgz#0f4584295c53a3628af7e6d79aca21ce57d1c6e7"
Expand Down Expand Up @@ -5106,6 +5115,10 @@ typedarray@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"

typescript@^2.4.0-dev.20170608:
version "2.4.1"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.4.1.tgz#c3ccb16ddaa0b2314de031e7e6fee89e5ba346bc"

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

0 comments on commit 0ad9430

Please sign in to comment.