Path aliases for imports in WebStorm

前端 未结 10 1939
日久生厌
日久生厌 2020-12-02 05:31

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

10条回答
  •  醉话见心
    2020-12-02 06:14

    [Deprecated answer. Starting since WS2017.2 Webstorm automatically parses and applies Webpack config (see @anstarovoyt comment)]

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

提交回复
热议问题