Skip to content

Instantly share code, notes, and snippets.

@wosephjeber
Last active September 11, 2023 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wosephjeber/c66015c8002ba276674c277648bf1cd1 to your computer and use it in GitHub Desktop.
Save wosephjeber/c66015c8002ba276674c277648bf1cd1 to your computer and use it in GitHub Desktop.
Quick Node script to find largest dependency in package.json
const { dependencies } = require('../package.json');
const deps = Object.keys(dependencies).map((p) => `${p}@${dependencies[p]}`);
function pluralize(count, singular, plural) {
if (count === 1) return `${count} ${singular}`;
return `${count} ${plural}`;
}
function wait(milliseconds) {
return new Promise((resolve, _reject) => {
setTimeout(resolve, milliseconds);
});
}
async function getResults() {
const results = [];
// Fetching the package stats one at a time, with a delay, because I pretty
// consistently got rate limited when fetching them faster.
for (const dep of deps) {
await fetch(`https://bundlephobia.com/api/size?package=${dep}`)
.then((response) => response.json())
.then((result) => {
results.push(result);
})
.catch(() => {
console.log(`could not fetch ${dep}`);
});
await wait(200);
process.stdout.write(
'Checked ' + results.length + ' of ' + deps.length + ' dependencies\r'
);
}
results.sort((a, b) => b.size - a.size);
results.forEach((result) => {
if (!result) return;
const { dependencyCount, name, size, gzip } = result;
const depsString = pluralize(dependencyCount, 'dependency', 'dependencies');
const sizeString = `${size / 1024}kb (${gzip / 1024}kb gzipped)`;
console.log(`${name} | ${sizeString} | ${depsString}`);
});
}
console.log(`Getting package stats for ${deps.length} dependencies...`);
getResults();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment