ant-design在create-react-app中配置按需加载组件

时间:2022-07-20 19:27:35

配置方法

在官方找到两种配置方法:
方法1
ant-design在create-react-app中配置按需加载组件
文档地址1

方法2
ant-design在create-react-app中配置按需加载组件
文档地址2

在这里推荐使用第二种方法。
在使用第一种方法时因为修改

/* package.json */
"scripts": {
-   "start": "react-scripts start", + "start": "react-app-rewired start", - "build": "react-scripts build", + "build": "react-app-rewired build", - "test": "react-scripts test --env=jsdom", + "test": "react-app-rewired test --env=jsdom", }

在需要自定义配置时运行命令

yarn run eject

在运行start时会报如下错误

> document-design@0.1.0 start /home/moyu/React/document-design
> react-app-rewired start

module.js:559
    throw err;
    ^

Error: Cannot find module 'react-scripts/package.json'
    at Function.Module._resolveFilename (module.js:557:15)
    at Function.resolve (internal/module.js:18:19)
    at Object.<anonymous> (/home/moyu/React/document-design/node_modules/_react-app-rewired@1.5.0@react-app-rewired/scripts/utils/paths.js:24:11)
    at Module._compile (module.js:662:30)
    at Object.Module._extensions..js (module.js:673:10)
    at Module.load (module.js:575:32)
    at tryModuleLoad (module.js:515:12)
    at Function.Module._load (module.js:507:3)
    at Module.require (module.js:606:17)
    at require (internal/module.js:11:18)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! document-design@0.1.0 start: `react-app-rewired start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the document-design@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/moyu/.npm/_logs/2018-03-26T07_02_16_812Z-debug.log

具体原因未知,希望有知道的大牛解答~

所以在这里我换成了第二种配置方法曲线救国。