Coming off of having a presentation on the subject of front-end operations using grunt, I was feeling like I have a certain amount of expertise with Grunt, and a new appriciation for it, but now I want to try my hand out on the new Gulp build tool for Node.js. Gulp leverages the core power behind Node.js and uses it to enforce developers to not write out verbose configuration files, but rather write their own tasks using the Node APIs.
You need to install gulp globally first so that it can be available on your path, to test try using the
--version to make sure that it is installed correctly.
Package.json and packages!
If you don't already have a
package.json in your project, you may want to use
npm init to get the nice little CLI dialog to create it for you. Once the file is available, we will want to install a local gulp package,
npm install --save-dev gulp. Now we can get started.
I try to use CoffeeScript whenever possible, I find there is less of a change of syntax errors like missing semi-colons or commas. Grunt had built in support for CoffeeScript, but Gulp does not, and I doubt it will. The easiest way I found to allow you to write tasks in coffee-script is by requiring it at the base
gulpfile.js. You will also need to install it with
npm install --save-dev coffee-script. I wanted to seperate out the tasks into a couple of small files as well, in a directory I called
_gulp so jekyll will ignore it by default.
Well let's compile sass down, there are two good plugin's out there. The first,
gulp-sass uses the
node-sass, which is wicked fast, but has less features and less stable. I went with the ruby sass plugin,
gulp-ruby-sass. Then it was pretty easy to get the plugin added to my project. Install the gulp plugin with
npm install --save-dev gulp-ruby-sass.
Now that we have these set up, I added the first task using the
gulp.task method to add the new task to the markup.
Moving each task into small modules
So just like
load-grunt-config, it is nice to have smaller pieces of code to work with, so splitting the tasks into modules would be fairly easy. The
scripts.coffee module would go into
./_src/scripts.coffee, then we could just pass the gulp object to the function.
Then it is really easy to include the individual tasks,
require('./scripts.coffee')(gulp), storing the config in JSON is super easy, although I guess I could use the package.json config key in the spec.
Writing my own Jekyll plugin with a few lines
Simple enough to get started. I think you start seeing how easy it would be to write your own plugins to use. The only jekyll plugin for gulp isn't very stable yet, so I thought I would give writting a custom task that uses the child process module to span a new process and handle the errors and stodout a bit.
Watch is built in!
Nifty, the watch task is just built into gulp in a nice way. It is just a method you use. Then it becomes really easy to write watch tasks here.
So what to do now?
I would like to get more projects using Gulp to try it out a bit more. It is hard though since I don't want to redo them just for the sake of prefering the tool. I will have to wait and see.