Skip to content

Instantly share code, notes, and snippets.

@MiguelCastillo
Created March 31, 2015 03:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MiguelCastillo/e456d864668cbf5c6aa3 to your computer and use it in GitHub Desktop.
Save MiguelCastillo/e456d864668cbf5c6aa3 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