bonsai-analyzer
Version:
Trim your dependency tree.
38 lines (31 loc) • 1.15 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = _default;
var _getModulesById = _interopRequireDefault(require("./getModulesById"));
var _flatten = _interopRequireDefault(require("../utils/flatten"));
function collectSingleChildrenOf(modulesById, module) {
return (0, _flatten.default)(module.requirements.map(requiredModule => {
const eReqModule = modulesById[requiredModule.id];
if (eReqModule && eReqModule.requiredByCount === 1) {
return [eReqModule, ...collectSingleChildrenOf(modulesById, eReqModule)];
} else {
return [];
}
}));
}
function _default(modules) {
const modulesById = (0, _getModulesById.default)(modules);
return modules.filter(eModule => {
return eModule.requiredByCount !== 1;
}).map(eModule => {
const records = [eModule, ...collectSingleChildrenOf(modulesById, eModule)];
return {
displayModule: eModule,
records: records,
collapsedSizeBytes: records.reduce((sum, eModule) => sum + eModule.size, 0)
};
});
}