通过react-app-wired和customize-cra添加Webpack别名

时间:2019-03-04 22:21:53

标签: reactjs webpack create-react-app

我正在尝试通过消除不必要的图标来缩小Ant Design库的大小。我在网上找到了该解决方案,https://github.com/ant-design/ant-design/issues/12011#issuecomment-423173228

该解决方案涉及像这样配置Webpack:

module.exports = {
  //...
  resolve: {
    alias: {
      "@ant-design/icons/lib/dist$": path.resolve(__dirname, "./src/icons.js")
    }
  }
};

我不知道该怎么做,因为我将react-app-rewiredcustomize-crababel-plugin-import一起使用,所以我的“ config-overrides.js”文件看起来像这样,与提供的示例完全不同。

const { override, fixBabelImports } = require('customize-cra');

module.exports = override(
    fixBabelImports('import', {
        libraryName: 'antd',
        style: 'css',
    }),
);

我正在使用以下依赖项:

"antd": "^3.14.1",
"babel-plugin-import": "^1.11.0",
"customize-cra": "^0.2.12",
"downshift": "^3.2.6",
"material-icons-react": "^1.0.4",
"react": "^16.8.3",
"react-app-rewired": "^2.1.0",
"react-dom": "^16.8.3",
"react-scripts": "^2.1.5",
"styled-components": "^4.1.3"

这看起来与Github问题中的示例非常不同。如何在我的“ config-overrides.js”文件中添加别名?

1 个答案:

答案 0 :(得分:1)

使用addWebpackAlias的插件customize-cra

/* global require, module, __dirname */
const { override, fixBabelImports, addWebpackAlias } = require('customize-cra')
const path = require('path')

module.exports = override(
  fixBabelImports('import', {
    libraryName: 'antd',
    libraryDirectory: 'es',
    style: 'css',
  }),
  addWebpackAlias({
    ['@']: path.resolve(__dirname, 'src')
  })
)