Skip to content

Commit

Permalink
feat(@schematics/angular): add migration to replace usages of `@nguni…
Browse files Browse the repository at this point in the history
…versal/builders`

This migration replaces usages of `@nguniversal/builders` with `@angular-devkit/build-angular`.
  • Loading branch information
alan-agius4 committed Oct 2, 2023
1 parent 2e0a6fe commit dc6b6ea
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{
"schematics": {}
"schematics": {
"replace-nguniversal-builders": {
"version": "17.0.0",
"factory": "./update-17/replace-nguniversal-builders",
"description": "Replace usages of '@nguniversal/builders' with '@angular-devkit/build-angular'."
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @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, chain } from '@angular-devkit/schematics';
import { removePackageJsonDependency } from '../../utility/dependencies';
import { allTargetOptions, updateWorkspace } from '../../utility/workspace';
import { ProjectType } from '../../utility/workspace-models';

export default function (): Rule {
return chain([
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) {
if (target.builder === '@nguniversal/builders:ssr-dev-server') {
target.builder = '@angular-devkit/build-angular:ssr-dev-server';
} else if (target.builder === '@nguniversal/builders:prerender') {
target.builder = '@angular-devkit/build-angular:prerender';
for (const [, options] of allTargetOptions(target, false)) {
// Remove and replace builder options
if (options['guessRoutes'] !== undefined) {
options['discoverRoutes'] = options['guessRoutes'];
delete options['guessRoutes'];
}

if (options['numProcesses'] !== undefined) {
delete options['numProcesses'];
}
}
}
}
}
}),
(host) => {
removePackageJsonDependency(host, '@nguniversal/builders');
},
]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* @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 { 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: {
app: {
root: '/project/lib',
sourceRoot: '/project/app/src',
projectType: ProjectType.Application,
prefix: 'app',
architect: {
'serve-ssr': {
builder: '@nguniversal/builders:ssr-dev-server',
options: {
browserTarget: 'appprerender:build',
serverTarget: 'appprerender:server',
},
configurations: {
production: {
browserTarget: 'appprerender:build:production',
serverTarget: 'appprerender:server:production',
},
},
},
prerender: {
builder: '@nguniversal/builders:prerender',
options: {
browserTarget: 'appprerender:build:production',
serverTarget: 'appprerender:server:production',
numProcesses: 1,
guessRoutes: false,
},
},
},
},
},
};

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

describe(`Migration to replace '@nguniversal/builders' with '@angular-devkit/build-angular'`, () => {
const schematicName = 'replace-nguniversal-builders';

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

let tree: UnitTestTree;
beforeEach(() => {
tree = new UnitTestTree(new EmptyTree());
createWorkSpaceConfig(tree);
tree.create(
'/package.json',
JSON.stringify(
{
devDependencies: {
'@nguniversal/builders': '0.0.0',
},
},
undefined,
2,
),
);
});

it(`should remove '@nguniversal/builders' from devDependencies`, async () => {
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
const { devDependencies } = JSON.parse(newTree.readContent('/package.json'));
expect(devDependencies['@nguniversal/builders']).toBeUndefined();
});

it(`should replace '@nguniversal/builders:ssr-dev-server' target`, async () => {
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
const {
projects: { app },
} = JSON.parse(newTree.readContent('/angular.json'));
expect(app.architect['serve-ssr'].builder).toBe('@angular-devkit/build-angular:ssr-dev-server');
});

it(`should replace '@nguniversal/builders:prerender' target`, async () => {
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
const {
projects: { app },
} = JSON.parse(newTree.readContent('/angular.json'));
expect(app.architect['prerender'].builder).toBe('@angular-devkit/build-angular:prerender');
});

it(`should replace old '@nguniversal/builders:prerender' options`, async () => {
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
const {
projects: { app },
} = JSON.parse(newTree.readContent('/angular.json'));
const { guessRoutes, numProcesses, discoverRoutes } = app.architect['prerender'].options;
expect(guessRoutes).toBeUndefined();
expect(discoverRoutes).toBeFalse();
expect(numProcesses).toBeUndefined();
});
});

0 comments on commit dc6b6ea

Please sign in to comment.