Skip to content

Instantly share code, notes, and snippets.

@kt3k
Last active November 7, 2015 08:39
Show Gist options
  • Save kt3k/53faa34b2ddae7430425 to your computer and use it in GitHub Desktop.
Save kt3k/53faa34b2ddae7430425 to your computer and use it in GitHub Desktop.
browserify-multiple-destination.md

This example shows how to set up a task of bundling multiple entry points into multiple destinations using browserify.

The below js task bundles all the .js files under src/ as entry points and writes the results under dest/.

var gulp = require('gulp');
var plugins = require('gulp-load-plugins')();
var browserify = require('browserify');
var gutil = require('gulp-util');

gulp.task('js', function () {

  return gulp.src('src/**/*.js', {base: 'src'})

    // transform file objects using gulp-map plugin
    .pipe(plugins.map(function (file) {

      gutil.log('bundling ' + file.path);

      // replace file contents with browserify's bundle stream
      file.contents = browserify(file.path, {debug: true}).bundle();

      return file;

    }))

    // transform streaming contents into buffer contents (because gulp-sourcemaps does not support streaming contents)
    .pipe(plugins.buffer())

    // load and init sourcemaps
    .pipe(plugins.sourcemaps.init({loadMaps: true}))

    // uglify
    .pipe(plugins.uglify())

    // write sourcemaps
    .pipe(plugins.sourcemaps.write('./'))

    .pipe(gulp.dest('dest'));]

});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment