create-react-app使用antd按需加载的样式无效问题的解决

2022-01-14,,,,

这篇文章主要介绍了create-react-app使用antd按需加载样式无效问题的解决,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

官网给出的按需加载解决方案,先安装 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按需加载的样式无效问题的解决.doc》

下载本文的Word格式文档,以方便收藏与打印。