Skip to content

Instantly share code, notes, and snippets.

@indieisaconcept
Last active February 2, 2023 19:58
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save indieisaconcept/936dbf33cfc844d87968 to your computer and use it in GitHub Desktop.
Save indieisaconcept/936dbf33cfc844d87968 to your computer and use it in GitHub Desktop.
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
Copy link

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