Skip to content

Commit 1e142cd

Browse files
clydinalan-agius4
authored andcommitted
refactor(@angular-devkit/build-angular): remove legacy ES5 build optimizer usage
The legacy standalone build optimizer was still used when targetting ES5 due to the new babel-based build optimizer only supporting the analysis of ES2015+ code. Mainly due to the presence of native classes, the new build optimizer is faster and significantly less complex. To fully remove the legacy build optimizer, ES5 downleveling of application code is now also performed by babel when the TypeScript configuration specifies an ES5 target. TypeScript will now internally emit ES2015 in this configuration and ES2015 code will be used as the input to the remainder of the build pipeline. This has been the behavior for third-party packages and now all code will be consistently downleveled to ES5 using the same tooling.
1 parent a7b2e6f commit 1e142cd

File tree

5 files changed

+10
-21
lines changed

5 files changed

+10
-21
lines changed

packages/angular_devkit/build_angular/BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ ts_library(
9494
module_root = "src/index.d.ts",
9595
deps = [
9696
"//packages/angular_devkit/architect",
97-
"//packages/angular_devkit/build_optimizer",
9897
"//packages/angular_devkit/build_webpack",
9998
"//packages/angular_devkit/core",
10099
"//packages/angular_devkit/core/node",

packages/angular_devkit/build_angular/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"dependencies": {
99
"@ampproject/remapping": "1.0.1",
1010
"@angular-devkit/architect": "0.0.0",
11-
"@angular-devkit/build-optimizer": "0.0.0",
1211
"@angular-devkit/build-webpack": "0.0.0",
1312
"@angular-devkit/core": "0.0.0",
1413
"@babel/core": "7.15.0",

packages/angular_devkit/build_angular/src/babel/webpack-loader.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ export default custom<AngularCustomOptions>(() => {
6666
const esTarget = scriptTarget as ScriptTarget | undefined;
6767
if (esTarget !== undefined) {
6868
if (esTarget < ScriptTarget.ES2015) {
69-
// TypeScript files will have already been downlevelled
70-
customOptions.forceES5 = !/\.tsx?$/.test(this.resourcePath);
69+
customOptions.forceES5 = true;
7170
} else if (esTarget >= ScriptTarget.ES2017) {
7271
customOptions.forceAsyncTransformation =
7372
!/[\\\/][_f]?esm2015[\\\/]/.test(this.resourcePath) && source.includes('async');

packages/angular_devkit/build_angular/src/webpack/configs/common.ts

+1-17
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {
10-
BuildOptimizerWebpackPlugin,
11-
buildOptimizerLoaderPath,
12-
} from '@angular-devkit/build-optimizer';
139
import {
1410
GLOBAL_DEFS_FOR_TERSER,
1511
GLOBAL_DEFS_FOR_TERSER_WITH_AOT,
@@ -301,17 +297,6 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
301297
});
302298
}
303299

304-
let buildOptimizerUseRule: RuleSetRule[] = [];
305-
if (buildOptions.buildOptimizer && wco.scriptTarget < ScriptTarget.ES2015) {
306-
extraPlugins.push(new BuildOptimizerWebpackPlugin());
307-
buildOptimizerUseRule = [
308-
{
309-
loader: buildOptimizerLoaderPath,
310-
options: { sourceMap: scriptsSourceMap },
311-
},
312-
];
313-
}
314-
315300
const extraMinimizers = [];
316301

317302
if (scriptsOptimization) {
@@ -422,10 +407,9 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
422407
cacheDirectory: findCachePath('babel-webpack'),
423408
scriptTarget: wco.scriptTarget,
424409
aot: buildOptions.aot,
425-
optimize: buildOptions.buildOptimizer && wco.scriptTarget >= ScriptTarget.ES2015,
410+
optimize: buildOptions.buildOptimizer,
426411
},
427412
},
428-
...buildOptimizerUseRule,
429413
],
430414
},
431415
...extraRules,

packages/angular_devkit/build_angular/src/webpack/configs/typescript.ts

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import { getSystemPath } from '@angular-devkit/core';
1010
import { CompilerOptions } from '@angular/compiler-cli';
1111
import { AngularWebpackLoaderPath, AngularWebpackPlugin } from '@ngtools/webpack';
12+
import { ScriptTarget } from 'typescript';
1213
import { Configuration } from 'webpack';
1314
import { WebpackConfigOptions } from '../../utils/build-options';
1415

@@ -45,6 +46,13 @@ function createIvyPlugin(
4546
compilerOptions.preserveSymlinks = buildOptions.preserveSymlinks;
4647
}
4748

49+
// Outputting ES2015 from TypeScript is the required minimum for the build optimizer passes.
50+
// Downleveling to ES5 will occur after the build optimizer passes via babel which is the same
51+
// as for third-party libraries. This greatly reduces the complexity of static analysis.
52+
if (wco.scriptTarget < ScriptTarget.ES2015) {
53+
compilerOptions.target = ScriptTarget.ES2015;
54+
}
55+
4856
const fileReplacements: Record<string, string> = {};
4957
if (buildOptions.fileReplacements) {
5058
for (const replacement of buildOptions.fileReplacements) {

0 commit comments

Comments
 (0)