Giter Club home page Giter Club logo

Comments (13)

jgoux avatar jgoux commented on May 18, 2024 164

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

from babel-plugin-module-resolver.

datenreisender avatar datenreisender commented on May 18, 2024 8

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

from babel-plugin-module-resolver.

damonbauer avatar damonbauer commented on May 18, 2024 2

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

from babel-plugin-module-resolver.

tleunen avatar tleunen commented on May 18, 2024

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.

from babel-plugin-module-resolver.

tangkunyin avatar tangkunyin commented on May 18, 2024

@tleunen how to use autocompletion in IntelliJ/WebStorm?

from babel-plugin-module-resolver.

datenreisender avatar datenreisender commented on May 18, 2024

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

from babel-plugin-module-resolver.

tleunen avatar tleunen commented on May 18, 2024

@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?

from babel-plugin-module-resolver.

harriswan9a avatar harriswan9a commented on May 18, 2024

@jgoux Thanks, it work for me.

from babel-plugin-module-resolver.

guilhermepontes avatar guilhermepontes commented on May 18, 2024

Thanks @jgoux!

from babel-plugin-module-resolver.

datenreisender avatar datenreisender commented on May 18, 2024

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

from babel-plugin-module-resolver.

e1024kb avatar e1024kb commented on May 18, 2024

Thanks @jgoux !

from babel-plugin-module-resolver.

rommyarb avatar rommyarb commented on May 18, 2024

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

from babel-plugin-module-resolver.

kevinpatel20 avatar kevinpatel20 commented on May 18, 2024

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

from babel-plugin-module-resolver.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.