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?
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
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With