Plato on Github
Report Home
lib/bundle_processor.js
Maintainability
68.00
Lines of code
247
Difficulty
54.85
Estimated Errors
2.07
Function weight
By Complexity
By SLOC
// Generated by CoffeeScript 1.6.2 /* This class process raw data parts from Gatherer: - replace modules with replacers - clean up dependencies tree - re-build structure for packer Короче, это пре-процессор для пакера, много лишних телодвижений нужно сделать с кодом и это превращается в кашу, так что тут мы все подготавливаем и красиво уложеным отдаем пакеру на финальную упаковку и завязывание бантиков */ (function() { var BundleProcessor, async, util, _; _ = require('lodash'); async = require('async'); util = require('util'); BundleProcessor = (function() { function BundleProcessor(_gatherer_, _options_) { this._gatherer_ = _gatherer_; this._options_ = _options_ != null ? _options_ : {}; this._do_logging_ = (this._options_.log != null) && this._options_.log === true && ((typeof console !== "undefined" && console !== null ? console.log : void 0) != null) ? true : false; } /* This META-method bulid package and process it in one touch */ BundleProcessor.prototype.buildAll = function(package_config, method_cb) { var _this = this; return this.buldRawPackageData(package_config, function(err, code) { if (err) { return method_cb(err); } return method_cb(null, _this.changePathsToHashesInJoinedSet(_this.joinBundleSets(_this.replaceDependenciesInRawPackageData(code)))); }); }; /* BENCHMARK IMPROVE!!! On simply test data I got about 97% of time in this method and 3% at all other So, to speed up all process we are need to cache THIS part - buldRawPackageData */ /* This method will build raw package data */ BundleProcessor.prototype.buldRawPackageData = function(package_config, method_cb) { var liberal_gatherer, strict_gatherer, _ref, _this = this; _ref = this._buildGatherers(package_config), liberal_gatherer = _ref.liberal_gatherer, strict_gatherer = _ref.strict_gatherer; return async.parallel({ bundle: function(par_cb) { return _this._compileBundleSet(strict_gatherer, package_config.bundle, par_cb); }, environment: function(par_cb) { return _this._compileBundleSet(strict_gatherer, package_config.environment, par_cb); }, replacement: function(par_cb) { return _this._compileBundleSet(liberal_gatherer, package_config.replacement, par_cb); } }, function(err, data) { if (err) { return method_cb(err); } return method_cb(null, data); }); }; /* This method replace filtered dependencies in raw data to 'replacement' content Yes, sync - nothing async here */ BundleProcessor.prototype.replaceDependenciesInRawPackageData = function(package_data) { var item, replace_processor, replacement_dict, _i, _len, _ref; if (!package_data.replacement.length) { return package_data; } replacement_dict = _.reduce(package_data.replacement, function(memo, val) { memo[val.package_name] = _.values(val.dependencies_tree['.'])[0]; return memo; }, {}); replace_processor = function(bundle_pack) { return _.each(bundle_pack, function(bundle_item) { return _.each(_.values(bundle_item.dependencies_tree), function(out_val) { var dep_key, _results; _results = []; for (dep_key in out_val) { if (!(replacement_dict[dep_key] != null)) { continue; } out_val[dep_key] = replacement_dict[dep_key]; _results.push(null); } return _results; }); }); }; _ref = [package_data.bundle, package_data.environment]; for (_i = 0, _len = _ref.length; _i < _len; _i++) { item = _ref[_i]; replace_processor(item); } return package_data; }; /* This method join bundle sets to flat structure */ BundleProcessor.prototype.joinBundleSets = function(package_data) { var reduce_fn, result_obj, step, step_data; result_obj = { source_code: {}, dependencies_tree: {}, names_map: {}, members: {} }; reduce_fn = function(memo, val) { var key, _i, _len, _ref; memo.members[val.package_name] = _.values(val.dependencies_tree['.'])[0]; delete val.dependencies_tree['.']; _ref = ['source_code', 'dependencies_tree', 'names_map']; for (_i = 0, _len = _ref.length; _i < _len; _i++) { key = _ref[_i]; memo[key] = _.extend(memo[key], val[key]); } return memo; }; for (step in package_data) { step_data = package_data[step]; result_obj["" + step + "_list"] = _.map(step_data, function(val) { return val.package_name; }); _.reduce(step_data, reduce_fn, result_obj); } return result_obj; }; /* This method will change all filepaths to it hashes. Its for squize names AND may reduce some items in sources, in case one code placed in diffenrent places - it may happened for modules in node_modules folders - they have theyown dependencies */ BundleProcessor.prototype.changePathsToHashesInJoinedSet = function(package_data) { var inner_key, inner_value, key, names_to_hash, out_key, out_value, pdm, pdt, psc, tdt, tmp_dependencies_tree, tmp_source_code, value, _ref, _ref1, _ref2; names_to_hash = package_data.names_map; _ref = (pdm = package_data.members); for (key in _ref) { value = _ref[key]; pdm[key] = names_to_hash[value]; } tmp_source_code = {}; _ref1 = (psc = package_data.source_code); for (key in _ref1) { value = _ref1[key]; tmp_source_code[names_to_hash[key]] = value; } package_data.source_code = tmp_source_code; tmp_dependencies_tree = {}; _ref2 = (pdt = package_data.dependencies_tree); for (out_key in _ref2) { out_value = _ref2[out_key]; tdt = tmp_dependencies_tree[names_to_hash[out_key]] = {}; for (inner_key in out_value) { inner_value = out_value[inner_key]; tdt[inner_key] = names_to_hash[inner_value]; } } package_data.dependencies_tree = tmp_dependencies_tree; return package_data; }; /* This method compile raw 'bundle' set with source, dep_trees and names */ BundleProcessor.prototype._compileBundleSet = function(gatherer, bundle_obj, method_cb) { var map_fn; map_fn = function(_arg, map_cb) { var part_name, part_path; part_name = _arg[0], part_path = _arg[1]; return gatherer(part_path, function(err, package_data) { if (err) { return map_cb(err); } package_data.package_name = part_name; return map_cb(null, package_data); }); }; return async.map(_.pairs(bundle_obj), map_fn, function(err, res) { if (err) { return method_cb(err); } return method_cb(null, res); }); }; /* This method build two pre-fired Gatherers: strict (for bundle and environment) and liberal (for replacement) Just shorten call + now we are may have ONE gather for all */ BundleProcessor.prototype._buildGatherers = function(package_config) { var liberal_filter, requireless_filter, strict_filter, _ref, _ref1, _this = this; requireless_filter = (_ref = package_config.requireless) != null ? _ref : []; liberal_filter = (_ref1 = package_config.exclude) != null ? _ref1 : []; strict_filter = liberal_filter.concat(_.keys(package_config.replacement)); return { liberal_gatherer: function(name, cb) { return _this._gatherer_.buildModulePack(name, { filters: liberal_filter, requireless: requireless_filter }, cb); }, strict_gatherer: function(name, cb) { return _this._gatherer_.buildModulePack(name, { filters: strict_filter, requireless: requireless_filter }, cb); } }; }; return BundleProcessor; })(); module.exports = BundleProcessor; }).call(this);