text: /* browserify task
     ---------------
     Bundle javascripty things with browserify!
     This task is set up to generate multiple separate bundles, from
     different sources, and to use Watchify when run from the default task.
     See browserifyConfig.bundleConfigs in gulp/config.js
  */
  import gulp from 'gulp';
  import fs from 'fs';
  import browserify from 'browserify';
  import watchify from 'watchify';
  import source from 'vinyl-source-stream';
  import babelify from 'babelify';
  import cssModulesify from 'css-modulesify';
  import { browserifyConfig, cssModulesifyConfig } from '../config';
  import handleErrors from '../util/handleErrors';
  import bundleLogger from '../util/bundleLogger';
  
  gulp.task('browserify', callback => {
    let bundleQueue = browserifyConfig.bundleConfigs.length;
  
    const browserifyThis = bundleConfig => {
  
      let bundler = browserify({
        // Required watchify args
        cache: {}, packageCache: {}, fullPaths: false,
        // Specify the entry point of app
        entries: bundleConfig.entries,
        // Add file extentions to make optional in requires
        extensions: browserifyConfig.extensions,
        // Enable source maps
        debug: browserifyConfig.debug
      });
  
      const bundle = () => {
        // Log when bundling starts
        bundleLogger.start(bundleConfig.outputName);
  
        return bundler
          .bundle()
          // Report compile errors
          .on('error', handleErrors)
          // Use vinyl-source-stream to make the
          // stream gulp compatible. Specifiy the
          // desired output filename here.
          .pipe(source(bundleConfig.outputName))
          // Specify the output destination
          .pipe(gulp.dest(bundleConfig.dest))
          .on('end', reportFinished);
      };
  
      bundler.transform(babelify.configure());
      bundler.plugin(cssModulesify, cssModulesifyConfig);
  
      if (global.isWatching) {
        // Wrap with watchify and rebundle on changes
        bundler = watchify(bundler);
        // Rebundle on update
        bundler.on('update', bundle);
      }
  
      const reportFinished = () => {
        // Log when bundling completes
        bundleLogger.end(bundleConfig.outputName);
  
        if (bundleQueue) {
          bundleQueue--;
          if (bundleQueue === 0) {
            callback();
          }
        }
      };
  
      return bundle();
    };
  
    // Start bundling with Browserify for each bundleConfig specified
    browserifyConfig.bundleConfigs.forEach(browserifyThis);
  });
  