VSCode Intellisense does not work with webpack + alias - visual-studio-code

I have a project using babel alias:
resolve: {
alias: {
vue: 'vue/dist/vue.js',
'#cmp': resolve('src/components'),
'#service': resolve('src/services'),
'#scss': resolve('src/assets/styles'),
}
}
and a component with:
import someService from '#service/some'
And the Intellisense does not work.
with:
import someService from '../../../../service/some'
It does.
Any suggestions?

Try creating a jsconfig.json and configuring the paths compiler options
{
"compilerOptions": {
"baseUrl": ".",
"module": "commonjs",
"paths": {
"#cmp/*": ["./src/components/*"]
}
}
}
You can find more information about paths and other compiler options here

This worked for me, as suggested here (I wanted #/ to resolve to ./src/):
{
"compilerOptions": {
"target": "es2017",
"allowSyntheticDefaultImports": false,
"baseUrl": "./",
"paths": {
"#/*": ["src/*"],
}
},
"exclude": ["node_modules", "dist"]
}
Minimal version, but I'd leave exclude too:
{
"compilerOptions": {
"baseUrl": "./",
"paths": {
"#/*": ["src/*"]
}
},
}

Just to add to Matt's answer, in a TypeScript project you don't need to create a jsconfig.json file, you can simply add it to your tsconfig.json file and vscode picks it up.
{
"compilerOptions": {
...
"paths": {
"#MyAlias/*": ["./myDirectrory/*"]
}
}
}

For those who want to get IntelliSense working for resolving directly from src like with your webpack config.
Ie.
import Requests from 'utils/requests';
and that file lives at src/utils/requests.js
...
# jsconfig.json
{
"compilerOptions": {
"baseUrl": "./",
"module": "commonjs",
"target": "es6",
"paths": {
"*": ["src/*"]
}
}
}
# webpack.config.js
resolve: {
symlinks: false,
modules: [
path.resolve('./src'),
'node_modules',
],
},

Related

Nex.js Module path aliases not clickable in vscode

I'm trying to get module path aliases clickable in vscode
// Ctrl+click Working fine
import Layout from "src/components/layout/Layout";
// Not working
import SEO from "#components/SEO";
My JSconfig.json file
{
"compilerOptions": {
"jsx": "react",
"baseUrl": ".",
"paths": {
"#components/*": ["/src/components/*"],
"#sections/*": ["/src/sections/*"],
"#assets/*": ["/src/assets/*"]
}
}
}
I have tried everything without success
Here is the solution
{
"compilerOptions": {
"jsx": "react",
"baseUrl": "./",
"paths": {
"#components/*": ["./src/components/*"],
"#sections/*": ["./src/sections/*"],
"#assets/*": ["./src/assets/*"]
}
}
}
Had same issue with 'Go to definition'. And on the end I found it was not working for me because I had both tsconfig.json and jsconfig.json in the project.
Seems when tsconfig.json is present, alias setting from jsconfig.json are ignored.
As I want gradually convert stuff into Typescript, but have lot of .js files I kept tsconfig.json, and added "**/*.js" to the "include" list, so it sees also .js files. Now aliases in vscode are clickable.
tsconfig.json:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"#/components/*": [
"components/*"
]
}
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
"**/*.js"
]
}

VSCode removing React from imports on save

after switching to vscode 1.68.1
every time on saving the file its automatically removing the React from the import
at first i though this could be an eslint issue as i am using eslint for formatting but after removing eslint plugin issue remains the same
here is my settings.json
{
"eslint.alwaysShowStatus": true,
"editor.formatOnSave": true,
"files.eol": "\r\n",
"editor.codeActionsOnSave": {
"source.fixAll": true,
"source.organizeImports": true
},
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
"prettier.arrowParens": "avoid",
"prettier.embeddedLanguageFormatting": "off",
"prettier.enable": false,
"eslint.format.enable": false,
"[css]": {
"editor.defaultFormatter": "aeschli.vscode-css-formatter"
},
"[html]": {
"editor.defaultFormatter": "vscode.html-language-features"
},
"[json]": {
"editor.defaultFormatter": "vscode.json-language-features"
},
"[jsonc]": {
"editor.defaultFormatter": "vscode.json-language-features"
}
}
and here is the .eslintrc
module.exports = {
"env": {
"browser": true,
"es6": true
},
"extends": "airbnb",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": [
"react", "react-hooks"
],
"rules": {
"linebreak-style": ["error", "windows"],
"react/forbid-prop-types": 0,
"react/prop-types": 0,
"max-len": ["error", { "code": 220 }]
},
"settings": {
"import/resolver": {
"node": {
"moduleDirectory": ["node_modules", "src/"]
}
}
}
};
Try adding these rules in eslintrc:
{
"rules": {
"react/jsx-uses-react": 0,
"react/react-in-jsx-scope": 0
}
}
OR
Try adding "jsx": "react" to your jsconfig:
{
"compilerOptions": {
"baseUrl": ".",
"jsx": "react",
"paths": {
"*": ["*", "src/*"]
}
}
}
This helps VS-Code understand that React is implicitly referenced by the jsx elements in your file.
I had the same problem in multiple projects, after adding these rules, solves the problem for me.
VS Code expects that every project declares a tsconfig.json or jsconfig.json and if none is provided, it defaults it to
{
"compilerOptions": {
"jsx": "react"
}
}
This effectively means that if you have React imports in your code, they will be preserved.
To adopt the latest react 17+ standard with the _jsx transform, the environment type needs to be react-jsx.
{
"compilerOptions": {
"jsx": "react-jsx"
}
}
{
// ...
"rules": {
// ...
"react/jsx-uses-react": "on",
"react/react-in-jsx-scope": "on"
}
}
Add these rules to your eslint config
add type on your tsx file, to prevent removed automatically by vscode
import React from 'react';
type _react = typeof React
.eslint
{
rules: {
'react/jsx-uses-react': 0, // ignore validation must include react scope in jsx/tsx
'react/react-in-jsx-scope': 0, // ignore validation must include react scope in jsx/tsx
'no-unused-vars': 'off', // disable default eslint unused variable
'#typescript-eslint/no-unused-vars': [ // ignore validation variable with prefix _
'warn',
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
caughtErrorsIgnorePattern: '^_'
}
]
}
}
I also had the same problem, when removing the extensions I found it was vscode, downgrading to version 1.67.2 solved it
Try removing the "source.organizeImports": true line from your settings, or setting it to false.
I think VS Code thinks the React import is unnecessary because its only used implicitly (which can be a problem if your compiler requires an explicit import to bundle correctly).

VS Code, React project, intellisense when using absolute import paths

In my react project when I use in:
//package.json
"name": "app"
then intellisense is not working for absolute path imports
//SomeComponent.js
import {StorageKeys} from 'app/Constants' // inellisense don't work
import {StorageKeys} from '../../Constants' // intellisense works
Tried some suggestions regarding "jsconfig.json" and setting compiler's base url but with no success.
Any Idea?
{
"compilerOptions": {
"target": "ES6",
"baseUrl": "./",
"paths" : {
"app/*" : ["./*"]
}
},
"exclude": [
"node_modules",
"**/node_modules/*"
]
}

VSCode intellisense jsconfig.json exclude jest types for src folder

how to exclude jest globals in intellisense from src/**/* ?
I want jest types to be seen only in test/**.spec.js files
current configuration:
{
"compilerOptions": {
"target": "es5",
"module": "commonJS",
"baseUrl": "./src",
},
"exclude": ["node_modules"],
"include": ["src/**/*", "typings/index.d.ts"],
"typeAcquisition": {
"exclude": [
"jest"
]
}
}
after that i still see jest types in intellisense

VS Code not recognising module aliases in Expo project

I've set up import aliases in my Expo project. It's compiling fine however VS Code doenst recognise the import:
In tsconfig.json
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"jsx": "react-native",
"lib": ["dom", "esnext"],
"moduleResolution": "node",
"noEmit": true,
"skipLibCheck": true,
"resolveJsonModule": true,
"baseUrl": "./",
"paths": {
"home": ["./"]
}
}
}
In babel.config.js
module.exports = function(api) {
api.cache(true);
return {
presets: ["babel-preset-expo"],
plugins: [
[
"module-resolver",
{
alias: {
home: "./"
}
}
]
]
};
};
The import in the file works: import Thing from "home/src/thing";
I thought this would make VS Code aware of the alias but it appears not to do anything
In jsconfig.json:
{
"compilerOptions": {
"target": "es2017",
"allowSyntheticDefaultImports": false,
"baseUrl": "./",
"paths": {
"home/*": ["./*"]
}
},
"exclude": ["node_modules", "dist"]
}