Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5a45f91

Browse files
alan-agius4clydin
authored andcommittedJun 1, 2020
fix(@schematics/angular): use ES2016 as syntax target for server bundles
The supported versions of Node.js support up to ES2018, the only reason why we don't use ES2017+ is because native `async` and `await` don't work with zone.js See: angular/angular#31730 With this change, we also ensure that we don't downlevel the server bundle to ES5 which is unnecessary. Closes: #17794
1 parent ece9a6a commit 5a45f91

File tree

6 files changed

+33
-10
lines changed

6 files changed

+33
-10
lines changed
 

‎packages/angular_devkit/build_angular/test/hello-world-app/src/tsconfig.server.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"extends": "./tsconfig.app.json",
33
"compilerOptions": {
44
"outDir": "../dist-server",
5+
"target": "es2016",
56
"baseUrl": "./",
67
"types": []
78
},

‎packages/schematics/angular/migrations/migration-collection.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,6 @@
8080
"factory": "./update-10/side-effects-package-json",
8181
"description": "Create a special 'package.json' file that is used to tell the tools and bundlers whether the code under the app directory is free of code with non-local side-effect."
8282
},
83-
"update-module-and-target-compiler-options": {
84-
"version": "10.0.0-beta.3",
85-
"factory": "./update-10/update-module-and-target-compiler-options",
86-
"description": "Update 'module' and 'target' TypeScript compiler options."
87-
},
8883
"update-angular-config": {
8984
"version": "10.0.0-beta.6",
9085
"factory": "./update-10/update-angular-config",
@@ -109,6 +104,11 @@
109104
"version": "10.0.0-beta.7",
110105
"factory": "./update-10/solution-style-tsconfig",
111106
"description": "Adding \"Solution Style\" tsconfig.json. This improves developer experience using editors powered by TypeScript’s language server."
107+
},
108+
"update-module-and-target-compiler-options": {
109+
"version": "10.0.0-rc.1",
110+
"factory": "./update-10/update-module-and-target-compiler-options",
111+
"description": "Update 'module' and 'target' TypeScript compiler options."
112112
}
113113
}
114114
}

‎packages/schematics/angular/migrations/update-10/update-module-and-target-compiler-options.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
import { dirname, join, normalize } from '@angular-devkit/core';
99
import { Rule, Tree } from '@angular-devkit/schematics';
10-
import { findPropertyInAstObject, removePropertyInAstObject } from '../../utility/json-utils';
10+
import { appendPropertyInAstObject, findPropertyInAstObject, removePropertyInAstObject } from '../../utility/json-utils';
1111
import { getWorkspace } from '../../utility/workspace';
1212
import { Builders } from '../../utility/workspace-models';
1313
import { readJsonFileAsAstObject } from '../update-9/utils';
@@ -68,6 +68,10 @@ export default function (): Rule {
6868
// This ensures that lazy-loaded works on the server.
6969
newModule: false,
7070
});
71+
72+
updateModuleAndTarget(host, p, {
73+
newTarget: 'es2016',
74+
});
7175
});
7276
break;
7377
case Builders.Karma:
@@ -102,7 +106,10 @@ function updateModuleAndTarget(host: Tree, tsConfigPath: string, replacements: M
102106
const recorder = host.beginUpdate(tsConfigPath);
103107
if (newTarget) {
104108
const targetAst = findPropertyInAstObject(compilerOptionsAst, 'target');
105-
if (targetAst?.kind === 'string' && oldTarget === targetAst.value.toLowerCase()) {
109+
110+
if (!targetAst && !oldTarget) {
111+
appendPropertyInAstObject(recorder, compilerOptionsAst, 'target', newTarget, 4);
112+
} else if (targetAst?.kind === 'string' && (!oldTarget || oldTarget === targetAst.value.toLowerCase())) {
106113
const offset = targetAst.start.offset + 1;
107114
recorder.remove(offset, targetAst.value.length);
108115
recorder.insertLeft(offset, newTarget);

‎packages/schematics/angular/migrations/update-10/update-module-and-target-compiler-options_spec.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,23 @@ describe('Migration to update target and module compiler options', () => {
131131
expect(target).toBe('es2018');
132132
});
133133

134-
135134
it(`should remove module in 'tsconfig.server.json'`, async () => {
136135
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise();
137-
const { module, target } = readJsonFile(newTree, 'src/tsconfig.server.json').compilerOptions;
136+
const { module } = readJsonFile(newTree, 'src/tsconfig.server.json').compilerOptions;
138137
expect(module).toBeUndefined();
139-
expect(target).toBeUndefined();
138+
});
139+
140+
it(`should add target in 'tsconfig.server.json'`, async () => {
141+
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise();
142+
const { target } = readJsonFile(newTree, 'src/tsconfig.server.json').compilerOptions;
143+
expect(target).toBe('es2016');
144+
});
145+
146+
it(`should update target to es2016 in 'tsconfig.server.json'`, async () => {
147+
tree.delete('src/tsconfig.server.json');
148+
createJsonFile(tree, 'src/tsconfig.server.json', { compilerOptions: { module: 'commonjs', target: 'es5' } });
149+
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise();
150+
const { target } = readJsonFile(newTree, 'src/tsconfig.server.json').compilerOptions;
151+
expect(target).toBe('es2016');
140152
});
141153
});

‎packages/schematics/angular/universal/files/root/tsconfig.server.json.template

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"extends": "./<%= tsConfigExtends %>",
33
"compilerOptions": {
44
"outDir": "<%= relativePathToWorkspaceRoot %>/out-tsc/server",
5+
"target": "es2016",
56
"types": [
67
"node"
78
]

‎packages/schematics/angular/universal/index_spec.ts

+2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ describe('Universal Schematic', () => {
9292
extends: './tsconfig.app.json',
9393
compilerOptions: {
9494
outDir: './out-tsc/server',
95+
target: 'es2016',
9596
types: ['node'],
9697
},
9798
files: [
@@ -116,6 +117,7 @@ describe('Universal Schematic', () => {
116117
extends: './tsconfig.app.json',
117118
compilerOptions: {
118119
outDir: '../../out-tsc/server',
120+
target: 'es2016',
119121
types: ['node'],
120122
},
121123
files: [

0 commit comments

Comments
 (0)
Please sign in to comment.