It is very important to have consistent import statements in your react projects, and to preserve relative paths hell, ex. ../../../utils/smth.ts
looks much more ugly than ~/utils/smth.ts
.
Unfortunately, latest versions of typescript support this trick worse and worse (I don't know why, in fact).
@craco/craco
package
Using Steps for installation:
yarn add @craco/craco
Make file
craco.config.js
in root of projectPut text below to
craco.config.js
:const path = require('path'); module.exports = { webpack: { alias: { '~': path.resolve(__dirname, 'src'), }, }, };
Change react scripts in
package.json
to:"scripts": { "start": "craco start", "build": "craco build", "test": "craco test", "eject": "craco eject" }
Start project and check everything works:
yarn start
react-app-rewired
and customize-cra
Using Steps for installation:
yarn add react-app-rewired
yarn add customize-cra
In
config-overrides.js
add those lines:const { addWebpackAlias } = require('customize-cra'); const path = require('path'); //... in module.export of configuration section: addWebpackAlias({ '~': path.resolve(__dirname, './src'), // ... });
And font forget to add similar aliases to
tsconfig.json
:"paths": { "~/*": [ "./src/*" ], },