Merge pull request #56 from enigmamarketing/master

Add ability to pass through transform plugins to browserify.
This commit is contained in:
Philipp Kunz 2016-05-13 19:55:09 +02:00
commit 540d9a9b56

View File

@ -2,10 +2,15 @@
import plugins = require("./gulpbrowser.plugins"); import plugins = require("./gulpbrowser.plugins");
let browserify = function() { let browserify = function(transforms) {
transforms = transforms || [];
if (!Array.isArray(transforms)) {
transforms = [transforms];
}
let forEach = function(file, enc, cb){ // do this with every chunk (file in gulp terms) let forEach = function(file, enc, cb){ // do this with every chunk (file in gulp terms)
let bundleCallback = function(err, bufferedContent) { // our bundle callback for when browserify is finished let bundleCallback = function(err, bufferedContent) { // our bundle callback for when browserify is finished
if (Buffer.isBuffer(bufferedContent)){ if (Buffer.isBuffer(bufferedContent)){
file.contents = bufferedContent; file.contents = bufferedContent;
@ -16,21 +21,30 @@ let browserify = function() {
} }
cb(null,file); cb(null,file);
}; };
if(file.contents.length > 0){ if(file.contents.length > 0){
plugins.browserify(file, { basedir: file.base }) let browserified = plugins.browserify(file, { basedir: file.base });
.bundle(bundleCallback);
transforms.forEach(function (transform) {
if (typeof transform === 'function') {
browserified.transform(transform);
} else {
browserified.transform(transform.transform, transform.options);
}
});
browserified.bundle(bundleCallback);
} else { } else {
plugins.beautylog.warn("gulp-browser: .browserify() file.contents appears to be empty"); plugins.beautylog.warn("gulp-browser: .browserify() file.contents appears to be empty");
cb(null,file); cb(null,file);
}; };
} }
let atEnd = function(cb){ let atEnd = function(cb){
cb(); cb();
} // no need to clean up after ourselves } // no need to clean up after ourselves
return plugins.through.obj(forEach,atEnd); // this is the through object that gets returned by gulpBrowser.browserify(); return plugins.through.obj(forEach,atEnd); // this is the through object that gets returned by gulpBrowser.browserify();
}; };
export = browserify; export = browserify;