Skip to content

Instantly share code, notes, and snippets.

@cyjake
Created May 13, 2019 05:55
Show Gist options
  • Save cyjake/898ecd9f3433a46e07bdb374a47ce409 to your computer and use it in GitHub Desktop.
Save cyjake/898ecd9f3433a46e07bdb374a47ce409 to your computer and use it in GitHub Desktop.
analyse web dependencies with porter
const Porter = require('@cara/porter');
const porter = new Porter({
paths: '.',
entries: 'examples/index.js',
});
(async function() {
await porter.ready;
const packages = [];
for (const pkg of porter.package.all) {
const { name, version, parent, files } = pkg;
await Promise.all(Object.values(files).map(mod => mod.obtain()));
const size = Object.values(files).reduce((total, mod) => {
return total + mod.cache.code.length;
}, 0);
packages.push({ name, size, parent, version });
}
packages.sort((a, b) => {
return a.size > b.size ? -1 : (a.size < b.size ? 1 : 0);
});
console.table(packages.map(({ name, version, size, parent }) => {
const path = [];
if (parent) {
path.unshift(parent.name);
while ((parent = parent.parent)) path.unshift(parent.name);
}
return { slug: `${name}/${version}`, size: `${(size / 1024.0).toFixed(2)}KB`, path: path.slice(1).join(' → ') };
}));
})()
.catch(err => {
console.error(err.stack);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment