Created
March 31, 2015 03:44
-
-
Save MiguelCastillo/e456d864668cbf5c6aa3 to your computer and use it in GitHub Desktop.
Adding require jquery to bootstrap via a browserify transform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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