Basic gulp.js plugins
Chapter - 6






Gulp Plugins

It is the Sixth part of the gulp for beginners series which deals in introducing gulp.js and then making you familiar with some basic gulp plugins. This article talks about 4 gulp plugins which are as follows :

  1. gulp-ext
  2. gulp-plumber
  3. gulp-util
  4. gulp-if

#gulp-ext

This is a very basc gulp plugin which is used to change , append or crop the extension of the file. The snippet is given below :


		        
var gulp = require('gulp');
var ext = require('gulp-ext');		
gulp.task('default', function () {
    return gulp.src('source/first.js')
        .pipe(ext.append('on'))
        .pipe(gulp.dest('dist'));
});
                
	            

Run it at bash or command line using the following command :


					
$gulp
					
	            

#gulp-plumber

This plugin is used to stop the breaking of pipeline due to some error in any of the plugin. The snippet is given below :


		        
var gulp = require('gulp');		
var plumber = require('gulp-plumber');
var uglify = require('gulp-uglify');
gulp.task('default', function () { 
gulp.src('source/*.js')
    .pipe(plumber())
    .pipe(uglify())
    .pipe(gulp.dest('/destination'));
});
                
	            

Run it at bash or command line using the following command :


		        
$gulp
                
	            

#gulp-util

This gulp plugin along with many other functionalities is used to generate logs . Snippet is given below :

							
var gulp = require('gulp');		
var util = require('gulp-util');
var uglify = require('gulp-uglify');

gulp.task('ut' , function() {
	gulp.src('source/*.js')
	.pipe(util.log('got some files'))
	.pipe(uglify())
	.pipe(util.log('uglified them'))
	.pipe(gulp.dest('destination'));
	.pipe(util.log('sent in destination folder'))
});
							
							

Run it at bash or command line using the following command :


							
$ gulp ut
							
						


#gulp-if

This gulp plugin is used when we want to run a task only iff certain condition is met. The snippet is given below :

							
var gulp = require('gulp');		
var gif = require('gulp-if');
var uglify = require('gulp-uglify');
 
var value = true;
 
gulp.task('abc', function() {
  gulp.src('source/*.js')
    .pipe(gif(value, uglify()))
    .pipe(gulp.dest('destination'));
});
							
							

Run it at bash or command line using the following command :


							
$ gulp abc
							
						

Summary

We learned about 4 packages related to gulp which includes gulp-ext , gulp-plumber , gulp-util , gulp-if .