Skip to content

Instantly share code, notes, and snippets.

@nateabele
Last active November 10, 2015 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nateabele/090bd6bda8b3e8f2de31 to your computer and use it in GitHub Desktop.
Save nateabele/090bd6bda8b3e8f2de31 to your computer and use it in GitHub Desktop.
In Search of the One True Gulpfile – http://radify.io/blog/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: paths.build },
spec: { src: paths.specSrc, dest: paths.specDest },
test: { src: paths.spec, pipe: jas({ includeStackTrace: true }) },
dist: { dest: paths.dist, file: `${config.build.name}.js`, smaps: true },
min: { dest: paths.dist, file: `${config.build.name}.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 || `${config.build.name}.js`)),
gif(!!opts.min, uglify()),
gif(!!opts.smaps, smaps.write('.')),
gulp.dest(opts.dest)
]);
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 = config.build.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', gulp.watch.bind(gulp, [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));
@jfmercer
Copy link

I think a few of the globbing patterns in your One True Gulpfile may be mistaken. For example, src/*/**.js should be src/**/*.js. Specifically, I'm thinking of lines 13, 16, & 18.

I've created a fork which has, I believe, the correct patterns. However, since I cannot PR a gist, there's no formal way for me to request & discuss potential changes.

@nateabele
Copy link
Author

Fixed, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment