Easily refactor imports in TypeScript files via the CLI.
npm install refactor-imports -g
Options:
--help Show help [boolean]
--version Show version number [boolean]
-p, --path [required]
-s, --current-import-sources [array] [required]
-t, --target-import-source
-r, --resolve-import-mapping [boolean] [default: false]
--tsconfig-path, --tsc
-e, --only-imported-exports [array] [default: []]
-d, --dry-run [boolean] [default: false]
-f, --fuzzy-match [boolean] [default: false]
--parser [choices: "ts", "tsx"] [default: "tsx"]
You need to pass the path (-p) and the current-import-sources (-s). Additionally, you must provide either target-import-source (-t) OR both resolve-import-mapping (-r) and tsconfig-path (-tsc).
refactor-imports -p ./src -s "my-old-lib" -t "my-new-lib"// before
import { foo, bar } from 'my-old-lib';
// after
import { foo, bar } from 'my-new-lib';You can specify more than one current import source if you want to merge imported exports from various import sources into one.
refactor-imports -p ./src -s "my-old-lib" "my-even-older-lib" -t "my-new-lib"// before
import { foo } from 'my-old-lib';
import { bar } from 'my-even-older-lib';
// after
import { foo, bar } from 'my-new-lib';Instead of just replacing whole import sources, you can also move selected imported exports of an import source to a new import source.
refactor-imports -p ./src -s "my-old-lib" -t "my-new-lib" -e "foo" "bar"// before
import { foo, bar, baz } from 'my-old-lib';
// after
import { baz } from 'my-old-lib';
import { foo, bar } from 'my-new-lib';You can use a regular expressions as current-import-source when you add the fuzzy-match (-f) argument.
refactor-imports -p ./src -s "@my-libs/.+" -t "my-new-lib" -f// before
import { foo } from '@my-libs/old';
import { bar } from '@my-libs/even-older';
// after
import { foo, bar } from '@my-libs/new';You can automatically resolve path aliases defined in your tsconfig.json to relative paths by using the resolve-import-mapping (-r) option along with tsconfig-path (-tsc).
refactor-imports -p ./src -s "@my-app" -r -tsc ./tsconfig.jsonGiven a tsconfig.json with path mappings like:
{
"compilerOptions": {
"paths": {
"@my-app/*": ["./src/*"]
}
}
}// before
import { foo } from '@my-app/utils/helpers';
// after
import { foo } from './utils/helpers'; // or '../utils/helpers' depending on file locationBy default, the tool uses the tsx parser to support JSX syntax. If you're working with plain TypeScript files without JSX, you can use the ts parser.
refactor-imports -p ./src -s "my-old-lib" -t "my-new-lib" --parser tsThe heavy-lifting of this tool is done by Facebook's jscodeshift and the transform-imports codemod written by suchipi. Thanks for doing the actual work!