Skip to content

Instantly share code, notes, and snippets.

@eliascotto
Last active May 29, 2019 13:04
Show Gist options
  • Save eliascotto/e4fd92185547a41f58af4b2e5464129c to your computer and use it in GitHub Desktop.
Save eliascotto/e4fd92185547a41f58af4b2e5464129c to your computer and use it in GitHub Desktop.
Gulp 4 config file for Jekyll
const path = require('path');
const child = require('child_process');
const browserSync = require('browser-sync');
const browserify = require('browserify');
const watchify = require('watchify');
const log = require('fancy-log');
const source = require('vinyl-source-stream');
const streamify = require('gulp-streamify');
const gulp = require('gulp');
const sass = require('gulp-sass');
const uglify = require('gulp-uglify');
const htmlmin = require('gulp-htmlmin');
const cleanCSS = require('gulp-clean-css');
const prefix = require('gulp-autoprefixer');
//
// Source and distribute folders paths
//
const srcDir = 'src/';
const srcStyle = srcDir + '_scss/';
const srcScripts = srcDir + '_js/';
const distDir = 'dist/';
const distStyle = distDir + 'css/';
const distScripts = distDir + 'js/';
//
// Main source files
//
const styleMain = 'main.scss';
const scriptMain = 'main.js';
const jekyll = done => {
let productionEnv = process.env;
productionEnv.JEKYLL_ENV = 'production';
const jekyll = child.spawn('jekyll' , ['build'], { env: productionEnv });
const jekyllLogger = (buffer) => {
buffer.toString()
.split(/\n/)
.forEach((message) => log('Jekyll: ' + message));
};
jekyll.stdout.on('data', jekyllLogger);
jekyll.stderr.on('data', jekyllLogger);
jekyll.on('close', done);
};
const scripts = () => {
const opts = {
entries: srcScripts + scriptMain,
debug: true,
};
const b = watchify(browserify(opts));
return b.bundle()
.on('error', log.error.bind(log, 'Browserify Error'))
.pipe(source('bundle.js'))
.pipe(streamify(uglify()))
.pipe(browserSync.reload({stream: true}))
.pipe(gulp.dest(distScripts))
.on('error', log);
};
const styles = () => {
return gulp.src(srcStyle + styleMain)
.pipe(sass({
style: 'compressed',
includePaths: ['scss'],
onError: browserSync.notify
}))
.pipe(prefix(['last 15 versions', '> 1%', 'ie 8', 'ie 7'], { cascade: true }))
.pipe(cleanCSS())
.pipe(browserSync.reload({stream: true}))
.pipe(gulp.dest(distStyle))
.on('error', log);
};
const html = () => {
return gulp.src([
path.join(distDir, '*.html'),
path.join(distDir, '*/*.html')
])
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(gulp.dest(distDir))
.on('error', log);
};
const browserSyncTask = done => {
browserSync({
open: false,
logFileChanges: true,
server: {
baseDir: './dist'
}
});
done();
};
const build = done => {
gulp.series(jekyll, gulp.parallel(styles, scripts, html))(done)
};
const browserSyncReload = done => {
browserSync.reload({stream:true});
done();
};
const watcher = done => {
gulp.watch([
path.join(srcStyle, '*.?(s)css'),
path.join(srcStyle, '**/*.?(s)css'),
], gulp.series(styles));
gulp.watch([
path.join(srcScripts, '*.js'),
path.join(srcScripts, '**/*.js'),
], gulp.series(scripts));
gulp.watch([
path.join(srcDir, '_layouts/*.html'),
path.join(srcDir, '_includes/*'),
path.join(srcDir, '_posts/*'),
path.join(srcDir, '*.html'),
path.join(srcDir, '*.md'),
], gulp.series(build, browserSyncReload));
done();
};
exports.build = build;
exports.default = gulp.series(build, browserSyncTask, watcher);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment