Skip to content

Commit d4b6e68

Browse files
author
John Doe
committed
refactor: add task derivation for build
1 parent 11deecf commit d4b6e68

8 files changed

Lines changed: 186 additions & 0 deletions

File tree

tools/build-target/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# build-target
2+
3+
An Nx plugin that automatically creates `build` targets for projects with a `tsconfig.lib.json` file.
4+
5+
## Overview
6+
7+
This plugin detects `tsconfig.lib.json` files in your workspace and automatically generates a `build` target for those
8+
projects. The build target uses the `@nx/js:tsc` executor to compile TypeScript libraries.
9+
10+
## Quick Start
11+
12+
### Using the Nx Plugin
13+
14+
The plugin is already registered in your `nx.json`:
15+
16+
```jsonc
17+
{
18+
"plugins": ["./tools/build-target/src/build-target.plugin.ts"],
19+
}
20+
```
21+
22+
Added target
23+
24+
```json
25+
{
26+
"projects": {
27+
"your-project-name": {
28+
"targets": {
29+
"build": {
30+
"dependsOn": ["^build"],
31+
"inputs": ["production", "^production"],
32+
"cache": true,
33+
"outputs": ["{options.outputPath}"],
34+
"executor": "@nx/js:tsc",
35+
"options": {
36+
"outputPath": "{projectRoot}/dist",
37+
"main": "{projectRoot}/src/index.ts",
38+
"tsConfig": "{ projectRoot}/tsconfig.lib.json",
39+
"assets": ["{projectRoot}/*.md"]
40+
}
41+
}
42+
}
43+
}
44+
}
45+
}
46+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const baseConfig = require('../../eslint.config.js').default;
2+
3+
module.exports = [
4+
...baseConfig,
5+
{
6+
files: ['**/*.json'],
7+
rules: {
8+
'@nx/dependency-checks': 'error',
9+
},
10+
},
11+
];

tools/build-target/project.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "build-target",
3+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
4+
"sourceRoot": "tools/build-target/src",
5+
"projectType": "library",
6+
"targets": {
7+
"lint": {},
8+
"unit-test": {}
9+
}
10+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type { CreateNodesV2, NxPlugin, TargetConfiguration } from '@nx/devkit';
2+
import * as path from 'node:path';
3+
4+
const TSCONFIG_LIB_FILE = 'tsconfig.lib.json';
5+
const BUILD_TARGET_NAME = 'build';
6+
7+
export function createBuildTargetConfig(): TargetConfiguration {
8+
return {
9+
dependsOn: ['^build'],
10+
inputs: ['production', '^production'],
11+
cache: true,
12+
executor: '@nx/js:tsc',
13+
outputs: ['{options.outputPath}'],
14+
options: {
15+
outputPath: '{projectRoot}/dist',
16+
main: '{projectRoot}/src/index.ts',
17+
tsConfig: `{projectRoot}/${TSCONFIG_LIB_FILE}`,
18+
assets: ['{projectRoot}/*.md'],
19+
},
20+
};
21+
}
22+
23+
const createNodesV2: CreateNodesV2 = [
24+
`**/${TSCONFIG_LIB_FILE}`,
25+
async configFilePaths => {
26+
return Promise.all(
27+
configFilePaths.map(async configFilePath => {
28+
const projectRoot = path.dirname(configFilePath);
29+
const normalizedProjectRoot = projectRoot === '.' ? '' : projectRoot;
30+
31+
return [
32+
configFilePath,
33+
{
34+
projects: {
35+
[normalizedProjectRoot]: {
36+
targets: {
37+
[BUILD_TARGET_NAME]: createBuildTargetConfig(),
38+
},
39+
},
40+
},
41+
},
42+
] as const;
43+
}),
44+
);
45+
},
46+
];
47+
48+
const buildTargetPlugin: NxPlugin = {
49+
name: 'build-target-nx-plugin',
50+
createNodesV2,
51+
};
52+
53+
export default buildTargetPlugin;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { createBuildTargetConfig } from './build-target.plugin';
3+
4+
describe('createBuildTargetConfig', () => {
5+
it('should return the correct target config', () => {
6+
expect(createBuildTargetConfig()).toStrictEqual({
7+
dependsOn: ['^build'],
8+
inputs: ['production', '^production'],
9+
cache: true,
10+
executor: '@nx/js:tsc',
11+
outputs: ['{options.outputPath}'],
12+
options: {
13+
outputPath: '{projectRoot}/dist',
14+
main: '{projectRoot}/src/index.ts',
15+
tsConfig: `{projectRoot}/tsconfig.lib.json`,
16+
assets: ['{projectRoot}/*.md'],
17+
},
18+
});
19+
});
20+
});

tools/build-target/tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"module": "commonjs",
5+
"verbatimModuleSyntax": false
6+
},
7+
"files": [],
8+
"include": [],
9+
"references": [
10+
{
11+
"path": "./tsconfig.test.json"
12+
}
13+
]
14+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "../../dist/out-tsc",
5+
"types": [
6+
"vitest/globals",
7+
"vitest/importMeta",
8+
"vite/client",
9+
"node",
10+
"vitest"
11+
]
12+
},
13+
"include": [
14+
"vite.config.ts",
15+
"vite.config.mts",
16+
"vitest.config.ts",
17+
"vitest.config.mts",
18+
"vitest.unit.config.ts",
19+
"src/**/*.test.ts",
20+
"src/**/*.spec.ts",
21+
"src/**/*.test.tsx",
22+
"src/**/*.spec.tsx",
23+
"src/**/*.test.js",
24+
"src/**/*.spec.js",
25+
"src/**/*.test.jsx",
26+
"src/**/*.spec.jsx",
27+
"src/**/*.d.ts"
28+
]
29+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { createUnitTestConfig } from '../../testing/test-setup-config/src/index.js';
2+
3+
export default createUnitTestConfig('build-target');

0 commit comments

Comments
 (0)