I use the following in gulp
to build a CSS file from a SASS (*.scss) file :
var gulp = require('gulp')
, sass = require('gulp-sass');
var colortheme = 'blue';
gulp.task('build_css', function() {
return gulp.src(['resources/assets/sass/app/' + colortheme + '.scss'])
.pipe(sass())
.pipe(gulp.dest('public/css/'));
} );
As you can see I use a different .scss file for each colortheme.
I would like to use only one .scss file but be able to pass the colortheme somehow as a parameter, so this can be used as a variable inside the .scss file.
Is this possible?
To convert the SASS variable to a CSS custom property you put curly brackets around it, and a hash in front. If you've used template literals in JavaScript it's the same thing, just with a # instead of a $ (because we already have $ in the variable name).
Declaration of a variable in SASS: In SASS, you can define a variable by using $ symbol at the starting of the name of the variable and followed by its value. Understanding scope of a variable: SASS variables can be declared anywhere in the document before it is used.
Description. You can set the default values for variables by adding ! default flag to the end of the variable value. It will not re-assign the value, if it is already assigned to the variable.
Setting up a task Still using the command line, you can install Gulp-sass by running npm install gulp-sass --save-dev . After that, require Gulp-sass in your gulpfile. js. Put var sass = require('gulp-sass'); under the line you required gulp.
Considering that it's SCSS you can simply plop the variable definitions at the beginning of the file using gulp-header
:
resources/assets/sass/app/theme.scss
.foo {
color: $themeColor;
font-family:$themeFont;
}
gulpfile.js
var gulp = require('gulp');
var sass = require('gulp-sass');
var header = require('gulp-header');
var colortheme = 'blue';
var font = 'sans-serif';
gulp.task('build_css', function() {
return gulp.src(['resources/assets/sass/app/theme.scss'])
.pipe(header('$themeColor: ' + colortheme + ';\n' +
'$themeFont: ' + font + ';\n'))
.pipe(sass())
.pipe(gulp.dest('public/css/'));
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With