Skip to content

Commit 379d9be

Browse files
authored
Merge branch 'master' into add-prettier-check
2 parents 45466e7 + 0d9cd56 commit 379d9be

22 files changed

+86
-85
lines changed

.github/workflows/publish.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ jobs:
2828
- name: Setup Node.js 18.x to publish to npmjs.org
2929
uses: actions/setup-node@v4
3030
with:
31-
node-version: '18.x'
32-
registry-url: 'https://registry.npmjs.org'
31+
node-version: "18.x"
32+
registry-url: "https://registry.npmjs.org"
3333

3434
- name: Install Packages
3535
run: yarn install --frozen-lockfile

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
src/declarations/*
2+
CHANGELOG.md

README.md

+26-18
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ Add it to _plugins_ in your _tsconfig.json_
4141
}
4242
}
4343
```
44+
4445
#### Example result
4546

4647
`core/index.ts`
48+
4749
```tsx
4850
// The following transforms path to '../utils/sum'
4951
import { sum } from "@utils/sum";
@@ -53,10 +55,10 @@ import { sum } from "@utils/sum";
5355

5456
- **Compile with `tsc`** — Use [ts-patch](https://github.com/nonara/ts-patch)
5557

58+
- **Use with ts-node** — Add `typescript-transform-paths/register` to `require` config.
59+
60+
`tsconfig.json`
5661

57-
- **Use with ts-node** — Add `typescript-transform-paths/register` to `require` config.
58-
59-
`tsconfig.json`
6062
```jsonc
6163
{
6264
"ts-node": {
@@ -72,6 +74,7 @@ import { sum } from "@utils/sum";
7274
- **Use with NX** — Add the `typescript-transform-paths/nx-transformer` to project config
7375

7476
`project.json`
77+
7578
```jsonc
7679
{
7780
/* ... */
@@ -81,7 +84,7 @@ import { sum } from "@utils/sum";
8184
"options": {
8285
/* ... */
8386
"transformers": [
84-
{
87+
{
8588
"name": "typescript-transform-paths/nx-transformer",
8689
"options": { "afterDeclarations": true }
8790
}
@@ -93,6 +96,7 @@ import { sum } from "@utils/sum";
9396
```
9497

9598
## Virtual Directories
99+
96100
TS allows defining
97101
[virtual directories](https://www.typescriptlang.org/docs/handbook/module-resolution.html#virtual-directories-with-rootdirs)
98102
via the `rootDirs` compiler option.
@@ -101,10 +105,10 @@ To enable virtual directory mapping, use the `useRootDirs` plugin option.
101105
```jsonc
102106
{
103107
"compilerOptions": {
104-
"rootDirs": [ "src", "generated" ],
108+
"rootDirs": ["src", "generated"],
105109
"baseUrl": ".",
106110
"paths": {
107-
"#root/*": [ "./src/*", "./generated/*" ]
111+
"#root/*": ["./src/*", "./generated/*"]
108112
},
109113
"plugins": [
110114
{ "transform": "typescript-transform-paths", "useRootDirs": true },
@@ -126,36 +130,40 @@ To enable virtual directory mapping, use the `useRootDirs` plugin option.
126130
```
127131

128132
`src/file1.ts`
133+
129134
```ts
130-
import '#root/file2.ts' // resolves to './file2'
135+
import "#root/file2.ts"; // resolves to './file2'
131136
```
137+
132138
`src/subdir/sub-file.ts`
139+
133140
```ts
134-
import '#root/file2.ts' // resolves to '../file2'
135-
import '#root/file1.ts' // resolves to '../file1'
141+
import "#root/file2.ts"; // resolves to '../file2'
142+
import "#root/file1.ts"; // resolves to '../file1'
136143
```
137144

138145
## Custom Control
139146

140147
### Exclusion patterns
141148

142149
You can disable transformation for paths based on the resolved file path. The `exclude` option allows specifying glob
143-
patterns to match against resolved file path.
150+
patterns to match against resolved file path.
144151

145152
For an example context in which this would be useful, see [Issue #83](https://github.com/LeDDGroup/typescript-transform-paths/issues/83)
146153

147154
Example:
155+
148156
```jsonc
149157
{
150158
"compilerOptions": {
151159
"paths": {
152-
"sub-module1/*": [ "../../node_modules/sub-module1/*" ],
153-
"sub-module2/*": [ "../../node_modules/sub-module2/*" ],
160+
"sub-module1/*": ["../../node_modules/sub-module1/*"],
161+
"sub-module2/*": ["../../node_modules/sub-module2/*"]
154162
},
155163
"plugins": [
156-
{
157-
"transform": "typescript-transform-paths",
158-
"exclude": [ "**/node_modules/**" ]
164+
{
165+
"transform": "typescript-transform-paths",
166+
"exclude": ["**/node_modules/**"]
159167
}
160168
]
161169
}
@@ -164,7 +172,7 @@ Example:
164172

165173
```ts
166174
// This path will not be transformed
167-
import * as sm1 from 'sub-module1/index'
175+
import * as sm1 from "sub-module1/index";
168176
```
169177

170178
### @transform-path tag
@@ -173,7 +181,7 @@ Use the `@transform-path` tag to explicitly specify the output path for a single
173181

174182
```ts
175183
// @transform-path https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js
176-
import react from 'react' // Output path will be the url above
184+
import react from "react"; // Output path will be the url above
177185
```
178186

179187
### @no-transform-path
@@ -182,7 +190,7 @@ Use the `@no-transform-path` tag to explicitly disable transformation for a sing
182190

183191
```ts
184192
// @no-transform-path
185-
import 'normally-transformed' // This will remain 'normally-transformed', even though it has a different value in paths config
193+
import "normally-transformed"; // This will remain 'normally-transformed', even though it has a different value in paths config
186194
```
187195

188196
## Articles

jest.config.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import { JestConfigWithTsJest } from 'ts-jest/dist/types';
1+
import { JestConfigWithTsJest } from "ts-jest/dist/types";
22

33
const config: JestConfigWithTsJest = {
44
testEnvironment: "node",
5-
preset: 'ts-jest',
6-
testRegex: '.*(test|spec)\\.tsx?$',
7-
moduleFileExtensions: [ 'ts', 'tsx', 'js', 'jsx', 'json', 'node' ],
5+
preset: "ts-jest",
6+
testRegex: ".*(test|spec)\\.tsx?$",
7+
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
88
transform: {
9-
'^.+\\.tsx?$': [
10-
'ts-jest',
9+
"^.+\\.tsx?$": [
10+
"ts-jest",
1111
{
12-
tsconfig: '<rootDir>/test/tsconfig.json'
13-
}
14-
]
12+
tsconfig: "<rootDir>/test/tsconfig.json",
13+
},
14+
],
1515
},
16-
modulePaths: [ "<rootDir>" ],
17-
roots: [ '<rootDir>' ],
18-
}
16+
modulePaths: ["<rootDir>"],
17+
roots: ["<rootDir>"],
18+
};
1919

2020
export default config;

nx-transformer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
'use strict'
2-
module.exports = require('./').nxTransformerPlugin;
1+
"use strict";
2+
module.exports = require("./").nxTransformerPlugin;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"test": "jest",
1111
"release": "standard-version",
1212
"--------------": "",
13-
"format": "prettier --write \"{src,test}/**/{*.js,!(*.d).ts}\"",
13+
"format": "prettier --write .",
1414
"clean": "npx -y rimraf -g dist **/*.tsbuildinfo ./test/projects/nx/dist",
1515
"clean:all": "yarn run clean && npx -y rimraf -g node_modules **/node_modules **/yarn.lock yarn.lock",
1616
"reset": "yarn run clean:all && yarn install && yarn build",

register.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
let tsNode;
22
try {
3-
tsNode = require('ts-node');
3+
tsNode = require("ts-node");
44
} catch {
55
throw new Error(
66
`Cannot resolve ts-node. Make sure ts-node is installed before using typescript-transform-paths/register`
77
);
88
}
99

1010
tsNode.register();
11-
require('./').register();
11+
require("./").register();

src/utils/elide-import-export.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ export function elideImportOrExportDeclaration(
9494
isExportSpecifier,
9595
} = tsInstance;
9696

97-
const isNamespaceExport = tsInstance.isNamespaceExport ?? ((node: Node): node is NamespaceExport => node.kind === SyntaxKind.NamespaceExport);
97+
const isNamespaceExport =
98+
tsInstance.isNamespaceExport ?? ((node: Node): node is NamespaceExport => node.kind === SyntaxKind.NamespaceExport);
9899

99100
if (tsInstance.isImportDeclaration(node)) {
100101
// Do not elide a side-effect only import declaration.

test/prepare.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ const tsp2 = require("tsp2");
99
* ****************************************************************************************************************** */
1010

1111
const rootDir = __dirname;
12-
const tsDirs = [
13-
"typescript-three",
14-
"typescript-four-seven",
15-
"typescript",
16-
];
12+
const tsDirs = ["typescript-three", "typescript-four-seven", "typescript"];
1713

1814
/* ****************************************************************************************************************** *
1915
* Patch TS Modules

test/projects/extras/tsconfig.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"include": [ "src" ],
2+
"include": ["src"],
33

44
"ts-node": {
5-
"require": [ "typescript-transform-paths/register" ]
5+
"require": ["typescript-transform-paths/register"]
66
},
77

88
"compilerOptions": {
@@ -17,7 +17,7 @@
1717

1818
"baseUrl": "./src",
1919
"paths": {
20-
"#identifier": [ "./id.ts" ]
20+
"#identifier": ["./id.ts"]
2121
},
2222

2323
"plugins": [

test/projects/general/tsconfig.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
"target": "es5",
44
"module": "ESNext",
55
"outDir": "__built",
6-
"moduleResolution" : "node",
6+
"moduleResolution": "node",
77

88
"declaration": true,
99
"baseUrl": "./",
1010
"paths": {
1111
"path": ["https://external.url/path.js"],
1212
"@*": ["*"],
13-
"#utils/*": [ "./utils/*", "./secondary/*" ],
13+
"#utils/*": ["./utils/*", "./secondary/*"],
1414
"*": ["*"]
1515
},
1616

test/projects/nx/packages/library1/project.json

+3-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
"targets": {
66
"build": {
77
"executor": "@nrwl/node:webpack",
8-
"outputs": [
9-
"{options.outputPath}"
10-
],
8+
"outputs": ["{options.outputPath}"],
119
"options": {
1210
"outputPath": "dist/library1",
1311
"main": "packages/library1/src/index.ts",
@@ -23,9 +21,8 @@
2321
}
2422
]
2523
},
26-
"dependsOn": [
27-
]
24+
"dependsOn": []
2825
}
2926
},
30-
"tags": [ ]
27+
"tags": []
3128
}

test/projects/nx/packages/library2/project.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"root": "packages/library2",
33
"sourceRoot": "packages/library2/src",
44
"projectType": "library",
5-
"targets": {
6-
},
5+
"targets": {},
76
"tags": []
87
}

test/projects/project-ref/a/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"files": [ "index.ts" ],
2+
"files": ["index.ts"],
33
"extends": "../tsconfig.base.json",
44
"compilerOptions": {
55
"outDir": "../lib/a",

test/projects/project-ref/b/tsconfig.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"files": [ "index.ts", "local/index.ts" ],
2+
"files": ["index.ts", "local/index.ts"],
33
"extends": "../tsconfig.base.json",
44
"compilerOptions": {
5-
"outDir": "../lib/b",
5+
"outDir": "../lib/b"
66
},
77
"references": [
88
{

test/projects/project-ref/tsconfig.base.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
"baseUrl": ".",
1111
"paths": {
12-
"#a/*": [ "./a/*" ],
13-
"#b/*": [ "./b/*" ]
12+
"#a/*": ["./a/*"],
13+
"#b/*": ["./b/*"]
1414
},
1515
"plugins": [
1616
{
17-
"transform": "../../../../src/index.ts",
17+
"transform": "../../../../src/index.ts"
1818
},
1919
{
2020
"transform": "../../../../src/index.ts",

test/projects/specific/tsconfig.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"include": [ "src", "generated" ],
2+
"include": ["src", "generated"],
33

44
"compilerOptions": {
55
"noEmit": true,
@@ -10,15 +10,15 @@
1010
"moduleResolution": "node",
1111
"declaration": true,
1212

13-
"rootDirs": [ "src", "generated" ],
13+
"rootDirs": ["src", "generated"],
1414

1515
"baseUrl": ".",
1616
"paths": {
17-
"#root/*": [ "./src/*", "./generated/*" ],
18-
"#exclusion/*": [ "./src/excluded/*" ],
19-
"#elision": [ "./src/type-elision" ],
20-
"#elision/*": [ "./src/type-elision/*" ],
21-
"#packages/*": [ "./src/packages/*" ]
17+
"#root/*": ["./src/*", "./generated/*"],
18+
"#exclusion/*": ["./src/excluded/*"],
19+
"#elision": ["./src/type-elision"],
20+
"#elision/*": ["./src/type-elision/*"],
21+
"#packages/*": ["./src/packages/*"]
2222
},
2323
"resolveJsonModule": true
2424
}

0 commit comments

Comments
 (0)