rollup-plugin-sass icon indicating copy to clipboard operation
rollup-plugin-sass copied to clipboard

export sass variables for include in JavaScript

Open svdoever opened this issue 4 years ago • 4 comments

I try to use the :export construct as described in https://stackoverflow.com/questions/61517117/cannot-export-sass-variables-into-javascript?noredirect=1&lq=1 to be able to use values of SASS variables in my JavaScript but can't get it working:

@import "variables";

:export {

    // Export the color palette to make it accessible to JS
    some-light-blue: $some-light-blue;
    battleship-grey: $battleship-grey;
    // etc...

}

Should this work with rollup-plugin-sass, or is there another method to accomplish this?

svdoever avatar Jun 04 '20 21:06 svdoever

Struggling with this same issue. Did you ever get it resolved?

gloverab avatar Dec 17 '20 23:12 gloverab

Dying for this feature.

dangnelson avatar Feb 08 '21 21:02 dangnelson

This is my implementation, I don't know if you can use it

index.scss

:export {
  --font-size-primary: 14px;
  --color-border-primary: #3B4144;
}

test.js

import index from './index.scss'
console.log(index)

dist/src/test.es5.js

var index = {"--font-size-primary":"14px","--color-border-primary":"#3B4144"};

console.log(index);

duowb avatar Jan 26 '22 02:01 duowb

@duowb Nice! .. I will look into integrating the feature later this week, or if you want to open a pull request ..?

elycruz avatar Aug 16 '22 20:08 elycruz

As pointed out in #71 turns out the plugin supported this all along (lol): Tl;dr You need to use 'icss-utils', along with 'postcss', to process the output css and then return an object containing all exported key/value pairs to the plugin, from the 'processor' field:

https://github.com/elycruz/rollup-plugin-sass/blob/f4b2e6457ed52fb2b4ade58c9853ebf124396980/test/index.test.ts#L290-L315

elycruz avatar Sep 17 '22 18:09 elycruz