安装 babel-plugin-import 插件. 下面方法二选一, 都可以实现 antd 的按需加载 https://ant.design/docs/react/getting-started-cn .
一, 配置 webpack.config.JS 文件
- {
- test: /.jsx?$/,
- exclude: /(node_modules|bower_components)/,
- use: [{
- loader: 'babel-loader'
- }],
- options: {
- "plugins": [
- [
- "import", {
- "libraryName": "antd",
- "style": true
- }
- ]
- ]
- }
- }
二, 配置 babelrc 文件
- {
- "presets": ["env", "react"],
- "plugins": [
- "transform-runtime",
- "transform-object-rest-spread",
- [
- "import", {
- "libraryName": "antd",
- "style": true
- }
- ]
- ]
- }
来源: http://www.bubuko.com/infodetail-3356854.html