Skip to content

Commit

Permalink
feat(core): allow configuring targets to forward args to its target d…
Browse files Browse the repository at this point in the history
…ependencies
  • Loading branch information
leosvelperez authored and vsavkin committed Jul 12, 2022
1 parent 5194551 commit 3ce6f35
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 30 deletions.
28 changes: 23 additions & 5 deletions docs/shared/configuration/packagejson.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ Targets can depend on other targets. This is the relevant portion of the configu
```json
"build": {
"dependsOn": ["^build"]
"dependsOn": ["^build"]
},
"test": {
"dependsOn": ["build"]
"dependsOn": ["build"]
}
```
Expand All @@ -152,14 +152,32 @@ instance, `"dependsOn": ["build"]` 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
You can also express the same configuration using:
```json
"build": {
"dependsOn": [{projects: "dependencies", target: "build"}]
"dependsOn": [{ "projects": "dependencies", "target": "build" }]
},
"test": {
"dependsOn": [{projects: "self", target: "build"}]
"dependsOn": [{ "projects": "self", "target": "build" }]
}
```
With the expanded syntax, you also have a third option available to configure how to handle the params passed to the target
dependencies. You can either forward them to the dependency target, or you can ignore them (default).
```json
"build": {
// forward params passed to this target to the dependency targets
"dependsOn": [{ "projects": "dependencies", "target": "build", "params": "forward" }]
},
"test": {
// ignore params passed to this target, won't be forwarded to the dependency targets
"dependsOn": [{ "projects": "self", "target": "build", "params": "ignore" }]
}
"lint": {
// ignore params passed to this target, won't be forwarded to the dependency targets
"dependsOn": [{ "projects": "self", "target": "build" }]
}
```
Expand Down
24 changes: 21 additions & 3 deletions docs/shared/configuration/projectjson.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,32 @@ Another common scenario is for a target to depend on another target of the same
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
You can also express the same configuration using:

```json
"build": {
"dependsOn": [{ projects: "dependencies", target: "build"}]
"dependsOn": [{ "projects": "dependencies", "target": "build" }]
},
"test": {
"dependsOn": [{ projects: "self", target: "build"}]
"dependsOn": [{ "projects": "self", "target": "build" }]
}
```

With the expanded syntax, you also have a third option available to configure how to handle the params passed to the target
dependencies. You can either forward them to the dependency target, or you can ignore them (default).

```json
"build": {
// forward params passed to this target to the dependency targets
"dependsOn": [{ "projects": "dependencies", "target": "build", "params": "forward" }]
},
"test": {
// ignore params passed to this target, won't be forwarded to the dependency targets
"dependsOn": [{ "projects": "self", "target": "build", "params": "ignore" }]
}
"lint": {
// ignore params passed to this target, won't be forwarded to the dependency targets
"dependsOn": [{ "projects": "self", "target": "build" }]
}
```

Expand Down
18 changes: 16 additions & 2 deletions packages/nx/schemas/nx-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,16 @@
"target": {
"type": "string",
"description": "The name of the target."
},
"params": {
"type": "string",
"description": "Configuration for params handling.",
"enum": ["ignore", "forward"],
"default": "ignore"
}
},
"additionalProperties": false
"additionalProperties": false,
"required": ["projects", "target"]
}
]
}
Expand Down Expand Up @@ -223,9 +230,16 @@
"target": {
"type": "string",
"description": "The name of the target."
},
"params": {
"type": "string",
"description": "Configuration for params handling.",
"enum": ["ignore", "forward"],
"default": "ignore"
}
},
"additionalProperties": false
"additionalProperties": false,
"required": ["projects", "target"]
}
]
}
Expand Down
9 changes: 8 additions & 1 deletion packages/nx/schemas/project-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,16 @@
"target": {
"type": "string",
"description": "The name of the target."
},
"params": {
"type": "string",
"description": "Configuration for params handling.",
"enum": ["ignore", "forward"],
"default": "ignore"
}
},
"additionalProperties": false
"additionalProperties": false,
"required": ["projects", "target"]
}
]
}
Expand Down
36 changes: 25 additions & 11 deletions packages/nx/schemas/workspace-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,33 @@
"type": "array",
"description": "Target dependency.",
"items": {
"type": "object",
"properties": {
"projects": {
"type": "string",
"description": "The projects that the targets belong to.",
"enum": ["self", "dependencies"]
"oneOf": [
{
"type": "string"
},
"target": {
"type": "string",
"description": "The name of the target."
{
"type": "object",
"properties": {
"projects": {
"type": "string",
"description": "The projects that the targets belong to.",
"enum": ["self", "dependencies"]
},
"target": {
"type": "string",
"description": "The name of the target."
},
"params": {
"type": "string",
"description": "Configuration for params handling.",
"enum": ["ignore", "forward"],
"default": "ignore"
}
},
"additionalProperties": false,
"required": ["projects", "target"]
}
},
"additionalProperties": false
]
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions packages/nx/src/config/workspace-json-project-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ export interface TargetDependencyConfig {
* The name of the target
*/
target: string;

/**
* Configuration for params handling.
*/
params?: 'ignore' | 'forward';
}

export type InputDefinition =
Expand Down
128 changes: 128 additions & 0 deletions packages/nx/src/tasks-runner/create-task-graph.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createTaskGraph } from 'nx/src/tasks-runner/create-task-graph';

describe('createTaskGraph', () => {
let projectGraph: ProjectGraph;

beforeEach(() => {
projectGraph = {
nodes: {
Expand Down Expand Up @@ -167,6 +168,133 @@ describe('createTaskGraph', () => {
});
});

it('should forward args when configured', () => {
projectGraph = {
nodes: {
app1: {
name: 'app1',
type: 'app',
data: {
root: 'app1-root',
files: [],
targets: {
'prebuild-base': {},
prebuild: {},
build: {
dependsOn: [
{
projects: 'dependencies',
target: 'build',
params: 'forward',
},
{ projects: 'self', target: 'prebuild', params: 'forward' },
],
},
test: {},
serve: {},
},
},
},
lib1: {
name: 'lib1',
type: 'lib',
data: {
root: 'lib1-root',
files: [],
targets: {
build: {
dependsOn: [
{
projects: 'dependencies',
target: 'build',
params: 'ignore',
},
],
},
test: {},
},
},
},
lib2: {
name: 'lib2',
type: 'lib',
data: {
root: 'lib2-root',
files: [],
targets: { build: {}, test: {} },
},
},
},
dependencies: {
app1: [
{ source: 'app1', target: 'lib1', type: 'static' },
{ source: 'app1', target: 'lib2', type: 'static' },
],
lib1: [{ source: 'lib1', target: 'lib2', type: 'static' }],
lib2: [],
},
};

const taskResult = createTaskGraph(
projectGraph,
{},
['app1'],
['build'],
'development',
{
myFlag: 'flag value',
}
);

expect(taskResult).toEqual({
roots: ['lib2:build', 'app1:prebuild'],
tasks: {
'app1:build': {
id: 'app1:build',
target: {
project: 'app1',
target: 'build',
},
overrides: { myFlag: 'flag value' },
projectRoot: 'app1-root',
},
'app1:prebuild': {
id: 'app1:prebuild',
target: {
project: 'app1',
target: 'prebuild',
},
overrides: { myFlag: 'flag value' },
projectRoot: 'app1-root',
},
'lib1:build': {
id: 'lib1:build',
target: {
project: 'lib1',
target: 'build',
},
overrides: { myFlag: 'flag value' },
projectRoot: 'lib1-root',
},
'lib2:build': {
id: 'lib2:build',
target: {
project: 'lib2',
target: 'build',
},
overrides: { __overrides_unparsed__: [] },
projectRoot: 'lib2-root',
},
},
dependencies: {
'app1:build': ['lib1:build', 'lib2:build', 'app1:prebuild'],
'app1:prebuild': [],
'lib1:build': ['lib2:build'],
'lib2:build': [],
},
});
});

it('should create graphs with dependencies', () => {
const taskGraph = createTaskGraph(
projectGraph,
Expand Down
Loading

1 comment on commit 3ce6f35

@vercel
Copy link

@vercel vercel bot commented on 3ce6f35 Jul 12, 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-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.