Skip to content

Commit

Permalink
feat(@schematics/angular): add migration for evalSourceMap, `vendor…
Browse files Browse the repository at this point in the history
…SourceMap` `profile` and `skipAppShell` options.
  • Loading branch information
alan-agius4 authored and mgechev committed Apr 27, 2020
1 parent 8fb7e58 commit 5855c14
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
"version": "10.0.0-beta.2",
"factory": "./update-9/schematic-options",
"description": "Replace deprecated 'styleext' and 'spec' Angular schematic options."
},
"update-angular-config": {
"version": "10.0.0-beta.3",
"factory": "./update-10/update-angular-config",
"description": "Remove deprecated 'evalSourceMap', `profile`, 'skipAppShell' and 'vendorSourceMap' from 'angular.json'."
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* @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 { workspaces } from '@angular-devkit/core';
import { Rule } from '@angular-devkit/schematics';
import { updateWorkspace } from '../../utility/workspace';
import { ProjectType } from '../../utility/workspace-models';

export default function (): Rule {
return updateWorkspace(workspace => {
for (const [, project] of workspace.projects) {
if (project.extensions.projectType !== ProjectType.Application) {
// Only interested in application projects since these changes only effects application builders
continue;
}

for (const [, target] of project.targets) {
// Only interested in Angular Devkit builders
if (!target?.builder.startsWith('@angular-devkit/build-angular')) {
continue;
}

// Check options
if (target.options) {
target.options = {
...updateVendorSourceMap(target.options),
evalSourceMap: undefined,
skipAppShell: undefined,
profile: undefined,
};
}

// Go through each configuration entry
if (!target.configurations) {
continue;
}

for (const configurationName of Object.keys(target.configurations)) {
target.configurations[configurationName] = {
...updateVendorSourceMap(target.configurations[configurationName]),
evalSourceMap: undefined,
skipAppShell: undefined,
profile: undefined,
};
}
}
}
});
}

type TargetOptions = workspaces.TargetDefinition['options'];

function updateVendorSourceMap(options: TargetOptions): TargetOptions {
if (!options) {
return {};
}

const { vendorSourceMap: vendor, sourceMap = true } = options;

if (vendor === undefined) {
return options;
}

if (sourceMap === true) {
return {
...options,
sourceMap: {
styles: true,
scripts: true,
vendor,
},
vendorSourceMap: undefined,
};
}

if (typeof sourceMap === 'object') {
return {
...options,
sourceMap: {
...sourceMap,
vendor,
},
vendorSourceMap: undefined,
};
}

return {
...options,
vendorSourceMap: undefined,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/**
* @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 { 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<Builders.Browser, JsonObject> {
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: {
build: {
builder: Builders.Browser,
options: {
skipAppShell: true,
sourceMap: true,
vendorSourceMap: true,
evalSourceMap: false,
buildOptimizer: true,
// tslint:disable-next-line:no-any
} as any,
configurations: {
one: {
sourceMap: false,
vendorSourceMap: false,
skipAppShell: false,
evalSourceMap: true,
buildOptimizer: false,
},
two: {
sourceMap: {
styles: false,
scripts: true,
},
evalSourceMap: true,
vendorSourceMap: false,
skipAppShell: true,
buildOptimizer: true,
},
// tslint:disable-next-line:no-any
} as any,
},
},
},
},
};

tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2));
}

describe(`Migration to update 'angular.json'`, () => {
const schematicName = 'update-angular-config';

const schematicRunner = new SchematicTestRunner(
'migrations',
require.resolve('../migration-collection.json'),
);

let tree: UnitTestTree;
beforeEach(() => {
tree = new UnitTestTree(new EmptyTree());
createWorkSpaceConfig(tree);
});

it(`should remove 'skipAppShell'`, async () => {
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise();
const { options, configurations } = getBuildTarget(newTree);

expect(options.skipAppShell).toBeUndefined();
expect(configurations).toBeDefined();
expect(configurations?.one.skipAppShell).toBeUndefined();
expect(configurations?.two.skipAppShell).toBeUndefined();
});

it(`should remove 'evalSourceMap'`, async () => {
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise();
const { options, configurations } = getBuildTarget(newTree);

expect(options.evalSourceMap).toBeUndefined();
expect(configurations).toBeDefined();
expect(configurations?.one.evalSourceMap).toBeUndefined();
expect(configurations?.two.evalSourceMap).toBeUndefined();
});

it(`should remove 'vendorSourceMap' and set 'vendor' option in 'sourceMap'`, async () => {
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise();
const { options, configurations } = getBuildTarget(newTree);

expect(options.vendorSourceMap).toBeUndefined();
expect(options.sourceMap).toEqual({
vendor: true,
scripts: true,
styles: true,
});
expect(options.vendorSourceMap).toBeUndefined();

expect(configurations).toBeDefined();
expect(configurations?.one.vendorSourceMap).toBeUndefined();
expect(configurations?.one.sourceMap).toBe(false);

expect(configurations?.two.vendorSourceMap).toBeUndefined();
expect(configurations?.two.sourceMap).toEqual({
vendor: false,
scripts: true,
styles: false,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default function (): Rule {
return (host, context) => {
const dependenciesToUpdate: Record<string, string> = {
'karma': '~5.0.0',
'protractor': '~5.4.4',
'ng-packagr': latestVersions.ngPackagr,
};

Expand Down
23 changes: 14 additions & 9 deletions packages/schematics/angular/utility/workspace-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,20 @@ export interface LibraryBuilderOptions {
}

export interface ServerBuilderOptions {
outputPath: string;
tsConfig: string;
main: string;
fileReplacements?: FileReplacements[];
optimization?: {
scripts?: boolean;
styles?: boolean;
};
sourceMap?: boolean;
outputPath: string;
tsConfig: string;
main: string;
fileReplacements?: FileReplacements[];
optimization?: {
scripts?: boolean;
styles?: boolean;
};
sourceMap?: boolean | {
scripts?: boolean;
styles?: boolean;
hidden?: boolean;
vendor?: boolean;
};
}

export interface AppShellBuilderOptions {
Expand Down

0 comments on commit 5855c14

Please sign in to comment.