帝王谷资源网 Design By www.wdxyy.com

官网给出的按需加载解决方案,先安装 babel-plugin-import

因为antd默认引入样式是less,所以需要手动配置为CSS,配置方法如下:

第一种方法:在package.json中配置,这种方法成功的前提是webpack里query下配置babelrc:true, 这样就会使用babelrc文件中的配置

"babel": {
  "presets": [
   "react-app"
  ],
  "plugins": [
   [
    "import",
    {
     "libraryName": "antd",
     "style": "css"
    }
   ]
  ]
 }

第二种方法:在webpack.config.dev和webpack.config.prod中配置:

module: {
  strictExportPresence: true,
  rules: [
   {
    oneOf: [
     // Process JS with Babel.
     {
      test: /\.(js|jsx|mjs)$/,
      include: paths.appSrc,
      loader: require.resolve('babel-loader'),
      options: {
        plugins: [
          // 引入样式为 css
          // style为true 则默认引入less
          ['import', { libraryName: 'antd', style: 'css' }],
        ]
      }
     }
   ]
  }
 ]
}

至此,就算是成功完成按需加载引入样式了

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
create-react-app,antd按需加载,create-react-app,antd

帝王谷资源网 Design By www.wdxyy.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
帝王谷资源网 Design By www.wdxyy.com