create-react-app 按需加载antd组件

按antd官网进行配置,执行npm start 会报错The "injectBabelPlugin" helper has been deprecated as of v2.0.
解决方案如下:

//1
create-react-app my-app
//2
cd my-app
//3
npm install [email protected] --save-dev
//4
npm install babel-plugin-import --save-dev
//5
npm install antd-mobile --save

修改package.json

"scripts": {
  "start": "react-app-rewired start",
  "build": "react-app-rewired build",
  "test": "react-app-rewired test --env=jsdom",
  "eject": "react-scripts eject"
},

在项目根目录创建一个 config-overrides.js

const { injectBabelPlugin } = require('react-app-rewired');
module.exports = function override(config,env) {
    config = injectBabelPlugin(['import',{ libraryName: 'antd-mobile',style:'css'}],config)
    return config
}

npm run build 报如下错误
Module build failed: BrowserslistError: Unknown browser query dead
修改package.json 把 "not dead"删掉

"browserslist": [
    ">0.2%",
    "not dead",//把这一行删掉
    "not ie <= 11",
    "not op_mini all"
  ],

你可能感兴趣的:(create-react-app 按需加载antd组件)