Skip to content

Instantly share code, notes, and snippets.

@builtbylane
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save builtbylane/921b2df18b224ba27467 to your computer and use it in GitHub Desktop.
Save builtbylane/921b2df18b224ba27467 to your computer and use it in GitHub Desktop.
watchifying multiple bundles with gulp
var gulp = require('gulp');
var filter = require('gulp-filter');
var source = require('vinyl-source-stream');
var gutil = require('gulp-util');
var path = require('path');
var colors = require('colors');
var createBundle = function(file) {
var method = (env.prebuild || env.stage || env.production) ? 'browserify' : 'watchify';
var bundler = require('browserify')({
cache: {}
, packageCache: {}
, fullPaths: true
, entries: [file.path]
, debug: true
});
var rebundle = function() {
var startTime = new Date().getTime();
gutil.log(( method + 'ing').magenta + ' : ' + file.path.yellow);
// uglify-ify it?
if (env.prebuild || env.production) {
gutil.log(('uglify-ify-ing').magenta + ' : ' + file.path.yellow);
bundler.transform({
global: true,
source_map: false
},require('uglifyify'));
}
bundler
.external('backbone')
.external('underscore')
.external('underscore.string')
.external('redefLibs')
// elsify stream
.transform(require('ejsify'))
.transform(require('brfs'))
// attempting to hijack the errors
.on('error', function(args){
gutil.log(args);
})
// log time
.on('end', function(){
var time = (new Date().getTime() - startTime) / 1000;
gutil.log( file.path.yellow + ' b-fied ' + time.magenta + ' s' );
})
// bundle it.
.bundle()
// return bundled content to a stream of the same name as original
.pipe(source(path.basename(file.path)))
// keep it organized by app name
.pipe( gulp.dest((env.prebuild ? 'dist/static/' : './static/') + path.dirname(file.path).replace( __dirname + '/apps/', '' )));
};
// watch.
if(!env.stage && !env.production && !env.prebuild ) {
bundler = require('watchify')(bundler);
bundler.on('update', rebundle);
}
// init
return rebundle();
};
gulp.task('browserify', function(){
// there are about 18 sources, and 35 dependencies total
return gulp.src( './apps/**/+(client|dev)*.js')
// pull each file out of the stream
.pipe(require('gulp-tap')(function(file, t) {
createBundle(file);
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment