The target
Can have vscode recognize @ paths and give path hints
steps
- Created in the project root directory
jsconfig.json
The configuration file - Add the following configuration to the configuration file
Jsconfig. Json in:
{
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@/*": ["src/*"]
}
}
}
Copy the code
summary
VSCode automatically reads the configuration in jsconfig.json to let VSCode know that @ is SRC
Use Craco to configure the path alias
The target
Configure the @ path alias so that the scaffold tool can recognize @
background
Create-react-app (CRA) hides all engineering configurations in the react-scripts package, so no configuration information or configuration files are seen in the project.
If you want to change the default CRA configuration, there are several options:
- 【 recommended 】 Use third-party libraries to modify, for example,
@craco/craco
- By performing
yarn eject
Command, releasereact-scripts
To the project (note: this operation is irreversible!!)
What is craco
Create React App Configuration Override, an easy and comprehensible configuration layer for create-react-app
Craco is a custom configuration tool for create-React-app.
Making the address
Configuration in ANTD
Procedure for using crACO
-
The installation package. npm i -D @craco/craco
-
In the project root directory, create the configuration file: craco.config.js. Custom changes can be made in the configuration file.
craco.config.js
Configuring a Path Alias
const path = require('path') module.exports = { webpack: { alias: { '@': path.join(__dirname, 'src') } } } Copy the code
-
Modify script commands in package.json
Package. The json:
// Change the start/build/test command to craco mode "scripts": {"start": "craco start", "build": "craco build", "test": "craco test", "eject": "react-scripts eject" },Copy the code
-
In code, the absolute path to the SRC directory can be represented by @
-
Restart the project for the configuration to take effect