Skip to content

Commit

Permalink
feat(core): merge more target options from targetDefaults (nrwl#12435)
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder authored Dec 29, 2022
1 parent 66a2658 commit c783ac5
Show file tree
Hide file tree
Showing 7 changed files with 603 additions and 51 deletions.
38 changes: 37 additions & 1 deletion docs/shared/reference/nx-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ In this case Nx will use the right `production` input for each project.

### Target Defaults

Target defaults provide ways to set common options for a particular target in your workspace. When building your project's configuration, we merge it with up to 1 default from this map. For a given target, we look at its name and its executor. We then check target defaults for any of the following combinations:

- `` `${executor}` ``
- `` `${targetName}` ``

Whichever of these we find first, we use as the base for that target's configuration. Some common scenarios for this follow.

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 `project.json` can be used to define the list of dependencies of an
individual target.
Expand Down Expand Up @@ -149,6 +156,35 @@ Another target default you can configure is `outputs`:
}
```

When defining any options or configurations inside of a target default, you may use the `{workspaceRoot}` and `{projectRoot}` tokens. This is useful for defining things like the outputPath or tsconfig for many build targets.

```json {% fileName="nx.json" %}
{
"targetDefaults": {
"@nrwl/js:tsc": {
"options": {
"main": "{projectRoot}/src/index.ts"
},
"configurations": {
"prod": {
"tsconfig": "{projectRoot}/tsconfig.prod.json"
}
},
"inputs": ["prod"],
"outputs": ["{workspaceRoot}/{projectRoot}"]
},
"build": {
"inputs": ["prod"],
"outputs": ["{workspaceRoot}/{projectRoot}"]
}
}
}
```

{% callout type="note" title="Target Default Priority" %}
Note that the inputs and outputs are respecified on the @nrwl/js:tsc default configuration. This is **required**, as when reading target defaults Nx will only ever look at one key. If there is a default configuration based on the executor used, it will be read first. If not, Nx will fall back to looking at the configuration based on target name. For instance, running `nx build project` will read the options from `targetDefaults[@nrwl/js:tsc]` if the target configuration for build uses the @nrwl/js:tsc executor. It **would not** read any of the configuration from the `build` target default configuration unless the executor does not match.
{% /callout %}

### Generators

Default generator options are configured in `nx.json` as well. For instance, the following tells Nx to always
Expand All @@ -174,7 +210,7 @@ named "default" is used by default. Specify a different one like this `nx run-ma
Tasks runners can accept different options. The following are the options supported
by `"nx/tasks-runners/default"` and `"@nrwl/nx-cloud"`.

| Property | Descrtipion |
| Property | Description |
| ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| cacheableOperations | defines the list of targets/operations that are cached by Nx |
| parallel | defines the max number of targets ran in parallel (in older versions of Nx you had to pass `--parallel --maxParallel=3` instead of `--parallel=3`) |
Expand Down
63 changes: 63 additions & 0 deletions e2e/nx-run/src/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,69 @@ describe('Nx Running Tests', () => {
);
}, 10000);

describe('target defaults + executor specifications', () => {
it('should be able to run targets with unspecified executor given an appropriate targetDefaults entry', () => {
const target = uniq('target');
const lib = uniq('lib');

updateJson('nx.json', (nxJson) => {
nxJson.targetDefaults ??= {};
nxJson.targetDefaults[target] = {
executor: 'nx:run-commands',
options: {
command: `echo Hello from ${target}`,
},
};
return nxJson;
});

updateFile(
`libs/${lib}/project.json`,
JSON.stringify({
name: lib,
targets: {
[target]: {},
},
})
);

expect(runCLI(`${target} ${lib} --verbose`)).toContain(
`Hello from ${target}`
);
});

it('should be able to pull options from targetDefaults based on executor', () => {
const target = uniq('target');
const lib = uniq('lib');

updateJson('nx.json', (nxJson) => {
nxJson.targetDefaults ??= {};
nxJson.targetDefaults[`nx:run-commands`] = {
options: {
command: `echo Hello from ${target}`,
},
};
return nxJson;
});

updateFile(
`libs/${lib}/project.json`,
JSON.stringify({
name: lib,
targets: {
[target]: {
executor: 'nx:run-commands',
},
},
})
);

expect(runCLI(`${target} ${lib} --verbose`)).toContain(
`Hello from ${target}`
);
});
});

describe('target dependencies', () => {
let myapp;
let mylib1;
Expand Down
26 changes: 20 additions & 6 deletions packages/nx/schemas/nx-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,26 @@
"type": "object",
"description": "Target defaults",
"properties": {
"executor": {
"description": "The function that Nx will invoke when you run this target",
"type": "string"
},
"options": {
"type": "object"
},
"outputs": {
"type": "array",
"items": {
"type": "string"
}
},
"configurations": {
"type": "object",
"description": "provides extra sets of values that will be merged into the options map",
"additionalProperties": {
"type": "object"
}
},
"inputs": {
"$ref": "#/definitions/inputs"
},
Expand Down Expand Up @@ -243,12 +263,6 @@
}
]
}
},
"outputs": {
"type": "array",
"items": {
"type": "string"
}
}
},
"additionalProperties": false
Expand Down
10 changes: 2 additions & 8 deletions packages/nx/src/config/nx-json.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PackageManager } from '../utils/package-manager';
import {
InputDefinition,
TargetConfiguration,
TargetDependencyConfig,
} from './workspace-json-project-json';

Expand All @@ -19,14 +20,7 @@ export interface NxAffectedConfig {
defaultBase?: string;
}

export type TargetDefaults = Record<
string,
{
outputs?: string[];
dependsOn?: (TargetDependencyConfig | string)[];
inputs?: (InputDefinition | string)[];
}
>;
export type TargetDefaults = Record<string, Partial<TargetConfiguration>>;

export type TargetDependencies = Record<
string,
Expand Down
Loading

0 comments on commit c783ac5

Please sign in to comment.