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
This commit is contained in:
Alan Agius 2020-05-19 12:11:14 +02:00 committed by Filipe Silva
parent a78d1c3ed1
commit a042290726
7 changed files with 125 additions and 17 deletions

View File

@ -3,7 +3,9 @@
"version": "0.0.1",
"peerDependencies": {
"@angular/common": "^<%= angularLatestVersion %>",
"@angular/core": "^<%= angularLatestVersion %>",
"@angular/core": "^<%= angularLatestVersion %>"
},
"dependencies": {
"tslib": "^<%= tsLibLatestVersion %>"
}
}

View File

@ -85,11 +85,6 @@
"factory": "./update-10/update-module-and-target-compiler-options",
"description": "Update 'module' and 'target' TypeScript compiler options."
},
"update-workspace-dependencies": {
"version": "10.0.0-beta.6",
"factory": "./update-10/update-dependencies",
"description": "Workspace dependencies updates."
},
"update-angular-config": {
"version": "10.0.0-beta.6",
"factory": "./update-10/update-angular-config",
@ -99,6 +94,16 @@
"version": "10.0.0-beta.7",
"factory": "./update-10/add-deprecation-rule-tslint",
"description": "Adds the tslint deprecation rule to tslint JSON configuration files."
},
"update-libraries-tslib": {
"version": "10.0.0-beta.7",
"factory": "./update-10/update-libraries-tslib",
"description": "Update library projects to use tslib version 2 as a direct dependency."
},
"update-workspace-dependencies": {
"version": "10.0.0-beta.7",
"factory": "./update-10/update-dependencies",
"description": "Workspace dependencies updates."
}
}
}

View File

@ -18,6 +18,7 @@ export default function (): Rule {
'karma': '~5.0.0',
'protractor': '~7.0.0',
'ng-packagr': latestVersions.ngPackagr,
'tslib': '^2.0.0',
};
for (const [name, version] of Object.entries(dependenciesToUpdate)) {

View File

@ -0,0 +1,42 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { join, normalize } from '@angular-devkit/core';
import { Rule } from '@angular-devkit/schematics';
import { NodeDependencyType, addPackageJsonDependency, removePackageJsonDependency } from '../../utility/dependencies';
import { getWorkspace } from '../../utility/workspace';
import { ProjectType } from '../../utility/workspace-models';
export default function (): Rule {
return async host => {
const workspace = await getWorkspace(host);
for (const [, project] of workspace.projects) {
if (project.extensions.projectType !== ProjectType.Library) {
// Only interested in library projects
continue;
}
const packageJsonPath = join(normalize(project.root), 'package.json');
if (!host.exists(packageJsonPath)) {
continue;
}
// Remove tslib from any type of dependency
removePackageJsonDependency(host, 'tslib', packageJsonPath);
// Add tslib as a direct dependency
addPackageJsonDependency(host, {
name: 'tslib',
version: '^2.0.0',
type: NodeDependencyType.Default,
}, packageJsonPath);
}
};
}

View File

@ -0,0 +1,58 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { EmptyTree } from '@angular-devkit/schematics';
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { ProjectType, WorkspaceSchema } from '../../utility/workspace-models';
function createWorkSpaceConfig(tree: UnitTestTree) {
const angularConfig: WorkspaceSchema = {
version: 1,
projects: {
lib: {
root: '/project/lib',
sourceRoot: '/project/lib/src',
projectType: ProjectType.Library,
prefix: 'lib',
architect: {},
},
},
};
tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2));
}
describe(`Migration to add tslib as a direct dependency in library projects`, () => {
const schematicName = 'update-libraries-tslib';
const schematicRunner = new SchematicTestRunner(
'migrations',
require.resolve('../migration-collection.json'),
);
let tree: UnitTestTree;
beforeEach(() => {
tree = new UnitTestTree(new EmptyTree());
createWorkSpaceConfig(tree);
tree.create('/project/lib/package.json', JSON.stringify({
peerDependencies: {
'@angular/common': '^9.0.0',
'@angular/core': '^9.0.0',
'tslib': '1.0.0',
},
}, undefined, 2));
});
it(`should update tslib to version 2 as a direct dependency`, async () => {
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise();
const { peerDependencies, dependencies } = JSON.parse(newTree.readContent('/project/lib/package.json'));
expect(peerDependencies['tslib']).toBeUndefined();
expect(dependencies['tslib']).toBe('^2.0.0');
});
});

View File

@ -15,7 +15,7 @@ import {
} from './json-utils';
const pkgJsonPath = '/package.json';
const PKG_JSON_PATH = '/package.json';
export enum NodeDependencyType {
Default = 'dependencies',
Dev = 'devDependencies',
@ -30,8 +30,8 @@ export interface NodeDependency {
overwrite?: boolean;
}
export function addPackageJsonDependency(tree: Tree, dependency: NodeDependency): void {
const packageJsonAst = _readPackageJson(tree);
export function addPackageJsonDependency(tree: Tree, dependency: NodeDependency, pkgJsonPath = PKG_JSON_PATH): void {
const packageJsonAst = _readPackageJson(tree, pkgJsonPath);
const depsNode = findPropertyInAstObject(packageJsonAst, dependency.type);
const recorder = tree.beginUpdate(pkgJsonPath);
if (!depsNode) {
@ -63,8 +63,8 @@ export function addPackageJsonDependency(tree: Tree, dependency: NodeDependency)
tree.commitUpdate(recorder);
}
export function removePackageJsonDependency(tree: Tree, name: string): void {
const packageJson = _readPackageJson(tree);
export function removePackageJsonDependency(tree: Tree, name: string, pkgJsonPath = PKG_JSON_PATH): void {
const packageJson = _readPackageJson(tree, pkgJsonPath);
const recorder = tree.beginUpdate(pkgJsonPath);
[
NodeDependencyType.Default,
@ -81,8 +81,8 @@ export function removePackageJsonDependency(tree: Tree, name: string): void {
tree.commitUpdate(recorder);
}
export function getPackageJsonDependency(tree: Tree, name: string): NodeDependency | null {
const packageJson = _readPackageJson(tree);
export function getPackageJsonDependency(tree: Tree, name: string, pkgJsonPath = PKG_JSON_PATH): NodeDependency | null {
const packageJson = _readPackageJson(tree, pkgJsonPath);
let dep: NodeDependency | null = null;
[
NodeDependencyType.Default,
@ -110,16 +110,16 @@ export function getPackageJsonDependency(tree: Tree, name: string): NodeDependen
return dep;
}
function _readPackageJson(tree: Tree): JsonAstObject {
function _readPackageJson(tree: Tree, pkgJsonPath: string): JsonAstObject {
const buffer = tree.read(pkgJsonPath);
if (buffer === null) {
throw new SchematicsException('Could not read package.json.');
throw new SchematicsException(`Could not read ${pkgJsonPath}.`);
}
const content = buffer.toString();
const packageJson = parseJsonAst(content, JsonParseMode.Strict);
if (packageJson.kind != 'object') {
throw new SchematicsException('Invalid package.json. Was expecting an object');
throw new SchematicsException(`Invalid ${pkgJsonPath}. Was expecting an object.`);
}
return packageJson;

View File

@ -12,7 +12,7 @@ export const latestVersions = {
RxJs: '~6.5.4',
ZoneJs: '~0.10.2',
TypeScript: '~3.9.2',
TsLib: '^1.12.0',
TsLib: '^2.0.0',
// The versions below must be manually updated when making a new devkit release.
// For our e2e tests, these versions must match the latest tag present on the branch.