Convert relative to alias imports
Simple utility to convert relative to aliased imports.
Usage
npx convert-relative-to-alias-imports <alias> <rootDirectory>
;
or use command line flags:
-
alias
(ora
) -
rootDir
(orr
)
Example
Given a project in which the code is in the ./src directory.
Add the following to your tsconfig.json:
{
"compilerOptions":{
"baseUrl": "src",
"paths": {
"@/*": ["./*"],
}
}
}
Then run the following command to update all import statements in your project:
npx convert-relative-to-alias-imports @ src
;
Your imports will now look like this:
import example from '@/utils/example';
Instead of:
import example from '../utils/example';
Your tests might complain about this setup. Possible solution is to add this to your jest config:
{
moduleNameMapper: {
'@/(.*)': '<rootDir>/src/$1',
},
}