美文网首页
react 按需加载antd样式不生效问题

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

作者: 风之伤_3eed | 来源:发表于2019-03-13 18:06 被阅读0次

官网给出的按需加载解决方案,先安装 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' }],
                ]
            }
         }
      ]
    }
  ]
}

相关文章

网友评论

      本文标题:react 按需加载antd样式不生效问题

      本文链接:https://www.haomeiwen.com/subject/laitmqtx.html