We’re at the end of 2015, and web technology has changed quite a bit since I started in 2002. Nowadays, for the front end stuff, there is a whole family of tools based on the node.js package manager (npm) that you can use to streamline and automate your workflow.
In this article (based on Windows), we’ll learn to use Gulp to do routine tasks such as concatenating and minifying JavaScript tasks. There’s another tool called Grunt with a similar purpose, and you’ll find all sorts of discussions on the internet comparing Grunt vs Gulp. Basically, Grunt is the older of the two and has a bigger community – an important factor considering that these tools are plugin-driven. However, I’m covering Gulp here as I felt it was more intuitive. For this small demonstration it has all the plugins we need, and performance (a common point of comparison) isn’t even a factor.
Setting up Gulp
The first thing we need is to install node.js:
There’s a chance you might already have node.js, if you installed it with Visual Studio 2015.
Once you have installed node.js, you should have npm in your path. Open a command prompt, and install Gulp using the following command:
npm install gulp -g
-g means globally, and thanks to this, gulp should now be in your path.
Next, we want to create a package.json file. This is a kind of project file for node.js-related stuff. We can use npm for this too:
npm init
npm will ask a bunch of questions in order to set up the package.json file, suggesting possible answers where it makes sense to do so. name and version are required, but you can leave the rest empty if you like:
Next, we need to install Gulp locally in our project:
npm install gulp --save-dev
This installs Gulp; –save-dev updates the package.json with a devDependencies field:
{ "name": "gulptest", "version": "1.0.0", "description": "Learning to use Gulp.", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Daniel D'Agostino", "license": "ISC", "devDependencies": { "gulp": "^3.9.0" } }
Plugins and the Gulp file
Gulp itself doesn’t do anything; it is just configured to run tasks. Its capabilities come from the plugins you install, and you configure it to do stuff using a Gulp file. For this simple example, we’re just going to use a few plugins:
npm install gulp-concat gulp-uglify --save-dev
Once again, –save-dev updates your devDependencies in package.json:
"devDependencies": { "gulp": "^3.9.0", "gulp-concat": "^2.6.0", "gulp-uglify": "^1.5.1" }
Next, create a file called gulpfile.js, and put the following code in it:
var gulp = require('gulp'), uglify = require('gulp-uglify'), concat = require('gulp-concat'); gulp.task('default', function() { return gulp.src('js/*.js') .pipe(concat('all.js')) .pipe(gulp.dest('dist/')); });
To test this out, I downloaded jquery and jquery-ui, and put the uncompressed Javascript files in a “js” folder. Having created the Gulpfile above, all you need is to run Gulp:
gulp
You should find a folder called dist, with a file called all.js in it, containing the contents of the files originally in the js folder:
Concatenating JavaScript is good for performance because the browser only needs to make a single request, rather than having to retrieve several small files. But we can do even better by minifying the JavaScript (using the gulp-uglify plugin). Just add the following line:
var gulp = require('gulp'), uglify = require('gulp-uglify'), concat = require('gulp-concat'); gulp.task('default', function() { return gulp.src('js/*.js') .pipe(concat('all.js')) .pipe(uglify()) .pipe(gulp.dest('dist/')); });
Run Gulp again, and you’ll find that all.js has been updated. In fact, it’s much smaller now, and it’s completely illegible:
Conclusion and Further Reading
The purpose of this article was to get you set up with Gulp, and see something working with the least possible hassle. Mark Goodyear’s article (on which this article is partly based) covers a lot of other common operations to carry out with Gulp. If you need to do anything particular – linting your JavaScript files, minifying your CSS, using Less, etc, there’s probably a plugin for it.
Beyond that, all you need to know is how to use Gulp effectively as part of your build process.
- Running Gulp without arguments makes it look for the “default” task. You can pass the name of a task to run as an argument, allowing you to run a variety of operations.
- How do you debug your minified JavaScript? You don’t. Use separate tasks for development and for release, and minify only in your release task.
- Ideally these tasks should be run automatically as part of your continuous integration.
- An ASP .NET 5 (formerly known as vNext) project in Visual Studio 2015 can easily integrate with npm tools, and you can configure it to run your tasks when you build.
- Not using Windows? These command line tools are easy to use on other platforms (although installing npm will obviously be different).
Update 8th January 2016: Check out “More Gulp in Practice“, the followup to this article.