4

Just started working again with Visual studio code after years on PHPStorm/Webstorm

I've decided to take the transition just because of how lightweight VSCode is and because I don't want to rely on a paid service/having it on every computer since VSCode is almost everywhere and free.

I started fresh

Vite + Vue3

Now I've come across several issues with Imports CTRL+Click - goto reference Autocompletes

my Vite.config is as follows - to enable aliases

import { defineConfig } from "vite";
import { fileURLToPath, URL } from "url";
import vue from "@vitejs/plugin-vue";
import path from "path";
// https://vitejs.dev/config/
/// <reference types="vitest" />
export default defineConfig({
    resolve: {
        extensions: [".js", ".json", ".vue", ".scss", ".css"],
        fallback: {
            crypto: path.resolve("crypto-browserify"),
            stream: path.resolve("stream-browserify"),
        },
        alias: {
            "@": fileURLToPath(new URL("./src", import.meta.url)),
            img: path.resolve(__dirname, "./public/img"),
        },
    },
    plugins: [vue()],
    test: {},
    server: {
        port: 8080,
    },
    build: {
        sourcemap: false,
        minify: false,
        assetsDir: "chunks",
    },
    css: {
        preprocessorOptions: {
            scss: {
                additionalData: `@use  "sass:math"; @import "./src/assets/scss/v2/legacy.scss"; @import "./src/assets/scss/common.scss";`,
            },
        },
    },
});

Now, with vite config alone I can import using the "@" alias - but no intellisense is taking place, I can't autocomplete imports nor can I ctrl + click

After adding a jsconfig.json file

{
    "compilerOptions": {
        "target": "ESNext",
        "baseUrl": ".",
        "paths": {
            "@/*": ["src/*"]
        }
    }
}

I am now able to import my components using the "@" and also have full intellisense on them and can CTRL+click them BUT, now I've lost the ability to import node_modules - lost all intellisense on that

So, if I use my vite/jsconfig I can ctrl+click/have auto complete on "@" alias but I lost my node_module import capabilities

If I remove those vite.config alias configuration and remove jsconfig I get back node_module intellisense and lost my project's intellisense.

What am I missing here? please help me figure this out.

I've also removed any / every npm import extension just so that I can understand how this works

Liad Goren
  • 299
  • 1
  • 4
  • 18
  • You're using both Vite's and the paths configurations in your `jsconfig.json`. Sometimes these conflict with each other. Use One instead – Abdulla Nilam Dec 20 '22 at 09:33
  • using only one prevents certain autocomplete and intellisense features from working ALSO, it just doesn't work - seems like I need vite alias prop for the imports to actually work and I need jsconfig for the intellisense and auto complete to work but then I lose access to node_modules imports – Liad Goren Dec 20 '22 at 10:16
  • @AbdullaNilam If i take out jsconfig, I can import from nodemodules and the '@' import alias works - but I cannot "point + click" to go go files with the "@" import alias I also can't go to .vue files regardless if i take out vite alias - alias imports doesn't work at all – Liad Goren Dec 20 '22 at 10:25

1 Answers1

5

The problem that you have here because of jsconfig.json file.

The presence of jsconfig.json file in a directory indicates that the directory is the root of a JavaScript Project. The jsconfig.json file specifies the root files and the options for the features provided by the JavaScript language service (vscode).

Most of the time you don't need it, but there is some examples where u can use it like IntelliSense customization. examples

more details:

jsconfig.json is a descendant of tsconfig.json, which is a configuration file for TypeScript. jsconfig.json is tsconfig.json with "allowJs" attribute set to true and since there is no actual compilation is required for JavaScript. These attributes exist because jsconfig.json is a descendant of tsconfig.json (just)

So, not all options are the same here like target :

The target setting changes which JS features are downleveled and which are left intact. For example, an arrow function () => this will be turned into an equivalent function expression if target is ES5 or lower.
Changing target also changes the default value of lib.

With that being said, vscode IntelliSense can be effected by those changes. so if u remove it, everything will works as expected.

In other words target can effect IntelliSense on jsconfig.json.

For your case, you just need to add it as following:


jsconfig.json

{
   "compilerOptions": {
      "baseUrl": ".",
      "paths": {
        "@/*": ["src/*"]
      }
   }
}

vite.config.js

alias: {
  '@/': path.resolve(__dirname, './src')
}

For more reading about jsconfig.json for vscode: here

Sohaib Alqasem
  • 226
  • 1
  • 6
  • 1
    HOLY HELL, This is the one! I just removed the Target and everything just works! One last issue, though I'm totally gonna vote this as the correct answer I can now ctrl+click and import using the "@" alias, so thank you; yet I still can't ctrl+click to go to .vue files Any idea on that specifically ? – Liad Goren Dec 21 '22 at 07:35
  • Sounds great Liad! Try to restart vscode if the issue still exist please explain more, like show me the (import statement & current jsconfig file ) that u are trying to preform ctrl+click on. Seems to work fine on my vscode (ctrl+click to go to .vue files). – Sohaib Alqasem Dec 21 '22 at 17:02
  • Final vite.config: ``` resolve: { extensions: [".js", ".json", ".vue", ".scss", ".css"], fallback: { crypto: path.resolve("crypto-browserify"), stream: path.resolve("stream-browserify"), }, alias: { "@": path.resolve(__dirname, "./src"), img: path.resolve(__dirname, `./public/img`), }, }, ``` and jsconfig.json ``` { "compilerOptions": { "baseUrl": ".", "paths": { "@/*": ["src/*"] } } } ``` using the Volar extension. And still, ctrl+click does not work for vue files, whether i include the vue extension on import or not. using @ alias – Liad Goren Dec 22 '22 at 05:49
  • First, let us agree that **baseUrl** must be specified if "paths" is specified, to understand it more u can read this [baseUrl](https://www.typescriptlang.org/tsconfig#baseUrl). Now based on that lets tallk about **paths** path mapping to be computed relative to **baseUrl** option. So, u need to get those right with your folder structure. and that's might effect your absolute & relative imports I believe. You can go over these things and see if u are missing something here. – Sohaib Alqasem Dec 22 '22 at 17:01
  • Can u share your `folder structure` & `import-statement` that have this issue (and in what file this import statement) if `ctrl+click` issue still exist !? – Sohaib Alqasem Dec 22 '22 at 17:03
  • Things not clear at my side cause I can't reproduce the same behavior as yours. Is works fine (On import statement I can `ctrl+clcik` and go to .vue file). – Sohaib Alqasem Dec 22 '22 at 17:09
  • folder structure: src/common/components/folderName/componentName once I import componentName from "@/common/components/folderName/componentName.vue" I can't ctrl+click my vite config and jsconfig are as you've provided in your magnificent answer. So I'm really not sure what went wrong here – Liad Goren Dec 25 '22 at 06:56
  • rewarded you with the bounty regardless. You've been a tremendous help so far – Liad Goren Dec 25 '22 at 06:57
  • @SohaibAlqasem WHAAAAT…it worked! After hours of scouring the net, thank you! Can you share your project starter setup for vite + react. I am having trouble setting up es-lint – bishop Jan 05 '23 at 07:37
  • For anyone who is having trouble with autocomplete not working for alias import i.e. @/components/, your baseUrl must be properly specified with the correct base path. For me, it was `baseUrl: '.'` and I changed it to `baseUrl: './'`and it started giving me proper suggestions at the import path. Inside - tsconfig.json – Chipsy Apr 12 '23 at 16:02