Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
In Search of the One True Gulpfile
import gulp from 'gulp';
import gif from 'gulp-if';
import smaps from 'gulp-sourcemaps';
import babel from 'gulp-babel';
import concat from 'gulp-concat';
import uglify from 'gulp-uglify';
import jas from 'gulp-jasmine';
import conn from 'gulp-connect';
import yargs from 'yargs';
import config from './package.json';
const args = yargs.argv, paths = {
src: 'src/**/*.js',
build: 'build',
dist: 'dist',
specSrc: 'spec/**/*Spec.js',
specDest: 'build/spec',
spec: 'build/spec/**/*Spec.js'
const conf = (name, override) => Object.assign(({
src: { dest: },
spec: { src: paths.specSrc, dest: paths.specDest },
test: { src: paths.spec, pipe: jas({ includeStackTrace: true }) },
dist: { dest: paths.dist, file: `${}.js`, smaps: true },
min: { dest: paths.dist, file: `${}.min.js`, min: true, smaps: false }
})[name], override || {});
const pipeline = opts => [gulp.src(opts.src || paths.src)].concat(opts.pipe || [
gif(!!opts.smaps, smaps.init()),
gif(!!opts.modules, babel({ modules: opts.modules })),
gif(!!opts.file, concat(opts.file || `${}.js`)),
gif(!!opts.min, uglify()),
gif(!!opts.smaps, smaps.write('.')),
const chain = steps => steps.reduce((prev, step) => prev && prev.pipe(step) || step);
const build = (name, opts) => chain(pipeline(conf(name, opts)));
const bind = (fn, ...args) => () => fn(...args);
gulp.task('build-src', bind(build, 'src'));
gulp.task('build-spec', bind(build, 'spec'));
gulp.task('test', ['build-src', 'build-spec'], bind(build, 'test'));
gulp.task('serve', () => {
var port =;
return (port) ? conn.server({ root: __dirname, port }) : console.log("Server not configured");
gulp.task('dist', bind(build, args.min ? 'min' : 'dist', Object.assign({ modules: 'common' }, args)));
gulp.task('watch',, [paths.src, paths.specSrc], ['test']));
gulp.task('live', ['serve', 'watch']);
gulp.task('default', ['test', 'dist']);
gulp.on('err', e => console.log("Gulp error:", e.err.stack));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment