react引入antd样式失效

官网给出的按需加载解决方案,先必须安装 antd和 babel-plugin-import

npm install antd --save

npm install babel-plugin-import --save-dev

因为antd默认引入样式是less,所以需要手动配置为CSS,配置方法如下:

运行 npm run eject暴露webpack.config.devwebpack.config.prod文件

第一种方法:在package.json中配置,这种方法成功的前提是webpackquery下配置babelrc:true

"babel": {
    "presets": [
      "react-app"
    ],
    "plugins": [
       ["import", {
         "libraryName": "antd",
         "libraryDirectory": "es",
         "style": "css"
       }]
     ]
  }

第二种方法:在webpack.config.devwebpack.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' }],
                ]
            }
         }
      ]
    }
  ]
}

 

你可能感兴趣的:(react引入antd样式失效)