Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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
You can’t perform that action at this time.