GithubHelp home page GithubHelp logo

rip21 / import-move-codemod Goto Github PK

View Code? Open in Web Editor NEW
8.0 2.0 0.0 1.49 MB

Codemod that helps with moving selectively imports from one package to another. Super useful in monorepos or just bulk refactorings of some 3rd party dependencies.

License: MIT License

JavaScript 0.60% TypeScript 99.40%
codemod babel-codemod babel imports move rename codeshift refactoring import

import-move-codemod's Introduction

import-move-codemod

npm

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

{
  "module": {
    "from": "one",
    "to": "two"
  },
  "specifiers": ["One"]
}

Then for example to run codemod against all the files in /src run

npx @codemod/cli -p import-move-codemod -o [email protected] --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"]
}
import { One } from 'one'

//      ↓ ↓ ↓ ↓ ↓ ↓

import { One } from 'two';
{
  module: {
    from: "one",
    to: "two"
  },
  specifiers: ["One", "Two"]
}
import { One, Two } from 'one'

//      ↓ ↓ ↓ ↓ ↓ ↓

import { One, Two } from 'two';
import { One as OneA, Two as TwoA } from 'one'

//      ↓ ↓ ↓ ↓ ↓ ↓

import { One as OneA, Two as TwoA } from 'two';
{
  module: {
    from: "one",
    to: "two"
  },
  specifiers: ["Two"]
}
import { One, Two } from 'one'

//      ↓ ↓ ↓ ↓ ↓ ↓

import { Two } from 'two';
import { One } from 'one';

Move named imports and default in one go

{
  module: {
    from: "one",
    to: "two"
  },
  specifiers: ["default", "One", "Two"]
}
import ADefault, { One, Two, Three } from 'one'

//     ↓ ↓ ↓ ↓ ↓ ↓

import ADefault, { One, Two } from 'two';
import { Three } from 'one';
{
  module: {
    from: "one",
    to: "two"
  },
  specifiers: ["default", "Two"]
}
import One, { Two } from 'one'

 //     ↓ ↓ ↓ ↓ ↓ ↓

import One, { Two } from 'two';

Move everything i.e. module rename

{
  module: {
    from: "one",
    to: "two"
  },
  specifiers: ["*"]
}
import ADefault, { One, Two, Three } from 'one'

//      ↓ ↓ ↓ ↓ ↓ ↓

import ADefault, { One, Two, Three } from 'two';

Move named imports and rename them

{
  module: {
    from: "one",
    to: "two"
  },
  specifiers: {
    One: "OneR",
    Two: "TwoR"
  }
}
import ADefault, { One, Two } from 'one'

//      ↓ ↓ ↓ ↓ ↓ ↓

import { OneR, TwoR } from 'two';
import ADefault from 'one';
import { One, Two } from 'one'

//      ↓ ↓ ↓ ↓ ↓ ↓

import { OneR, TwoR } from 'two';

Move default import to named

{
  module: {
    from: "one",
    to: "two"
  },
  specifiers: { default: "One" }
}
import One, { Two } from 'one'

//      ↓ ↓ ↓ ↓ ↓ ↓

import { One } from 'two';
import { Two } from 'one';
import One from 'one'

//      ↓ ↓ ↓ ↓ ↓ ↓

import { One } from 'two';

Move only default import

{
  module: {
    from: "one",
    to: "two"
  },
  specifiers: ["default"]
}
import ADefault, { One, Two, Three } from 'one'

//      ↓ ↓ ↓ ↓ ↓ ↓

import ADefault from 'two';
import { One, Two, Three } from 'one';
import One from 'one'

//      ↓ ↓ ↓ ↓ ↓ ↓

import One from 'two';

Make default import from named

{
  module: {
    from: "one",
    to: "two"
  },
  specifiers: {
    One: "default"
  }
}
import { One } from 'one'

//      ↓ ↓ ↓ ↓ ↓ ↓

import One from 'two';

import-move-codemod's People

Contributors

rip21 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

import-move-codemod's Issues

Reuse existing import if already defined in the file

If we move from let say packlets/utils to @acme/utils and in the file, there is already a @acme/utils imports we should add imports to it and do not create a new one.

So from:

import { fn1 } from 'packlets/utils'
import { fn2 } from '@acme/utils'

To:

import { fn2, fn1 } from '@acme/utils'

Currently it will generate following:

import { fn1 } from '@acme/utils'
import { fn2 } from '@acme/utils'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.