Today we're going to talk about Gulp, a web development tool for automating tasks, similar to Grunt.
Why use it?
Just like Grunt, Gulp helps you automate small repetitive tasks, such as minification, testing, and optimization.
But what makes Gulp different from Grunt?
They're very similar with each other. Both of them use separate plugins to run the tasks. We can also define the configuration of each task in a configuration file. The only difference is probably the syntax and the way you use it.
How to use it?
In my post about Grunt, we created a simple web page that display the current time and a button that changes its color when it's clicked. In this post, we'll make the same web page and automate these tasks using Gulp:
- Checking Javascript files' syntax and best practices.
- Concat all the Javascript files into 1 file.
- Minified the concatted Javascript file.
Here's the project structure:
.
├── dist
│ ├── index.html # main page
├── src
│ ├── main.js # main page's script
│ ├── button.js # button events' script
├── package.json #
└── gulpfile.js # Gulp's configuration file
dist/index.html
Here's the main page. Notice that we include a file named bundle.min.js here. This file is not yet exist and is the final Javascript file that we will create using Gulp.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<div id="time"></div>
<button class="toggle-color">Toggle color</button>
<script src="./bundle.min.js"></script>
</body>
</html>
src/main.js
document.getElementById('time').innerHTML = new Date();
src/button.js
var buttons = document.getElementsByClassName('toggle-color');
for(var i=0; i<buttons.length; i++) {
buttons[i].onclick = function() {
this.style.background = 'red';
};
}
gulpfile.js
This file is where we configure Gulp. Similar to Grunt, we imported the plugins that we want to use, defined the tasks, and register them.
const { src, dest, series, watch } = require('gulp');
// 1. import plugins
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');
const rename = require('gulp-rename');
const jshint = require('gulp-jshint');
// 2. define tasks
function task_jshint() {
return src('./src/*.js')
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'));
}
function task_concat() {
return src('./src/*.js')
.pipe(concat('bundle.js'))
.pipe(dest('dist/'))
}
function task_uglify() {
return src('./dist/bundle.js')
.pipe(uglify())
.pipe(rename({extname: '.min.js'}))
.pipe(dest('dist/'));
}
function task_watch() {
watch('./src/*.js', task_jshint);
}
// 3. register tasks
exports.default = series(task_jshint, task_concat, task_uglify);
exports.watch = task_watch;
Here's what each task will do:
task_jshintwill check the syntax of all Javascript files in thesrcfolder. This task uses gulp-jshint plugin.task_concatwill concat all Javascript files in thesrcfolder into one file (bundle.js) and put it in thedistfolder. This task uses gulp-concat plugin.task_uglifywill minify the bundle.js and name it bundle.min.js. This task usesgulp-uglifyplugin andgulp-renameplugin. We needgulp-rename, becausegulp-uglifydoesn't rename its output file.task_watchwill run Gulp'swatchAPI and run thetask_jshinttask everytime there's any change in Javascript files in thesrcfolder.
And then we registered two tasks that we can use default and watch:
- You can use
defaultby typinggulpto your console. It will runtask_jshint,task_concat, andtask_uglifyin that order. - You can use
watchby typinggulp watchto your console. It will runtask_watchand check your Javascript files' syntax in real-time.
Don't forget to install these following packages:
gulpandgulp-cli.
npm install gulp gulp-cli --save-dev- All of the plugins that we use.
// for checking syntax npm install jshint jshint-stylish gulp-jshint --save-dev // for concatting files npm install gulp-concat --save-dev // for minifying files npm install gulp-uglify gulp-rename --save-dev
Now, we can try running each registered task:
- Try running
gulpin your console to concat and minify your Javascript files. Open index.html to see your web page. - Try running
gulp watchin your console and try making a mistake in your Javascript files. Gulp will notify you right away.
That's it for now!