Skip to content

Instantly share code, notes, and snippets.

@jondlm
Created October 1, 2015 22:32
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 jondlm/57327ec760191f0513bb to your computer and use it in GitHub Desktop.
Save jondlm/57327ec760191f0513bb to your computer and use it in GitHub Desktop.
// 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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment