VSCode IntelliSense: How to resolve path aliases in SCSS module imports
Asked Answered
M

2

12

In VS Code I would like to be able to jump into other SCSS modules using path aliases as defined in tsconfig.json.

Our Webpack/Vite build setup works fine (e.g. via sass-loader/options/sassOptions/includePaths Webpack config). And I also know in PHP Storm this is possible by setting the given path to resolve as a Resource Directory.

However, so far I was not able to find a setting, a plugin or configuration that could make this work for VS Code IntelliSense.

Folder Structure

  • modules
    • components
      • Component.tsx
    • styles
      • styles1.module.scss
      • styles2.module.scss
  • tsconfig.json

styles1.module.scss

.class1 {
  display: flex;
}

Component.tsx - Import works fine

import styles1 from '@/styles/styles1.module.scss'; // import + intellisense works fine
import styles2 from '@/styles/styles2.module.scss';

styles2.module.scss - IntelliSense does not work

@import '@/styles/styles1.module.scss'; 

tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "modules",
    "paths": {
      "@/styles/*": ["styles/*"]
    }
  }
}

Any help appreciated! Thanks.

VS Code IntelliSense Error

Mugwump answered 23/2, 2022 at 10:31 Comment(2)
did you figure out how to solve this? I'm facing the same issueSudiesudnor
any luck to solve this?Carrousel
E
6

just use alias-tool and Path Autocomplete

https://marketplace.visualstudio.com/items?itemName=dgeibi.alias-tool https://marketplace.visualstudio.com/items?itemName=ionutvmi.path-autocomplete

.vscode/settings.json

{
    "alias-tool.mappings": {
        "@": "${folder}"
    },
    "path-autocomplete.pathMappings": {
         "@": "${folder}"
    }
}
Elbring answered 1/6, 2023 at 11:13 Comment(1)
You are awesome. This really works! I don't even need the path-autocomplete extension. I just installed the alias tool extension and added it to my VSCode json config. "alias-tool.mappings": { "@": "${folder}", "@scss": "${folder}/src/scss", "@assets": "${folder}/src/assets", },Odum
G
-1

I try serval ways:

  • remove .next and restart server
  • remove node_modules and install, restart server
  • Finally I just restart vscode and it work.
Girt answered 25/8, 2022 at 5:18 Comment(1)
url aliases are not exclusive to nextjsTweak

© 2022 - 2024 — McMap. All rights reserved.