Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
browserify + watchify + viny-transform + multiple bundles
// this setup assumes you are using gulp-config for file.src & file.dest
// but can be adapted to vanilla easily.
'use strict';
var transform = require('vinyl-transform'),
browserify = require('browserify'),
watchify = require('watchify'),
// consider using gulp-load-plugins
changed = require('gulp-changed'),
gif = require('gulp-if'),
rename = require('gulp-rename'),
uglify = require('gulp-uglify'),
_ = require('lodash'),
// Task defaults
defaults = _.defaults({}, watchify.args);
module.exports = function (gulp) {
var watch = global.watch,
options = this.options(defaults),
file = this.file,
cache = {},
bundler,
bundle,
notify = function (filename) {
gulp.util.log(gulp.util.colors.green('') + ' ' + filename);
};
bundler = function (options) {
// leverage vinyl-transform to turn
// readable stream into vinyl object
return transform(function(filename) {
// return previous bundle
if (cache[filename]) {
return cache[filename].bundle();
}
var b = browserify(filename, options);
// transforms
b.transform('debowerify');
// events
b.on('bundle', notify.bind(null, 'BUNDLE ' + filename));
if (watch) {
b = watchify(b);
// events
b.on('update', bundle.bind(null, true));
// cache for use during watch
cache[filename] = b;
}
return b.bundle();
});
};
bundle = function (check) {
return gulp.src(file.src)
// don't check on first run ( i.e initialisation )
// but do for subsequent calls via bundle.on('update')
.pipe(gif(check, changed(file.dest, {
extension: '.js'
})))
.pipe(bundler(options))
.pipe(gulp.dest(file.dest))
// minification
.pipe(uglify())
.pipe(rename({
suffix: '.min'
}))
.pipe(gulp.dest(file.dest));
};
return bundle(!watch);
};
@saborrie

This comment has been minimized.

Copy link

commented Jul 20, 2016

Is this spaced out so that the teacher can write between the lines in red pen?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.