Created
May 23, 2018 07:13
-
-
Save phdesign/39b2409f2cbdac791120900b17dff79c to your computer and use it in GitHub Desktop.
Parse a webpack json output to compute size of our files vs. depedendencies
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
const buildStats = require('./build.json'); | |
function getSize(modules) { | |
return modules.reduce((acc, val) => acc + val.size, 0); | |
} | |
const tpl = (type, modules) => `${type} files | |
-------- | |
no. of modules: ${modules.length} | |
total size: ${Math.round(getSize(modules) / 1000)} KB` | |
function parseOptions() { | |
const options = { chunk: -1, listChunks: false }; | |
var args = process.argv.slice(2); | |
for (var i = 0; i < args.length; i++) { | |
if (args[i] === '--chunk' || args[i] === '-c') { | |
options.chunk = args[++i]; | |
} else if (args[i] === '--list' || args[i] === '-l') { | |
options.listChunks = true; | |
} | |
} | |
return options; | |
} | |
function printChunkStats(chunk) { | |
const allModules = chunk.modules; | |
const myModules = chunk.modules.filter(m => m.name.startsWith('./src')); | |
console.log(chunk.names[0] + '\n========\n'); | |
console.log(tpl('my', allModules)); | |
console.log(''); | |
console.log(tpl('all', myModules)); | |
const diff = (getSize(myModules) / getSize(allModules)) * 100; | |
console.log(`\nmy code: ${Math.round(diff)}%\n`); | |
} | |
function init(options) { | |
if (options.listChunks) { | |
const chunksNames = buildStats.chunks.map(c => c.names[0]); | |
console.log(`chunks: ${JSON.stringify(chunksNames)}\n`); | |
return; | |
} | |
if (options.chunk === -1) | |
buildStats.chunks.forEach((val) => printChunkStats(val)); | |
else if (options.chunk >= 0 && options.chunk <= buildStats.chunks.length) | |
printChunkStats(buildStats.chunks[options.chunk]); | |
} | |
const options = parseOptions(); | |
init(options); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment