Skip to content

Instantly share code, notes, and snippets.

@aharris
Created August 19, 2015 13:40
Show Gist options
  • Save aharris/44805b66f1d0af598a20 to your computer and use it in GitHub Desktop.
Save aharris/44805b66f1d0af598a20 to your computer and use it in GitHub Desktop.
Gulp Browserify
gulp.task('browserify-js', function () {
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var globby = require('globby');
var through = require('through2');
var uglify = require('gulp-uglify');
var sourcemaps = require('gulp-sourcemaps');
var reactify = require('reactify');
var gutil = require('gulp-util');
// gulp expects tasks to return a stream, so we create one here.
var bundledStream = through();
bundledStream
// turns the output bundle stream into a stream containing
// the normal attributes gulp plugins expect.
// Name of compiled file
.pipe(source('app.js'))
// the rest of the gulp task, as you would normally write it.
// here we're copying from the Browserify + Uglify2 recipe.
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
// Add gulp plugins to the pipeline here.
.on('error', gutil.log)
.pipe(sourcemaps.write('./'))
// Destination of compiled file
.pipe(gulp.dest('./dest/js/'))
.pipe(connect.reload());
// "globby" replaces the normal "gulp.src" as Browserify
// creates it's own readable stream.
// Files and paths to include
globby([
'./src/js/app.js',
'./src/js/exports.js'
// './src/components/**/*.js'
], function(err, entries) {
// ensure any errors from globby are handled
if (err) {
bundledStream.emit('error', err);
return;
}
// create the Browserify instance.
var b = browserify({
entries: entries,
debug: true,
transform: [reactify]
});
// pipe the Browserify stream into the stream we created earlier
// this starts our gulp pipeline.
b.bundle().pipe(bundledStream);
});
// finally, we return the stream, so gulp knows when this task is done.
return bundledStream;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment