2016-12-13 22:01:25 +00:00
|
|
|
import * as stream from 'stream'
|
|
|
|
import plugins = require('./gulpbrowser.plugins')
|
|
|
|
import * as q from 'q'
|
2016-04-01 13:56:35 +00:00
|
|
|
|
2016-12-13 22:01:25 +00:00
|
|
|
let browserify = function (transforms = []) {
|
2016-05-12 10:15:04 +00:00
|
|
|
|
2016-05-13 14:31:24 +00:00
|
|
|
if (!Array.isArray(transforms)) {
|
2016-12-13 22:01:25 +00:00
|
|
|
transforms = [transforms]
|
2016-05-12 10:15:04 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 22:01:25 +00:00
|
|
|
let forEach = function (file, enc, cb) { // do this with every chunk (file in gulp terms)
|
2016-05-12 10:15:04 +00:00
|
|
|
|
2016-12-13 22:01:25 +00:00
|
|
|
let bundleCallback = function (err, bufferedContent) { // our bundle callback for when browserify is finished
|
|
|
|
if (Buffer.isBuffer(bufferedContent)) {
|
|
|
|
file.contents = bufferedContent
|
2016-04-01 13:56:35 +00:00
|
|
|
} else {
|
2016-12-13 22:01:25 +00:00
|
|
|
console.log('gulp-browser: .browserify() ' + err.message)
|
|
|
|
cb(new Error(err.message), file)
|
|
|
|
return
|
2016-04-01 13:56:35 +00:00
|
|
|
}
|
2016-12-13 22:01:25 +00:00
|
|
|
cb(null, file)
|
|
|
|
}
|
2016-05-12 10:15:04 +00:00
|
|
|
|
2016-12-13 22:01:25 +00:00
|
|
|
if (file.contents.length > 0) {
|
|
|
|
let browserified = plugins.browserify(file, { basedir: file.base })
|
2016-05-12 10:15:04 +00:00
|
|
|
|
|
|
|
transforms.forEach(function (transform) {
|
|
|
|
if (typeof transform === 'function') {
|
2016-12-13 22:01:25 +00:00
|
|
|
browserified.transform(transform)
|
2016-05-12 10:15:04 +00:00
|
|
|
} else {
|
2016-12-13 22:01:25 +00:00
|
|
|
browserified.transform(transform.transform, transform.options)
|
2016-05-12 10:15:04 +00:00
|
|
|
}
|
2016-12-13 22:01:25 +00:00
|
|
|
})
|
2016-05-12 10:15:04 +00:00
|
|
|
|
2016-12-13 22:01:25 +00:00
|
|
|
browserified.bundle(bundleCallback)
|
2016-04-01 13:56:35 +00:00
|
|
|
} else {
|
2016-12-13 22:01:25 +00:00
|
|
|
console.warn('gulp-browser: .browserify() file.contents appears to be empty')
|
|
|
|
cb(null, file)
|
2016-04-01 13:56:35 +00:00
|
|
|
};
|
|
|
|
}
|
2016-05-12 10:15:04 +00:00
|
|
|
|
2016-12-13 22:01:25 +00:00
|
|
|
let atEnd = function (cb) {
|
|
|
|
cb()
|
2016-04-01 13:56:35 +00:00
|
|
|
} // no need to clean up after ourselves
|
2016-05-12 10:15:04 +00:00
|
|
|
|
2016-12-13 22:01:25 +00:00
|
|
|
return plugins.through2.obj(forEach, atEnd) // this is the through object that gets returned by gulpBrowser.browserify();
|
|
|
|
}
|
2016-02-26 16:57:53 +00:00
|
|
|
|
2016-12-13 22:01:25 +00:00
|
|
|
export = browserify
|