UNPKG

6.07 kBMarkdownView Raw
1# typescript-transform-paths
2
3[![npm version](https://img.shields.io/npm/v/typescript-transform-paths.svg)](https://www.npmjs.com/package/typescript-transform-paths)
4[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2FLeDDGroup%2Ftypescript-transform-paths%2Fbadge%3Fref%3Dmaster&style=flat)](https://actions-badge.atrox.dev/LeDDGroup/typescript-transform-paths/goto?ref=master)
5[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
6[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
7![All Contributors](https://img.shields.io/badge/all_contributors-10-orange.svg?style=flat-square)
8
9Transform compiled source module resolution paths using TypeScript's `paths` config, and/or custom resolution paths.
10
11## Setup Steps
12
13### 1. Install
14
15```sh
16<yarn|npm|pnpm> add -D typescript-transform-paths
17```
18
19### 2. Configure
20
21Add it to _plugins_ in your _tsconfig.json_
22
23#### Example Config
24
25```jsonc
26{
27 "compilerOptions": {
28 "baseUrl": "./",
29 // Configure your path mapping here
30 "paths": {
31 "@utils/*": ["utils/*"]
32 },
33 // Note: To transform paths for both the output .js and .d.ts files, you need both of the below entries
34 "plugins": [
35 // Transform paths in output .js files
36 { "transform": "typescript-transform-paths" },
37
38 // Transform paths in output .d.ts files (Include this line if you output declarations files)
39 { "transform": "typescript-transform-paths", "afterDeclarations": true }
40 ]
41 }
42}
43```
44#### Example result
45
46`core/index.ts`
47```tsx
48// The following transforms path to '../utils/sum'
49import { sum } from "@utils/sum";
50```
51
52### 3. Usage
53
54- **Compile with `tsc`** — Use [ts-patch](https://github.com/nonara/ts-patch)
55
56
57- **Use with ts-node** — Add `typescript-transform-paths/register` to `require` config.
58
59 `tsconfig.json`
60 ```jsonc
61 {
62 "ts-node": {
63 "transpileOnly": true,
64 "require": [ "typescript-transform-paths/register" ],
65 },
66 "compilerOptions" { /* ... */ }
67 }
68 ```
69
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 ```
94
95## Virtual Directories
96TS allows defining
97[virtual directories](https://www.typescriptlang.org/docs/handbook/module-resolution.html#virtual-directories-with-rootdirs)
98via the `rootDirs` compiler option.
99To enable virtual directory mapping, use the `useRootDirs` plugin option.
100
101```jsonc
102{
103 "compilerOptions": {
104 "rootDirs": [ "src", "generated" ],
105 "baseUrl": ".",
106 "paths": {
107 "#root/*": [ "./src/*", "./generated/*" ]
108 },
109 "plugins": [
110 { "transform": "typescript-transform-paths", "useRootDirs": true },
111 { "transform": "typescript-transform-paths", "useRootDirs": true, "afterDeclarations": true }
112 ]
113 }
114}
115```
116
117#### Example
118
119```
120- src/
121 - subdir/
122 - sub-file.ts
123 - file1.ts
124- generated/
125 - file2.ts
126```
127
128`src/file1.ts`
129```ts
130import '#root/file2.ts' // resolves to './file2'
131```
132`src/subdir/sub-file.ts`
133```ts
134import '#root/file2.ts' // resolves to '../file2'
135import '#root/file1.ts' // resolves to '../file1'
136```
137
138## Custom Control
139
140### Exclusion patterns
141
142You can disable transformation for paths based on the resolved file path. The `exclude` option allows specifying glob
143patterns to match against resolved file path.
144
145For an example context in which this would be useful, see [Issue #83](https://github.com/LeDDGroup/typescript-transform-paths/issues/83)
146
147Example:
148```jsonc
149{
150 "compilerOptions": {
151 "paths": {
152 "sub-module1/*": [ "../../node_modules/sub-module1/*" ],
153 "sub-module2/*": [ "../../node_modules/sub-module2/*" ],
154 },
155 "plugins": [
156 {
157 "transform": "typescript-transform-paths",
158 "exclude": [ "**/node_modules/**" ]
159 }
160 ]
161 }
162}
163```
164
165```ts
166// This path will not be transformed
167import * as sm1 from 'sub-module1/index'
168```
169
170### @transform-path tag
171
172Use the `@transform-path` tag to explicitly specify the output path for a single statement.
173
174```ts
175// @transform-path https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js
176import react from 'react' // Output path will be the url above
177```
178
179### @no-transform-path
180
181Use the `@no-transform-path` tag to explicitly disable transformation for a single statement.
182
183```ts
184// @no-transform-path
185import 'normally-transformed' // This will remain 'normally-transformed', even though it has a different value in paths config
186```
187
188## Articles
189
190- [Node Consumable Modules With Typescript Paths](https://medium.com/@ole.ersoy/node-consumable-modules-with-typescript-paths-ed88a5f332fa?postPublishedType=initial) by [oleersoy](https://github.com/oleersoy)
191
192## Project Guidelines for Contributors
193
194- Package Manager: `yarn` (`yarn install`)
195- Commit messages: [Conventional Commit Specs](https://www.conventionalcommits.org/en/v1.0.0/)
196- Format before commit: `prettier` (`yarn run format`)
197- Releases: `standard-version` (`yarn run release`)
198
199## Maintainers
200
201<!-- prettier-ignore-start -->
202<!-- markdownlint-disable -->
203<table>
204 <tr>
205 <td align="center"><a href="https://github.com/nonara"><img src="https://avatars0.githubusercontent.com/u/1427565?v=4" width="100px;" alt=""/><br /><sub><b>Ron S.</b></sub></a></td>
206 <td align="center"><a href="https://github.com/danielpza"><img src="https://avatars2.githubusercontent.com/u/17787042?v=4" width="100px;" alt=""/><br /><sub><b>Daniel Perez Alvarez</b></sub></a></td>
207 </tr>
208</table>