Created
June 16, 2020 13:13
-
-
Save albertdahlin/261b94a356e1ab31f5b49dc7053b57f1 to your computer and use it in GitHub Desktop.
Get some stats on each Elm module's contribution to your compiled code size.
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
fs = require("fs"); | |
let buff = fs.readFileSync(0); | |
let source = buff.toString(); | |
let lines = source.split('\n'); | |
let count = {}; | |
let lastName = null; | |
for (let line of lines) { | |
let name = line.match(/^var ([\w\$]+)\$/); | |
if (name) { | |
lastName = name[1].replace(/^\$(\w+)\$(\w+)\$/, '$1/$2:').replace(/\$/g, '.'); | |
} | |
if (lastName) { | |
count[lastName] = count[lastName] ? count[lastName] + 1 : 1; | |
} | |
} | |
let modules = Object.keys(count) | |
.map(key => { | |
let parts = key.split(':'); | |
let package = parts[0]; | |
let module = parts[1]; | |
return { | |
name: module, | |
package: package, | |
count: count[key] | |
} | |
}); | |
function byCount(a, b) { | |
return b.count - a.count; | |
} | |
var byPackage = {}; | |
for (let module of modules) { | |
if (byPackage[module.package]) { | |
byPackage[module.package].push(module); | |
} else { | |
byPackage[module.package] = [ module ]; | |
} | |
} | |
const sumCount = modules => modules.reduce((sum, mod) => sum + mod.count, 0); | |
var packages = | |
Object | |
.entries(byPackage) | |
.map(([ key, value ]) => { | |
return { | |
name: key, | |
modules: value.sort(byCount), | |
count: sumCount(value), | |
} | |
}) | |
.sort(byCount); | |
for (package of packages) { | |
process.stdout.write(`${package.count} ${package.name}\n`); | |
for (module of package.modules) { | |
process.stdout.write(` ${String(module.count).padStart(4)} ${module.name}\n`); | |
} | |
} | |
console.log(modules.reduce((acc, mod) => acc + mod.count, 0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment