I found the solution

I realized that the same thing I did for a Laravel Mix 5->6 upgrade worked for Vite too, as per this Github comment of mine.

The gist of it is: _foo.scss (with the exported variables) should be renamed to foo.module.scss, and rather than doing a destructured import of the variables from the scss file, the entire default needs to be imported, and then that can be const destructured.

So rather than doing:

import { primary } from './foo.module.scss';

You would need to do this instead:

import foo from './foo.module.scss';
const { primary } = foo;
🌐
Sass
sass-lang.com › documentation › js-api
Sass: sass | JS API
The legacy API has two entrypoints for compiling Sass to CSS. Each one can compile either a Sass file by passing in LegacyFileOptions or a string of Sass code by passing in a LegacyStringOptions. renderSync runs synchronously. It's by far the fastest option when using Dart Sass, but at the ...
🌐
GitHub
github.com › MakhBeth › js-to-scss
GitHub - MakhBeth/js-to-scss: Converts js objects, like configuration ones, in scss variables, ready to be injected inside your Sass engine via the data option
Converts js objects, like configuration ones, in scss variables, ready to be injected inside your Sass engine via the data option - MakhBeth/js-to-scss
Starred by 18 users
Forked by 2 users
Languages   JavaScript 100.0% | JavaScript 100.0%
🌐
GitHub
github.com › medialize › sass.js
GitHub - medialize/sass.js: Sass.js - API for emscripted libsass to run in the browser
Have a look at the Interactive Playground to play around with compiling SCSS to CSS in your browser. This is a convenience API for emscripted libsass (at v3.6.2). If you're looking to run Sass in node, you're probably looking for node-sass. Sass.js and node-sass should generate the same results.
Starred by 1.2K users
Forked by 136 users
Languages   JavaScript 74.0% | CSS 10.4% | C++ 8.0% | HTML 5.8% | Shell 1.8% | JavaScript 74.0% | CSS 10.4% | C++ 8.0% | HTML 5.8% | Shell 1.8%
Top answer
1 of 10
26

If you use webpack you can use sass-loader to exportvariables like:

$animation-length-ms: $animation-length + 0ms;

:export {
  animationMillis: $animation-length-ms;
}

and import them like

import styles from '../styles/animation.scss'    
const millis = parseInt(styles.animationMillis)

https://blog.bluematador.com/posts/how-to-share-variables-between-js-and-sass/

2 of 10
14

I consider my solution to be quite hokey; but it does work...

In my _base.scss I have some variables defined:

$menu_bg: rgb(45, 45, 45);
$menu_hover: rgb(0, 0, 0);

In a menu.scss I have:

@import "base";

#jquery_vars {
  .menu_bg {
    background-color: $menu_bg;
  }
  .menu_hover {
    background-color: $menu_hover;
  }
}

And in a handy page template:

<span class="is_hidden" id="jquery_vars">
  <span class="is_hidden menu_bg"></span>
  <span class="is_hidden menu_hover"></span>
</span>

Finally this allows in a nearby jQuery script:

var menu_bg = $('#jquery_vars .menu_bg').css("background-color");
var menu_hover = $('#jquery_vars .menu_hover').css("background-color");

This is so ugly my dad is wearing a bag on his head.

jQuery can pull arbitrary CSS values from page elements; but those elements have to exist. I did try pulling some of these values from raw CSS without creating the spans in the HTML and jQuery came up with undefined. Obviously, if these variables are assigned to "real" objects on your page, you don't really need the arbitrary #jquery_vars element. At the same time, one might forget that .sidebar-left nice-menu li is the vital element being use to feed variables to jQuery.

If someone has anything else, it's got to be cleaner than this...

🌐
npm
npmjs.com › package › js-to-scss
js-to-scss - npm
Converts js objects, like configuration ones, in scss variables, ready to be injected inside your Sass engine via the data option. Latest version: 1.1.0, last published: 2 years ago.
      » npm install js-to-scss
    
Published   Oct 28, 2023
Version   1.1.0
Author   Davide Di Pumpo
🌐
Perficient Blogs
blogs.perficient.com › 2025 › 01 › 17 › integrating scss with javascript
Integrating SCSS with JavaScript / Blogs / Perficient
January 17, 2025 - While you can’t access them directly in JS, you can output them as CSS custom properties: ... Integrating Syntactically Awesome Style Sheets with JavaScript broadens the horizons for web developers, enabling styles to be as dynamic and data driven as the content they represent.
Find elsewhere
🌐
npm
npmjs.com › package › json-to-scss
json-to-scss - npm
A small utility to convert js & json file(s) to scss/sass file(s).. Latest version: 1.6.2, last published: 5 years ago. Start using json-to-scss in your project by running `npm i json-to-scss`. There are 10 other projects in the npm registry ...
      » npm install json-to-scss
    
Published   Sep 18, 2020
Version   1.6.2
Author   Renaud Lapoële
🌐
npm
npmjs.com › package › sass-to-js
sass-to-js - npm
Library to pass Sass variables via CSS to JavaScript. Latest version: 2.0.0, last published: 4 years ago. Start using sass-to-js in your project by running `npm i sass-to-js`. There are 1 other projects in the npm registry using sass-to-js.
      » npm install sass-to-js
    
Published   Feb 20, 2022
Version   2.0.0
Author   Serg Hospodarets
🌐
CSS-Tricks
css-tricks.com › getting-javascript-to-talk-to-css-and-sass
Getting JavaScript to Talk to CSS and Sass | CSS-Tricks
August 26, 2020 - I use TailwindCSS in my pipeline ... to a theme object. The source for Tailwind config are json files, so those can be imported directly into js without any sass mumbo-jumbo ... Thanks for sharing. Where I’m stuck is going the other direction: accessing JavaScript variables from SCSS...
🌐
Blue Matador
bluematador.com › blog › how-to-share-variables-between-js-and-sass
How to Share Variables Between Javascript and Sass
Now, in Javascript, we just need to import the styles from the stylesheet, and parse an int out of the variable we exported! // js/animation.js import styles from '../styles/animation.scss' import CSSTransitionGroup from 'react-transition-group/CSSTransitionGroup' const millis = parseInt(s...
🌐
GitHub
github.com › lil5 › jss-to-scss
GitHub - lil5/jss-to-scss: Convert JSS objects to SCSS
Convert JSS objects to SCSS. Contribute to lil5/jss-to-scss development by creating an account on GitHub.
Author   lil5
🌐
GitHub
github.com › malyw › sass-to-js
GitHub - shospodarets/sass-to-js: Library to pass Sass variables ...
Import sass/_sass-to-js.scss library file: @import "sass-to-js/sass/sass-to-js"; After that you can pass any your Sass maps variables to util function sassToJs.
Starred by 38 users
Forked by 9 users
Languages   JavaScript 65.2% | SCSS 23.4% | HTML 11.4% | JavaScript 65.2% | SCSS 23.4% | HTML 11.4%
🌐
JSON Formatter
jsonformatter.org › css-to-scss
Best CSS to SCSS Converter
CSS to SCSS is very unique tool for convert CSS to SCSS and allows to download, save, share and print CSS data..
🌐
GitHub
github.com › sass › node-sass
GitHub - sass/node-sass: :rainbow: Node.js bindings to libsass · GitHub
Node-sass is a library that provides binding for Node.js to LibSass, the C version of the popular stylesheet preprocessor, Sass. It allows you to natively compile .scss files to css at incredible speed and automatically via a connect middleware.
Starred by 8.5K users
Forked by 1.3K users
Languages   C++ 82.0% | JavaScript 12.6% | C 2.0% | Shell 1.8% | M4 0.6% | Makefile 0.5%
🌐
GitHub
github.com › saviomd-beta › convert-scss-sass-to-css-in-js
GitHub - saviomd-beta/convert-scss-sass-to-css-in-js
Contribute to saviomd-beta/convert-scss-sass-to-css-in-js development by creating an account on GitHub.
Author   saviomd-beta