4

Is it possible to define a special mapping that enables PhpStorm (or other WebStorm based IDEs) to have the ability to find files located in SvelteKit's special $lib directory alias?

For example, in PhpStorm, I'm importing global styles like so:

<script context="module">
    import '$lib/global-styles.scss';
</script>

However, the IDE unfortunately displays "Cannot find declaration to go to" when attempting to navigate to that particular file:

Popup error displaying "Cannot find declaration to go to"

patricknelson
  • 977
  • 10
  • 16

3 Answers3

8

Seems the fix was reasonably easy (inspired by this answer). Just add a path alias to any JS file in the root of your project called .webstorm.js (filename isn't important):

// eslint-disable
System.config({
    "paths": {
        "$lib/*": "./src/lib/*",
    }
});

Now navigation to paths under $lib/* should "just work" ✨ automatically. Unfortunately I can't find documentation for this special WebStorm-specific System.config() workaround, the best I could find was this comment on Jetbrains' issue tracker.


Edit (May 2, 2023): Also, for TypeScript support, use the method suggested in This Answer by Tyrone which may have broader support outside of just Jetbrains IDEs

patricknelson
  • 977
  • 10
  • 16
6

I had this issue but resolved it (since I am using Typescript) by adding a paths entry in my ./tsconfig.json at the root of my project.

{
  // ...
  "compilerOptions": {
      "paths": {
          "$lib/*": ["src/lib/*"],
      }
   }
}
Tyrone Wilson
  • 4,328
  • 2
  • 31
  • 35
  • That's great, thanks Tyrone. That works for me in PhpStorm as well. I'll drop this into my answer and credit you! – patricknelson May 03 '23 at 02:38
  • 1
    p.s. Thanks for editing, my intent was to call attention to your answer. Updating it to actually link to your answer as well. – patricknelson May 03 '23 at 08:02
3

Try to add baseUrl in tsconfig.json of your project:

{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
    ...
    "baseUrl": "./src"
}
Midnight
  • 41
  • 2