官网给出的按需加载解决方案,先必须安装 antd和 babel-plugin-import
npm install antd --save
npm install babel-plugin-import --save-dev
因为antd默认引入样式是less,所以需要手动配置为CSS,配置方法如下:
运行 npm run eject暴露webpack.config.dev
和webpack.config.prod文件
第一种方法:在package.json
中配置,这种方法成功的前提是webpack
里query
下配置babelrc:true
"babel": {
"presets": [
"react-app"
],
"plugins": [
["import", {
"libraryName": "antd",
"libraryDirectory": "es",
"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' }],
]
}
}
]
}
]
}