React create-react-app antd 按需加载

安装 babel-plugin-import 插件

yarn add babel-plugin-import –dev

创建 .babelrc

1
{
2
    "presets": [
3
        "react-app"
4
    ],
5
    "plugins": [
6
        ["import",{
7
            "libraryName":"antd",
8
            "libraryDirectory":"es",
9
            "style":"css"  // `style: true` 会加载 less 文件
10
        }]
11
    ]
12
}

运行 yarn run eject,将配置暴露出来

删除 package.json 内的 babel

在 config/webpack.config.js ,启用 babelrc 将 babelrc:false 改为 true。

重新启动项目


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!