I use webpack path aliases for ES6 module loading.
E.g. If I define an alias for utils
instead of something likeimport Foo from "../../../utils/foo"
, I can doimport Foo from "utils/foo"
The problem is that once I start using aliases, WebStorm looses track of the import and I'm left with warnings and no auto-completion.
Is there a way to instruct WebStorm to use such aliases?
🎉 Path aliasing or aliases are preconfigured names used to replace long paths in files and resolve to certain directories on a codebase.
The option “Do not import exactly from” allows you to tell the IDE that you don't want any imports that are from the specific path (e.g. @material-ui/core ) or path pattern (e.g. @material-ui/core/** ). The IDE will then use an alternative path if there is one.
Yes, there is.
In fact, Webstorm can't automatically parse and apply Webpack config, but you can set up aliases the same way.
You just have to mark the parent folder of "utils" (in your example) as a resource root (right-click, mark directory as / resource root).
We just managed to do with the following structure :
/src /A /B /C
We have A B and C folders declared as alias in Webpack. And in Webstorm we marked "src" as "Resource Root".
And now we can simply import :
import A/path/to/any/file.js
instead of
import ../../../../../A/path/to/any/file.js
while still having Webstorm correctly parsing and indexing all code, link to files, autocompleting and so on ...
I managed to set up aliases for WebStorm 2017.2 within webpack like this:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With