Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path aliases for imports in WebStorm

I use webpack path aliases for ES6 module loading.

E.g. If I define an alias for utils instead of something like
import Foo from "../../../utils/foo", I can do
import 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?

like image 759
Bogdan D Avatar asked Jan 22 '16 09:01

Bogdan D


People also ask

What are path aliases?

🎉 Path aliasing or aliases are preconfigured names used to replace long paths in files and resolve to certain directories on a codebase.

Do not import exactly from?

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.


2 Answers

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).

right click on folder

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 ...

like image 116
Jalil Avatar answered Sep 21 '22 15:09

Jalil


I managed to set up aliases for WebStorm 2017.2 within webpack like this:

enter image description here

like image 23
Tomasz Mularczyk Avatar answered Sep 25 '22 15:09

Tomasz Mularczyk