Created
August 31, 2017 07:58
-
-
Save eddiesholl/431f6a05923a2d895af9a9a8cec9f6ad to your computer and use it in GitHub Desktop.
Revisions
-
eddiesholl created this gist
Aug 31, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,63 @@ const input = require('./factory.json') const addPaths = (name, prefix) => prefix === '' ? name : `${prefix}/${name}` const createNode = (name, subPath) => { return { name, path: addPaths(name, subPath), cost: 0, children: [] } } const getOrCreateChild = (tree, childName, subPath) => { const existingChild = tree.children.find(e => e.name === childName) if (existingChild) { return existingChild } else { const newChild = createNode(childName, subPath) tree.children.push(newChild) return newChild } } const mergePaths = (currentTree, modulePath, moduleSize, elems, subPath = '') => { if (elems.length === 0) { return currentTree } const elemHead = elems[0] const nodeForHead = getOrCreateChild(currentTree, elemHead, subPath) nodeForHead.cost = nodeForHead.cost + moduleSize if (elems.length === 1) { nodeForHead.module = modulePath } mergePaths( nodeForHead, modulePath, moduleSize, elems.slice(1), addPaths(elemHead, subPath)) return currentTree } const rootNode = createNode('root') const subset = input // .slice(0, 5) const result = subset.reduce((currentTree, curr) => { const [modulePath, moduleSize] = curr const elems = modulePath.split('/') return mergePaths(currentTree, modulePath, moduleSize, elems) }, rootNode) const sortTree = (node) => { node.children.sort((a, b) => b.cost - a.cost) node.children.forEach(sortTree) } sortTree(result) console.log(JSON.stringify(result, 0, 2)) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ # Initial pass of jq to trim down webpack output jq '.modules | map([.name, .profile.factory])' stats.json > factory.json # Create a tree summary node factory.js > tree.json # Once you have generated a tree summary jq '.children[0].children[0].children[] | { name:.name, cost:.cost, path:.path }' tree.json