Babel-plugin-module-resolver: Teach Microsofts VS Codes autocompletion about where to find the modules

Created on 25 Oct 2016  ·  13Comments  ·  Source: tleunen/babel-plugin-module-resolver

You explain how to teach Atom or IntelliJ/WebStorm where to find the files specified with this plugin. It would be great, if you would also describe a way for Microsofts VS Code. Do you know how to achieve it in Code?

help wanted

Most helpful comment

Hello,

I gave it a try tonight and it worked !

I mapped my src directory to ~ so I can access all my top folders like this :

import DB from '~/lib/database'
import { User } from '~/domain/user'

1. Teach VS Code's Intellisense where to look at

So you can jump to the definition and have autocompletion. ✨

To do that, create a jsconfig.json file at the root of your project :

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "~/*": [
        "src/*"
      ]
    }
  },
  "exclude": [
    "node_modules"
  ]
}

2. Teach VS Code to autocomplete your paths

Install the extension Path Intellisense

This extension allows the autocompletion of the custom paths.

Configure the extension in your project's settings under .vscode/settings.json :

{
  "path-intellisense.mappings": {
    "~": "${workspaceRoot}/src"
  }
}

3. Teach Babel how to resolve your custom paths

The last step is to install and configure babel-plugin-module-resolver so the code compile!

Here is my .babelrc :

{
  "plugins": [
    [
      "module-resolver",
      {
        "alias": {
          "~": "./src"
        }
      }
    ]
  ]
}

4. Profit

image

All 13 comments

I'm not a user of VSCode, but I will try to find the information and add it in the readme, unless someone finds it first.

@datenreisender @tleunen you can use the Path Intellisense extenion with the "path-intellisense.mappings" option to get what you need.

@tleunen how to use autocompletion in IntelliJ/WebStorm?

@tangkunyin IntelliJ is described in the README and not in the scope of this issue.

@damonbauer The Path Intellisense extension helps with path names but not with the majority of Intellisense, e.g. with

import {someFunction} from 'utils/MyUtilFn';

the extension helps me to complete utils/MyUtilFn, but not to complete someFunction, does not support Cmd-click to jump to its definition (neither on filename nor on function) and not enable me to use rename symbol like on relative imports.

@damonbauer @datenreisender - I'm trying again to find a good solution with vscode. I did try the plugin Path Intellisense, but it seems it only supports for alias?

How would you setup a root directory without having to alias all the sub files/directories?

Hello,

I gave it a try tonight and it worked !

I mapped my src directory to ~ so I can access all my top folders like this :

import DB from '~/lib/database'
import { User } from '~/domain/user'

1. Teach VS Code's Intellisense where to look at

So you can jump to the definition and have autocompletion. ✨

To do that, create a jsconfig.json file at the root of your project :

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "~/*": [
        "src/*"
      ]
    }
  },
  "exclude": [
    "node_modules"
  ]
}

2. Teach VS Code to autocomplete your paths

Install the extension Path Intellisense

This extension allows the autocompletion of the custom paths.

Configure the extension in your project's settings under .vscode/settings.json :

{
  "path-intellisense.mappings": {
    "~": "${workspaceRoot}/src"
  }
}

3. Teach Babel how to resolve your custom paths

The last step is to install and configure babel-plugin-module-resolver so the code compile!

Here is my .babelrc :

{
  "plugins": [
    [
      "module-resolver",
      {
        "alias": {
          "~": "./src"
        }
      }
    ]
  ]
}

4. Profit

image

@jgoux Thanks, it work for me.

Thanks @jgoux!

Thanks, @jgoux, for describing the correct solution. I incorporated that into PR #246, so others will also find it more easily.

Thanks @jgoux !

@jgoux THANK YOU VERY MUCH BROTHER! YOU SAVED ME! 😍

how to resolve js files with extensions like .native.js, .web.js , .android.js and .ios.js in path intellisense

Was this page helpful?
0 / 5 - 0 ratings

Related issues

melloc01 picture melloc01  ·  7Comments

ViktorAksionov picture ViktorAksionov  ·  3Comments

kruyvanna picture kruyvanna  ·  4Comments

wlopz picture wlopz  ·  7Comments

kkomaz picture kkomaz  ·  7Comments