react 按需加载antd样式不生效问题

react 按需加载antd样式不生效问题

0.0792019.03.13 18:06:09字数 114阅读 5,607

官网给出的按需加载解决方案,先安装 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' }],
                ]
            }
         }
      ]
    }
  ]
}
原文地址:https://www.cnblogs.com/sexintercourse/p/15663758.html