From 268a03b63094d9c680401bc0977edafb22826ce3 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Fri, 30 Jul 2021 07:46:37 +0200 Subject: [PATCH] feat(@schematics/angular): add migration to update the workspace config With this change we add a migration to update the workspace configuration to version 13 by removed deprecated options and builders. --- .../migrations/migration-collection.json | 5 + .../update-13/update-angular-config.ts | 35 +++++++ .../update-13/update-angular-config_spec.ts | 95 +++++++++++++++++++ .../angular/utility/workspace-models.ts | 1 - 4 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 packages/schematics/angular/migrations/update-13/update-angular-config.ts create mode 100644 packages/schematics/angular/migrations/update-13/update-angular-config_spec.ts diff --git a/packages/schematics/angular/migrations/migration-collection.json b/packages/schematics/angular/migrations/migration-collection.json index 0b2713c3b88b..acfcf643db4d 100644 --- a/packages/schematics/angular/migrations/migration-collection.json +++ b/packages/schematics/angular/migrations/migration-collection.json @@ -134,6 +134,11 @@ "version": "13.0.0", "factory": "./update-13/schematic-options", "description": "Remove no longer valid Angular schematic options from `angular.json`." + }, + "update-angular-config-v13": { + "version": "13.0.0", + "factory": "./update-13/update-angular-config", + "description": "Remove deprecated options from 'angular.json' that are no longer present in v13." } } } diff --git a/packages/schematics/angular/migrations/update-13/update-angular-config.ts b/packages/schematics/angular/migrations/update-13/update-angular-config.ts new file mode 100644 index 000000000000..fd7d98daf5bb --- /dev/null +++ b/packages/schematics/angular/migrations/update-13/update-angular-config.ts @@ -0,0 +1,35 @@ +/** + * @license + * Copyright Google LLC 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 { Rule } from '@angular-devkit/schematics'; +import { allTargetOptions, updateWorkspace } from '../../utility/workspace'; + +export default function (): Rule { + return updateWorkspace((workspace) => { + for (const [, project] of workspace.projects) { + for (const [name, target] of project.targets) { + // Delete removed tslint builder + if (target.builder === '@angular-devkit/build-angular:tslint') { + project.targets.delete(name); + continue; + } + + if (!target.builder.startsWith('@angular-devkit/build-angular')) { + continue; + } + + // Only interested in Angular Devkit builders + for (const [, options] of allTargetOptions(target)) { + delete options.extractCss; + delete options.servePathDefaultWarning; + delete options.hmrWarning; + } + } + } + }); +} diff --git a/packages/schematics/angular/migrations/update-13/update-angular-config_spec.ts b/packages/schematics/angular/migrations/update-13/update-angular-config_spec.ts new file mode 100644 index 000000000000..e681d4857f3d --- /dev/null +++ b/packages/schematics/angular/migrations/update-13/update-angular-config_spec.ts @@ -0,0 +1,95 @@ +/** + * @license + * Copyright Google LLC 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 { JsonObject } from '@angular-devkit/core'; +import { EmptyTree } from '@angular-devkit/schematics'; +import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; +import { + BuilderTarget, + Builders, + ProjectType, + WorkspaceSchema, +} from '../../utility/workspace-models'; + +function getBuildTarget(tree: UnitTestTree): BuilderTarget { + return JSON.parse(tree.readContent('/angular.json')).projects.app.architect.build; +} + +function createWorkSpaceConfig(tree: UnitTestTree) { + const angularConfig: WorkspaceSchema = { + version: 1, + projects: { + app: { + root: '', + sourceRoot: 'src', + projectType: ProjectType.Application, + prefix: 'app', + architect: { + lint: { + builder: '@angular-devkit/build-angular:tslint', + }, + build: { + builder: Builders.Browser, + options: { + scripts: [{ lazy: true, name: 'bundle-1.js' }], + extractCss: false, + sourceMaps: true, + buildOptimizer: false, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + } as any, + configurations: { + one: { + aot: true, + }, + two: { + extractCss: true, + aot: true, + }, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + } as any, + }, + }, + }, + }, + }; + + tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2)); +} + +const schematicName = 'update-angular-config-v13'; + +describe(`Migration to update 'angular.json'. ${schematicName}`, () => { + const schematicRunner = new SchematicTestRunner( + 'migrations', + require.resolve('../migration-collection.json'), + ); + + let tree: UnitTestTree; + beforeEach(() => { + tree = new UnitTestTree(new EmptyTree()); + createWorkSpaceConfig(tree); + }); + + it(`should remove 'extractCss'`, async () => { + const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); + const { options, configurations } = getBuildTarget(newTree); + + expect(options.extractCss).toBeUndefined(); + expect(configurations).toBeDefined(); + expect(configurations?.one.extractCss).toBeUndefined(); + expect(configurations?.two.extractCss).toBeUndefined(); + }); + + it(`should remove tslint builder`, async () => { + const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); + + const { build, lint } = JSON.parse(newTree.readContent('/angular.json')).projects.app.architect; + expect(build).toBeDefined(); + expect(lint).toBeUndefined(); + }); +}); diff --git a/packages/schematics/angular/utility/workspace-models.ts b/packages/schematics/angular/utility/workspace-models.ts index 87d698822884..5bfa9c0878f4 100644 --- a/packages/schematics/angular/utility/workspace-models.ts +++ b/packages/schematics/angular/utility/workspace-models.ts @@ -49,7 +49,6 @@ export interface BrowserBuilderOptions extends BrowserBuilderBaseOptions { optimization?: boolean; outputHashing?: OutputHashing; resourcesOutputPath?: string; - extractCss?: boolean; namedChunks?: boolean; aot?: boolean; extractLicenses?: boolean;