Forked from MiguelCastillo/jquery-bootstrap-browserify.js
Created
September 3, 2016 13:21
-
-
Save peters/7ee97300680d44e1f314e0c5f53ccd12 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