How to set up Gulp for Compass compilation and minification, JavaScript minification, livereloading, and use with ExpressionEngine.
#node files
/node_modules/*
#sass cache files
.sass-cache
//load plugins
var gulp = require('gulp'),
compass = require('gulp-compass'),
autoprefixer = require('gulp-autoprefixer'),
minifycss = require('gulp-minify-css'),
uglify = require('gulp-uglify'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
notify = require('gulp-notify'),
livereload = require('gulp-livereload'),
plumber = require('gulp-plumber'),
path = require('path');
//the title and icon that will be used for the Grunt notifications
var notifyInfo = {
title: 'Gulp',
icon: path.join(__dirname, 'gulp.png')
};
//error notification settings for plumber
var plumberErrorHandler = { errorHandler: notify.onError({
title: notifyInfo.title,
icon: notifyInfo.icon,
message: "Error: <%= error.message %>"
})
};
//styles
gulp.task('styles', function() {
return gulp.src(['src/scss/**/*.scss'])
.pipe(plumber(plumberErrorHandler))
.pipe(compass({
css: 'html/css',
sass: 'src/scss',
image: 'html/images'
}))
.pipe(autoprefixer('last 2 version', 'safari 5', 'ie 7', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
.pipe(gulp.dest('html/css'))
.pipe(rename({ suffix: '.min' }))
.pipe(minifycss())
.pipe(gulp.dest('html/css'));
});
//scripts
gulp.task('scripts', function() {
return gulp.src('src/js/**/*.js')
.pipe(plumber(plumberErrorHandler))
.pipe(concat('main.js'))
.pipe(gulp.dest('html/js'))
.pipe(rename({ suffix: '.min' }))
.pipe(uglify())
.pipe(gulp.dest('html/js'));
});
//watch
gulp.task('live', function() {
livereload.listen();
//watch .scss files
gulp.watch('src/scss/**/*.scss', ['styles']);
//watch .js files
gulp.watch('src/js/**/*.js', ['scripts']);
//reload when a template file, the minified css, or the minified js file changes
gulp.watch('templates/**/*.html', 'html/css/styles.min.css', 'html/js/main.min.js', function(event) {
gulp.src(event.path)
.pipe(plumber())
.pipe(livereload())
.pipe(notify({
title: notifyInfo.title,
icon: notifyInfo.icon,
message: event.path.replace(__dirname, '').replace(/\\/g, '/') + ' was ' + event.type + ' and reloaded'
})
);
});
});
I freaking love working with technologies like Grunt and Gulp, and wanted to share how to get my current EE front-end workflow set up. With a few tweaks, this can also be used with virtually any other sites (I've used it with Laravel, static sites, Craft, etc).
npm install gulp -g
If a package.json file already exists with list of all of the dependencies that Gulp will need for the project, then you can simply switch to the project root directory in the site terminal and run the command npm install
. This only needs to happen once so that the dependencies can be downloaded for the project.
If a package.json file does not exist, switch to the project root directory for the site in a terminal. For this configuration, you can install gulp and the needed dependencies using the command npm install gulp gulp-compass gulp-autoprefixer gulp-minify-css gulp-uglify gulp-rename gulp-concat gulp-notify gulp-livereload gulp-plumber --save-dev
. You only need to do this once so that the dependencies can be downloaded for the project.
Copy the gulp.js file (below) to the base of your site. The provided gulp.js file is set up to work with the following project directory structure.
Make sure Growl is running, as notification will appear in it whenever the browser is livereloaded or whenever there is an error.
gulp
. You can also run any of the other task names, like gulp styles
.gulp live
. If the livereload browser extensions are installed and enabled, the browser will automatically refresh when any changes are made to style and javascript files in the src directory, or to any template files in the templates directory.You can change the title and image that show up in the Growl messages, by editing the notifyInfo
object values. :)