$ npm install typescript-transform-paths
Transform module resolution paths in compiled output source to conform with TypeScript
internal resolution via tsconfig.json
settings (paths
, rootDirs
, baseUrl
)
# NPM
npm i -D typescript-transform-paths
# Yarn
yarn add -D typescript-transform-paths
Add it to plugins in your tsconfig.json
{
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@utils/*": ["utils/*"]
},
"plugins": [
// Tranform paths in output .js files
{ "transform": "typescript-transform-paths" },
// Tranform paths in output .d.ts files
{ "transform": "typescript-transform-paths", "afterDeclarations": true }
]
}
}
core/index.ts
import { sum } from "@utils/sum";
sum(2, 3);
core/index.js
(compiled output)
// core/index.js
var sum_1 = require("../utils/sum");
sum_1.sum(2, 3);
TS allows defining
virtual directories
via the rootDirs
compiler option.
To enable virtual directory mapping, use the useRootDirs
plugin option.
{
"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.ts
src/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'
yarn
(yarn install
)prettier
(yarn run format
)standard-version
(yarn run release
)Ron S. |
Daniel Perez Alvarez |
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
© 2010 - cnpmjs.org x YWFE | Home | YWFE