Skip to content

Commit

Permalink
feat(core): generalize setting target defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Jun 15, 2022
1 parent da1faaf commit f86a5ff
Show file tree
Hide file tree
Showing 29 changed files with 621 additions and 382 deletions.
64 changes: 31 additions & 33 deletions docs/shared/configuration/packagejson.md
Expand Up @@ -37,21 +37,11 @@ You can add Nx-specific configuration as follows:
"targets": {
"build": {
"outputs": ["dist/libs/mylib"],
"dependsOn": [
{
"target": "build",
"projects": "dependencies"
}
]
"dependsOn": ["^build"]
},
"test": {
"outputs": [],
"dependsOn": [
{
"target": "build",
"projects": "self"
}
]
"dependsOn": ["build"]
}
}
}
Expand All @@ -77,13 +67,13 @@ sure that `mylib`'s dependencies are built as well. This doesn't mean Nx is goin
artifacts are already in the right place, Nx will do nothing. If they aren't in the right place, but they are available
in the cache, Nx will retrieve them from the cache.
Depending on another target of the same project is very common. That's why we provide some syntax sugar, so
`"dependsOn": [{"target": "build", "projects": "self"}]` can be shortened to `"dependsOn": ["build"]`.
Another common scenario is for a target to depend on another target of the same project. For instance, `dependsOn` of
the `test` target tells Nx that before it can test `mylib` it needs to make sure that `mylib` is built, which will
result in `mylib`'s dependencies being built as well.
> You can also express the same configuration using `{ projects: "self", target: "build"}`
> and `{ projects: "dependencies", target: "build"}`.
This configuration is usually not needed. Nx comes with reasonable defaults (imported in `nx.json`) which implement the
configuration above.
Expand Down Expand Up @@ -185,13 +175,10 @@ The following is an expanded version showing all options. Your `nx.json` will li
"tsconfig.base.json": "*",
"nx.json": "*"
},
"targetDependencies": {
"build": [
{
"target": "build",
"projects": "dependencies"
}
]
"targetDefaults": {
"build": {
"dependsOn": ["^build"]
}
},
"cli": {
"defaultCollection": "@nrwl/js"
Expand Down Expand Up @@ -272,30 +259,41 @@ In the example above:
- Changing `globalFile` only affects `myapp`.
- Changing any CSS file inside the `styles` directory only affects `myapp`.
### Target Dependencies
### Target Defaults
Targets can depend on other targets. A common scenario is having to build dependencies of a project first before
building the project. The `dependsOn` property in `package.json` can be used to define the list of dependencies of an
individual target.
Often the same `dependsOn` configuration has to be defined for every project in the repo, and that's when
defining `targetDependencies` in `nx.json` is helpful.
defining `targetDefaults` in `nx.json` is helpful.
```json
{
"targetDependencies": {
"build": [
{
"target": "build",
"projects": "dependencies"
}
]
"targetDefaults": {
"build": {
"dependsOn": ["^build"]
}
}
}
```
The configuration above is identical to adding `{"dependsOn": ["^build"]}` to every build target of every project.
Another target default you can configure is `outputs`:
```json
{
"targetDefaults": {
"build": {
"outputs": ["./custom-dist"]
}
}
}
```
The configuration above is identical to adding `{"dependsOn": [{"target": "build", "projects": "dependencies"]}` to
every build target of every project.
> Previous versions of Nx supported `targetDependencies` to configure dependencies in `nx.json`. `targetDefaults` is the
> same mechanism but generalized to support other properties.
### CLI Options
Expand Down
77 changes: 31 additions & 46 deletions docs/shared/configuration/projectjson.md
Expand Up @@ -29,12 +29,7 @@ Let's look at the following `project.json`:
"test": {
"executor": "@nrwl/jest:jest",
"outputs": [],
"dependsOn": [
{
"target": "build",
"projects": "self"
}
],
"dependsOn": ["build"],
"options": {
"jestConfig": "libs/mylib/jest.config.js",
"tsConfig": "libs/mylib/tsconfig.spec.json"
Expand All @@ -43,12 +38,7 @@ Let's look at the following `project.json`:
"build": {
"executor": "@nrwl/js:tsc",
"outputs": ["dist/libs/mylib"],
"dependsOn": [
{
"target": "build",
"projects": "dependencies"
}
],
"dependsOn": ["^build"],
"options": {
"tsConfig": "libs/mylib/tsconfig.lib.json",
"main": "libs/mylib/src/main.ts"
Expand Down Expand Up @@ -79,12 +69,7 @@ Let's look at a sample test target:
"test": {
"executor": "@nrwl/jest:jest",
"outputs": [],
"dependsOn": [
{
"target": "build",
"projects": "self"
}
],
"dependsOn": ["build"],
"options": {
"jestConfig": "libs/mylib/jest.config.js",
"tsConfig": "libs/mylib/tsconfig.spec.json"
Expand Down Expand Up @@ -126,12 +111,7 @@ The `configurations` property provides extra sets of values that will be merged
"build": {
"executor": "@nrwl/js:tsc",
"outputs": ["dist/libs/mylib"],
"dependsOn": [
{
"target": "build",
"projects": "dependencies"
}
],
"dependsOn": ["^build"],
"options": {
"tsConfig": "libs/mylib/tsconfig.lib.json",
"main": "libs/mylib/src/main.ts"
Expand Down Expand Up @@ -177,13 +157,13 @@ sure that `mylib`'s dependencies are built as well. This doesn't mean Nx is goin
artifacts are already in the right place, Nx will do nothing. If they aren't in the right place, but they are available
in the cache, Nx will retrieve them from the cache.

Depending on another target of the same project is very common. That's why we provide some syntax sugar, so
`"dependsOn": [{"target": "build", "projects": "self"}]` can be shortened to `"dependsOn": ["build"]`.

Another common scenario is for a target to depend on another target of the same project. For instance, `dependsOn` of
the `test` target tells Nx that before it can test `mylib` it needs to make sure that `mylib` is built, which will
result in `mylib`'s dependencies being built as well.

> You can also express the same configuration using `{ projects: "self", target: "build"}`
> and `{ projects: "dependencies", target: "build"}`.
This configuration is usually not needed. Nx comes with reasonable defaults (imported in `nx.json`) which implement the
configuration above.

Expand Down Expand Up @@ -274,13 +254,10 @@ The following is an expanded version showing all options. Your `nx.json` will li
"tsconfig.base.json": "*",
"nx.json": "*"
},
"targetDependencies": {
"build": [
{
"target": "build",
"projects": "dependencies"
}
]
"targetDefaults": {
"build": {
"dependsOn": ["^build"]
}
},
"cli": {
"defaultCollection": "@nrwl/js"
Expand Down Expand Up @@ -361,30 +338,38 @@ In the example above:
- Changing `globalFile` only affects `myapp`.
- Changing any CSS file inside the `styles` directory only affects `myapp`.

### Target Dependencies
### Target Defaults

Targets can depend on other targets. A common scenario is having to build dependencies of a project first before
building the project. The `dependsOn` property in `package.json` can be used to define the list of dependencies of an
building the project. The `dependsOn` property in `project.json` can be used to define the list of dependencies of an
individual target.

Often the same `dependsOn` configuration has to be defined for every project in the repo, and that's when
defining `targetDependencies` in `nx.json` is helpful.
defining `targetDefaults` in `nx.json` is helpful.
```json
{
"targetDependencies": {
"build": [
{
"target": "build",
"projects": "dependencies"
}
]
"targetDefaults": {
"build": {
"dependsOn": ["^build"]
}
}
}
```
The configuration above is identical to adding `{"dependsOn": [{"target": "build", "projects": "dependencies"]}` to
every build target of every project.
The configuration above is identical to adding `{"dependsOn": ["^build"]}` to every build target of every project.
Another target default you can configure is `outputs`:
```json
{
"targetDefaults": {
"build": {
"outputs": ["./custom-dist"]
}
}
}
```
### CLI Options
Expand Down

1 comment on commit f86a5ff

@vercel
Copy link

@vercel vercel bot commented on f86a5ff Jun 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx.dev

Please sign in to comment.