//项目创建
npx create-react-app projectname
1 yarn add react-app-rewired customize-cra
2 在项目根目录下创建一个config-overrides.js
修改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",
+ "test": "react-app-rewired test",
}
config-overrides.js:
// @符号
const path = require("path");
const {
override,
addWebpackAlias
} = require("customize-cra");
//按需加载组件
module.exports = override(
addWebpackAlias({
["@"]: path.resolve(__dirname, "src")
}),
);