2

I'm using gulp to compile sass into css. Right now I have color variable defined in sass. In _varibales.scss.

  $color: #009999;

In app.scss I have used this variable in multiple places.

    .button{
      color:$color
    }
    .background{
       background :$color 
       padding:10px
    }
    .element{
       margin:0 auto
    } 

So is there a way to extract classes or id's having this variable and generate a new file called color.css using gulp?

2
  • Do you want to extract into a css file ONLY those classes or ids with a sass variable? So not .element in you r example? Commented Jul 5, 2017 at 19:11
  • Yes exactly but my output file should contain .background { background : somecolor} .button{color : somecolor} Commented Jul 6, 2017 at 9:21

2 Answers 2

2

I have come up with this solution using line-reader:

var gulp = require("gulp");
var sass = require("gulp-sass");
var rename = require("gulp-rename");
var fs = require('fs');

// read a file line-by-line
var lineReader = require('line-reader');

gulp.task('default', function () {

  var myVar;
  var re;
  var s = "";  // will build up temp file with this variable

  fs.readFile('variabales.scss', 'utf8', function (err, contents) {

     // e.g., assumes a single line format like   $color: #009999;
     // myVar will = $color
    myVar = contents.split(/:/)[0].trim();

     // want to include lines that have a {, } or @ symbol on them
     // as well as the $___ sass variable
    re = new RegExp('(\\' + myVar + ')|{|}|@');
  });

  var readStream = fs.createReadStream('app.scss');
  lineReader.eachLine(readStream, function(line, last) {

    if (line.match( re )) {
      s += line + "\n";
    }   

    if (last) {

      fs.writeFileSync('temp.scss', s);

      gulp.src("temp.scss")
        // gulp-sass gets rid of the empty ruleset automatically!
        .pipe(sass().on('error', sass.logError))
        .pipe(rename('app.css'))
        .pipe(gulp.dest('./'));

      return false; // stop reading
    }
  })
});

Your app.scssfile should have an import statement at the very top like:

@import "variabales.scss";

.button{
    color : $color;
}

.background{
    background : $color;
    padding : 10px;
}

.element{
    margin : 0 auto;
}

So Sass can retrieve the variable from variabales.scss.

No doubt this could be improved by writing to a buffer instead of to a temp file or directly to a stream.

If you wanted your final app.css file to have more sass variables in it than just one, you could generalize this by modifying the regexp to match on "$" instead of the specific "$color".

  re = new RegExp('[\\${}@]');

And then you could rid of the fs. readFile call.

Here is the resulting app.css file:

.button {
  color: #009999; }

.background {
  background: #009999; }
Sign up to request clarification or add additional context in comments.

1 Comment

@Ameer Khan is this what you were looking for?
0

You can do two tasks in gulp to extract separate files and then get them with a link in your document:

gulp.task('scss-variables', function() {
   return gulp.src('path/variables.scss')
        .pipe(gulp.dest('variables.css'))
});
gulp.task('scss-app', function() {
   return gulp.src('path/app.scss')
        .pipe(gulp.dest('app.css'))
});

But it is much more appropiate to import one file into the other with scss:

/* app.scss */
import 'variables';

.button{
  color:$color
}
.background{
   background :$color 
   padding:10px
}
.element{
   margin:0 auto
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.