蚂蚁设计因babel-plugin-import和create-react-app-rewired失败

时间:2019-01-26 21:11:08

标签: reactjs webpack create-react-app antd babel-loader

我正在遵循minimizing library size的Ant Design指南。

我已经安装了“ react-app-rewired”,“ customize-cra”和“ babel-plugin-import”。这是我完整的package.json文件:

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "antd": "^3.10.0",
    "react": "^16.5.1",
    "react-dom": "^16.5.1",
    "react-infinite-scroller": "^1.2.2",
    "react-scripts": "1.1.5"
  },
  "scripts": {
    "analyze": "source-map-explorer build/static/js/main.*",
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:3001",
  "devDependencies": {
    "babel-plugin-import": "^1.11.0",
    "customize-cra": "^0.2.10",
    "react-app-rewired": "^2.0.2",
    "source-map-explorer": "^1.6.0"
  }
}

我还有一个config-overrides.js文件:

const { override, fixBabelImports } = require('customize-cra');
module.exports = override(
    fixBabelImports('import', {
        libraryName: 'antd',
        libraryDirectory: 'es',
        style: 'css',
    }),
);

但是,“启动”命令失败,并显示以下错误:

/home/vedantroy/Programming/app_name/client/node_modules/customize-cra/index.js:49
  getBabelLoader(config).options.plugins.push(plugin);
                        ^

TypeError: Cannot read property 'options' of undefined
    at Object.config (/home/vedantroy/Programming/app_name/client/node_modules/customize-cra/index.js:49:25)
    at Object.webpack (/home/vedantroy/Programming/app_name/client/node_modules/lodash.flow/index.js:177:42)
    at Object.<anonymous> (/home/vedantroy/Programming/app_name/client/node_modules/react-app-rewired/scripts/start.js:22:15)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:266:19)

如何解决此错误/可能的原因是什么?我很确定我完全遵循了Ant Design的指南。

2 个答案:

答案 0 :(得分:1)

看起来您正在使用react-scripts@1.x,而您需要react-app-rewired@1.6.2更改以上版本并按照@中提到的步骤进行操作 https://www.npmjs.com/package/react-app-rewire-antd-theme

它将开始工作。

注意:customize-cra用于react-app-rewired@2.x

答案 1 :(得分:0)

您可以使用useBabelRc()的{​​{1}}功能,并且在.babelrc中,可以为antd编写babel配置。