Skip to content

Instantly share code, notes, and snippets.

@eddiesholl
Created August 31, 2017 07:58
Show Gist options
  • Select an option

  • Save eddiesholl/431f6a05923a2d895af9a9a8cec9f6ad to your computer and use it in GitHub Desktop.

Select an option

Save eddiesholl/431f6a05923a2d895af9a9a8cec9f6ad to your computer and use it in GitHub Desktop.

Revisions

  1. eddiesholl created this gist Aug 31, 2017.
    63 changes: 63 additions & 0 deletions factory.js
    Original 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))
    8 changes: 8 additions & 0 deletions jq_commands
    Original 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