replace-package
TypeScript icon, indicating that this package has built-in type declarations

0.0.4 • Public • Published

replace-package

A tool to replace legacy package

replace-package

Total Downloads Latest Release License

from:

import classnames from 'classnames';

const Component = ({ active }) => {
  return (
    <div className={classnames({ ['text-black']: active })}>orz</div>
  );
};

to:

import clsx from 'clsx';

const Component = ({ active }) => {
  return (
    <div className={clsx({ ['text-black']: active })}>orz</div>
  );
};

How to use

step 1: init

npx replace-package@latest init

codemod.json

{
  "replace-package": [{
    "name": "",
    "source": "",
    "importDefault": false,
    "legacyName": "",
    "legacySource": "",
    "legacyImportDefault": false,
  }]
}

step 2: change config

{
  "replace-package": [{
    "name": "clsx",
    "source": "clsx",
    "importDefault": true,
    "legacyName": "classnames",
    "legacySource": "classnames",
    "legacyImportDefault": true,
  }]
}

step 3: transform

npx replace-package@latest src/**/*.(ts|tsx)

It will check your git directory is clean, you can use '--force' to skip the check.

Example

component

codemod.json

{
  "replace-package": [{
    "name": "Form",
    "source": "@ant-design/compatible",
    "importDefault": false,
    "legacyName": "Form",
    "legacySource": "antd",
    "legacyImportDefault": false
  }]
}

image

by path(VSCode copy path)

{
  "replace-package": [{
    "name": "Form",
    "source": "/Users/xx/workspace/xx/src/components/Form",
    "importDefault": false,
    "legacyName": "Form",
    "legacySource": "antd",
    "legacyImportDefault": false
  }]
}

image

image

utils

codemod.json

{
  "replace-package": [{
    "legacyName": "classnames",
    "legacySource": "classnames",
    "legacyImportDefault": true,
    "name": "clsx",
    "source": "clsx",
    "importDefault": true
  }]
}

image

or

{
  "replace-package": [{
    "legacyName": "classnames",
    "legacySource": "/Users/xx/workspace/xx/src/utils/classnames",
    "legacyImportDefault": true,
    "name": "clsx",
    "source": "clsx",
    "importDefault": true
  }]
}

name import

legacy

import { Form as NewForm, Select, Input, DatePicker } from 'antd';
import { Form } from '@ant-design/compatible';

//...
<Form layout="horizontal"></Form>

codemod.json

{
  "replace-package": [{
    "legacyName": "Form",
    "legacySource": "@ant-design/compatible",
    "legacyImportDefault": false,
    "name": "Form",
    "source": "antd",
    "importDefault": false
  }]
}

transformed

import { Form as NewForm, Select, Input, DatePicker } from 'antd';

//...
<NewForm layout="horizontal"></NewForm>

Readme

Keywords

none

Package Sidebar

Install

npm i replace-package

Weekly Downloads

1

Version

0.0.4

License

MIT

Unpacked Size

69.4 kB

Total Files

27

Last publish

Collaborators

  • shiyangzhaoa