Tasks are main entities

Task enables a modular approach for configuring Gulp. Everything we can write in gulpfile.js are tasks.

Let’s start how to write tasks in gulpfile.js

gulp.task('task-name', function() {
   //do stuff here
});

gulp.task('dependenttask', ['mytask'], function() {
  //do stuff after 'mytask' is done.
});

First, upon you have to install the required plugins as per your requirement then you can use it by tasks.

var sass = require('gulp-sass');
var minifyCSS = require('gulp-minify-css');
var concat = require('gulp-concat');

gulp.task('sass', function() {
 return gulp.src('app/scss/**/*.scss')
        .pipe(sass())
        .pipe(gulp.dest('app/css/'));
});

There are some main functions.

  1. gulp.src – Emit files matching one or more globs
  2. gulp.dest – Write files to directories
  3. gulp.symlink – Write files to symlinks
  4. gulp.task – Define tasks
  5. gulp.lastRun – Get timestamp of last successful run
  6. gulp.parallel – Run tasks in parallel
  7. gulp.series – Run tasks in series
  8. gulp.watch – Do something when a file changes
  9. gulp.tree – Get the tree of tasks
  10. gulp.registry – Get or set the task registry

Gulp Src

It takes the expressions

gulp.src(['*.js', '!b*.js', 'bad.js'])

// Array of different folders

gulp.src(['client/*.js', '!client/b*.js', 'client/bad.js'])

Gulp Dest

Can be piped to and it will write files. Re-emits all data passed to it so you can pipe to multiple folders. Folders that don’t exist will be created.

gulp.src('./client/templates/*.pug')
  .pipe(pug())
  .pipe(gulp.dest('./build/templates'))
  .pipe(minify())
  .pipe(gulp.dest('./build/minified_templates'));

Functions exactly like gulp.dest, but will create symlinks instead of copying a directory.

Gulp Task

Functions to create new tasks. The syntax is given above.

Gulp Lastrun

Returns the timestamp of the last time the task ran successfully. The time will be the time the task started. Returns undefinedif the task has not run yet.

gulp.lastRun('someTask', 1000) //would return 1426000004000.

Gulp Parallel

When the returned function is executed, the tasks or functions will be executed in parallel, all being executed at the same time. If an error occurs, all execution will complete.

gulp.task('one', function(done) {
  // do stuff
  done();
});

gulp.task('two', function(done) {
  // do stuff
  done();
});

gulp.task('default', gulp.parallel('one', 'two', function(done) {
  // do more stuff
  done();
}));

Gulp Series

When the returned function is executed, the tasks or functions will be executed in series, each waiting for the prior to finish. If an error occurs, execution will stop.

gulp.task('one', function(done) {
  // do stuff
  done();
});

gulp.task('two', function(done) {
  // do stuff
  done();
});

gulp.task('default', gulp.series('one', 'two', function(done) {
  // do more stuff
  done();
}));

Gulp Watch

Takes a path string, an array of path strings, a glob string or an array of glob strings as globs to watch on the filesystem. Also optionally takes options to configure the watcher and a fn to execute when a file changes.

gulp.watch('js/**/*.js', gulp.parallel('concat', 'uglify'));

Gulp Tree

Returns the tree of tasks. Inherited from undertaker.

var fs = require('fs');
var Undertaker = require('undertaker');

var taker = new Undertaker();

taker.task('task1', function(cb){
  // do things

  cb(); // when everything is done
});

taker.task('task2', function(){
  return fs.createReadStream('./myFile.js')
    .pipe(fs.createWriteStream('./myFile.copy.js'));
});

taker.task('task3', function(){
  return new Promise(function(resolve, reject){
    // do things

    resolve(); // when everything is done
  });
});

taker.task('combined', taker.series('task1', 'task2'));

taker.task('all', taker.parallel('combined', 'task3'));

Gulp Registry

Get or set the underlying task registry.

//gulpfile.js
var gulp = require('gulp');

var companyTasks = require('./myCompanyTasksRegistry.js');

gulp.registry(companyTasks);

gulp.task('one', gulp.parallel('someCompanyTask', function(done) {
  console.log('in task one');
  done();
}));
taker.task('combined', taker.series('task1', 'task2'));

taker.task('all', taker.parallel('combined', 'task3'));
//myCompanyTasksRegistry.js
var util = require('util');

var DefaultRegistry = require('undertaker-registry');

function MyCompanyTasksRegistry() {
  DefaultRegistry.call(this);
}
util.inherits(MyCompanyTasksRegistry, DefaultRegistry);

MyCompanyTasksRegistry.prototype.init = function(gulp) {
  gulp.task('clean', function(done) {
    done();
  });
  gulp.task('someCompanyTask', function(done) {
    console.log('performing some company task.');
    done();
  });
};

module.exports = new MyCompanyTasksRegistry();