Path aliases for imports in WebStorm

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

    You can define custom paths, so WebStorm/PhpStorm can understand your aliases. But make sure, they are identical with your aliases. Create file in your root directory and call it something like this: webStorm.config.js (any js file will be ok). Then configure your paths inside:

    System.config({
      "paths": {
        "components/*": "./src/components/*",
        "core/*": "./src/core/*",
        ...
      }
    });
    

    WebStorm/PhpStorm will recognize System as it's own module and will treat this file as configuration.

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

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

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

    For anyone struggling: path.resolve() must be called with "__dirname" first argument for Idea (Websorm) to be able to resolve the path correctly.

    Will work for Idea (Websorm):

    alias: {
        '@someAlias': pathLib.resolve(__dirname, 'path/to/directory')
    }
    

    Will not work for Idea (Websorm) (while still being valid webpack alias):

    alias: {
        '@someAlias': pathLib.resolve('path/to/directory')
    }
    
    0 讨论(0)
  • 2020-12-02 06:31

    Webstorm can't read webpack.config if module.exports return a function. For example

    module.exports = function (webpackEnv) {
      return {
        mode: isEnvProduction ? 'production' : isEnvDevelopment && 'development',
        ...
     }
    }
    

    Check your config file, maybe this cause you are a problem.

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