Skip to content

Commit 37d669a

Browse files
committed
feat(@angular/cli): support ES2015 target
1 parent 1652aa3 commit 37d669a

File tree

13 files changed

+183
-59
lines changed

13 files changed

+183
-59
lines changed

docs/documentation/build.md

+10
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,13 @@ Note: service worker support is experimental and subject to change.
366366
Use file name for lazy loaded chunks.
367367
</p>
368368
</details>
369+
370+
<details>
371+
<summary>compilation-target</summary>
372+
<p>
373+
<code>--compilation-target</code> (aliases: <code>-ct, -es5, -es2015</code>) <em>default value: ES5</em>
374+
</p>
375+
<p>
376+
Defines the compilation target (ES5 or ES2015).
377+
</p>
378+
</details>

package-lock.json

+74-46
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
"stylus": "^0.54.5",
9393
"stylus-loader": "^3.0.1",
9494
"typescript": "~2.4.2",
95+
"uglifyjs-webpack-plugin": "1.0.0-beta.1",
9596
"url-loader": "^0.5.7",
9697
"webpack": "~3.5.5",
9798
"webpack-concat-plugin": "1.4.0",

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const Command = require('../ember-cli/lib/models/command');
99
const config = CliConfig.fromProject() || CliConfig.fromGlobal();
1010
const buildConfigDefaults = config.getPaths('defaults.build', [
1111
'sourcemaps', 'baseHref', 'progress', 'poll', 'deleteOutputPath', 'preserveSymlinks',
12-
'showCircularDependencies', 'commonChunk', 'namedChunks'
12+
'showCircularDependencies', 'commonChunk', 'namedChunks', 'compilationTarget'
1313
]);
1414

1515
// defaults for BuildOptions
@@ -176,6 +176,18 @@ export const baseBuildCommandOptions: any = [
176176
aliases: ['nc'],
177177
description: 'Use file name for lazy loaded chunks.',
178178
default: buildConfigDefaults['namedChunks']
179+
},
180+
{
181+
name: 'compilation-target',
182+
type: String,
183+
default: buildConfigDefaults['compilationTarget'],
184+
aliases: [
185+
'ct',
186+
{ 'ES5': 'ES5' }, { 'ES2015': 'ES2015' },
187+
// Support lowercase in the alias.
188+
{ 'es5': 'ES5' }, { 'es2015': 'ES2015' },
189+
],
190+
description: 'Defines the compilation target (ES5 or ES2015).'
179191
}
180192
];
181193

packages/@angular/cli/lib/config/schema.json

+5
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,11 @@
496496
"namedChunks": {
497497
"description": "Use file name for lazy loaded chunks.",
498498
"type": "boolean"
499+
},
500+
"compilationTarget": {
501+
"description": "Defines the compilation target (ES5 or ES2015).",
502+
"type": "string",
503+
"default": "ES5"
499504
}
500505
}
501506
},

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

+1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ export interface BuildOptions {
2525
showCircularDependencies?: boolean;
2626
buildOptimizer?: boolean;
2727
namedChunks?: boolean;
28+
compilationTarget?: 'ES5' | 'ES2015';
2829
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ export class NgCliWebpackConfig {
7474
throw new Error("Invalid build target. Only 'development' and 'production' are available.");
7575
}
7676

77+
buildOptions.compilationTarget = buildOptions.compilationTarget || 'ES5';
78+
buildOptions.compilationTarget =
79+
buildOptions.compilationTarget.toUpperCase() as 'ES5' | 'ES2015';
80+
if (buildOptions.compilationTarget !== 'ES5' && buildOptions.compilationTarget !== 'ES2015') {
81+
throw new Error("Invalid compilation target. Only 'ES5' and 'ES2015' are available.");
82+
}
83+
7784
if (buildOptions.buildOptimizer
7885
&& !(buildOptions.aot || buildOptions.target === 'production')) {
7986
throw new Error('The `--build-optimizer` option cannot be used without `--aot`.');

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

+4
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
153153
resolve: {
154154
extensions: ['.ts', '.js'],
155155
modules: ['node_modules', nodeModules],
156+
mainFields: [
157+
...(buildOptions.compilationTarget === 'ES2015' ? ['es2015'] : []),
158+
'browser', 'module', 'main'
159+
],
156160
symlinks: !buildOptions.preserveSymlinks
157161
},
158162
resolveLoader: {

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

+36-12
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ import { StaticAssetPlugin } from '../../plugins/static-asset';
99
import { GlobCopyWebpackPlugin } from '../../plugins/glob-copy-webpack-plugin';
1010
import { WebpackConfigOptions } from '../webpack-config';
1111

12+
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
13+
1214

1315
export const getProdConfig = function (wco: WebpackConfigOptions) {
1416
const { projectRoot, buildOptions, appConfig } = wco;
1517

1618
let extraPlugins: any[] = [];
17-
let entryPoints: {[key: string]: string[]} = {};
19+
let entryPoints: { [key: string]: string[] } = {};
1820

1921
if (appConfig.serviceWorker) {
2022
const nodeModules = path.resolve(projectRoot, 'node_modules');
@@ -66,7 +68,7 @@ export const getProdConfig = function (wco: WebpackConfigOptions) {
6668
extraPlugins.push(new GlobCopyWebpackPlugin({
6769
patterns: [
6870
'ngsw-manifest.json',
69-
{glob: 'ngsw-manifest.json', input: path.resolve(projectRoot, appConfig.root), output: ''}
71+
{ glob: 'ngsw-manifest.json', input: path.resolve(projectRoot, appConfig.root), output: '' }
7072
],
7173
globOptions: {
7274
cwd: projectRoot,
@@ -99,7 +101,7 @@ export const getProdConfig = function (wco: WebpackConfigOptions) {
99101
}));
100102
}
101103

102-
const uglifyCompressOptions: any = { screw_ie8: true, warnings: buildOptions.verbose };
104+
const uglifyCompressOptions: any = {};
103105

104106
if (buildOptions.buildOptimizer) {
105107
// This plugin must be before webpack.optimize.UglifyJsPlugin.
@@ -110,21 +112,43 @@ export const getProdConfig = function (wco: WebpackConfigOptions) {
110112
uglifyCompressOptions.passes = 3;
111113
}
112114

115+
if (buildOptions.compilationTarget === 'ES2015') {
116+
extraPlugins.push(new UglifyJSPlugin({
117+
sourceMap: buildOptions.sourcemaps,
118+
uglifyOptions: {
119+
ecma: 6,
120+
warnings: buildOptions.verbose,
121+
ie8: false,
122+
mangle: true,
123+
compress: uglifyCompressOptions,
124+
output: {
125+
ascii_only: true,
126+
comments: false
127+
},
128+
}
129+
}));
130+
} else {
131+
uglifyCompressOptions.screw_ie8 = true;
132+
uglifyCompressOptions.warnings = buildOptions.verbose;
133+
extraPlugins.push(new webpack.optimize.UglifyJsPlugin(<any>{
134+
mangle: { screw_ie8: true },
135+
compress: uglifyCompressOptions,
136+
output: { ascii_only: true },
137+
sourceMap: buildOptions.sourcemaps,
138+
comments: false
139+
}));
140+
141+
}
142+
113143
return {
114144
entry: entryPoints,
115-
plugins: extraPlugins.concat([
145+
plugins: [
116146
new webpack.EnvironmentPlugin({
117147
'NODE_ENV': 'production'
118148
}),
119149
new webpack.HashedModuleIdsPlugin(),
120150
new webpack.optimize.ModuleConcatenationPlugin(),
121-
new webpack.optimize.UglifyJsPlugin(<any>{
122-
mangle: { screw_ie8: true },
123-
compress: uglifyCompressOptions,
124-
output: { ascii_only: true },
125-
sourceMap: buildOptions.sourcemaps,
126-
comments: false
127-
})
128-
])
151+
...extraPlugins
152+
]
129153
};
130154
};

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

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ function _createAotPlugin(wco: WebpackConfigOptions, options: any) {
6363
};
6464
}
6565

66+
options.compilerOptions = options.compilerOptions || {};
67+
options.compilerOptions.target = buildOptions.compilationTarget;
68+
6669
return new AotPlugin(Object.assign({}, {
6770
mainPath: path.join(projectRoot, appConfig.root, appConfig.main),
6871
i18nFile: buildOptions.i18nFile,

packages/@angular/cli/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"stylus": "^0.54.5",
7777
"stylus-loader": "^3.0.1",
7878
"typescript": ">=2.0.0 <2.6.0",
79+
"uglifyjs-webpack-plugin": "1.0.0-beta.1",
7980
"url-loader": "^0.5.7",
8081
"webpack": "~3.5.5",
8182
"webpack-concat-plugin": "1.4.0",

0 commit comments

Comments
 (0)