Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global SCSS variables #232

Closed
daarxwalker opened this issue Jun 26, 2020 · 20 comments
Closed

Global SCSS variables #232

daarxwalker opened this issue Jun 26, 2020 · 20 comments

Comments

@daarxwalker
Copy link

daarxwalker commented Jun 26, 2020

Hey guys,
have a one little problem. I tried to have global SCSS variables. Compilation works, but extension seems not. I get it, when importing nothing, it doesn't know about variables.
I added this to rollup.config.js as argument to autoPreprocess():

const autoPreprocessConfig = {
	scss: { includePaths: ['src/styles'], data: '@import "theme";' },
	postcss: {
		plugins: [ require('autoprefixer') ],
	},
}

In svelte.config.js doesn't work, so that's why rollup.config.js.
I have file _theme.scss in included path, as I said, compilation works.
But when I use variables like this:

<style type="text/scss">
    .test {
        color: $color-primary;
    }
</style>

..it shows at the beggining of component this

The file cannot be parsed because script or style require a preprocessor that doesn't seem to be setup.
Did you setup a `svelte.config.js`?
See https://github.com/sveltejs/language-tools/tree/master/packages/svelte-vscode#using-with-preprocessors for more info.
@dummdidumm
Copy link
Member

dummdidumm commented Jun 26, 2020

Does the error occur only when using global variables, or always? If always, did you follow the instructions for SCSS? Especially the last point (setting node path) might be what you need to do.

@daarxwalker
Copy link
Author

daarxwalker commented Jun 26, 2020

Snímek obrazovky 2020-06-26 v 17 26 35

You mean this, right? I did it on the start of svelting.
Ad.: Only global SCSS variables

@daarxwalker
Copy link
Author

Really dunno what to do. Any idea?

@daarxwalker
Copy link
Author

Need something like global reference to variables, so extension will know about it + autocompleting.

@dummdidumm
Copy link
Member

You need to have the same preprocessor config setup in your svelte.config.js as in your rollup.config.js.

@daarxwalker
Copy link
Author

daarxwalker commented Jun 26, 2020

I did it. Still same warning. Same preprocess config in svelte.config.js && rollup.config.js (client && server)
Snímek obrazovky 2020-06-26 v 17 58 08
Snímek obrazovky 2020-06-26 v 17 58 19

@daarxwalker
Copy link
Author

Restarted lang-server, restarted vscode, nothing changed, still warning in componen.

@daarxwalker
Copy link
Author

Looks like the component can't reach variable.
Snímek obrazovky 2020-06-26 v 18 04 11

@blindfish3
Copy link

blindfish3 commented Jun 26, 2020

@daarxwalker - IIRC includePaths does not by definition make the variables available within all your SCSS code. It just provides SCSS specific paths from which imports can be resolved - e.g. to avoid having to hard-code full paths. So for example you might include 'node_modules' if you know you have dependencies there...

So I think you still need an @import for your dependencies inside your component <style> tag. I don't have time to confirm in docs yet; but that's what I'd check first ;)

@daarxwalker
Copy link
Author

daarxwalker commented Jun 26, 2020

I know. I tried without data and import, still same message. But it's really pretty bad, if I can't just inject variables to all components in one place. I can live with @import 'theme'; in all components, but it still doesn't work.

@dummdidumm
Copy link
Member

dummdidumm commented Jun 26, 2020

If your build runs fine, this is "just" an IDE intellisense error which we have to fix. So you can continue programming and ignore the error until we have come up with a solution.

@daarxwalker
Copy link
Author

daarxwalker commented Jun 26, 2020

I know, i know. But it's really annoying and I don't know, if I don't have to do something more in configs. Thanks anyway for guides.

@dummdidumm
Copy link
Member

I cannot reproduce this.

This is what I did:

  1. use svelte starter template
  2. npm i --D svelte-preprocess sass (note: not node-sass, to get around the annoying node environment thing)
  3. add theme.sccs inside src, content $bla: blue;
  4. enhance rollup to deal with it
  5. create svelte.config.js with this:
import autoPreprocess from 'svelte-preprocess';

export const preprocess = autoPreprocess({
  includePaths: ['src'],
});
  1. add lang="scss" to style tag in App.svelte, add @import './theme'; at the top and background: $bla; to main styling.

@daarxwalker
Copy link
Author

@dummdidumm

  • I'm using Sapper with TypeScript.

  • yarn add -D sass

  • src/styles/_theme.scss
    Snímek obrazovky 2020-06-27 v 20 21 23

  • rollup.config.js
    Snímek obrazovky 2020-06-27 v 20 19 55
    Snímek obrazovky 2020-06-27 v 20 19 38

  • svelte.config.js same as rollup.config.js
    Snímek obrazovky 2020-06-27 v 20 21 59

  • component
    Snímek obrazovky 2020-06-27 v 20 20 17

dummdidumm pushed a commit to dummdidumm/language-tools that referenced this issue Jun 28, 2020
Narrow down in which step the error occurs and try to extract better error messages.

sveltejs#86, sveltejs#232, sveltejs#129
@dummdidumm
Copy link
Member

You cannot use the new syntax inside the svelte.config.js. You need to write it like this:

const autoPreprocess = require('svelte-preprocess');

module.exports = {
  preprocess: autoPreprocess({
    scss: { includePaths: ['src/styles'], data: '@import "theme";' },
    postcss: { plugins: [require('autoprefixer')] }
  }),
};

This worked for me.

@blindfish3
Copy link

blindfish3 commented Jun 28, 2020

@daarxwalker

  • component
    Snímek obrazovky 2020-06-27 v 20 20 17

You still don't have an @import statement here

@daarxwalker
Copy link
Author

@dummdidumm You're right about svelte.config.js, warning disappear. What about autocomplete, still not working. Really need to have manual @import "theme"; in every component or it's in data prop just fine and problem with autocomplete is somewhere else?

@blindfish3 I know. I want it global. It's standard thing with SCSS.

@dummdidumm
Copy link
Member

Since the error went away I'm going to close this.

Autocomplete is not supported yet, please open another feature request for this.

dummdidumm added a commit that referenced this issue Jun 29, 2020
* (feat) better error diagnostics

Narrow down in which step the error occurs and try to extract better error messages.

#86, #232, #129

* (feat) show svelte.config.js errors

* cleanup

* tests

* fix error msg
@krisalcordo
Copy link

krisalcordo commented Mar 18, 2021

This worked for me:

svelte.config.js


module.exports = {
  preprocess: autoPreprocess({
    scss: {  prependData: `@import 'src/styles/main.scss';`},
    postcss: { plugins: [require('autoprefixer')] }
  }),
 #};

rollup.config.js

svelte({
            dev: !production, // run-time checks      
            // Extract component CSS — better performance
            css: css => css.write(`bundle.css`),
            hot: isNollup,
            preprocess: [
                autoPreprocess({
                    scss: { prependData: `@import 'src/styles/main.scss';`},
                    postcss: { plugins: [postcssImport()] },
                    defaults: { style: 'postcss' }
                })
            ]
        }),

App.svelte

<style global lang="scss">
</style>

If you want the errors in terminal to go away on rollup.config.js

svelte({
            dev: !production, // run-time checks      
            // Extract component CSS — better performance
            css: css => css.write(`bundle.css`),
            hot: isNollup,
            preprocess: [
                autoPreprocess({
                    scss: { prependData: `@import 'src/styles/main.scss';`},
                    postcss: { plugins: [postcssImport()] },
                    defaults: { style: 'postcss' }
                })
            ],
            onwarn: (warning, handler) => {
                const { code, frame } = warning;
                if (code === "css-unused-selector")
                    return;
        
                handler(warning);
            }
        }),

The coolest thing is my main.scss file can import partials.

@import 'resets';
@import 'border_box';
@import 'colors';
@import 'fonts';
@import 'forms';

Documentation here: https://github.com/sveltejs/svelte-preprocess/blob/main/docs/getting-started.md

@babakfp
Copy link

babakfp commented Aug 4, 2021

Hi
I'm having a great day, working with Svelte
Screenshot 2021-08-04 104314
Screenshot 2021-08-04 103939
Screenshot 2021-08-04 104035

@kjaonline

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants