Skip to content

Commit

Permalink
fix(@schematics/angular): use ES2016 as syntax target for server bundles
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alan-agius4 authored and clydin committed Jun 1, 2020
1 parent ece9a6a commit 5a45f91
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "./tsconfig.app.json",
"compilerOptions": {
"outDir": "../dist-server",
"target": "es2016",
"baseUrl": "./",
"types": []
},
Expand Down
10 changes: 5 additions & 5 deletions packages/schematics/angular/migrations/migration-collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@
"factory": "./update-10/side-effects-package-json",
"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."
},
"update-module-and-target-compiler-options": {
"version": "10.0.0-beta.3",
"factory": "./update-10/update-module-and-target-compiler-options",
"description": "Update 'module' and 'target' TypeScript compiler options."
},
"update-angular-config": {
"version": "10.0.0-beta.6",
"factory": "./update-10/update-angular-config",
Expand All @@ -109,6 +104,11 @@
"version": "10.0.0-beta.7",
"factory": "./update-10/solution-style-tsconfig",
"description": "Adding \"Solution Style\" tsconfig.json. This improves developer experience using editors powered by TypeScript’s language server."
},
"update-module-and-target-compiler-options": {
"version": "10.0.0-rc.1",
"factory": "./update-10/update-module-and-target-compiler-options",
"description": "Update 'module' and 'target' TypeScript compiler options."
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
import { dirname, join, normalize } from '@angular-devkit/core';
import { Rule, Tree } from '@angular-devkit/schematics';
import { findPropertyInAstObject, removePropertyInAstObject } from '../../utility/json-utils';
import { appendPropertyInAstObject, findPropertyInAstObject, removePropertyInAstObject } from '../../utility/json-utils';
import { getWorkspace } from '../../utility/workspace';
import { Builders } from '../../utility/workspace-models';
import { readJsonFileAsAstObject } from '../update-9/utils';
Expand Down Expand Up @@ -68,6 +68,10 @@ export default function (): Rule {
// This ensures that lazy-loaded works on the server.
newModule: false,
});

updateModuleAndTarget(host, p, {
newTarget: 'es2016',
});
});
break;
case Builders.Karma:
Expand Down Expand Up @@ -102,7 +106,10 @@ function updateModuleAndTarget(host: Tree, tsConfigPath: string, replacements: M
const recorder = host.beginUpdate(tsConfigPath);
if (newTarget) {
const targetAst = findPropertyInAstObject(compilerOptionsAst, 'target');
if (targetAst?.kind === 'string' && oldTarget === targetAst.value.toLowerCase()) {

if (!targetAst && !oldTarget) {
appendPropertyInAstObject(recorder, compilerOptionsAst, 'target', newTarget, 4);
} else if (targetAst?.kind === 'string' && (!oldTarget || oldTarget === targetAst.value.toLowerCase())) {
const offset = targetAst.start.offset + 1;
recorder.remove(offset, targetAst.value.length);
recorder.insertLeft(offset, newTarget);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,23 @@ describe('Migration to update target and module compiler options', () => {
expect(target).toBe('es2018');
});


it(`should remove module in 'tsconfig.server.json'`, async () => {
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise();
const { module, target } = readJsonFile(newTree, 'src/tsconfig.server.json').compilerOptions;
const { module } = readJsonFile(newTree, 'src/tsconfig.server.json').compilerOptions;
expect(module).toBeUndefined();
expect(target).toBeUndefined();
});

it(`should add target in 'tsconfig.server.json'`, async () => {
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise();
const { target } = readJsonFile(newTree, 'src/tsconfig.server.json').compilerOptions;
expect(target).toBe('es2016');
});

it(`should update target to es2016 in 'tsconfig.server.json'`, async () => {
tree.delete('src/tsconfig.server.json');
createJsonFile(tree, 'src/tsconfig.server.json', { compilerOptions: { module: 'commonjs', target: 'es5' } });
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise();
const { target } = readJsonFile(newTree, 'src/tsconfig.server.json').compilerOptions;
expect(target).toBe('es2016');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "./<%= tsConfigExtends %>",
"compilerOptions": {
"outDir": "<%= relativePathToWorkspaceRoot %>/out-tsc/server",
"target": "es2016",
"types": [
"node"
]
Expand Down
2 changes: 2 additions & 0 deletions packages/schematics/angular/universal/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ describe('Universal Schematic', () => {
extends: './tsconfig.app.json',
compilerOptions: {
outDir: './out-tsc/server',
target: 'es2016',
types: ['node'],
},
files: [
Expand All @@ -116,6 +117,7 @@ describe('Universal Schematic', () => {
extends: './tsconfig.app.json',
compilerOptions: {
outDir: '../../out-tsc/server',
target: 'es2016',
types: ['node'],
},
files: [
Expand Down

0 comments on commit 5a45f91

Please sign in to comment.