node.jsconditional-statementsgulpgulp-if

Conditionals in gulp


I'm a bit confused over how to best set up conditionals in a gulp file and could use some clarification.

Am I right in that you cannot do ordinary if statements inside a pipe chain? If you can, how would you type it out?

Right now I'm using gulp-if to do something like this:

// Minify
.pipe( gulpif(env === 'production',
    minifyCSS({keepSpecialComments:0,})
))

// Rename
.pipe( gulpif(env === 'production',
    rename({suffix: '.min'})
))

But, these two blocks would optimally be inside the same if condition. Is this possible? I can't get that to work.


Solution

  • You could use lazypipe for minify and rename transforms. It would be something like this:

    var minifyAndRenameCSS = lazypipe()
      .pipe(minifyCSS({keepSpecialComments:0})
      .pipe(rename({suffix: '.min'}));
    
    gulp.task('build', function() {
      return gulp.src('*.css')
        .pipe(gulpif(env === 'production', minifyAndRenameCSS()))
    });