Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a variable while buiding SASS with gulp?

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?

like image 495
Dylan Avatar asked May 11 '16 20:05

Dylan


People also ask

How do I assign a Sass variable to a variable in CSS?

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).

How do I declare a variable in Sass?

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.

How can we set default value to the variable in Sass?

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.

How do you use gulp and Sass?

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.


1 Answers

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/'));
});
like image 129
Sven Schoenung Avatar answered Oct 09 '22 05:10

Sven Schoenung