Skip to content

Instantly share code, notes, and snippets.

@matthewbeta
Created September 8, 2014 11:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save matthewbeta/25a33958de7cf5a8145d to your computer and use it in GitHub Desktop.
Save matthewbeta/25a33958de7cf5a8145d to your computer and use it in GitHub Desktop.
example gulpfile for jekyll and sass
var gulp = require('gulp');
var gutil = require('gulp-util');
var sass = require('gulp-ruby-sass') ;
var prefix = require('gulp-autoprefixer');
var uglify = require('gulp-uglify');
var concat = require('gulp-concat');
var filter = require('gulp-filter');
var sourcemaps = require('gulp-sourcemaps');
var minifycss = require('gulp-minify-css');
var browserSync = require('browser-sync');
var cp = require('child_process');
var exec = cp.exec;
var os = require('os');
var ifaces = os.networkInterfaces();
// Check if running on windows, if we are use jekyll.bat as child_process, if its not just use jekyll]
// more info: http://stackoverflow.com/q/21856861/
var jekyll = process.platform === "win32" ? "jekyll.bat" : "jekyll";
// THIS FUNCTION GETS YOUR LOCAL IP SO WE CAN SERVE THE BUILT SITE FROM IT
// No more having to keep swapping the IP
var lookupIpAddress = null;
for (var dev in ifaces) {
if(dev != "en1" && dev != "en0") {
continue;
}
ifaces[dev].forEach(function(details){
if (details.family=='IPv4') {
lookupIpAddress = details.address;
}
});
};
var ipAddress = lookupIpAddress;
// The browser sync task
// info here http://www.browsersync.io/docs/gulp/
gulp.task('browser-sync', ['sass', 'jekyll-build'], function() {
browserSync.init({
// available options here: http://www.browsersync.io/docs/options/
server: {
baseDir: './build'
},
// uses the ipaddress variable we made up top
host: ipAddress,
// A delay between changes and reloading so 2 changes in quick succession don't get missed
reloadDelay: 2000,
// all off by default, but enables multiple browsers to all browse in sync
ghostMode: {
clicks: false,
links: false,
forms: false,
scroll: false
},
// opens the browser when you run gulp
open: true,
// displays message in browser when reload happens or styles injected
notify: true,
// shows browsers connected in command line
logConnections: true
})
});
gulp.task('sass', function (){
// source file
gulp.src('./dev/assets/sass/*.scss')
.pipe(sass({
sourcemap: true,
sourcemapPath: '../../',
style: 'expanded',
// show any errors in browser sync in browser notifications
onError: browserSync.notify
}))
// where the files are built to
.pipe(gulp.dest('./dev/assets/css'))
// autoprefixer task http://git.io/FV2akw
.pipe(prefix(
// modern browsers only as POC
// not suitable setting for production environments
"last 1 version"
))
.pipe(gulp.dest('./dev/assets/css'))
// minify the css
.pipe(minifycss())
// remove and source map (css.map) files from the stream as they will cause full page reload : (
.pipe(filter('**/*.css'))
// final css
.pipe(gulp.dest('./build/assets/css'))
// pipe the stream to the browsersync reload task to update the css without refreshing the page
.pipe(browserSync.reload({stream:true}));
});
// Jekyll task - basically runs the command line command 'jekyll build' with node
gulp.task('jekyll-build', function (done) {
// uses our jekyll variable we made earlier
return cp.spawn(jekyll, ['build'], {stdio: 'inherit'})
.on('close', done);
});
// runs the jekyll command above but reloads the browser when complete
gulp.task('jekyll-rebuild', ['jekyll-build'], function () {
browserSync.reload();
});
// runs the jekyll command but using the staging options
gulp.task('jekyll-staging', function (){
exec('jekyll build --config [_config.yml,_config.staging.yml]', function(err, stdout, stderr) {
console.log(stdout);
});
});
// watch the sass and html/js files for changes, then run the appropriate task
gulp.task('watch', function () {
gulp.watch('./dev/assets/*/sass/**/*.scss', ['sass']);
gulp.watch(['./dev/**/*.html', './dev/js/**/*.js'], ['jekyll-rebuild']);
});
// The default task run when you do 'gulp' on the command line
gulp.task('default', ['browser-sync', 'watch']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment