Skip to content

Instantly share code, notes, and snippets.

@allejo
Created December 8, 2015 20:50
Show Gist options
  • Save allejo/c26fced887783acc85d1 to your computer and use it in GitHub Desktop.
Save allejo/c26fced887783acc85d1 to your computer and use it in GitHub Desktop.
A Gulp setup for building Jekyll websites with auto-reloading in browsers
var gulp = require('gulp');
var spawn = require('child_process').spawn;
var tinylr = require('tiny-lr')();
var jekyllPid;
function notifyLiveReload(event) {
var fileName = require('path').relative(__dirname, event.path);
tinylr.changed({
body: {
files: [fileName]
}
});
}
gulp.task('jekyll-build', function() {
if (jekyllPid) { jekyllPid.kill(); }
jekyllPid = spawn("bundle", ['exec', 'jekyll', 'build', '--watch'], {stdio: 'inherit'});
});
gulp.task('express', function() {
var express = require('express');
var app = express();
app.use(require('connect-livereload')({port: 35729}));
app.use(express.static(__dirname + "/_site"));
app.listen(8000, '0.0.0.0');
});
gulp.task('livereload', function() {
tinylr.listen(35729);
});
gulp.task('watch', function() {
gulp.watch('_config.yml', ['jekyll-build']);
gulp.watch(['_site/**/*'], notifyLiveReload);
});
gulp.task('default', ['jekyll-build', 'express', 'livereload', 'watch']);
{
"devDependencies": {
"connect-livereload": "^0.5.4",
"express": "^4.13.3",
"gulp": "^3.9.0",
"path": "^0.12.7",
"tiny-lr": "^0.2.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment