Path aliases for imports in WebStorm

前端 未结 10 1891
日久生厌
日久生厌 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:09

    This is answered in a comment but to save people digging into comments and link only information, here it is:

    As of WS2017.2 this will be done automatically. The information is here.

    According to this, webstorm will automatically resolve aliases that are included within the webpack.config in the root of the project. If you have a custom structure and your webpack.config isn't in the root folder then go to Settings | Languages & Frameworks | JavaScript | Webpack and set the option to the config you require.

    Note: Most setups have a base config which then call a dev or prod version. In order for this to work properly, you need to tell webstorm to use the dev one.

    0 讨论(0)
  • 2020-12-02 06:10

    For the record: in PHPSTORM, working with laravel mix, I managed to solve this by creating a webpack.config.js file separately like:

    const path = require('path')
    const webpack = require('webpack')
    
    module.exports = {
      ...
      resolve: {
        extensions: ['.js', '.json', '.vue'],
        alias: {
          '~': path.resolve(__dirname, './resources/assets/js')
        }
      },
      ...
    }
    

    And then importing it in the webpack.mix.js like:

    const config = require('./webpack.config')
    ...
    mix.webpackConfig(config)
    

    Make sure the webpack configuration file is pointed correctly in the configuration of the PhpStorm in: Settings > Languages & Frameworks > Javascript > Webpack

    0 讨论(0)
  • 2020-12-02 06:10

    Not right now, We were also using path aliases for the files in our react project. The import names were shorter but we lost a lot on static checking of webstorm as well as completion features.

    We later came up with a decision to reduce the code to only 3 levels of depth, as well a single level for the common parts. The path completion feature of webstom (ctrl + space) even helps reduce the typing overhead. The production build does not use longer names, so hardly makes any difference in final code.

    I will suggest please reconsider your decision about aliases. You loose semantic meaning of modules coming from node_modules and your own code, as well as referencing the alias files again and again to make sense of your code, is a much bigger overhead.

    0 讨论(0)
  • 2020-12-02 06:13

    add jsconfig.js on your project root

    {
      "compilerOptions": {
        "baseUrl": ".",
        "paths": {
          "~/*": ["./src/*"]
        }
      }
    }
    
    
    0 讨论(0)
  • 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 ...

    0 讨论(0)
  • 2020-12-02 06:18

    In PHPStorm (using 2017.2 currently), I have not been able to get webpack configs to work properly in regards to aliases.

    My fix involves using the "Directories" section of the main settings. I just had to mark each folder referenced by an alias as a sources root, then click the properties dropdown for each and specify the alias as a "Package prefix". This made everything link up for me.

    Not sure if the Directories section exists in WebStorm, but if it does, this seems to be a fool-proof method for getting import aliases working.

    0 讨论(0)
提交回复
热议问题