Last active
August 29, 2015 14:03
-
-
Save jayperryworks/154a1b65adaf2415e5e5 to your computer and use it in GitHub Desktop.
gulpfile for jekyll (compiled)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// based on generator-gulp-webapp 0.1.0 | |
var gulp = require('gulp'); | |
// var connect = require('gulp-connect'); | |
// local server port | |
var SERVE_PORT = 4000; | |
// tell me what the error is! | |
// -> prevent .pipe from dying on error w/ gulp-plumber | |
// -> and give more useful error messages | |
var showError = function(err) { | |
$.util.beep(); | |
console.log(err); | |
}; | |
// useful file paths | |
var path = { | |
src : 'src', | |
build : 'build', | |
deploy : 'deploy', | |
bower : '_bower_components', | |
assets : 'assets', | |
css : 'assets/styles', | |
js : 'assets/scripts', | |
img : 'assets/images' | |
}; | |
// load plugins | |
var $ = require('gulp-load-plugins')(); | |
gulp.task('scripts', function () { | |
return gulp.src(path.build + '/' + path.js + '/**/*.js') | |
.pipe($.jshint()) | |
.pipe($.jshint.reporter(require('jshint-stylish'))) | |
.pipe($.size()); | |
}); | |
gulp.task('jekyll', function() { | |
var reportOptions = { | |
err: true, | |
stderr: true, | |
stdout: true | |
} | |
return gulp.src('') | |
.pipe($.plumber({ | |
errorHandler: showError | |
})) | |
// run a build and then reload the browser | |
.pipe($.exec("jekyll build")) | |
.pipe($.exec.reporter(reportOptions)) | |
.pipe($.connect.reload()); | |
}); | |
gulp.task('jekyll-deploy', function() { | |
return gulp.src('') | |
// run a build with the deploy config | |
.pipe($.exec("jekyll build --config _config.yml,_config.deploy.yml")); | |
}); | |
// run jekyll, concatenate everything, update css & script links | |
gulp.task('html', ['scripts', 'jekyll-deploy'], function () { | |
var jsFilter = $.filter('**/*.js'); | |
var cssFilter = $.filter('**/*.css'); | |
return gulp.src(path.build + '/**/*{.html,.md}') | |
.pipe($.useref.assets({searchPath: '{.tmp,' + path.build + '}'})) | |
.pipe(jsFilter) | |
.pipe($.uglify()) | |
.pipe(jsFilter.restore()) | |
.pipe(cssFilter) | |
.pipe($.csso()) | |
.pipe(cssFilter.restore()) | |
.pipe($.useref.restore()) | |
.pipe($.useref()) | |
.pipe(gulp.dest(path.deploy)) | |
.pipe($.size()); | |
}); | |
// optimize images | |
gulp.task('images', function () { | |
return gulp.src(path.build + '/' + path.img + '/**/*') | |
.pipe($.cache($.imagemin({ | |
optimizationLevel: 3, | |
progressive: true, | |
interlaced: true | |
}))) | |
.pipe(gulp.dest(path.deploy + '/' + path.img)) | |
.pipe($.size()); | |
}); | |
gulp.task('clean', function () { | |
return gulp.src(['.tmp', path.deploy + '/**/*', '!' + path.deploy + '/{.git,.gitignore,CNAME,README.md,LICENSE}'], { read: false }).pipe($.clean()); | |
}); | |
// livereload server | |
gulp.task('connect', function() { | |
$.connect.server({ | |
root: path.build, | |
port: SERVE_PORT, | |
livereload: true | |
}); | |
}); | |
// -------- Main Tasks ----------------------------------------------------------------------------- | |
gulp.task('build', ['jekyll-build']); | |
gulp.task('deploy', ['clean'], function() { | |
gulp.start(['html', 'images']); | |
}); | |
gulp.task('watch', ['connect'], function() { | |
gulp.watch(path.src + '/**/*{.html,.md,.scss,.js,.yml}', ['jekyll']); | |
}); | |
// inject bower components | |
gulp.task('wiredep', function() { | |
var wiredep = require('wiredep').stream; | |
gulp.src(path.src + '/' + path.css + '/*.scss') | |
.pipe(wiredep({ | |
directory: path.src + '/' + path.bower | |
})) | |
.pipe(gulp.dest(path.src + '/' + path.css)); | |
gulp.src(path.src + '/*.html') | |
.pipe(wiredep({ | |
directory: path.src + '/' + path.bower | |
})) | |
.pipe(gulp.dest(path.src)); | |
}); | |
gulp.task('default', ['jekyll', 'watch']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment