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";

src/plugins/nx-transformer-plugin.ts

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:[email protected]",
1111
"typescript-four-seven": "npm:[email protected]",
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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"root": "packages/library1",
3+
"sourceRoot": "packages/library1/src",
4+
"projectType": "library",
5+
"targets": {
6+
"build": {
7+
"executor": "@nrwl/node:webpack",
8+
"outputs": [
9+
"{options.outputPath}"
10+
],
11+
"options": {
12+
"outputPath": "dist/library1",
13+
"main": "packages/library1/src/index.ts",
14+
"outputFileName": "index.js",
15+
"tsConfig": "packages/library1/tsconfig.lib.json",
16+
"sourceMap": false,
17+
"transformers": [
18+
{
19+
"name": "typescript-transform-paths/nx-transformer",
20+
"options": {
21+
"afterDeclarations": true
22+
}
23+
}
24+
]
25+
},
26+
"dependsOn": [
27+
]
28+
}
29+
},
30+
"tags": [ ]
31+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { name as library2Name } from "library2";
2+
export default library2Name;
3+
4+
console.log(library2Name);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"files": [],
4+
"include": [],
5+
"references": [
6+
{
7+
"path": "./tsconfig.lib.json"
8+
}
9+
]
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"module": "commonjs",
5+
"outDir": "../../dist/library1",
6+
"declaration": true,
7+
"types": ["node"]
8+
},
9+
"include": ["src/*.ts"]
10+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "library2",
3+
"version": "0.0.1"
4+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"root": "packages/library2",
3+
"sourceRoot": "packages/library2/src",
4+
"projectType": "library",
5+
"targets": {
6+
},
7+
"tags": []
8+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const name = "library2";
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"files": [],
4+
"include": [],
5+
"references": [
6+
{
7+
"path": "./tsconfig.lib.json"
8+
}
9+
]
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"module": "commonjs",
5+
"outDir": "../../dist/out-tsc",
6+
"declaration": true,
7+
"types": ["node"]
8+
},
9+
"exclude": ["**/*.spec.ts", "**/*.test.ts"],
10+
"include": ["**/*.ts"]
11+
}

test/projects/nx/tsconfig.base.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"compileOnSave": false,
3+
"compilerOptions": {
4+
"rootDir": ".",
5+
"sourceMap": true,
6+
"declaration": false,
7+
"moduleResolution": "node",
8+
"emitDecoratorMetadata": true,
9+
"experimentalDecorators": true,
10+
"importHelpers": true,
11+
"target": "es2015",
12+
"module": "esnext",
13+
"lib": ["es2017", "dom"],
14+
"skipLibCheck": true,
15+
"skipDefaultLibCheck": true,
16+
"baseUrl": ".",
17+
"paths": {
18+
"library1": ["packages/library1/src/index.ts"],
19+
"library2": ["packages/library2/src/index.ts"]
20+
}
21+
},
22+
"exclude": ["node_modules", "tmp"]
23+
}

test/projects/nx/workspace.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"version": 2,
3+
"projects": {
4+
"library1": "packages/library1",
5+
"library2": "packages/library2"
6+
}
7+
}

test/projects/specific/yarn.lock

Lines changed: 0 additions & 8 deletions
This file was deleted.

test/tests/nx.test.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import ts from "typescript";
2+
import { nxTransformerPlugin } from "../../src";
3+
import path from "path";
4+
import { projectsPaths } from "../config";
5+
import { execSync } from "child_process";
6+
import { readFileSync, rmSync } from "fs";
7+
import * as transformerModule from "../../src/transformer";
8+
9+
/* ****************************************************************************************************************** *
10+
* Tests
11+
* ****************************************************************************************************************** */
12+
13+
describe(`NX Transformer`, () => {
14+
describe("Plugin", () => {
15+
let mockedTransformer: jest.SpyInstance;
16+
17+
const program: any = { x: 1 };
18+
19+
beforeAll(async () => {
20+
mockedTransformer = jest.spyOn(transformerModule, "default").mockReturnValue(<any>(() => {}));
21+
});
22+
afterAll(() => {
23+
mockedTransformer.mockClear();
24+
});
25+
beforeEach(() => {
26+
mockedTransformer.mockReset();
27+
});
28+
29+
test(`Before properly routes transform`, () => {
30+
const config: any = { a: 2 };
31+
32+
nxTransformerPlugin.before(config, program);
33+
34+
expect(mockedTransformer).toBeCalledTimes(1);
35+
expect(mockedTransformer.mock.lastCall).toHaveLength(2);
36+
37+
const [recProgram, recConfig] = mockedTransformer.mock.lastCall;
38+
expect(recProgram).toBe(program);
39+
expect(recConfig).toStrictEqual(config);
40+
});
41+
42+
test(`After properly routes transform`, () => {
43+
const config: any = { a: 2, afterDeclarations: true };
44+
45+
nxTransformerPlugin.afterDeclarations(config, program);
46+
47+
expect(mockedTransformer).toBeCalledTimes(1);
48+
expect(mockedTransformer.mock.lastCall).toHaveLength(2);
49+
50+
const [recProgram, recConfig] = mockedTransformer.mock.lastCall;
51+
expect(recProgram).toBe(program);
52+
expect(recConfig).toStrictEqual({ ...config, afterDeclarations: true });
53+
});
54+
});
55+
56+
describe(`(e2e) Works in NX project`, () => {
57+
const projectRoot = ts.normalizePath(path.join(projectsPaths, "nx"));
58+
59+
// TODO - Investigate ways to do without emit
60+
test(`Transformer works for emitted declaration`, () => {
61+
execSync("yarn run build", { cwd: projectRoot });
62+
63+
try {
64+
const file = readFileSync(`${projectRoot}/dist/library1/packages/library1/src/index.d.ts`, "utf8");
65+
expect(file).toMatch(/import { name as library2Name } from "..\/..\/library2\/src";/);
66+
} finally {
67+
rmSync(`${projectRoot}/dist`, { recursive: true, force: true });
68+
}
69+
});
70+
});
71+
});

0 commit comments

Comments
 (0)