23
loading...
This website collects cookies to deliver better user experience
../
in the imports of those files.import ExampleComponent from '../../../../../../src/components/ExampleComponent/'
../
brings some pain when we need to maintain our code, among them, the rework of fixing the relative path every time we need to change the location of an imported file or import file.import ExampleComponent from '~/components/ExampleComponent/'
~
prefix was set as an absolute path to a directory in our project, in this case the directory is the src
folder. That way, no matter what folder our file is in, when we use the ~/
prefix, we will always be importing the absolute path of the src
folder.yarn add -D react-app-rewired customize-cra
yarn add -D babel-plugin-root-import
config-overrides.js
. He will be responsible for establishing the root folder of our project.const { addBabelPlugin, override } = require('customize-cra')
module.exports = override(
addBabelPlugin([
'babel-plugin-root-import',
{
rootPathSuffix: 'src',
},
])
)
jsconfig.json
. He will be responsible for making VSCODE understand the custom paths.{
"compilerOptions": {
"baseUrl": "src",
"paths": {
"~/*": ["*"]
}
}
}
{
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-scripts eject"
}
}
react-scripts
with react-app-rewired
with the exception of the eject
script.~/
at import time.import ExampleComponent from '~/components/ExampleComponent/'