Angular Plugin

Angular logo

The Nx Plugin for Angular contains executors, generators, and utilities for managing Angular applications, and libraries within an Nx workspace. It provides:

  • Integration with libraries such as Jest, Cypress, Karma, Protractor, and Storybook.
  • Helper services, and functions to use along with NgRx libraries.
  • Scaffolding for upgrading AngularJS applications.
  • Scaffolding for creating buildable libraries that can be published to npm.
  • Utilities for automatic workspace refactoring.

Adding the Angular plugin

Adding the Angular plugin to a workspace can be done with the following:

#yarn
yarn add -D @nrwl/angular
#npm
npm install -D @nrwl/angular

Angular Projects in an Nx Workspace

Building Angular applications within an Nx workspace is similar to building within a vanilla Angular CLI workspace, with a few differences.

  • ESLint is used as the default linter, configurable in the generators section of the workspace configuration.
  • Jest is used as the default unit test runner, configurable in the generators section of the workspace configuration.
  • Cypress is used as the default E2E test runner, configurable in the generators section of the workspace configuration.
  • E2E tests are included in a separate project from the Angular application itself.
  • The Nx CLI delegates common commands such as build, serve, test, lint, and e2e to the Angular CLI.

The file structure for an Angular application looks like:

myorg/
ā”œā”€ā”€ apps/
ā”‚   ā”œā”€ā”€ myapp/
ā”‚   ā”‚   ā”œā”€ā”€ src/
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ app/
ā”‚   ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ app.component.html
ā”‚   ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ app.component.scss
ā”‚   ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ app.component.spec.ts
ā”‚   ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ app.component.ts
ā”‚   ā”‚   ā”‚   ā”‚   ā””ā”€ā”€ app.module.ts
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ assets/
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ environments/
ā”‚   ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ environment.prod.ts
ā”‚   ā”‚   ā”‚   ā”‚   ā””ā”€ā”€ environment.ts
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ favicon.ico
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ index.html
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ main.ts
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ polyfills.ts
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ styles.scss
ā”‚   ā”‚   ā”‚   ā””ā”€ā”€ test-setup.ts
ā”‚   ā”‚   ā”œā”€ā”€ .browserslistrc
ā”‚   ā”‚   ā”œā”€ā”€ .eslintrc.json
ā”‚   ā”‚   ā”œā”€ā”€ jest.config.js
ā”‚   ā”‚   ā”œā”€ā”€ tsconfig.app.json
ā”‚   ā”‚   ā”œā”€ā”€ tsconfig.editor.json
ā”‚   ā”‚   ā”œā”€ā”€ tsconfig.json
ā”‚   ā”‚   ā””ā”€ā”€ tsconfig.spec.json
ā”‚   ā””ā”€ā”€ myapp-e2e/
ā”‚       ā”œā”€ā”€ src/
ā”‚       ā”‚   ā”œā”€ā”€ fixtures/
ā”‚       ā”‚   ā”‚   ā””ā”€ā”€ example.json
ā”‚       ā”‚   ā”œā”€ā”€ integration/
ā”‚       ā”‚   ā”‚   ā””ā”€ā”€ app.spec.ts
ā”‚       ā”‚   ā”œā”€ā”€ plugins/
ā”‚       ā”‚   ā”‚   ā””ā”€ā”€ index.ts
ā”‚       ā”‚   ā””ā”€ā”€ support/
ā”‚       ā”‚       ā”œā”€ā”€ app.po.ts
ā”‚       ā”‚       ā”œā”€ā”€ commands.ts
ā”‚       ā”‚       ā””ā”€ā”€ index.ts
ā”‚       ā”œā”€ā”€ .eslintrc.json
ā”‚       ā”œā”€ā”€ cypress.json
ā”‚       ā”œā”€ā”€ tsconfig.e2e.json
ā”‚       ā””ā”€ā”€ tsconfig.json
ā”œā”€ā”€ libs/
ā”œā”€ā”€ tools/
ā”‚   ā”œā”€ā”€ generators/
ā”‚   ā””ā”€ā”€ tsconfig.tools.json
ā”œā”€ā”€ .editorconfig
ā”œā”€ā”€ .eslintrc.json
ā”œā”€ā”€ .gitignore
ā”œā”€ā”€ .prettierignore
ā”œā”€ā”€ .prettierrc
ā”œā”€ā”€ angular.json
ā”œā”€ā”€ decorate-angular-cli.js
ā”œā”€ā”€ jest.config.js
ā”œā”€ā”€ jest.preset.js
ā”œā”€ā”€ nx.json
ā”œā”€ā”€ package.json
ā”œā”€ā”€ README.md
ā””ā”€ā”€ tsconfig.base.json

See Also

Executors / Builders

  • delegate-build - Delegates the build to a different target while supporting incremental builds.
  • ng-packagr-lite - Builds a library with support for incremental builds.
  • package - Builds and packages an Angular library to be distributed as an NPM package. It supports incremental builds.
  • webpack-browser - Builds a browser application with support for incremental builds and custom webpack configuration.

Generators

  • application - Creates an Angular application.
  • convert-tslint-to-eslint - Converts a project from TSLint to ESLint.
  • downgrade-module - Sets up a Downgrade Module.
  • karma - Adds Karma configuration to a workspace.
  • karma-project - Adds Karma configuration to a project.
  • library - Creates an Angular library.
  • move - Moves an Angular application or library to another folder within the workspace and updates the project configuration.
  • ngrx - Adds NgRx support to an application or library.
  • setup-mfe - Generate a Module Federation configuration for a given Angular application.
  • stories - Creates stories/specs for all components declared in a project.
  • storybook-configuration - Adds Storybook configuration to a project.
  • storybook-migrate-defaults-5-to-6 - Generates default Storybook configuration files using Storybook version >=6.x specs, for projects that already have Storybook instances and configurations of versions <6.x.
  • storybook-migrate-stories-to-6-2 - Migrates stories to match the new syntax in v6.2 where the component declaration should be in the default export.
  • upgrade-module - Sets up an Upgrade Module.
  • web-worker - Creates a Web Worker.

Public API

  • DataPersistence - Angular Service that provides convenience methods for common operations of persisting data.
  • fetch - Handles data fetching, and correct ordering of fetching using NgRx Effects.
  • navigation - Handles data fetching based on navigating to a certain component using NgRx Effects.
  • optimisticUpdate - Handles optimistic updates (updating the client first) using NgRx Effects.
  • pessimisticUpdate - Handles pessimistic updates (updating the server first) using NgRx Effects.
  • NxModule - An NgModule used to register the Angular providers, including DataPersistence.