Modularizing gulpfile


Modularizing gulpfile.js is as easy as separating each gulp task into its own file.

Imagine you have the following:

// gulpfile.js

var gulp = require('gulp');

gulp.task('task1', function() {
    return console.log('running task1');
});

gulp.task('task2', function() {
    return console.log('running task2');
});

You can modularize the file like so:

// task1.js

var gulp = require('gulp');

gulp.task('task1', function() {
    return console.log('running task1');
});
// task2.js

var gulp = require('gulp');

gulp.task('task2', function() {
    return console.log('running task2');
});
// gulpfile.js

require('./task1');
require('./task2');

// make sure you have gulp installed globally and locally:
// `npm i -g gulp & npm i gulp`

// to see all tasks, run `gulp -T`

You can even organize them in a directory. Because the gulp working directory is determined by where gulpfile.js is located, gulp.src and gulp.dest still works as expected.

See example: