问题
How to set import shortcuts/aliases in create-react-app? From this:
import { Layout } from '../../Components/Layout'
to this:
import { Layout } from '@Components/Layout'
I have a webpack
4.42.0 version.
I don't have a webpack.config.js file in the root directory. I've tried to create one myself with this code inside:
const path = require('path')
module.exports = {
resolve: {
alias: {
'@': path.resolve(__dirname, 'src/'),
}
}
};
But it doesn't seem to work. I've seen the NODE_PATH=.
variant in .env
file. But I believe, it is deprecated - better not to use. And also, I have a posstcss.config.js
file. Because I've installed the TailwindCss and I import the CSS library there. I've tried to paste the same code there, but it also didn't work.
回答1:
In order for webpack's aliases to work, you need to configure the default webpack.config.js
of create-react-app
.
The official way is to use the eject script.
But the recommended way is to use a library without ejecting, like craco.
After following the installation, add craco.config.js
to your root folder with the desired configuration.
My example:
// craco.config.js
const path = require(`path`);
const alias = require(`./src/config/aliases`);
const SRC = `./src`;
const aliases = alias(SRC);
const resolvedAliases = Object.fromEntries(
Object.entries(aliases).map(([key, value]) => [key, path.resolve(__dirname, value)]),
);
module.exports = {
webpack: {
alias: resolvedAliases,
},
};
Where aliases.js
is a helper function:
const aliases = (prefix = `src`) => ({
'@atoms': `${prefix}/components/atoms`,
'@molecules': `${prefix}/components/molecules`,
'@organisms': `${prefix}/components/organisms`,
'@templates': `${prefix}/components/templates`,
'@components': `${prefix}/components`,
'@config': `${prefix}/config`,
'@enums': `${prefix}/enums`,
'@hooks': `${prefix}/hooks`,
'@icons': `${prefix}/components/atoms/Icons`,
'@styles': `${prefix}/styles`,
'@utils': `${prefix}/utils`,
'@state': `${prefix}/state`,
'@types': `${prefix}/types`,
'@storybookHelpers': `../.storybook/helpers`,
});
module.exports = aliases;
In addition, you should add jsconfig.json
file for path IntelliSense in VSCode, see followup question.
Now such code with InteliSense will work:
import {ColorBox} from '@atoms';
import {RECOIL_STATE} from '@state';
回答2:
Simplest way to archive this follow below steps. ( same way as @deniis-wash showed as but in simple form)
- Installation - install and setup CRACO.
yarn add @craco/craco
# OR
npm install @craco/craco --save
- Create a
craco.config.js
file in the root directory and configure CRACO:
/* craco.config.js */
const path = require(`path`);
module.exports = {
webpack: {
alias: {
'@': path.resolve(__dirname, 'src/'),
'@Components': path.resolve(__dirname, 'src/components'),
'@So_on': path.resolve(__dirname, 'src/so_on'),
}
},
};
- Update the existing calls to
react-scripts
in the scripts section of yourpackage.json
file to use the craco CLI:
/* package.json */
"scripts": {
"start": "craco start",
"build": "craco build"
"test": "craco test"
}
Done. setup is completed.
Now lets test it.
// Before
import Button from "./form/Button"
import { Layout } from '../../Components/Layout'
// After
import Button from "@/form/Button"
import { Layout } from '@Components/Layout'
Documentation Craco
Thank you. :)
回答3:
It is finally possible with Create React App v.3
Just put:
{
"compilerOptions": {
"baseUrl": "src"
},
"include": ["src"]
}
into jsconfig.json
or tsconfig.json
if you use Typescript
Here is wonderful article about this.
来源:https://stackoverflow.com/questions/63067555/how-to-make-an-import-shortcut-alias-in-create-react-app