Created
February 17, 2016 04:16
-
-
Save tw15egan/868411a3c39c6277d4fa to your computer and use it in GitHub Desktop.
Gulpfile for Jekyll [Compatible with Github Pages]
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
var gulp = require('gulp'); | |
var browserSync = require('browser-sync'); | |
var sass = require('gulp-sass'); | |
var prefix = require('gulp-autoprefixer'); | |
var cp = require('child_process'); | |
var nano = require('gulp-cssnano'); | |
var maps = require('gulp-sourcemaps'); | |
var uglify = require('gulp-uglify'); | |
var rename = require("gulp-rename"); | |
var jekyll = process.platform === 'win32' ? 'jekyll.bat' : 'jekyll'; | |
var messages = { | |
jekyllBuild: '<span style="color: grey">Running:</span> $ jekyll build' | |
}; | |
/** | |
* Build the Jekyll Site | |
*/ | |
gulp.task('jekyll-build', function (done) { | |
browserSync.notify(messages.jekyllBuild); | |
return cp.spawn( 'bundle' , ['exec', 'jekyll', 'build', '--config', '_config.yml,_config_dev.yml'], {stdio: 'inherit'}) | |
.on('close', done); | |
}); | |
/** | |
* Rebuild Jekyll & do page reload | |
*/ | |
gulp.task('jekyll-rebuild', ['jekyll-build'], function () { | |
browserSync.reload(); | |
}); | |
/** | |
* Wait for jekyll-build, then launch the Server | |
*/ | |
gulp.task('browser-sync', ['sass', 'jekyll-build'], function() { | |
browserSync({ | |
server: { | |
baseDir: '_site' | |
} | |
}); | |
}); | |
/** | |
* Compile files from _scss into both _site/css (for live injecting) and site (for future jekyll builds) | |
*/ | |
gulp.task('sass', function () { | |
return gulp.src('css/main.scss') | |
.pipe(maps.init()) | |
.pipe(sass({ | |
includePaths: ['_sass'], | |
onError: browserSync.notify | |
})) | |
.pipe(prefix(['last 15 versions', '> 1%', 'ie 8', 'ie 7'], { cascade: true })) | |
.pipe(nano()) | |
.pipe(maps.write('./')) | |
.pipe(gulp.dest('_site/css')) | |
.pipe(browserSync.reload({stream:true})) | |
.pipe(gulp.dest('css')); | |
}); | |
/** | |
* Compile files from js into both _site/js (for live injecting) and site (for future jekyll builds) | |
*/ | |
gulp.task('js', function () { | |
return gulp.src('js/*.js') | |
.pipe(uglify()) | |
.pipe(rename('app.min.js')) | |
.pipe(gulp.dest('_site/js')) | |
.pipe(browserSync.reload({stream:true})) | |
.pipe(gulp.dest('js')); | |
}); | |
/** | |
* Watch scss files for changes & recompile | |
* Watch html/md files, run jekyll & reload BrowserSync | |
*/ | |
gulp.task('watch', function () { | |
gulp.watch('_sass/**/*.scss', ['sass']); | |
gulp.watch('js/*.js', ['js']); | |
gulp.watch(['*.html', '_layouts/*.html', '_posts/*'], ['jekyll-rebuild']); | |
}); | |
/** | |
* Default task, running just `gulp` will compile the sass, | |
* compile the jekyll site, launch BrowserSync & watch files. | |
*/ | |
gulp.task('default', ['browser-sync', 'watch']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment