Created
October 1, 2015 22:32
-
-
Save jondlm/57327ec760191f0513bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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