|
| 1 | +# import-move-codemod |
| 2 | + |
| 3 | +Codemod to move imports from one module to another. Super useful in huge monorepos or huge migrations of 3rd party packages. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +Inside your project root run |
| 8 | +For `npm` |
| 9 | +``` |
| 10 | +npm i import-move-codemod --save-dev |
| 11 | +``` |
| 12 | +or for `yarn` |
| 13 | +``` |
| 14 | +yarn add import-move-codemod --dev |
| 15 | +``` |
| 16 | +or for `pnpm` |
| 17 | +``` |
| 18 | +pnpm add import-move-codemod --D |
| 19 | +``` |
| 20 | + |
| 21 | +Before the run you need to create a file with a config, let say `codemod.json` |
| 22 | +```json |
| 23 | +{ |
| 24 | + "module": { |
| 25 | + "from": "one", |
| 26 | + "to": "two" |
| 27 | + }, |
| 28 | + "specifiers": ["One"] |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +Then for example to run codemod against all the files in `/src` run |
| 33 | +``` |
| 34 | +npx @codemod/cli --p import-move-codemod -o import-move-codemod=@codemod.json --printer prettier /src |
| 35 | +``` |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +## What refactorings are available |
| 41 | + |
| 42 | +### Move of one or multiple named imports from one module to another |
| 43 | + |
| 44 | +``` |
| 45 | +{ |
| 46 | + module: { |
| 47 | + from: "one", |
| 48 | + to: "two" |
| 49 | + }, |
| 50 | + specifiers: ["One"] |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +```ts |
| 55 | +import { One } from 'one' |
| 56 | + |
| 57 | +// ↓ ↓ ↓ ↓ ↓ ↓ |
| 58 | + |
| 59 | +import { One } from 'two'; |
| 60 | +``` |
| 61 | + |
| 62 | +``` |
| 63 | +{ |
| 64 | + module: { |
| 65 | + from: "one", |
| 66 | + to: "two" |
| 67 | + }, |
| 68 | + specifiers: ["One", "Two"] |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +```ts |
| 73 | +import { One, Two } from 'one' |
| 74 | + |
| 75 | +// ↓ ↓ ↓ ↓ ↓ ↓ |
| 76 | + |
| 77 | +import { One, Two } from 'two'; |
| 78 | +``` |
| 79 | + |
| 80 | +```ts |
| 81 | +import { One as OneA, Two as TwoA } from 'one' |
| 82 | + |
| 83 | +// ↓ ↓ ↓ ↓ ↓ ↓ |
| 84 | + |
| 85 | +import { One as OneA, Two as TwoA } from 'two'; |
| 86 | + |
| 87 | +``` |
| 88 | + |
| 89 | +``` |
| 90 | +{ |
| 91 | + module: { |
| 92 | + from: "one", |
| 93 | + to: "two" |
| 94 | + }, |
| 95 | + specifiers: ["Two"] |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +```ts |
| 100 | +import { One, Two } from 'one' |
| 101 | + |
| 102 | +// ↓ ↓ ↓ ↓ ↓ ↓ |
| 103 | + |
| 104 | +import { Two } from 'two'; |
| 105 | +import { One } from 'one'; |
| 106 | +``` |
| 107 | + |
| 108 | +### Move named imports and default in one go |
| 109 | + |
| 110 | +``` |
| 111 | +{ |
| 112 | + module: { |
| 113 | + from: "one", |
| 114 | + to: "two" |
| 115 | + }, |
| 116 | + specifiers: ["default", "One", "Two"] |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | + |
| 121 | +```ts |
| 122 | +import ADefault, { One, Two, Three } from 'one' |
| 123 | + |
| 124 | +// ↓ ↓ ↓ ↓ ↓ ↓ |
| 125 | + |
| 126 | +import ADefault, { One, Two } from 'two'; |
| 127 | +import { Three } from 'one'; |
| 128 | +``` |
| 129 | + |
| 130 | +``` |
| 131 | +{ |
| 132 | + module: { |
| 133 | + from: "one", |
| 134 | + to: "two" |
| 135 | + }, |
| 136 | + specifiers: ["default", "Two"] |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +```ts |
| 141 | +import One, { Two } from 'one' |
| 142 | + |
| 143 | + // ↓ ↓ ↓ ↓ ↓ ↓ |
| 144 | + |
| 145 | +import One, { Two } from 'two'; |
| 146 | +``` |
| 147 | + |
| 148 | +### Move everything i.e. module rename |
| 149 | + |
| 150 | +``` |
| 151 | +{ |
| 152 | + module: { |
| 153 | + from: "one", |
| 154 | + to: "two" |
| 155 | + }, |
| 156 | + specifiers: ["*"] |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +```ts |
| 161 | +import ADefault, { One, Two, Three } from 'one' |
| 162 | + |
| 163 | +// ↓ ↓ ↓ ↓ ↓ ↓ |
| 164 | + |
| 165 | +import ADefault, { One, Two, Three } from 'two'; |
| 166 | + |
| 167 | +``` |
| 168 | + |
| 169 | +### Move named imports and rename them |
| 170 | + |
| 171 | +``` |
| 172 | +{ |
| 173 | + module: { |
| 174 | + from: "one", |
| 175 | + to: "two" |
| 176 | + }, |
| 177 | + specifiers: { |
| 178 | + One: "OneR", |
| 179 | + Two: "TwoR" |
| 180 | + } |
| 181 | +} |
| 182 | +``` |
| 183 | + |
| 184 | +```ts |
| 185 | +import ADefault, { One, Two } from 'one' |
| 186 | + |
| 187 | +// ↓ ↓ ↓ ↓ ↓ ↓ |
| 188 | + |
| 189 | +import { OneR, TwoR } from 'two'; |
| 190 | +import ADefault from 'one'; |
| 191 | +``` |
| 192 | + |
| 193 | +```ts |
| 194 | +import { One, Two } from 'one' |
| 195 | + |
| 196 | +// ↓ ↓ ↓ ↓ ↓ ↓ |
| 197 | + |
| 198 | +import { OneR, TwoR } from 'two'; |
| 199 | +``` |
| 200 | + |
| 201 | +### Move default import to named |
| 202 | +``` |
| 203 | +{ |
| 204 | + module: { |
| 205 | + from: "one", |
| 206 | + to: "two" |
| 207 | + }, |
| 208 | + specifiers: { default: "One" } |
| 209 | +} |
| 210 | +``` |
| 211 | + |
| 212 | +```ts |
| 213 | +import One, { Two } from 'one' |
| 214 | + |
| 215 | +// ↓ ↓ ↓ ↓ ↓ ↓ |
| 216 | + |
| 217 | +import { One } from 'two'; |
| 218 | +import { Two } from 'one'; |
| 219 | +``` |
| 220 | + |
| 221 | +```ts |
| 222 | +import One from 'one' |
| 223 | + |
| 224 | +// ↓ ↓ ↓ ↓ ↓ ↓ |
| 225 | + |
| 226 | +import { One } from 'two'; |
| 227 | +``` |
| 228 | + |
| 229 | +### Move only default import |
| 230 | + |
| 231 | +``` |
| 232 | +{ |
| 233 | + module: { |
| 234 | + from: "one", |
| 235 | + to: "two" |
| 236 | + }, |
| 237 | + specifiers: ["default"] |
| 238 | +} |
| 239 | +``` |
| 240 | + |
| 241 | +```ts |
| 242 | +import ADefault, { One, Two, Three } from 'one' |
| 243 | + |
| 244 | +// ↓ ↓ ↓ ↓ ↓ ↓ |
| 245 | + |
| 246 | +import ADefault from 'two'; |
| 247 | +import { One, Two, Three } from 'one'; |
| 248 | +``` |
| 249 | + |
| 250 | +```ts |
| 251 | +import One from 'one' |
| 252 | + |
| 253 | +// ↓ ↓ ↓ ↓ ↓ ↓ |
| 254 | + |
| 255 | +import One from 'two'; |
| 256 | +``` |
| 257 | + |
| 258 | +### Make default import from named |
| 259 | + |
| 260 | +``` |
| 261 | +{ |
| 262 | + module: { |
| 263 | + from: "one", |
| 264 | + to: "two" |
| 265 | + }, |
| 266 | + specifiers: { |
| 267 | + One: "default" |
| 268 | + } |
| 269 | +} |
| 270 | +``` |
| 271 | + |
| 272 | +```ts |
| 273 | +import { One } from 'one' |
| 274 | + |
| 275 | +// ↓ ↓ ↓ ↓ ↓ ↓ |
| 276 | + |
| 277 | +import One from 'two'; |
| 278 | +``` |
0 commit comments