Skip to content

Commit 39ad0d2

Browse files
committed
feat(@angular/cli): add ngo support
1 parent 0d3d9ef commit 39ad0d2

File tree

9 files changed

+53
-3
lines changed

9 files changed

+53
-3
lines changed

docs/documentation/build.md

+10
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,13 @@ Note: service worker support is experimental and subject to change.
300300
Run build when files change.
301301
</p>
302302
</details>
303+
304+
<details>
305+
<summary>ngo</summary>
306+
<p>
307+
<code>--ngo</code>
308+
</p>
309+
<p>
310+
Enables NGO optimizations when using `--aot`.
311+
</p>
312+
</details>

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"magic-string": "^0.19.0",
7171
"memory-fs": "^0.4.1",
7272
"minimatch": "^3.0.3",
73+
"ngo-loader": "github:filipesilva/ngo#filipe",
7374
"node-modules-path": "^1.0.0",
7475
"nopt": "^4.0.1",
7576
"opn": "4.0.2",

packages/@angular/cli/commands/build.ts

+6
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ export const baseBuildCommandOptions: any = [
125125
default: true,
126126
aliases: ['dop'],
127127
description: 'Delete output path before build.'
128+
},
129+
{
130+
name: 'ngo',
131+
type: Boolean,
132+
default: false,
133+
description: 'Enables NGO optimizations when using `--aot`.'
128134
}
129135
];
130136

packages/@angular/cli/models/build-options.ts

+1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ export interface BuildOptions {
1818
poll?: number;
1919
app?: string;
2020
deleteOutputPath?: boolean;
21+
ngo?: boolean;
2122
}

packages/@angular/cli/models/webpack-config.ts

+4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ export class NgCliWebpackConfig {
7070
if (buildOptions.target !== 'development' && buildOptions.target !== 'production') {
7171
throw new Error("Invalid build target. Only 'development' and 'production' are available.");
7272
}
73+
74+
if (buildOptions.ngo && !(buildOptions.aot || buildOptions.target === 'production')) {
75+
throw new Error('The `--ngo` option cannot be used without `--aot` (or `--prod`).');
76+
}
7377
}
7478

7579
// Fill in defaults for build targets

packages/@angular/cli/models/webpack-configs/common.ts

+11
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
6363
extraPlugins.push(new ProgressPlugin({ profile: buildOptions.verbose, colors: true }));
6464
}
6565

66+
67+
if (buildOptions.ngo) {
68+
extraRules.push({
69+
test: /\.js$/,
70+
use: [{
71+
loader: 'ngo-loader',
72+
options: { sourceMap: buildOptions.sourcemaps }
73+
}]
74+
});
75+
}
76+
6677
return {
6778
devtool: buildOptions.sourcemaps ? 'source-map' : false,
6879
resolve: {

packages/@angular/cli/models/webpack-configs/typescript.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ function _createAotPlugin(wco: WebpackConfigOptions, options: any) {
7474
}, options));
7575
}
7676

77-
7877
export const getNonAotConfig = function(wco: WebpackConfigOptions) {
7978
const { appConfig, projectRoot } = wco;
8079
const tsConfigPath = path.resolve(projectRoot, appConfig.root, appConfig.tsconfig);
@@ -86,7 +85,7 @@ export const getNonAotConfig = function(wco: WebpackConfigOptions) {
8685
};
8786

8887
export const getAotConfig = function(wco: WebpackConfigOptions) {
89-
const { projectRoot, appConfig } = wco;
88+
const { projectRoot, buildOptions, appConfig } = wco;
9089
const tsConfigPath = path.resolve(projectRoot, appConfig.root, appConfig.tsconfig);
9190
const testTsConfigPath = path.resolve(projectRoot, appConfig.root, appConfig.testTsconfig);
9291

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

101+
let ngoLoader: any = [];
102+
if (buildOptions.ngo) {
103+
ngoLoader = [{
104+
loader: 'ngo-loader',
105+
options: { sourceMap: buildOptions.sourcemaps }
106+
}];
107+
}
108+
102109
return {
103-
module: { rules: [{ test: /\.ts$/, loader: webpackLoader }] },
110+
module: { rules: [{ test: /\.ts$/, use: [...ngoLoader, webpackLoader] }] },
104111
plugins: [ _createAotPlugin(wco, pluginOptions) ]
105112
};
106113
};

packages/@angular/cli/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"lodash": "^4.11.1",
5656
"memory-fs": "^0.4.1",
5757
"minimatch": "^3.0.3",
58+
"ngo-loader": "github:filipesilva/ngo#filipe",
5859
"node-modules-path": "^1.0.0",
5960
"nopt": "^4.0.1",
6061
"opn": "4.0.2",

tests/e2e/tests/build/ngo.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ng } from '../../utils/process';
2+
import { expectFileToMatch } from '../../utils/fs';
3+
import { expectToFail } from '../../utils/utils';
4+
5+
6+
export default function () {
7+
return ng('build', '--aot', '--ngo')
8+
.then(() => expectToFail(() => expectFileToMatch('dist/vendor.js', /\.decorators =/)));
9+
}

0 commit comments

Comments
 (0)