Johan Dahl
Johan Dahl

Reputation: 1742

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.

Upvotes: 3

Views: 1490

Answers (2)

CRIS
CRIS

Reputation: 346

Base on https://www.npmjs.com/package/lazypipe

minifyAndRenameCSS should be:

var minifyAndRenameCSS = lazypipe()
  .pipe(minifyCSS, {keepSpecialComments: 0})
  .pipe(rename, {suffix: '.min'});

There shouldn't be any function calls inside the pipes. If the function needs some arguments separate them with commas (i.e. .pipe(myFunc, arg1, arg2)).

Maybe you've got the right answer by now, I'm just sharing so that other viewers will know.

Upvotes: 1

Eloy Pineda
Eloy Pineda

Reputation: 2187

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()))
});

Upvotes: 3

Related Questions