ts-imports-normalizer
v0.1.0
Published
Rewrite relative TypeScript imports to a configured path alias
Maintainers
Readme
ts-imports-normalizer
A fast command-line tool that rewrites relative TypeScript imports to a configured path alias. It is written in Rust and distributed with prebuilt executables, so consumers only need Node.js.
Supported platforms:
- Windows x64
- Linux x64
- macOS x64
- macOS arm64
Installation
Install it as a development dependency:
npm install --save-dev ts-imports-normalizeror
yarn add --dev ts-imports-normalizerUsage
Pass the source directory, alias prefix, and one or more TypeScript files:
npx fix-import-paths --source-root ./src --alias @/ ./src/example.ts ./src/components/button.tsxOnly .ts and .tsx files inside --source-root are processed. Files that do not need changes are left untouched.
The tool works well with lint-staged, which appends the staged filenames to the command automatically:
{
"lint-staged": {
"src/**/*.{ts,tsx}": "fix-import-paths --source-root ./src --alias @/"
}
}You can also define a regular package script:
{
"scripts": {
"fix-imports": "fix-import-paths --source-root ./src --alias @/"
}
}Then supply the files after --:
package.json
npm run fix-imports -- ./src/example.tsOptions
--source-root <path>: root directory that imports should be relative to--alias <prefix>: alias written into imports, such as@/
