Skip to content

Commit a042290

Browse files
alan-agius4filipesilva
authored andcommittedMay 20, 2020
fix(@schematics/angular): update to tslib 2.0.0
TypeScript 3.9 requires tslib 2.0.0, with this change we; - Update tslib existing and new workspaces to use tslib 2.0.0 - Update new and existing libraries to use tslib 2.0.0 as a direct depedency. Tslib version is bound to the TypeScript version used to compile the library. Thus, we shouldn't list `tslib` as a `peerDependencies`. This is because, a user can install libraries which have been compiled with older versions of TypeScript and thus require multiple `tslib` versions to be installed. Closes: #17753 Reference: TOOL-1374
1 parent a78d1c3 commit a042290

File tree

7 files changed

+125
-17
lines changed

7 files changed

+125
-17
lines changed
 

‎packages/schematics/angular/library/files/package.json.template

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"version": "0.0.1",
44
"peerDependencies": {
55
"@angular/common": "^<%= angularLatestVersion %>",
6-
"@angular/core": "^<%= angularLatestVersion %>",
6+
"@angular/core": "^<%= angularLatestVersion %>"
7+
},
8+
"dependencies": {
79
"tslib": "^<%= tsLibLatestVersion %>"
810
}
911
}

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,6 @@
8585
"factory": "./update-10/update-module-and-target-compiler-options",
8686
"description": "Update 'module' and 'target' TypeScript compiler options."
8787
},
88-
"update-workspace-dependencies": {
89-
"version": "10.0.0-beta.6",
90-
"factory": "./update-10/update-dependencies",
91-
"description": "Workspace dependencies updates."
92-
},
9388
"update-angular-config": {
9489
"version": "10.0.0-beta.6",
9590
"factory": "./update-10/update-angular-config",
@@ -99,6 +94,16 @@
9994
"version": "10.0.0-beta.7",
10095
"factory": "./update-10/add-deprecation-rule-tslint",
10196
"description": "Adds the tslint deprecation rule to tslint JSON configuration files."
97+
},
98+
"update-libraries-tslib": {
99+
"version": "10.0.0-beta.7",
100+
"factory": "./update-10/update-libraries-tslib",
101+
"description": "Update library projects to use tslib version 2 as a direct dependency."
102+
},
103+
"update-workspace-dependencies": {
104+
"version": "10.0.0-beta.7",
105+
"factory": "./update-10/update-dependencies",
106+
"description": "Workspace dependencies updates."
102107
}
103108
}
104109
}

‎packages/schematics/angular/migrations/update-10/update-dependencies.ts

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default function (): Rule {
1818
'karma': '~5.0.0',
1919
'protractor': '~7.0.0',
2020
'ng-packagr': latestVersions.ngPackagr,
21+
'tslib': '^2.0.0',
2122
};
2223

2324
for (const [name, version] of Object.entries(dependenciesToUpdate)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { join, normalize } from '@angular-devkit/core';
10+
import { Rule } from '@angular-devkit/schematics';
11+
import { NodeDependencyType, addPackageJsonDependency, removePackageJsonDependency } from '../../utility/dependencies';
12+
import { getWorkspace } from '../../utility/workspace';
13+
import { ProjectType } from '../../utility/workspace-models';
14+
15+
16+
export default function (): Rule {
17+
return async host => {
18+
const workspace = await getWorkspace(host);
19+
20+
for (const [, project] of workspace.projects) {
21+
if (project.extensions.projectType !== ProjectType.Library) {
22+
// Only interested in library projects
23+
continue;
24+
}
25+
26+
const packageJsonPath = join(normalize(project.root), 'package.json');
27+
if (!host.exists(packageJsonPath)) {
28+
continue;
29+
}
30+
31+
// Remove tslib from any type of dependency
32+
removePackageJsonDependency(host, 'tslib', packageJsonPath);
33+
34+
// Add tslib as a direct dependency
35+
addPackageJsonDependency(host, {
36+
name: 'tslib',
37+
version: '^2.0.0',
38+
type: NodeDependencyType.Default,
39+
}, packageJsonPath);
40+
}
41+
};
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { EmptyTree } from '@angular-devkit/schematics';
10+
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
11+
import { ProjectType, WorkspaceSchema } from '../../utility/workspace-models';
12+
13+
function createWorkSpaceConfig(tree: UnitTestTree) {
14+
const angularConfig: WorkspaceSchema = {
15+
version: 1,
16+
projects: {
17+
lib: {
18+
root: '/project/lib',
19+
sourceRoot: '/project/lib/src',
20+
projectType: ProjectType.Library,
21+
prefix: 'lib',
22+
architect: {},
23+
},
24+
},
25+
};
26+
27+
tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2));
28+
}
29+
30+
describe(`Migration to add tslib as a direct dependency in library projects`, () => {
31+
const schematicName = 'update-libraries-tslib';
32+
33+
const schematicRunner = new SchematicTestRunner(
34+
'migrations',
35+
require.resolve('../migration-collection.json'),
36+
);
37+
38+
let tree: UnitTestTree;
39+
beforeEach(() => {
40+
tree = new UnitTestTree(new EmptyTree());
41+
createWorkSpaceConfig(tree);
42+
43+
tree.create('/project/lib/package.json', JSON.stringify({
44+
peerDependencies: {
45+
'@angular/common': '^9.0.0',
46+
'@angular/core': '^9.0.0',
47+
'tslib': '1.0.0',
48+
},
49+
}, undefined, 2));
50+
});
51+
52+
it(`should update tslib to version 2 as a direct dependency`, async () => {
53+
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise();
54+
const { peerDependencies, dependencies } = JSON.parse(newTree.readContent('/project/lib/package.json'));
55+
expect(peerDependencies['tslib']).toBeUndefined();
56+
expect(dependencies['tslib']).toBe('^2.0.0');
57+
});
58+
});

‎packages/schematics/angular/utility/dependencies.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
} from './json-utils';
1616

1717

18-
const pkgJsonPath = '/package.json';
18+
const PKG_JSON_PATH = '/package.json';
1919
export enum NodeDependencyType {
2020
Default = 'dependencies',
2121
Dev = 'devDependencies',
@@ -30,8 +30,8 @@ export interface NodeDependency {
3030
overwrite?: boolean;
3131
}
3232

33-
export function addPackageJsonDependency(tree: Tree, dependency: NodeDependency): void {
34-
const packageJsonAst = _readPackageJson(tree);
33+
export function addPackageJsonDependency(tree: Tree, dependency: NodeDependency, pkgJsonPath = PKG_JSON_PATH): void {
34+
const packageJsonAst = _readPackageJson(tree, pkgJsonPath);
3535
const depsNode = findPropertyInAstObject(packageJsonAst, dependency.type);
3636
const recorder = tree.beginUpdate(pkgJsonPath);
3737
if (!depsNode) {
@@ -63,8 +63,8 @@ export function addPackageJsonDependency(tree: Tree, dependency: NodeDependency)
6363
tree.commitUpdate(recorder);
6464
}
6565

66-
export function removePackageJsonDependency(tree: Tree, name: string): void {
67-
const packageJson = _readPackageJson(tree);
66+
export function removePackageJsonDependency(tree: Tree, name: string, pkgJsonPath = PKG_JSON_PATH): void {
67+
const packageJson = _readPackageJson(tree, pkgJsonPath);
6868
const recorder = tree.beginUpdate(pkgJsonPath);
6969
[
7070
NodeDependencyType.Default,
@@ -81,8 +81,8 @@ export function removePackageJsonDependency(tree: Tree, name: string): void {
8181
tree.commitUpdate(recorder);
8282
}
8383

84-
export function getPackageJsonDependency(tree: Tree, name: string): NodeDependency | null {
85-
const packageJson = _readPackageJson(tree);
84+
export function getPackageJsonDependency(tree: Tree, name: string, pkgJsonPath = PKG_JSON_PATH): NodeDependency | null {
85+
const packageJson = _readPackageJson(tree, pkgJsonPath);
8686
let dep: NodeDependency | null = null;
8787
[
8888
NodeDependencyType.Default,
@@ -110,16 +110,16 @@ export function getPackageJsonDependency(tree: Tree, name: string): NodeDependen
110110
return dep;
111111
}
112112

113-
function _readPackageJson(tree: Tree): JsonAstObject {
113+
function _readPackageJson(tree: Tree, pkgJsonPath: string): JsonAstObject {
114114
const buffer = tree.read(pkgJsonPath);
115115
if (buffer === null) {
116-
throw new SchematicsException('Could not read package.json.');
116+
throw new SchematicsException(`Could not read ${pkgJsonPath}.`);
117117
}
118118
const content = buffer.toString();
119119

120120
const packageJson = parseJsonAst(content, JsonParseMode.Strict);
121121
if (packageJson.kind != 'object') {
122-
throw new SchematicsException('Invalid package.json. Was expecting an object');
122+
throw new SchematicsException(`Invalid ${pkgJsonPath}. Was expecting an object.`);
123123
}
124124

125125
return packageJson;

‎packages/schematics/angular/utility/latest-versions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const latestVersions = {
1212
RxJs: '~6.5.4',
1313
ZoneJs: '~0.10.2',
1414
TypeScript: '~3.9.2',
15-
TsLib: '^1.12.0',
15+
TsLib: '^2.0.0',
1616

1717
// The versions below must be manually updated when making a new devkit release.
1818
// For our e2e tests, these versions must match the latest tag present on the branch.

0 commit comments

Comments
 (0)
Please sign in to comment.