module.exports = createJavaScriptBundles;

var fs = require('fs');
var path = require('path');
var browserify = require('browserify');
var Uglify = require('uglify-js');

var RUN_AS_SCRIPT = module && !module.parent;
var PRODUCTION_MODE = process.env.NODE_ENV === 'production';

function log() {
  return console.error.apply(console, arguments);
}

function minify(buf) {
  return Uglify.minify(buf.toString('utf8'), {fromString: true}).code;
}

function createJavaScriptBundles(opts, cb) {
  opts = opts || {};
  if (typeof opts === 'function') {
    cb = opts; opts = {};
  }

  var commonOutput = opts.commonFile ||
    path.join(__dirname, '../public/build/common.js');

  var bundleOutput = opts.bundleOutput||
    path.join(__dirname, '../public/build/designer-bundle.js');

  var designerEntry = opts.designerEntry ||
    path.join(__dirname, '../public/designer/js/index.js');

  var externalLibraries = opts.external || [
    'jquery',
    'vendor/customevent',
    'vendor/l10n',
    'vendor/reporter',
    'webmaker-analytics',
    'webmaker-language-picker',
  ];

  var buildOptions = opts.buildOptions || {
    noparse: ['jquery'],
    insertGlobals: !PRODUCTION_MODE,
    detectGlobals: PRODUCTION_MODE,
  };

  var common = browserify(buildOptions);
  var designer = browserify(buildOptions);
  var expect = 2; // number of callbacks to expect before finishing
  var globalError = false;

  common.require(externalLibraries);
  common.bundle(process(commonOutput));

  designer.add(designerEntry);
  designer.external(externalLibraries);
  designer.bundle(process(bundleOutput));

  function process(file) {
    return function (err, code) {
      if (err && !globalError)
        return cb(globalError = err);
      if (PRODUCTION_MODE)
        code = minify(code);
      fs.writeFileSync(file, code);
      log('Wrote %d bytes to %s', code.length, file);
      return finish();
    };
  }

  function finish() {
    if (--expect > 0) return;
    return cb && cb();
  }
}

if (RUN_AS_SCRIPT) {
  createJavaScriptBundles();
}

@@ -1,12 +1,608 @@
+/**%0A * Bundle JavaScript for common modules (jQuery, i10n, etc.) as well as%0A * the designer%0A *%0A * Usage:%0A *   This can be run directly, %60node bundle-javascripts%60, which uses the%0A *   defaults, or it can be required as a module if custom options need%0A *   to be passed, %22var bundleJS = require('./bundle-javascripts')%22.%0A *%0A *   In development mode (that is, any environment where%0A *   %60process.env.NODE_ENV !== %22production%22%60) we sacrifice build size%0A *   for speed: files are left unminified, and optional global%0A *   definitions are always included to skip some expensive file%0A *   parsing.%0A */%0A%0A
 module.expor
@@ -1168,16 +1168,106 @@
 %7D;%0A  %7D%0A%0A
+  var minifyOutput = typeof opts.minify !== 'undefined' ? opts.minify : PRODUCTION_MODE;%0A%0A
   var co
@@ -2416,31 +2416,28 @@
     if (
-PRODUCTION_MODE
+minifyOutput
 )%0A      

Add header explaining usage.