30

Background

Consider the following _variables.scss file:

/* Define all colours */
$white:    #fff;
$black:    #000;
$grey:     #ccc;
// etc...

// Export the color palette to make it accessible to JS
:export {
    white: $white;
    black: $black;
    grey: $grey;
    // etc...
}

The purpose of the above code is to make the SCSS variables available to Javascript by means of importing like so:

import variables from 'variables.scss';

See a more detailed description here.

The Problem

Now consider the following template (I have used as Vue.js template as an example but this is relevant to numerous frameworks):

<!-- Template here... -->

<style lang="scss" scoped>

    // Import Partials
    @import "~core-styles/brand/_variables.scss";
    @import "~core-styles/brand/_mixins.scss";

    // Styles here...

</style>

In the above example I have used the scoped attribute as this demonstrates the worst case scenario for the upcoming problem, but even without scoped the problem is still relevant.

The above SCSS will compile to something along the lines of:

[data-v-9a6487c0]:export {
    white: #fff;
    black: #000;
    grey: #ccc;
    // etc...
}

In addition, with the scoped attribute, this will repeat every time _variables.scss is imported into a template, and can potentially result in additional redundant code. In some cases, for large applications (many components and a large colour palette), this can literally add 000's of lines of completely redundant code.

My Question

Is there a way to export the SCSS variables to Javascript without exporting them to CSS?

Potential (dirty) Solution

I am ideally trying to avoid a solution of having a separate file named, for example, _export.scss where its purpose is simply to export all SCSS variables to JS, but it is excluded from all CSS builds...

Just to expand on the above dirty solution, this is what I am currently resorting to (in my case, on a standard size website, it has so far saved me ~600 lines of redundant CSS code):

_export.scss

/*
 |--------------------------------------------------------------------------
 | SASS Export
 |--------------------------------------------------------------------------
 |
 | Define any variables that should be exported to Javascript. This file
 | is excluded from the CSS builds in order to prevent the variables from
 | being exported to CSS.
 |
 */

@import "variables";

:export {

    // Export the color palette to make it accessible to JS
    white: #fff;
    black: #000;
    grey: #ccc;
    // etc...

}

Then, in my Javascript instead of importing from _variables.scss, I import from _export.scss like so:

import styles from 'core-styles/brand/_export.scss';

And finally, the export statement, can now be removed from the _variables.scss file, thus preventing the compiled CSS export code.

Note: The _export.scss file must be excluded from the SCSS compilation!

danronmoon
  • 3,814
  • 5
  • 34
  • 56
Ben Carey
  • 16,540
  • 19
  • 87
  • 169

2 Answers2

31

Note: I have posted this answer because it seems there is no better solution, however, if someone subsequently provides a better solution, I will be more than happy to accept it.

It seems that the only real solution to this is to extract the export statement out of the _variables.scss file and place it into its own _export.scss file which will subsequently not be included in the SCSS compliation.

This will look something like this:

_variables.scss - included in the SCSS compilation

/* Define all colours */
$white:    #fff;
$black:    #000;
$grey:     #ccc;
// etc...

_export.scss - not included in the SCSS compilation

@import "variables";

:export {

    // Export the color palette to make it accessible to JS
    white: #fff;
    black: #000;
    grey: #ccc;
    // etc...

}

And then your app.scss (I use brand.scss) file will look something like this (note the absence of @include "export";):

@import "variables";
@import "mixins";
@import "core";
@import "animations";
// etc...

Then, _export.scss is simply referenced only in JavaScript like so (note that core-styles is just an alias that I used in my projects):

import styles from 'core-styles/brand/_export.scss';
danronmoon
  • 3,814
  • 5
  • 34
  • 56
Ben Carey
  • 16,540
  • 19
  • 87
  • 169
  • 1
    I'm having some difficulties achieving what you have outlined here, would you mind taking a look at my question here: https://stackoverflow.com/questions/61517117/cannot-export-sass-variables-into-javascript – Matt May 02 '20 at 20:41
  • 2
    If I do this with rollup the styles is an empty string, e.g. "". Any experience with getting this to work with Rollup? – Serge van den Oever Jun 04 '20 at 21:44
  • 7
    This wasn't working for me as is, I kept getting an empty object back for the `styles` variable. After updating `_export.scss` to be `_export.module.scss`, I was then able to get back the variables – Stetzon Jun 10 '21 at 19:15
  • @Stetzon, your comment was the only solution that worked for me as well. I wonder what your webpack.js looks like – Pelicer Jul 23 '21 at 13:20
  • @Stetzon's solution works for Parcel as well – Danry Nov 28 '21 at 06:13
  • 1
    When I use `:export` in one of my SCSS files, I get an webpack error: "Error: Module parse failed: Unexpected token" using sass-loader@13.0.2_sass@1.54.4+webpack@5.74.0 in an Angular / TS app. Any idea what is missing? – Peter T. Nov 11 '22 at 10:34
  • @PeterT. Have you solved your problem? I have the same thing! – Murat Can OĞUZHAN Feb 15 '23 at 13:42
  • 1
    @Murat No, I used a different approach and a workaround without `:export` then. – Peter T. Feb 20 '23 at 09:39
  • How is your workaround I also had to there a workaround like keeping the content in a variable. – Murat Can OĞUZHAN Feb 20 '23 at 14:41
1

I wonder if this is caused by using @import instead of using @use.

@import is deprecated precisely because it copy-pastes, so multiple imports result in multiple copies of the same contents.

Ron Newcomb
  • 2,886
  • 21
  • 24