-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Webpack Alias #2188
Comments
You can put
in your Hope it helps! |
@gaearon Thanks ! It works like a charm . I just to want to clarify to community how to import files after adding Assuming you want to import Instead of import Post from '../components/Post'
class Dashboard //... blahblahbla It will be : import Post from 'components/Post' // 👈🏼 <----
class Dashboard //... blahblahbla |
The problem with using NODE_PATH is, if you want to restructure your project, you have to update all your import statements. Whereas if you can use webpacks aliasing, all you have to do is update the alias. Also '@components' is more convenient than 'src/js/components'. |
In addition to what @ashleyphillips-dev2rights is saying, how can we handle multiple aliases for shared directories like shared components, directories etc using NODE_PATH ? For example, with webpack I can have aliases set as:
which makes imports much cleaner, for example:
|
Would be nice if this is supported. |
@SujitGadkari Does this actually work with CRA? I haven't been able to get it working. |
@danyim No it doesn't. Currently there is no way of providing custom webpack config for CRA unless you eject the app. One possible solution is to allow users to provide custom config that gets merged with base CRA webpack config before execution, but I believe @gaearon and his team wants to avoid opening up webpack config for CRA's flexibility in choosing/replacing build tool in future. I do still feel that ability to add aliases is useful enough to implement somehow. |
Right, It's time to eject it.
alias: {
+ '@': path.resolve(__dirname, '../src'),
// ...
}, And you can do this: import Chart from '@/components/Chart'; |
@Runrioter Ejecting is exactly what we are trying to avoid. I understand using something like create-react-app (withuot ejecting) will never suit all use cases, but I would consider aliasing a very common thing which shouldn't mean having to eject to be able to do it. Not ejecting has lots of advantages we want to keep so I prefer to not eject where possible, and aliasing is something that most projects can benefit from. |
There is already an alias in CRA for
Where would this change go if we want to add this behavior to an unejected CRA? |
Maybe add |
I struggled to this issue also and found out the article very details (as @gaearon mentioned) at https://medium.com/@ktruong008/absolute-imports-with-create-react-app-4338fbca7e3d |
I guess I have to go change all the import statements in my project, or give up on migrating it to CRA. It's probably not worth the time-sink. If I must use eject, I may as well just keep the existing structure. Is there no way to do aliases through babel or something? |
I agree, this would be a great addition |
I have ejected the application and configured the resolve.alias without success @gaearon |
We don't provide support once you eject, sorry! I'm going to lock this issue to prevent further bumps because there's so many active parties. If anyone has issues, please file a new issue. |
Hi, I'm wondering if create-react-app supports aliasing. I thought that certain top-level folders can be aliased to prevent having a lot of relative paths. Cheers!
The text was updated successfully, but these errors were encountered: