umijs / babel-plugin-import

Modularly import plugin for babel.
3.16k stars 405 forks source link

Be compatible with React.lazy and Loadable Components. #300

Open jinliming2 opened 5 years ago

jinliming2 commented 5 years ago

Can this plugin compatible with React.lazy and Loadable Components?

import { Layout, Menu } from 'antd';
↓ ↓ ↓ ↓ ↓ ↓
const Layout = loadable(() => import('antd/es/layout'));
const Menu = loadable(() => import('antd/es/menu'));

Or something like:

const { Layout, Menu } = loadable(() => import('antd'));
↓ ↓ ↓ ↓ ↓ ↓
const Layout = loadable(() => import('antd/es/layout'));
const Menu = loadable(() => import('antd/es/menu'));
KrishnaPG commented 4 years ago

Looking for the same. For some reason, React.lazy does not seem to work with with antd. I am using CRA and unable to make the antd components load lazily no matter how much I tried. Wish they were compatible.

KrishnaPG commented 4 years ago

Was able to make the antd component load lazily as below:

const Card = React.lazy(() => import(/* webpackChunkName: "antSidebars", webpackPreload: true */ 'antd/lib/card/index'));
const Menu = React.lazy(() => import(/* webpackChunkName: "antSidebars", webpackPreload: true */ 'antd/lib/menu/index'));
const MenuItem = React.lazy(() => import(/* webpackChunkName: "antSidebars", webpackPreload: true */ 'antd/lib/menu/MenuItem'));
const SubMenu = React.lazy(() => import(/* webpackChunkName: "antSidebars", webpackPreload: true */ 'antd/lib/menu/SubMenu'));
tianwawawa commented 2 years ago

什么原因呢 有其他解决办法吗