<commit_before>var utils = require("belty");
var jsBundler = require("bit-bundler-browserpack");
var configurator = require("setopt")();


function Bundler(options) {
  this._plugins = [];

  if (!options.provider) {
    options.provider = jsBundler(options);
  }

  configurator.configure(this, options);
}


Bundler.prototype.configure = function(options) {
  configurator.configure(this, options);
  return this;
};


Bundler.prototype.provider = function(provider) {
  this._provider = provider;
  return this;
};


Bundler.prototype.plugins = function(plugins) {
  if (!plugins) {
    throw new TypeError("Must provide plugins to register");
  }

  this._plugins = this._plugins.concat(utils.toArray(plugins));
  return this;
};


Bundler.prototype.bundle = function(context) {
  if (!context) {
    throw new TypeError("Must provide bundle context");
  }

  if (!this._provider) {
    throw new TypeError("Bundler does not have a provider configured");
  }

  //
  // Run bundler first.  Not quite sure this is the best way to handle
  // the flow of building bundles.
  // return Promise
  //   .resolve(this._provider.bundle(context))
  //   .then(setBundle(context))
  //   .then(runPlugins(this));
  //

  //
  // Evaluate whether executing the plugins first is more efficient
  // or desirable than building the main bundle and then forcing
  // each and every plugin that splits the main bundle to rebundle
  // the main bundle...  I currently can't see why we wouldn't want
  // to run the plugins first, we are going this route for now.
  //
  var bundler = this;
  return runPlugins(this)(context)
    .then(function(context) {
      return Promise
        .resolve(bundler._provider.bundle(context))
        .then(function(result) {
          return context.setBundle(result);
        });
    });
};


function runPlugins(bundler) {
  return function pluginRunner(context) {
    return bundler._plugins
      .reduce(function(next, plugin) {
        return next
          .then(function(context) {
            return plugin(bundler._provider, context);
          })
          .then(function(result) {
            return context.configure(result);
          });
      }, Promise.resolve(context));
  };
}


module.exports = Bundler;<commit_msg>Adding support for pre and post bundling plugins<commit_after>var utils = require("belty");
var types = require("dis-isa");
var jsBundler = require("bit-bundler-browserpack");
var configurator = require("setopt")();


function Bundler(options) {
  this._prebundle = [];
  this._postbundle = [];

  if (!options.provider) {
    options.provider = jsBundler(options);
  }

  configurator.configure(this, options);
}


Bundler.prototype.configure = function(options) {
  configurator.configure(this, options);
  return this;
};


Bundler.prototype.provider = function(provider) {
  this._provider = provider;
  return this;
};


Bundler.prototype.plugins = function(plugins) {
  var bundler = this;

  utils.toArray(plugins).forEach(function(plugin) {
    if (types.isFunction(plugin)) {
      plugin = {
        prebundle: plugin
      };
    }

    configurator.configure(bundler, plugin);
  });

  return this;
};


Bundler.prototype.prebundle = function(plugins) {
  if (!plugins) {
    throw new TypeError("Must provide plugins to register for prebundle");
  }

  this._prebundle = this._prebundle.concat(utils.toArray(plugins));
  return this;
};


Bundler.prototype.postbundle = function(plugins) {
  if (!plugins) {
    throw new TypeError("Must provide plugins to register for postbundle");
  }

  this._postbundle = this._postbundle.concat(utils.toArray(plugins));
  return this;
};


Bundler.prototype.bundle = function(context) {
  if (!context) {
    throw new TypeError("Must provide bundle context");
  }

  if (!this._provider) {
    throw new TypeError("Bundler does not have a provider configured");
  }

  return [runPlugins(this, this._prebundle), runBundler(this), runPlugins(this, this._postbundle)]
    .reduce(function(promise, next) {
      return promise.then(next);
    }, Promise.resolve(context));
};


function runBundler(bundler) {
  return function(context) {
    return Promise
      .resolve(bundler._provider.bundle(context))
      .then(function(result) {
        return context.setBundle(result);
      });
  };
}


function runPlugins(bundler, plugins) {
  return function pluginRunner(context) {
    return plugins.reduce(function(next, plugin) {
      return next
        .then(function(context) {
          return plugin(bundler._provider, context);
        })
        .then(function(result) {
          return context.configure(result);
        });
    }, Promise.resolve(context));
  };
}


module.exports = Bundler;
{'fraction_non_alphanumeric': 0.1266171792152704, 'fraction_numerical': 0.0, 'mean_word_length': 3.342541436464088, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '863237', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1376, 'n_words': 327}