1.装包
yarn add react-app-rewired customize-cra
2.创建目录
在项目根目录下创建一个config-overrides.js
const path = require("path");
const {
override,//重写webpack配置
addWebpackAlias//区别名
} = require("customize-cra");
module.exports = override(
addWebpackAlias({
["@"]: path.resolve(__dirname, "src")
}),
);
3.修改package.json
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-scripts eject"
},
4.添加一个paths.json文件(ts版)
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["src/*"],
"@components/*": ["src/components/*"],
"@pages/*": ["src/pages/*"],
"@utils/*": ["src/utils/*"]
}
}
}
5.在tsconbfig.json中怎加extends扩展文件
{
//第一行时增加的代码
"extends": "./paths.json",
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": [
"src"
]
}