简体   繁体   中英

Ant Design Fails With babel-plugin-import and create-react-app-rewired

I am following Ant Design's guide for minimizing library size .

I have installed "react-app-rewired", "customize-cra", and "babel-plugin-import". This is my full package.json file:

{
  "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"
  }
}

I also have a config-overrides.js file:

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

However, the "start" command fails with the following error:

/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)

How do I fix this error/what are possible causes? I'm pretty sure I followed Ant Design's guide exactly.

Looks like you are using react-scripts@1.x for which you need react-app-rewired@1.6.2 Change the above version and follow the steps mentioned @ https://www.npmjs.com/package/react-app-rewire-antd-theme

It will start working.

Note: customize-cra is for react-app-rewired@2.x

你可以使用customize-cra useBabelRc()函数,在.babelrc中,你可以为antd编写babel config。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM