LESS provides reference imports to only import things like variables and mixins from the given file without affecting the compiled CSS: @import (reference) "foo.less"
.
In SCSS the same effect can be achieved by using node-sass custom importers like node-sass-filter-importer with its @import '[variables, mixins] from style.scss';
.
Even so, it seems hard to setup, especially if node-sass is being used as part of Webpack or @angular/cli build process. In many cases you will find it more convenient to extract variables, functions etc. from the file you are interested in into a separate file and import that file normally. That’s what this little tool is for.
-
yarn install --dev @earshinov/extract-scss-variables
-
yarn run extract-scss-variables index.scss variables.scss
The last command will extract variables, functions and mixins from index.scss
and its imported files into variables.scss
.
Example input:
@mixin defaultFont() {
font-family: Rubik;
font-weight: normal;
}
@import './button';
$buttonBackgroundColor: #e0e0e0;
$buttonColor: black;
$buttonBorderRadius: 4px;
button, input[type=button], input[type=submit] {
@include defaultFont;
appearance: none;
background-color: $buttonBackgroundColor;
color: $buttonColor;
border-radius: $buttonBorderRadius;
}
Example output:
@import './variables';
@import './button';
@import './variables';
button, input[type=button], input[type=submit] {
@include defaultFont;
appearance: none;
background-color: $buttonBackgroundColor;
color: $buttonColor;
border-radius: $buttonBorderRadius;
}
@mixin defaultFont() {
font-family: Rubik;
font-weight: normal;
}
$buttonBackgroundColor: #e0e0e0;
$buttonColor: black;
$buttonBorderRadius: 4px;
-
To install dependencies and run commands, use Yarn rather than
npm
-
To lint the code with ESLint, run
yarn run lint
-
To test the code with Jest, run
yarn run test
-
To build NPM package, run
yarn run build
-
To publish NPM package, run
yarn run publish --access public
. It is important to useyarn run publish
instead ofyarn publish
to lint, test and build the code before publication. If you are wondering why theprepublish
script is not used for this purpose, here is the reason.
Inspired by
SCSS manipulation is implemented using PostCSS:
-
AST explorer (In the top menu, select CSS and postcss. In postcss options, select scss parser.)