Skip to content

Commit 6619164

Browse files
committed
feat: Added NX Transformer
1 parent cbeb29c commit 6619164

26 files changed

+5850
-738
lines changed

README.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ import { sum } from "@utils/sum";
5151

5252
### 3. Usage
5353

54-
- **To compile with `tsc`** — Use [ts-patch](https://github.com/nonara/ts-patch)
54+
- **Compile with `tsc`** — Use [ts-patch](https://github.com/nonara/ts-patch)
5555

5656

57-
- **To use with ts-node** — Add `typescript-transform-paths/register` to `require` config.
57+
- **Use with ts-node** — Add `typescript-transform-paths/register` to `require` config.
5858

5959
`tsconfig.json`
6060
```jsonc
@@ -67,7 +67,30 @@ import { sum } from "@utils/sum";
6767
}
6868
```
6969

70-
- **To use with node** — Use the register script: `node -r typescript-transform-paths/register src/index.ts`
70+
- **Use with node** — Use the register script: `node -r typescript-transform-paths/register src/index.ts`
71+
72+
- **Use with NX** — Add the `typescript-transform-paths/nx-transformer` to project config
73+
74+
`project.json`
75+
```jsonc
76+
{
77+
/* ... */
78+
"targets": {
79+
"build": {
80+
/* ... */
81+
"options": {
82+
/* ... */
83+
"transformers": [
84+
{
85+
name": "typescript-transform-paths/nx-transformer",
86+
"options": { "afterDeclarations": true }
87+
}
88+
]
89+
}
90+
}
91+
}
92+
}
93+
```
7194
7295
## Virtual Directories
7396
TS allows defining

nx-transformer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
'use strict'
2+
module.exports = require('./').nxTransformerPlugin;

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
"release": "standard-version",
1212
"--------------": "",
1313
"format": "prettier --write \"{src,test}/**/{*.js,!(*.d).ts}\"",
14-
"clean": "rimraf dist **/*.tsbuildinfo",
15-
"clean:all": "yarn run clean && rimraf node_modules test/node_modules test/.yarn-cache",
14+
"clean": "npx -y rimraf dist **/*.tsbuildinfo ./test/projects/nx/dist",
15+
"clean:all": "yarn run clean && npx -y rimraf node_modules **/node_modules **/yarn.lock yarn.lock",
1616
"reset": "yarn run clean:all && yarn install",
1717
"-------------- ": "",
1818
"prebuild": "rimraf dist",
19-
"install:tests": "cd test && yarn install && cd projects/extras && yarn install",
19+
"install:tests": "cd test && yarn install",
2020
"prepare": "yarn run install:tests"
2121
},
2222
"keywords": [
@@ -48,7 +48,8 @@
4848
"types",
4949
"README.md",
5050
"CHANGELOG.md",
51-
"register.js"
51+
"register.js",
52+
"nxTransformerPlugin.js"
5253
],
5354
"devDependencies": {
5455
"@types/jest": "^29.2.0",

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export default transformer;
33

44
export { TsTransformPathsConfig } from "./types";
55
export { register } from "./register";
6+
export { nxTransformerPlugin } from "./plugins";

src/plugins/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./nx-transformer-plugin";
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { NxTransformerPlugin } from "../../types";
2+
import transformer from "../transformer";
3+
import ts from "typescript";
4+
5+
/* ****************************************************************************************************************** *
6+
* Locals
7+
* ****************************************************************************************************************** */
8+
9+
const voidTransformer: ts.TransformerFactory<ts.SourceFile> = () => (s: ts.SourceFile) => s;
10+
11+
/* ****************************************************************************************************************** *
12+
* Transformer
13+
* ****************************************************************************************************************** */
14+
15+
export const nxTransformerPlugin: NxTransformerPlugin = {
16+
before: (pluginConfig, program) =>
17+
pluginConfig?.afterDeclarations ? voidTransformer : transformer(program, { ...pluginConfig }),
18+
afterDeclarations: (pluginConfig, program) =>
19+
!pluginConfig?.afterDeclarations ? voidTransformer : transformer(program, { ...pluginConfig }),
20+
};

test/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@
1010
"typescript-three": "npm:typescript@3.6.5",
1111
"typescript-four-seven": "npm:typescript@4.7.4",
1212
"typescript": "latest",
13-
"ts-patch": "link:../node_modules/ts-patch"
13+
"ts-patch": "link:../node_modules/ts-patch",
14+
"ts-node": "link:../node_modues/ts-node",
15+
"@nrwl/cli": "^15.0.0",
16+
"@nrwl/js": "^15.0.0",
17+
"@nrwl/node": "^15.0.0",
18+
"@nrwl/workspace": "^15.0.0",
19+
"nx": "^15.0.0"
1420
},
1521
"workspaces": [
1622
"projects/*"

test/projects/nx/nx.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"extends": "@nrwl/workspace/presets/core.json",
3+
"npmScope": "nx",
4+
"affected": {
5+
"defaultBase": "main"
6+
},
7+
"cli": {
8+
"defaultCollection": "@nrwl/workspace"
9+
},
10+
"tasksRunnerOptions": {
11+
"default": {
12+
"runner": "@nrwl/workspace/tasks-runners/default",
13+
"options": {
14+
"cacheableOperations": ["build", "lint", "test", "e2e"],
15+
"useDaemonProcess": false
16+
}
17+
}
18+
}
19+
}

test/projects/nx/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "nx",
3+
"version": "0.0.0",
4+
"scripts": {
5+
"build": "yarn nx build library1 --skip-nx-cache"
6+
},
7+
"private": true,
8+
"dependencies": {},
9+
"devDependencies": {
10+
"typescript-transform-paths": "link:../../../"
11+
}
12+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "library1",
3+
"version": "0.0.1"
4+
}

0 commit comments

Comments
 (0)