官網給出的按需加載解決方案,先安裝 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' }],
]
}
}
]
}
]
}