Skip to content

Commit

Permalink
chore(misc): address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Dec 29, 2022
1 parent 4537439 commit 7d844c4
Show file tree
Hide file tree
Showing 3 changed files with 276 additions and 82 deletions.
20 changes: 19 additions & 1 deletion docs/shared/reference/nx-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ In this case Nx will use the right `production` input for each project.

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:

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

Expand Down Expand Up @@ -157,6 +156,25 @@ 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"
}
}
}
}
}
```

### Generators

Default generator options are configured in `nx.json` as well. For instance, the following tells Nx to always
Expand Down
248 changes: 201 additions & 47 deletions packages/nx/src/config/workspaces.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { NxJsonConfiguration } from './nx-json';
import { vol } from 'memfs';

import * as fastGlob from 'fast-glob';
import { TargetConfiguration } from './workspace-json-project-json';

jest.mock('fs', () => require('memfs').fs);

Expand Down Expand Up @@ -146,15 +147,15 @@ describe('Workspaces', () => {

describe('target defaults', () => {
const targetDefaults = {
'build#nx:run-commands': {
options: {
key: 't:e',
},
},
'nx:run-commands': {
options: {
key: '*:e',
},
configurations: {
['test-config']: {
val: 'b',
},
},
},
build: {
options: {
Expand All @@ -163,13 +164,6 @@ describe('Workspaces', () => {
},
};

it('should prefer target#executor key', () => {
expect(
readTargetDefaultsForTarget('build', targetDefaults, 'nx:run-commands')
.options['key']
).toEqual('t:e');
});

it('should prefer executor key', () => {
expect(
readTargetDefaultsForTarget(
Expand Down Expand Up @@ -197,17 +191,16 @@ describe('Workspaces', () => {
).toBeNull();
});

it.each(['configurations', 'options'])(
'should merge %s if executor matches',
(property) => {
describe('options', () => {
it('should merge if executor matches', () => {
expect(
mergeTargetConfigurations(
{
root: '.',
targets: {
build: {
executor: 'target',
[property]: {
options: {
a: {},
},
},
Expand All @@ -216,53 +209,48 @@ describe('Workspaces', () => {
'build',
{
executor: 'target',
[property]: {
options: {
a: 'overriden',
b: {},
},
}
)[property]
).options
).toEqual({ a: {}, b: {} });
}
);
});

it.each(['configurations', 'options'])(
'should merge %s if executor is only provided by target',
(property) => {
it('should merge if executor is only provided on the project', () => {
expect(
mergeTargetConfigurations(
{
root: '',
root: '.',
targets: {
build: {
executor: 'target',
[property]: {
options: {
a: {},
},
},
},
},
'build',
{
[property]: {
options: {
a: 'overriden',
b: {},
},
}
)[property]
).options
).toEqual({ a: {}, b: {} });
}
);
});

it.each(['configurations', 'options'])(
'should merge %s if executor is only provided by defaults',
(property) => {
it('should merge if executor is only provided in the defaults', () => {
expect(
mergeTargetConfigurations(
{
root: '',
root: '.',
targets: {
build: {
[property]: {
options: {
a: {},
},
},
Expand All @@ -271,25 +259,24 @@ describe('Workspaces', () => {
'build',
{
executor: 'target',
[property]: {
options: {
a: 'overriden',
b: {},
},
}
)[property]
).options
).toEqual({ a: {}, b: {} });
}
);
});

it.each(['configurations', 'options'])(
'should not merge %s if executor is only provided by defaults',
(property) => {
it('should not merge if executor is only provided by defaults', () => {
expect(
mergeTargetConfigurations(
{
root: '',
targets: {
build: {
[property]: {
executor: 'other',
options: {
a: {},
},
},
Expand All @@ -298,13 +285,180 @@ describe('Workspaces', () => {
'build',
{
executor: 'target',
[property]: {
options: {
b: {},
},
}
)[property]
).toEqual({ a: {}, b: {} });
}
);
).options
).toEqual({ a: {} });
});

it('should resolve workspaceRoot and projectRoot tokens', () => {
expect(
mergeTargetConfigurations(
{
root: 'my/project',
targets: {
build: {
options: {
a: '{workspaceRoot}',
},
},
},
},
'build',
{
executor: 'target',
options: {
b: '{workspaceRoot}/dist/{projectRoot}',
},
}
).options
).toEqual({ a: '{workspaceRoot}', b: 'dist/my/project' });
});
});

describe('configurations', () => {
const projectConfigurations: TargetConfiguration['configurations'] = {
dev: {
foo: '1',
},
prod: {
bar: '2',
},
};

const defaultConfigurations: TargetConfiguration['configurations'] = {
dev: {
foo: 'z',
other: '2',
},
baz: {
x: '3',
},
};

const merged: TargetConfiguration['configurations'] = {
dev: {
foo: '1',
other: '2',
},
prod: { bar: '2' },
baz: { x: '3' },
};

it('should merge configurations if executor matches', () => {
expect(
mergeTargetConfigurations(
{
root: '.',
targets: {
build: {
executor: 'target',
configurations: projectConfigurations,
},
},
},
'build',
{
executor: 'target',
configurations: defaultConfigurations,
}
).configurations
).toEqual(merged);
});

it('should merge if executor is only provided on the project', () => {
expect(
mergeTargetConfigurations(
{
root: '.',
targets: {
build: {
executor: 'target',
configurations: projectConfigurations,
},
},
},
'build',
{
configurations: defaultConfigurations,
}
).configurations
).toEqual(merged);
});

it('should merge if executor is only provided in the defaults', () => {
expect(
mergeTargetConfigurations(
{
root: '.',
targets: {
build: {
configurations: projectConfigurations,
},
},
},
'build',
{
executor: 'target',
configurations: defaultConfigurations,
}
).configurations
).toEqual(merged);
});

it('should not merge if executor doesnt match', () => {
expect(
mergeTargetConfigurations(
{
root: '',
targets: {
build: {
executor: 'other',
configurations: projectConfigurations,
},
},
},
'build',
{
executor: 'target',
configurations: defaultConfigurations,
}
).configurations
).toEqual(projectConfigurations);
});

it('should resolve workspaceRoot and projectRoot tokens', () => {
expect(
mergeTargetConfigurations(
{
root: 'my/project',
targets: {
build: {
configurations: {
dev: {
a: '{workspaceRoot}',
},
},
},
},
},
'build',
{
executor: 'target',
configurations: {
prod: {
a: '{workspaceRoot}/dist/{projectRoot}',
},
},
}
).configurations
).toEqual({
dev: { a: '{workspaceRoot}' },
prod: { a: 'dist/my/project' },
});
});
});
});
});
Loading

0 comments on commit 7d844c4

Please sign in to comment.