Browserify + Globs

Browserify + Uglify2 shows how to setup a basic gulp task to bundle a JavaScript file with its dependencies, and minify the bundle with UglifyJS while preserving source maps. It does not, however, show how one may use gulp and Browserify with multiple entry files.

See also: the Combining Streams to Handle Errors recipe for handling errors with Browserify or UglifyJS in your stream.

'use strict';

var browserify = require('browserify');
var gulp = require('gulp');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var globby = require('globby');
var through = require('through2');
var log = require('gulplog');
var uglify = require('gulp-uglify');
var sourcemaps = require('gulp-sourcemaps');
var reactify = require('reactify');

gulp.task('javascript', function () {
  // gulp expects tasks to return a stream, so we create one here.
  var bundledStream = through();

    // turns the output bundle stream into a stream containing
    // the normal attributes gulp plugins expect.
    // the rest of the gulp task, as you would normally write it.
    // here we're copying from the Browserify + Uglify2 recipe.
    .pipe(sourcemaps.init({loadMaps: true}))
      // Add gulp plugins to the pipeline here.
      .on('error', log.error)

  // "globby" replaces the normal "gulp.src" as Browserify
  // creates it's own readable stream.
  globby(['./entries/*.js']).then(function(entries) {
    // 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.
  }).catch(function(err) {
    // ensure any errors from globby are handled
    bundledStream.emit('error', err);

  // finally, we return the stream, so gulp knows when this task is done.
  return bundledStream;
Last Updated: 8/11/2018, 4:54:05 PM