官網(wǎng)給出的按需加載解決方案,先安裝 babel-plugin-import
因?yàn)閍ntd默認(rèn)引入樣式是less,所以需要手動(dòng)配置為CSS,配置方法如下:
第一種方法:在package.json
中配置,這種方法成功的前提是webpack
里query
下配置babelrc:true
, 這樣就會(huì)使用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 則默認(rèn)引入less
['import', { libraryName: 'antd', style: 'css' }],
]
}
}
]
}
]
}
至此,就算是成功完成按需加載引入樣式了