1

In vite project, I defined an env variable in .env:

VITE_THEME=1

And I have a variables.scss file to define a variable:

$theme-color: red;

I can use import.meta.env.VITE_THEME to get VITE_THEME in js, but now I want to use it in variables.scss to change the theme-color like:

$theme-color: import.meta.env.VITE_THEME == 1 ? red : yellow;

How can I do this?

2
  • 2
    Did you have a look at sass-loader documentation ? It might be what you're looking for. Commented Mar 22, 2022 at 9:11
  • You can't do that. SASS is a CSS preprocessor, which means that all SASS specific information disapears when you compile it to CSS. You can create custom class and add an element with js. Commented Mar 22, 2022 at 10:13

1 Answer 1

1

In this case, you should use a CSS-in-JS library. CSS is not a programming language, and you shouldn't just fetch some data with it. Alternatively, you can conditionally add a class to the body tag in JS and configure styles in CSS depending on that. There are ways to compile your SCSS so that it gets some data from a file, but it won't be reactive. If you want to only change an element, you could just conditionally apply classes with the style attribute too.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.