Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork29
Transforms module resolution paths using TypeScript path mapping and/or custom paths
License
LeDDGroup/typescript-transform-paths
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Transform compiled source module resolution paths using TypeScript'spaths config, and/or custom resolution paths
Tip
Upgrading from v3 to v4? See themigration documentation.
Important
We arelooking for maintainers.
Explorealternatives.
<yarn|npm|pnpm> add -D typescript-transform-paths
Add it toplugins in yourtsconfig.json file.
{"compilerOptions": {"baseUrl":"./",// Configure your path mapping here"paths": {"@utils/*": ["utils/*"], },// Note: to transform paths for both the output .js and .d.ts files,// you need both of the below entries"plugins": [// Transform paths in output .js files {"transform":"typescript-transform-paths" },// Transform paths in output .d.ts files (include this line if you output declarations files) {"transform":"typescript-transform-paths","afterDeclarations":true }, ], },}core/index.ts
// The following import path is transformed to '../utils/sum'import{sum}from"@utils/sum";
Compile with
tsc— Usets-patch.Run with
ts-node— See theWiki.Integrate withNx — Add the
typescript-transform-paths/plugins/nxtransformer to the project configuration.project.json{/* ...*/"targets": {"build": {/* ...*/"options": {/* ...*/"transformers": [ {"name":"typescript-transform-paths/plugins/nx","options": {"afterDeclarations":true }, }, ], }, }, },}
TypeScript allows definingVirtual Directories via therootDirs compiler option.
To enable Virtual Directory mapping, use theuseRootDirs plugin option.
tsconfig.json
{"compilerOptions": {"rootDirs": ["src","generated"],"baseUrl":".","paths": {"#root/*": ["./src/*","./generated/*"], },"plugins": [ {"transform":"typescript-transform-paths","useRootDirs":true }, {"transform":"typescript-transform-paths","useRootDirs":true,"afterDeclarations":true }, ], },}├src/├─ subdir/│ └─ sub-file.ts├─ file1.ts├generated/├─ file2.tssrc/file1.ts
import"#root/file2.ts";// Resolves to './file2'
src/subdir/sub-file.ts
import"#root/file2.ts";// Resolves to '../file2'import"#root/file1.ts";// Resolves to '../file1'
You can disable transformation for paths based on the resolved file path.
Theexclude option allows specifying Glob patterns to match against those resolved file paths.
For an example context in which this would be useful, seeissue #83.
tsconfig.json
{"compilerOptions": {"paths": {"sub-module1/*": ["../../node_modules/sub-module1/*"],"sub-module2/*": ["../../node_modules/sub-module2/*"], },"plugins": [ {"transform":"typescript-transform-paths","exclude": ["**/node_modules/**"], }, ], },}// This path will NOT be transformedimport*assm1from"sub-module1/index";
Use the@transform-path tag to explicitly specify the output path for a single statement.
//@transform-path https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.jsimportreactfrom"react";// Output path will be the URL defined above
Use the@no-transform-path tag to explicitly disable transformation for a single statement.
//@no-transform-pathimport"normally-transformed";// This will remain 'normally-transformed' even though// it has a different value in paths config
| typescript-transform-paths | TypeScript | Node.js |
|---|---|---|
| ^4.0.0 | >=5.x | >=22 |
| ^3.5.2 | >=3.6.5, >=4.x, >=5.x | >=18 |
- Package Manager:
yarn(yarn install) - Format and lint the code before commit:
prettier(yarn format && yarn lint) - Commit messages:Conventional Commit Specs
- Releases:
changelogen(yarn release)
GH_TOKEN=$(gh auth token) yarn releaseRon S. | Daniel Perez |
About
Transforms module resolution paths using TypeScript path mapping and/or custom paths
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.