import-move-codemod
Codemod to move imports from one module to another. Super useful in huge monorepos or huge migrations of 3rd party packages.
Usage
Inside your project root run
For npm
npm i import-move-codemod --save-dev
or for yarn
yarn add import-move-codemod --dev
or for pnpm
pnpm add import-move-codemod --D
Before the run you need to create a file with a config, let say codemod.json
Then for example to run codemod against all the files in /src
run
npx @codemod/cli -p import-move-codemod -o import-move-codemod=@codemod.json --printer prettier /src
What refactorings are available
Move of one or multiple named imports from one module to another
{
module: {
from: "one",
to: "two"
},
specifiers: ["One"]
}
// ↓ ↓ ↓ ↓ ↓ ↓ ;
{
module: {
from: "one",
to: "two"
},
specifiers: ["One", "Two"]
}
// ↓ ↓ ↓ ↓ ↓ ↓ ;
// ↓ ↓ ↓ ↓ ↓ ↓ ;
{
module: {
from: "one",
to: "two"
},
specifiers: ["Two"]
}
// ↓ ↓ ↓ ↓ ↓ ↓ ;;
Move named imports and default in one go
{
module: {
from: "one",
to: "two"
},
specifiers: ["default", "One", "Two"]
}
// ↓ ↓ ↓ ↓ ↓ ↓ ;;
{
module: {
from: "one",
to: "two"
},
specifiers: ["default", "Two"]
}
// ↓ ↓ ↓ ↓ ↓ ↓ ;
Move everything i.e. module rename
{
module: {
from: "one",
to: "two"
},
specifiers: ["*"]
}
// ↓ ↓ ↓ ↓ ↓ ↓ ;
Move named imports and rename them
{
module: {
from: "one",
to: "two"
},
specifiers: {
One: "OneR",
Two: "TwoR"
}
}
// ↓ ↓ ↓ ↓ ↓ ↓ ;;
// ↓ ↓ ↓ ↓ ↓ ↓ ;
Move default import to named
{
module: {
from: "one",
to: "two"
},
specifiers: { default: "One" }
}
// ↓ ↓ ↓ ↓ ↓ ↓ ;;
// ↓ ↓ ↓ ↓ ↓ ↓ ;
Move only default import
{
module: {
from: "one",
to: "two"
},
specifiers: ["default"]
}
// ↓ ↓ ↓ ↓ ↓ ↓ ;;
// ↓ ↓ ↓ ↓ ↓ ↓ ;
Make default import from named
{
module: {
from: "one",
to: "two"
},
specifiers: {
One: "default"
}
}
// ↓ ↓ ↓ ↓ ↓ ↓ ;