gulp is an
open-source
Open source is source code that is made freely available for possible modification and redistribution. Products include permission to use the source code, design documents, or content of the product. The open-source model is a decentralized sof ...
JavaScript
JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
toolkit created by Eric Schoffstall
used as a streaming
build system (similar to a more package-focused
Make
Make or MAKE may refer to:
*Make (magazine), a tech DIY periodical
*Make (software), a software build tool
*Make, Botswana, in the Kalahari Desert
*Make Architects
Make Architects is an international architecture practice headquartered in London ...
) in
front-end web development
Front-end web development is the development of the graphical user interface of a website, through the use of HTML, CSS, and JavaScript, so that users can view and interact with that website.
Tools used for front-end development
There are several ...
.
It is a task runner built on
Node.js
Node.js is an open-source server environment. Node.js is cross-platform and runs on Windows, Linux, Unix, and macOS. Node.js is a back-end JavaScript runtime environment. Node.js runs on the V8 JavaScript Engine and executes JavaScript code ou ...
and
npm, used for automation of time-consuming and repetitive tasks involved in web development like
minification, concatenation, cache busting,
unit testing
In computer programming, unit testing is a software testing method by which individual units of source code—sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures&md ...
,
linting
Lint, or a linter, is a static code analysis tool used to flag programming errors, bugs, stylistic errors and suspicious constructs. The term originates from a Unix utility that examined C language source code.
History
Stephen C. Johnson, a com ...
, optimization, etc.
gulp uses a code-over-configuration approach to define its tasks and relies on its small, single-purpose plugins to carry them out. The gulp ecosystem includes more than 3500 such plugins.
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 one single-purposed plugin to another 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
In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs that ...
,
deploying, creating notifications, unit testing, linting, etc.
Need for a task runner
Task-runners like gulp and
Grunt
Grunt, grunts or grunting may refer to:
Sound and music
* Grunting (tennis), in tennis refers to the loud noise, sometimes described as "shrieking" or "screaming", made by some players during their strokes
* Death grunt, the death metal singin ...
are built on Node.js rather than
npm 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 them 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 the creation of many temporary files. Whereas gulp streams through the file system do 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
A command-line interpreter or command-line processor uses a command-line interface (CLI) to receive commands from a user in the form of lines of text. This provides a means of setting parameters for the environment, invoking executables and pro ...
(CLI)
shell and require two files,
package.json
, which is used to list the various plugins for gulp, and
gulpfile.js
(or simply
gulpfile
), These, as per build tool convention, are often found in the root directory of the package's source code. The gulpfile contains most of the logic that gulp needs to run its build tasks. First, all the necessary modules are loaded and then tasks are defined in the gulpfile. All the necessary plugins specified in the gulpfile are listed in the devDependencies section of
The default task runs by
$gulp
. Individual tasks can be defined by gulp.task and are run by
gulp
.
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 dependency in the following format.
//Adding dependencies
var gulp = require ('gulp');
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.
gulp.task('taskName', function () );
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.
function fn1 ()
function fn2 ()
// Task with array of function names
gulp.task('taskName', gulp.parallel(fn1, fn2));
Default task
The default task is to be defined at the end of the gulpfile. It can be run by the
gulp
command in the shell. In the case below, the default task does nothing.
// Gulp default task
gulp.task('default', fn);
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.
Example tasks
Image Task
The module definition can be at the beginning of
Gulpfile.js
like so:
var imagemin = require('gulp-imagemin');
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.
// Images task
gulp.task('images', function () );
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.
// Script task
gulp.task('scripts', function () );
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.
// Rerun the task When a file changes
gulp.task('watch', function (cb) );
Furthermore, it is possible to accomplish an update of the browser content using the Watch-tasks.
For this, there are numerous options and plugins.
See also
*
Grunt (software)
Grunt is a JavaScript task runner, a tool used to automatically perform frequent tasks such as minification, compilation, unit testing, and linting. It uses a command-line interface to run custom tasks defined in a file (known as a Gruntfile) ...
*
Browserify
Browserify is an open-source JavaScript bundler tool that allows developers to write and use Node.js-style modules that compile for use in the browser.
Examples
Execution
$ browserify source.js -o target.js
This adds the source of all ...
References
Literature
*
*
*
External links
*
* {{GitHub, gulpjs/gulp, gulp
JavaScript programming tools