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!
build.optimizeDeps
andbuild.rollupOptions.external
options as well as the postcss-exclude-files plugin with thecss.postcss.plugins
option and the filter option of the plugin set to'**/*.module.*'
. Nothing seems to have any effect. – Portiaportico