Last active
November 10, 2015 14:39
-
-
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/
This file contains hidden or 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
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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think a few of the globbing patterns in your One True Gulpfile may be mistaken. For example,
src/*/**.js
should besrc/**/*.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.