Nx & Angular
This document covers the difference between Nx Devkit and Angular Devkit. See the Nx Devkit guide for more in-depth details about Nx Devkit.
Nx comes with a devkit to write generators and executors, but you can also use Angular devkit (schematics and builders). In other words, you can use an Angular schematic to implement a generator, and you can use an Angular builder to implement an executor.
What are the differences between Nx Devkit and Angular Devkit?
The following is a generator written using Nx Devkit:
import { Tree, formatFiles, generateFiles } from '@nx/devkit'; import * as path from 'path'; interface Schema { name: string; skipFormat: boolean; } export default async function (tree: Tree, options: Schema) { generateFiles( tree, path.join(__dirname, 'files'), path.join('tools/generators', schema.name), options ); if (!schema.skipFormat) { await formatFiles(tree); } }
The following is an analogous generator written as an Angular Schematic.
import { apply, branchAndMerge, chain, mergeWith, Rule, template, url, move, } from '@angular-devkit/schematics'; import { formatFiles } from '@nx/workspace'; import { toFileName } from '@nx/workspace'; interface Schema { name: string; skipFormat: boolean; } export default function (options: Schema): Rule { const templateSource = apply(url('./files'), [ template({ dot: '.', tmpl: '', ...(options as any), }), move('tools/generators'), ]); return chain([ branchAndMerge(chain([mergeWith(templateSource)])), formatFiles(options), ]); }
chain([mergeWith(apply(url
is replaced with generateFiles
)externalSchematic
) that is required when using Angular Schematics.The Nx CLI can invoke Nx Generator or Angular Schematics directly. When the user runs:
❯
nx g mygenerator params
❯
nx g myschematic params
The Nx CLI will identify whether it is running an Nx generator or an Angular schematic and will invoke it using the right machinery. The user doesn't have to know how the generator is implemented.
At times, however, it might be useful to use an Nx Devkit generator in an Angular Schematic or vice versa.
Making an Angular Schematic out of Nx Devkit Generator:
First, you need to
export async function mygenerator(tree: Tree, options: Schema) { } export const mygeneratorSchematic = convertNxGenerator(mygenerator);
Then, you might need to register it in the collections.json
:
{ "name": "Nx React", "version": "0.1", "extends": ["@nx/workspace"], "schematics": { "mygenerator": { "factory": "./src/generators/mygenerator/mygenerator#mygeneratorSchematic", "schema": "./src/generators/mygenerator/schema.json" } }, "generators": { "init": { "factory": "./src/generators/mygenerator/mygenerator#mygenerator", "schema": "./src/generators/mygenerator/schema.json" } } }
Making an Nx Devkit Generator out of Angular Schematic:
export const libraryGenerator = wrapAngularDevkitSchematic( '@schematics/angular', 'library' ); export async function mygenerator(tree: Tree, options: Schema) { await libraryGenerator(tree, options); }
The following is an executor written using Nx Devkit:
interface Schema { message: string; allCaps: boolean; } export default async function ( options: Schema, context: ExecutorContext ): Promise<{ success: true }> { if (options.allCaps) { console.log(options.message.toUpperCase()); } else { console.log(options.message); } return { success: true }; }
The following is an analogous executor written as an Angular builder:
interface Schema { message: string; allCaps: boolean; } export function run( options: Schema, context: BuilderContext ): Observable<{ success: true }> { if (options.allCaps) { console.log(options.message.toUpperCase()); } else { console.log(options.message); } return of({ success: true }); } export default createBuilder<NextBuildBuilderOptions>(run);
createBuilder
.The schema files for both Nx Devkit executors and Angular Builders are the same. Nx can run both of them in the same way.
If you are writing an Nx plugin, use Nx Devkit. It's much easier to use and debug. It has better docs and more people supporting it.
Do you have to rewrite your Nx Plugin if it is written using Angular Devkit? No. Nx supports both and will always support both. And, most importantly, the end user might not even know what you used to write a generator or an executor.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4