UNPKG

bonsai-analyzer

Version:
112 lines (96 loc) 2.9 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = getEntryHierarchy; exports.flattenChunksByParent = flattenChunksByParent; exports.ROOT_ID = void 0; var _getChunkName = _interopRequireDefault(require("./getChunkName")); var _getChunkNamesFromImportedModules = _interopRequireDefault(require("./getChunkNamesFromImportedModules")); var _getEntryChunks = _interopRequireDefault(require("./getEntryChunks")); const ROOT_ID = Number.MIN_SAFE_INTEGER; exports.ROOT_ID = ROOT_ID; function getSyncName(name) { if (name.indexOf('./node_modules/promise-loader?') === 0) { return name.replace('./node_modules/promise-loader?', ''); } else { const match = name.match(/import\('([\w\S]+)'\)/); if (match) { return match[1]; } else { return name; } } } function getEntryHierarchy(stats) { const importedChunkNames = (0, _getChunkNamesFromImportedModules.default)(stats); const entryChunks = (0, _getEntryChunks.default)(stats); const rootChunks = []; const chunksById = {}; const asyncChunksById = {}; entryChunks.forEach(chunk => { const name = (0, _getChunkName.default)(chunk, importedChunkNames); chunksById[chunk.id] = { id: chunk.id, ids: [chunk.id], name: name, names: chunk.names, children: [] }; const syncName = getSyncName(name); if (syncName !== name) { asyncChunksById[chunk.id] = { id: chunk.id, ids: [chunk.id], name: (0, _getChunkName.default)(chunk, importedChunkNames), names: chunk.names, children: [] }; } }); entryChunks.forEach(chunk => { const id = chunk.id; const child = chunksById[id]; if (chunk.parents.length === 0) { rootChunks.push(child); } else { chunk.parents.forEach(parentId => { const parent = chunksById[parentId]; const asyncParent = asyncChunksById[parentId]; if (asyncParent) { asyncParent.children.push(child); } else { parent.children.push(child); } }); } }); return { id: ROOT_ID, ids: [], name: '', names: [], // $FlowFixMe: Object.values has an Array<mixed> here children: rootChunks.concat(Object.values(asyncChunksById)) }; } function flattenChunksByParent(chunksByParent) { const flatChunks = []; const visitedChunks = {}; function appendChildren(children, indent) { children.forEach(child => { if (!visitedChunks[child.id]) { flatChunks.push({ id: child.id, name: child.name, indent: indent }); visitedChunks[child.id] = true; } appendChildren(child.children, indent + 1); }); } appendChildren(chunksByParent, 0); return flatChunks; }