Skip to content

Nicolab/gulp-if-else

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 

gulp-if-else

Actual version published on NPM Dependencies npm module downloads per month

A plugin for Gulp, allows conditional task, with "if" callback and "else" callback (optional).

Install

npm install gulp-if-else

Usage

ifElse(condition, ifCallback [, elseCallback])

var ifElse = require('gulp-if-else');

// your code ...

gulp.src(source)
  .pipe( ifElse(condition, ifCallback, elseCallback) )

Works as a basic condition.

To understand the logic, ifElse is equivalent to

if(condition) {

  // condition is truthy

  return ifCallback();
}else{

  // condition is falsy

  // if "elseCallback" is provided
  if(elseCallback) {
    return elseCallback();
  }

  // if not "elseCallback" returns the stream
  return stream;
}

Examples

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

  gulp.src('./public/css/*.css')
    .pipe(ifElse(process.env.NODE_ENV === 'production',

      // called if "NODE_ENV" is "production"
      minifyCSS,

      // called if "NODE_ENV" is "not" "production" (else)
      function() {
        return minifyCSS({debug: true});
    }))
    .pipe(gulp.dest('./dist/css'))
});

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

  var isDev = process.env.NODE_ENV === 'development';

  gulp.src('./public/js/app.js')
    .pipe(browserify())

    // here, "uglify" (function) is called only if "isDev" is "true"
    .pipe(ifElse(isDev, uglify))
    .pipe(gulp.dest('./dist/js'))
});

Unit tests

gulp-if-else is unit tested with Unit.js

Run the tests

cd node_modules/gulp-if-else

npm test

Other conditional plugins for Gulp

LICENSE

MIT license

Author

Nicolas Tallefourtane - Nicolab.net
Nicolas Talle
Make a donation via Paypal

About

[Gulp plugin] Conditional task with "if" callback and "else" callback (optional) : gulp.src(source).pipe( ifElse(condition, ifCallback, elseCallback) )

Resources

License

Stars

Watchers

Forks

Packages

No packages published