Gulp condition inside pipe

max li picture max li · Nov 28, 2014 · Viewed 24.3k times · Source

How can I do a condition inside Gulp pipe to output to a different destination.

g.task('sass', function() {
  return g.src(sources.sass).pipe(changed(output.css)).pipe(sass({
    style: 'compressed',
    sourcemap: true
  })).pipe(function() {
    if (..) {
      g.dest(output.css);
    } else {
      g.dest(output.css2);
    }
  }).pipe(notify('scss converted to css and
compressed
<%=f ile.relative %>'));
});

Answer

Daniel Naab picture Daniel Naab · Nov 29, 2014

Use the gulp-if plugin:

var gulpif = require('gulp-if');

g.task('sass', function() {
    return g.src(sources.sass)
        .pipe(changed(output.css))
        .pipe(sass({style:'compressed', sourcemap:true}))

        // Conditional output
        .pipe(gulpif(condition1, g.dest(output.css)))
        .pipe(gulpif(condition2, g.dest(output.css2)))

        .pipe(notify('scss converted to css and compressed <%= file.relative %>'));
});