/import-codemod

Primary LanguageJavaScriptMIT LicenseMIT

npm Travis

import-codemod

Flexible codemod for moving around imports.

It's super useful for migrating giant codebases to react-primitives. It might be useful for some other things.

Usage

Create a config file (or use the included src/configs/config.primitives.js)

yarn global add jscodeshift
yarn add import-codemod

jscodeshift <path> -t ./node_modules/import-codemod/lib/index.js --config path-to-config.js

Things it can do

Move select named imports from one module to another

import { Text, View } from 'react-native';
// ->
import { Text, View } from 'react-primitives';

config.js

module.exports = {
  mappings: [
    module: {
      from: 'react-native',
      to: 'react-primitives',
    },
    specifiers: [
      'Text',
      'View',
    ],
  },
}

Move all named imports from one module to another

import { Text, View, lots, of, others } from 'react-native';
// ->
import { Text, View, lots, of, others } from 'react-primitives';

config.js

module.exports = {
  mappings: [
    module: {
      from: 'react-native',
      to: 'react-primitives',
    },
    specifiers: [
      '*',
    ],
  ],
}

Move and rename named imports from one module to another

import { Text, View } from 'react-native';
// ->
import { Words, Box } from 'react-primitives';

config.js

module.exports = {
  mappings: [
    module: {
      from: 'react-native',
      to: 'react-primitives',
    },
    specifiers: {
      'Text': 'Words',
      'View': 'Box',
    },
  ],
}

Move default imports from one module to another

import Foo from 'foo';
// ->
import Foo from 'bar';

config.js

module.exports = {
  mappings: [
    module: {
      from: 'react-native',
      to: 'react-primitives',
    },
    specifiers: ['default'],
  ],
}

Move default imports from one module to named imports of another

import Foo from 'foo';
// ->
import { Foo } from 'bar';

config.js

module.exports = {
  mappings: [
    module: {
      from: 'react-native',
      to: 'react-primitives',
    },
    specifiers: {
      'default': 'Foo'
    },
  ],
}

Exclude files from being transformed

// @ignoreMe

config.js

module.exports = {
  ignoreMark: '@ignoreMe',
  mappings: [],
}