Skip to content

Instantly share code, notes, and snippets.

@jondlm
Created October 1, 2015 22:32

Revisions

  1. jondlm created this gist Oct 1, 2015.
    46 changes: 46 additions & 0 deletions webpack_bundle_analysis.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    // Make sure you have a `bundle.js` file in the current dir and run this like:
    // # go run webpack_bundle_analysis.go | sort -n

    package main

    import (
    "fmt"
    "io/ioutil"
    "regexp"
    "strings"
    )

    func check(e error) {
    if e != nil {
    panic(e)
    }
    }

    func main() {
    re := regexp.MustCompile("[*] [0-9]+ [*]")
    m := make(map[string]int)

    dat, err := ioutil.ReadFile("./bundle.js")
    check(err)

    lines := strings.Split(string(dat), "\n")

    cur := ""

    for _, line := range lines {
    if re.MatchString(line) {
    cur = line
    } else {
    if _, ok := m[cur]; ok {
    m[cur] = m[cur] + len(line)
    } else {
    m[cur] = 0
    }
    }
    }

    total := float64(len(string(dat)))
    for k, v := range m {
    fmt.Printf("%d -- %s -- %f%% of total\n", v, k, float64(v)/total*100)
    }
    }