Using Executors / Builders
Executors perform actions on your code. This can include building, linting, testing, serving and many other actions.
Executors can be written using @nrwl/devkit or @angular-devkit. Executors written with the @angular-devkit are called Builders.
There are two main differences between an executor and a shell script or an npm script:
- Executors encourage a consistent methodology for performing similar actions on unrelated projects. i.e. A developer switching between teams can be confident that
nx build project2will buildproject2with the default settings, just likenx build project1builtproject1. - Nx can leverage this consistency to perform the same executor across multiple projects. i.e.
nx affected --target==testwill run thetestexecutor on every project that is affected by the current code change.
Executor Definitions
The executors that are available for each project are defined and configured in the /workspace.json file.
1{
2 "projects": {
3 "cart": {
4 "root": "apps/cart",
5 "sourceRoot": "apps/cart/src",
6 "projectType": "application",
7 "generators": {},
8 "targets": {
9 "build": {
10 "executor": "@nrwl/web:build",
11 "options": {
12 "outputPath": "dist/apps/cart",
13 ...
14 },
15 "configurations": {
16 "production": {
17 "sourceMap": false,
18 ...
19 }
20 }
21 },
22 "test": {
23 "executor": "@nrwl/jest:jest",
24 "options": {
25 ...
26 }
27 }
28 }
29 }
30 }
31}
Note: There are a few property keys in
workspace.jsonthat have interchangeable aliases. You can replacegeneratorswithschematics,targetswitharchitectorexecutorwithbuilder.
Each project has its executors defined in the targets property. In this snippet, cart has two executors defined - build and test.
Note:
buildandtestcan be any strings you choose. For the sake of consistency, we maketestrun unit tests for every project andbuildproduce compiled code for the projects which can be built.
Each executor definition has an executor property and, optionally, an options and a configurations property.
executoris a string of the from[package name]:[executor name]. For thebuildexecutor, the package name is@nrwl/weband the executor name isbuild.optionsis an object that contains any configuration defaults for the executor. These options vary from executor to executor.configurationsallows you to create presets of options for different scenarios. All the configurations start with the properties defined inoptionsas a baseline and then overwrite those options. In the example, there is aproductionconfiguration that overrides the default options to setsourceMaptofalse.
Running Executors
The nx run cli command (or the shorthand versions) can be used to run executors.
nx run [project]:[command]
nx run cart:build
As long as your command name doesn't conflict with an existing nx cli command, you can use this short hand:
nx [command] [project]
nx build cart
You can also use a specific configuration preset like this:
nx [command] [project] --configuration=[configuration]
nx build cart --configuration=production
Or you can overwrite individual executor options like this:
nx [command] [project] --[optionNameInCamelCase]=[value]
nx build cart --outputPath=some/other/path