UNPKG

bonsai-analyzer

Version:
50 lines (45 loc) 1.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = getRawStats; exports.getStatsJson = getStatsJson; exports.getMultiStatsJson = getMultiStatsJson; function invariant(condition, message) { if (!condition) { // eslint-disable-next-line no-console console.warn(`Could not find 'chunks' field.`); throw new Error(message); } } function getRawStats(json) { try { const stats = getStatsJson(json); return [stats]; } catch (singleError) { try { const multiStats = getMultiStatsJson(json); return multiStats.children; } catch (multiError) { throw new Error('Unable to find required fields.'); } } } function getStatsJson(json) { invariant(json, 'Empty object found. Stats file must be a populated object.'); invariant(json.chunks, `Could not find 'chunks' field.`); invariant(json.modules, `Could not find 'modules' field.`); invariant(Array.isArray(json.chunks), `Found field 'chunks' but the value is not an array.`); invariant(Array.isArray(json.modules), `Found field 'modules' but the value is not an array.`); json.modules.forEach(mod => { delete mod.source; }); return json; } function getMultiStatsJson(json) { invariant(json, 'Empty object found. Stats file must be a populated object.'); invariant(json.children, `Could not find 'children' field.`); invariant(Array.isArray(json.children), `Found field 'children' but the value is not an array.`); json.children.forEach(getStatsJson); return json; }