Supriya Ghosh (Editor)

Gulp.js

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Stable release
  
4.0.0

License
  
MIT License

Available in
  
JavaScript

Developer(s)
  
Fractal and contributors of the GitHub community

Repository
  
github.com/gulpjs/gulp/

Platform
  
Cross-platform, see Legacy browser support

gulp.js is an open-source JavaScript toolkit by Fractal Innovations and the open source community at git, used as a streaming build system in front-end web development.

Contents

It is a task runner built on Node.js and Node Package Manager (npm), used for automation of time-consuming and repetitive tasks involved in web development like minification, concatenation, cache busting, unit testing, linting, optimization etc.

gulp uses a code-over-configuration approach to define its tasks and relies on its small, single-purposed plugins to carry them out. gulp ecosystem has 300+ such plugins made available to choose from.

Overview

gulp is a build tool in JavaScript built on node streams. These streams facilitate the connection of file operations through pipelines. gulp reads the file system and pipes the data at hand from its one single-purposed plugin to other through the .pipe() operator, doing one task at a time. The original files are not affected until all the plugins are processed. It can be configured either to modify the original files or to create new ones. This grants the ability to perform complex tasks through linking its numerous plugins. The users can also write their own plugins to define their own tasks. Unlike other task runners that run tasks by configuration, gulp requires knowledge of JavaScript and coding to define its tasks. gulp is a build system which means apart from running tasks, it is also capable of copying files from one location to another, compiling, deploying, creating notifications, unit testing, linting etc.

Need for a task runner

Task-runners like gulp and grunt are built on node rather than npm is because the basic npm scripts are inefficient when executing multiple tasks. Even though some developers prefer npm scripts because they can be simple and easy to implement, there are numerous ways where gulp and grunt seem to have an advantage over each other and the default provided scripts. Grunt runs tasks by transforming files and saves as new ones in temporary folders and the output of one task is taken as input for another and so on until the output reaches the destination folder. This involves a lot of I/O calls and creation of many temporary files. Whereas gulp streams through the file system and does not require any of these temporary locations decreasing the number of I/O calls thus, improving performance. Grunt uses configuration files to perform tasks whereas gulp requires its build file to be coded. In grunt, each plugin needs to be configured to match its input location to the previous plugin’s output. In gulp, the plugins are automatically pipe-lined.

Operation

The gulp tasks are run from a Command Line Interface (CLI) shell and require package.json and gulpfile.js(simply gulpfile) in the project root directory. The gulpfile is where plugins are loaded and tasks are defined. First, all the necessary modules are loaded and then tasks are defined in the gulpfile. All the necessary plugins specified in the gulpfile are installed into the devDependencies. The default task runs by $gulp. Individual tasks can be defined by gulp.task and are run by gulp <task> <othertask>. Complex tasks are defined by chaining the plugins with the help of .pipe() operator.

Anatomy of gulpfile

gulpfile is the place where all the operations are defined in gulp. The basic anatomy of the gulpfile consists of required plugins included at the top, definition of the tasks and a default task at the end.

Plugins

Any installed plugin that is required to perform a task is to be added at the top of the gulpfile as a depedency in the following format.

Tasks

The tasks can then be created. A gulp task is defined by gulp.task and takes the name of the task as the first parameter and a function as the second parameter.

The following example shows the creation of a gulp tasks. The first parameter taskName is mandatory and specifies the name by which the task in the shell can be executed

Alternatively, a task that performs several predefined functions can be created. Those functions are passed as the second parameter in the form of an array.

Default task

The default task is to be defined at the end of the gulpfile. It can be run by gulp command in the shell. In the case below, the default task does nothing.

The default task is used in gulp to run any number of dependent sub tasks defined above in a sequential order automatically. gulp can also monitor source files and run an appropriate task when changes are made to the files. The sub tasks are to be mentioned as the elements of the array in the second parameter. The process can be triggered by simply running the default task by gulp command.

Image Task

For the following example, the gulp-imagemin plugin is required. To install the plugin, run the command npm install --save-dev gulp-imagemin.

Subsequently, the module must be at the beginning of gulpfile 'defined' as:

The subsequent image task optimizes images. gulp.src () retrieves all the images with the extension .png, .gif or .jpg in the directory 'images-orig/'.

.pipe (imagemin ()) channels the images found, through the optimization process and with .pipe (gulp.dest ()) the optimized images are saved to the 'images/' folder. Without gulp.dest () the images would indeed be optimized, but are not stored. Since the optimized images are stored to another folder, the original images remain unaltered.

Scripts Task

In the following example, all JavaScript files from the directory 'scripts/' are optimized with .pipe (uglify ()) and gulp. dest ( 'scripts/') overwrites the original files with the output. For this, one must first return to the required gulp-uglify plugin on npm package installer and at the beginning of gulpfile , the module should be defined.

Watch Task

The Watch-task serves to react to changes in files. In the following example, the tasks with the names scripts and images are called when any of JavaScript files or images change in the specified directories.

Furthermore, it is possible to accomplish an update of the browser content using the Watch-tasks. For this, there are numerous options and plugins.

Literature

  • Jed Mao; Maximilian Schmitt; Tomasz Stryjewski; Cary Country Holt; William Lubelski (2014). Developing a Gulp Edge (1st ed.). Bleeding Edge Press. ISBN 978-1-939902-14-6. 
  • Den Odell (2014). "Build Tools and Automation". Pro JavaScript Development Coding, Capabilities, and Tooling. Apress. ISBN 978-1-4302-6268-8. 
  • Maynard, Travis (2015). Getting Started with Gulp. Packt Publishing Ltd. ISBN 9781784393472. 
  • References

    Gulp.js Wikipedia