Giter Site home page Giter Site logo

codemod-v5's Introduction

English | 简体中文

Ant Design v5 Codemod

A collection of codemod scripts that help upgrade antd v5 using jscodeshift and postcss.(Inspired by react-codemod)

NPM version NPM downloads Github Action

Usage

Before run codemod scripts, you'd better make sure to commit your local git changes firstly.

# Run directly through npx
npx -p @ant-design/codemod-v5 antd5-codemod src

# Or run directly through pnpm
pnpm --package=@ant-design/codemod-v5 dlx antd5-codemod src

Codemod scripts introduction

v5-removed-component-migration

Replace import for removed component in v5.

  • Change Comment import from @ant-design/compatible.
  • Change PageHeader import from @ant-design/pro-layout.
  • Use BackTop from FloatButton.BackTop.
- import { Avatar, BackTop, Comment, PageHeader } from 'antd';
+ import { Comment } from '@ant-design/compatible';
+ import { PageHeader } from '@ant-design/pro-layout';
+ import { Avatar, FloatButton } from 'antd';

  ReactDOM.render( (
    <div>
      <PageHeader
        className="site-page-header"
        onBack={() => null}
        title="Title"
        subTitle="This is a subtitle"
      />
      <Comment
        actions={actions}
        author={<a>Han Solo</a>}
        avatar={<Avatar src="https://joeschmoe.io/api/v1/random" alt="Han Solo" />}
        content={
          <p>
            We supply a series of design principles, practical patterns and high quality design
            resources (Sketch and Axure), to help people create their product prototypes beautifully
            and efficiently.
          </p>
        }
        datetime={
          <span title="2016-11-22 11:22:33">8 hours ago</span>
        }
      />
-     <BackTop />
+     <FloatButton.BackTop />
    </div>
  );

v5-props-changed-migration

Change props usage from v4 to v5.

import { Tag, Modal, Slider } from 'antd';

const App = () => {
  const [visible, setVisible] = useState(false);

  return (
    <>
-     <Tag
-       visible={visible}
-     />
+     {visible ? <Tag /> : null}
      <Modal
-       visible={visible}
+       open={visible}
      />
-     <Slider tooltipVisible={visible} tooltipPlacement="bottomLeft" />
+     <Slider tooltip={{ placement: "bottomLeft", open: visible }} />
    </>
  );
};

v5-removed-static-method-migration

  • Replace message.warn with message.warning.
  • Replace notification.close with notification.destroy.
import { message, notification } from 'antd';

const App = () => {
  const [messageApi, contextHolder] = message.useMessage();
  const onClick1 = () => {
-   message.warn();
+   message.warning();
  }
  const onClick2 = () => {
-   messageApi.warn();
+   messageApi.warning();
  };

  const [notificationApi] = notification.useNotification();
  const onClick3 = () => {
-   notification.close();
+   notification.destroy();
  }
  const onClick4 = () => {
-   notificationApi.close();
+   notificationApi.destroy();
  };

  return <>{contextHolder}</>;
};

v5-remove-style-import

Comment out the style file import from antd (in js file).

- import 'antd/es/auto-complete/style';
- import 'antd/lib/button/style/index.less';
- import 'antd/dist/antd.compact.min.css';
+ // import 'antd/es/auto-complete/style';
+ // import 'antd/lib/button/style/index.less';
+ // import 'antd/dist/antd.compact.min.css';

Remove Antd Less in less file

Comment out the style file import from antd in less file.

- @import (reference) '~antd/dist/antd.less';
- @import '~antd/es/button/style/index.less';
+ /* @import (reference) '~antd/dist/antd.less'; */
+ /* @import '~antd/es/button/style/index.less'; */
@import './styles.less';

body {
  font-size: 14px;
}

Development

  npm run release
  npm publish

License

MIT

codemod-v5's People

Contributors

dependabot[bot] avatar vagusx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

codemod-v5's Issues

beta version bug

  • 提示用户已使用 antd 的 variables.less
  • 提示用户导入 antd/dist/reset.css
  • #10 #11
  • 处理 BackTop 从 FloatButton 导入 #7
  • 移除 v3 -> v4 时,compatible 导入的 css import '@ant-design/compatible/assets/index.css'; #9
  • message.warn #25

~antd/es/style/themes/default.less not find

✖ Webpack
Compiled with some errors in 10.79s

ERROR Failed to compile with 1 errors 12:29:23

error in ./src/.umi/plugin-layout/layout/layout/style.less

Module build failed (from ./node_modules/@umijs/bundler-webpack/lib/webpack/plugins/mini-css-extract-plugin/src/loader.js):
ModuleBuildError: Module build failed (from ./node_modules/@umijs/deps/compiled/less-loader/cjs.js):

@import '~antd/es/style/themes/default.less';
^
ENOENT: no such file or directory, open '/Users/admin/frontend/node_modules/antd/es/style/themes/default.less'
in /Users/admin/frontend/src/.umi/plugin-layout/layout/layout/style.less (line 1, column 0)
at /Users/admin/frontend/node_modules/@umijs/deps/compiled/webpack/4/bundle4.js:105544:20
at /Users/admin/frontend/node_modules/@umijs/deps/compiled/webpack/4/bundle4.js:47997:11
at /Users/admin/frontend/node_modules/@umijs/deps/compiled/webpack/4/bundle4.js:47863:18
at context.callback (/Users/admin/frontend/node_modules/@umijs/deps/compiled/webpack/4/bundle4.js:47741:13)
at runMicrotasks ()
at processTicksAndRejections (internal/process/task_queues.js:95:5)

@ ./src/.umi/plugin-layout/layout/layout/index.tsx 12:0-22
@ ./src/.umi/plugin-layout/Layout.tsx
@ ./src/.umi/core/routes.ts
@ ./src/.umi/umi.ts
@ multi ./src/.umi/umi.ts

npx -p @ant-design/codemod-v5 antd5-codemod client 报错,无法自动升级

Reproduction link

https://xxx/xxx.com

Steps to reproduce

[email protected] [email protected],控制台执行npx -p @ant-design/codemod-v5 antd5-codemod client

What is expected?

项目自动升级至v5

What is actually happening?

npx: 240 安装成功,用时 452.573 秒
Transform v5-props-changed-migration
Processing 788 files...
Spawning 3 workers...
Sending 50 files to free worker...
Sending 50 files to free worker...
Sending 50 files to free worker...
/Users/beike/.npm/_npx/21543/lib/node_modules/@ant-design/codemod-v5/transforms/v5-props-changed-migration.js:203
if (nodePath.value?.value?.type === 'JSXExpressionContainer') {
^

SyntaxError: Unexpected token '.'
at wrapSafe (internal/modules/cjs/loader.js:915:16)
at Module._compile (internal/modules/cjs/loader.js:963:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at setup (/Users/beike/.npm/_npx/21543/lib/node_modules/@ant-design/codemod-v5/node_modules/jscodeshift/src/Worker.js:91:18)
at Object. (/Users/beike/.npm/_npx/21543/lib/node_modules/@ant-design/codemod-v5/no/Users/beike/.npm/_npx/21543/lib/node_modules/@ant-design/codemod-v5/transforms/v5-props-changed-migration.js:203
if (nodePath.value?.value?.type === 'JSXExpressionContainer') {
^

SyntaxError: Unexpected token '.'
at wrapSafe (internal/modules/cjs/loader.js:915:16)
at Module._compile (internal/modules/cjs/loader.js:963:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at setup (/Users/beike/.npm/_npx/21543/lib/node_modules/@ant-design/codemod-v5/node_modules/jscodeshift/src/Worker.js:91:18)
at Object. (/Users/beike/.npm/_npx/21543/lib/node_modules/@ant-design/codemod-v5/node_modules/jscodeshift/src/Worker.js:45:3)
at Module._compile (internal/modules/cjs/loader.js:999:30)
de_modules/jscodeshift/src/Worker.js:45:3)
at Module._compile (internal/modules/cjs/loader.js:999:30)
/Users/beike/.npm/_npx/21543/lib/node_modules/@ant-design/codemod-v5/transforms/v5-props-changed-migration.js:203
if (nodePath.value?.value?.type === 'JSXExpressionContainer') {

Environment Info
antd 4.24.7
React 18.2.0
System macOS Big Sur 版本11.6
Browser 未涉及

div上的className 也被转换成rootClassName了

理论上,只有Drawer中的className才应该被转换成rootClassName,结果发现,Drawer里面有children时,children中多有元素的className 都被转换成rootClassName了
<Drawer mask={false} destroyOnClose open={visible} onClose={() => { setVisible(false); }} closable={false} placement="bottom" getContainer={false} rootClassName={Style.Drawer} height={168} > <div rootClassName={Style.CloseWrap}> <div rootClassName={Style.CloseContainer}> <div rootClassName={Style.CloseMask}> <div tabIndex={0} role="button" onClick={() => { setVisible(false); }} onKeyDown={() => {}} rootClassName={Style.Close} > <div rootClassName={Style.Plus}>+</div> </div> </div> </div> </div> <div rootClassName={Style.Root}> {map(item => ( <File {...item} key={prop('type')(item)} onClick={() => { if (prop('canEdit')(item)) { push( /${getFeatureBy('link')(STAGING_CALCULATION)}/pay-approve/${prop('type')(
item,
)}/${currentPayApproveId}, ); } else { message.warning(请先完成${prop('preTableName')(item)}); } }} /> ))(fileList)} </div> </Drawer>

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.