官網(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' }], ] } } ] } ]}至此,就算是成功完成按需加載引入樣式了
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持錯(cuò)新站長(zhǎng)站。
新聞熱點(diǎn)
疑難解答
圖片精選