Skip to content

Instantly share code, notes, and snippets.

@eddiesholl
Created August 31, 2017 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddiesholl/431f6a05923a2d895af9a9a8cec9f6ad to your computer and use it in GitHub Desktop.
Save eddiesholl/431f6a05923a2d895af9a9a8cec9f6ad to your computer and use it in GitHub Desktop.
Tools for processing webpack output, to help identify time spent
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))
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment