Skip to content

Instantly share code, notes, and snippets.

Last active February 10, 2019 23:21
Show Gist options
  • Save joemaller/b6372dea1bdaf2a81761 to your computer and use it in GitHub Desktop.
Save joemaller/b6372dea1bdaf2a81761 to your computer and use it in GitHub Desktop.
A simple gulpfile example

Build-tools and Live Reload

As creating for the web has gotten more complex, build tools have become an essential part of my workflow. These tools are usually used to automate repetitive tasks such as pre-processing CSS stylesheets from Sass or Less, or compiling JavaScript from many smaller files. They can also reload browsers when files change -- not having to constantly mash reload is a significant productivity boost. Working without auto-refresh now feels like trying to type in mittens.

Gulp and Grunt run from the Terminal, CodeKit and LiveReload are standalone Mac apps. Gulp is relatively new, is very fast and its task definition files are written in JavaScript. Grunt is more established, works well and has a ton of people using it, but can be slower and requires a lot more configuration using verbose JSON files. CodeKit and LiveReload help with common workflows, Gulp and Grunt can do just about anything imaginable.

I’d been happily using Grunt for a while, but increasing buzz around Gulp was impossible to ignore and I decided to give it a try. I was immediately sold on Gulp's speed and how quickly I was able to write basic tasks. Before Grunt, I used CodeKit.

Gulp is still a young project and definitely rough around the edges, but I still prefer it to Grunt. Wrapping my head around Node Streams took a little work, but that knowledge investment isn’t limited to this tool. There’s also some really good thinking going into Gulp 4, and many of Gulp's warts will be fixed when the new version is released.

I'm working towards moving our gulpfiles into a public repository, but for now here's a simple gulpfile for building an HTML template. In less than 1/10 of a second, it does the following:

  • Compiles and injects CSS styles from SCSS source files.
  • Copies the images folder to the build directory
  • Creates a zip archive of all images, also in the build directory
  • Starts a LiveReload server, injects a code snippet and watches files for changes
'use strict';
var gulp = require('gulp');
var sass = require('node-sass');
var rename = require('gulp-rename');
var replace = require('gulp-replace');
var zip = require('gulp-zip');
var rimraf = require('gulp-rimraf');
var webserver = require('gulp-webserver');
gulp.task('build', ['images'], function() {
var css = sass.renderSync({
file: 'template/scss/styles.scss'
return gulp.src('template/responsive.html')
.pipe(replace('/* INSERT_CSS_HERE */', css))
gulp.task('clean', function() {
return gulp.src('build', {
read: false
gulp.task('images', function() {
return gulp.src('template/images/*')
gulp.task('webserver', ['build'], function() {
return gulp.src('build')
livereload: true,
fallback: 'index.html'
gulp.task('watch', ['webserver'], function() {
return'template/**', ['build']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment