Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save peters/7ee97300680d44e1f314e0c5f53ccd12 to your computer and use it in GitHub Desktop.
Save peters/7ee97300680d44e1f314e0c5f53ccd12 to your computer and use it in GitHub Desktop.
Adding require jquery to bootstrap via a browserify transform
var browserify = require('browserify');
var gulp = require('gulp');
var source = require('vinyl-source-stream');
var through = require('through');
var libs = ['jquery', 'bootstrap'];
gulp.task('app-bundle', function() {
var appBundler = browserify(['./app.js']);
libs.forEach(function(lib) {
appBundler.external(lib);
});
return appBundler.bundle()
.pipe(source('app-bundle.js'))
.pipe(gulp.dest('./build'));
});
gulp.task('vendor-bundle', function() {
var vendorBundler = browserify();
vendorBundler.transform(function(file) {
if (file.indexOf('bootstrap') === -1) {
return through();
}
var data = '';
return through(write, end);
function write (buf) { data += buf }
function end () {
data = "var jQuery = require('jquery');\n\n" + data;
this.queue(data);
this.queue(null);
}
}, {global: true});
libs.forEach(function(lib) {
vendorBundler.require(lib);
});
return vendorBundler.bundle()
.pipe(source('vendor-bundle.js'))
.pipe(gulp.dest('./build'));
});
gulp.task('default', ['app-bundle', 'vendor-bundle']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment