Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode can't find scss import in Svelte component if path contains $lib alias

Vite preprocess can handle $lib aliases inside component styles. I would like to use

<style lang="scss">
  @import '$lib/styles/mixins';

  ...
</styles>

which works as expected.

But VSCode with Svelte extension doesn't handle these import and show annoying lint error

Error: Can't find stylesheet to import.

Is there any way to fix it or mute error?

like image 481
farincz Avatar asked Oct 29 '25 17:10

farincz


1 Answers

I believe this issue happens in a monorepo.

To work around this problem, define a custom scss importer in the vitePreprocess preprocessor.

Your svelte.config.js file should look something like this:

import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { vitePreprocess } from '@sveltejs/kit/vite';

const dirname = path.dirname(fileURLToPath(import.meta.url));

/** @type {import('@sveltejs/kit').Config} */
export default {
  preprocess: vitePreprocess({
    style: {
      css: {
        preprocessorOptions: {
          scss: {
            importer: [
              (url) => {
                if (url.startsWith('$lib')) {
                  return {
                    file: url.replace(/^\$lib/, path.join(dirname, 'src', 'lib')),
                  };
                }
                return url;
              },
            ],
          },
        },
      },
    },
  }),

  // other options
};
like image 148
Alimo Avatar answered Oct 31 '25 12:10

Alimo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!