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 May 31, 2017
1 parent 0d3d9ef commit 39ad0d2
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 3 deletions.
10 changes: 10 additions & 0 deletions docs/documentation/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,13 @@ Note: service worker support is experimental and subject to change.
Run build when files change.
</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 @@ -70,6 +70,7 @@
"magic-string": "^0.19.0",
"memory-fs": "^0.4.1",
"minimatch": "^3.0.3",
"ngo-loader": "github:filipesilva/ngo#filipe",
"node-modules-path": "^1.0.0",
"nopt": "^4.0.1",
"opn": "4.0.2",
Expand Down
6 changes: 6 additions & 0 deletions packages/@angular/cli/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ export const baseBuildCommandOptions: any = [
default: true,
aliases: ['dop'],
description: 'Delete output path before build.'
},
{
name: 'ngo',
type: Boolean,
default: false,
description: 'Enables NGO optimizations when using `--aot`.'
}
];

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 @@ -18,4 +18,5 @@ export interface BuildOptions {
poll?: number;
app?: string;
deleteOutputPath?: 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 @@ -63,6 +63,17 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
extraPlugins.push(new ProgressPlugin({ profile: buildOptions.verbose, colors: true }));
}


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

return {
devtool: buildOptions.sourcemaps ? 'source-map' : false,
resolve: {
Expand Down
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 @@ -55,6 +55,7 @@
"lodash": "^4.11.1",
"memory-fs": "^0.4.1",
"minimatch": "^3.0.3",
"ngo-loader": "github:filipesilva/ngo#filipe",
"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 =/)));
}

0 comments on commit 39ad0d2

Please sign in to comment.