Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Svelte VSCode doesn't recognize scss prependData for variable resolution

I'm using Svelte with Rollup, and trying to get scss to work importing an alias, or with global import. My app compiles just fine. But the problem is, VSCode (or svelte extension, i don't know) doesn't recognize aliases, and says my file has errors. I can still run my app, but every single file looks red.

Attempt 1

I tried making an alias and importing via it

// jsconfig.json
{
  "compilerOptions": {
    "baseUrl": ".",
      "paths": {
        "src/*": [
          "src/*"
        ],
      }
  }
}
// App.svelte

<style lang="scss">
  @import "src/style/theme.scss"; // Error: Can't find stylesheet to import
</style>

Attempt 2

Tried prepending the import to every single file

// rollup.config.js

const config = {
  plugins: [
    svelte({
      preprocess: sveltePreprocess({
        scss: {
          prependData: `@import './src/style/theme.scss';`,
          includePaths: [path.resolve(__dirname)],
        },
      }),
  ]
}
// src/style/theme.scss

$black: #000000;
// App.svelte

<style lang='scss'>
  color: $black // Error: undefined variable
</style>

Both attempts work when compiling, but VSCode keeps saying there's errors. How do I stop VSCode from not understanding? I'd rather stick to Attempt 1, but any way I can solve this I'm fine.

like image 706
Gabriel d'Agosto Avatar asked May 21 '26 07:05

Gabriel d'Agosto


1 Answers

I was looking for a solution to the same problem and came across this post on the Svelte reddit https://www.reddit.com/r/sveltejs/comments/pmham1/sveltekit_how_to_set_up_global_scss_accessible_to/

Copy of the config from the top answer on that:

import preprocess from 'svelte-preprocess'
import path, { dirname } from 'path'
import { fileURLToPath } from 'url'
import adapter from '@sveltejs/adapter-node'

const filePath = dirname(fileURLToPath(import.meta.url))
const sassPath = `${filePath}/src/lib/style/`

const config = {
    // Consult https://github.com/sveltejs/svelte-preprocess
    // for more information about preprocessors
    preprocess: preprocess({
        scss: {
            prependData: `@import '${sassPath}_global-imports.scss';`
        }
    }),

   ....
}

export default config

This seems to have solved it for me.

like image 194
PinkFluffy1Corn Avatar answered May 24 '26 09:05

PinkFluffy1Corn



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!