Skip to content

Instantly share code, notes, and snippets.

@Vestride
Created April 29, 2021 08:14
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 Vestride/6b0ff4e964a09cf3b14153b791a96e62 to your computer and use it in GitHub Desktop.
Save Vestride/6b0ff4e964a09cf3b14153b791a96e62 to your computer and use it in GitHub Desktop.
Convert 2 bundle analyzer outputs to a markdown table
#!/usr/bin/env node
const fs = require("fs").promises;
const path = require("path");
const filename = path.resolve(process.argv[2]);
// Match like "name-bundle.c660430462be77d62c18.js (2.02 MB)"
const regex = /(.+)\s\((.+)\)/;
async function getFileLines() {
let contents;
try {
const buffer = await fs.readFile(filename);
contents = buffer.toString();
} catch (error) {
console.error(error);
}
return contents.split("\n");
}
function getBundleComparison(lines) {
const data = {};
const sizes = new Map();
let isBeforeBundle = true;
for (line of lines) {
if (line === "---") {
isBeforeBundle = false;
continue;
}
const match = regex.exec(line);
if (!match) {
continue;
}
const key = match[1];
let filename = key;
const size = match[2];
const hasFilename = !!data[filename];
// If the current filename isn't already tracked, there is a file with the
// same size, and that pair hasn't been "used", use the existing filename with that size.
if (!hasFilename && sizes.has(size) && sizes.get(size).length === 1) {
[filename] = sizes.get(size);
} else if (!hasFilename) {
// Otherwise, if the filename isn't tracked yet, create the initial data for it.
data[filename] = {
before: "",
after: "",
};
}
// Update the sizes map with this line item.
sizes.set(size, [...(sizes.get(size) || []), key]);
// Update the pairs object.
data[filename][isBeforeBundle ? "before" : "after"] = size;
}
return data;
}
function getMarkdown(comparison) {
const header = "| filename | before | after |\n|----|---|----|\n";
const rows = Object.entries(comparison).map(
([key, value]) =>
`| ${key.replace(/~/g, "\\~")} | ${value.before} | ${value.after} |`
);
return `${header}${rows.join("\n")}`;
}
async function run() {
const lines = await getFileLines();
const data = getBundleComparison(lines);
const markdown = getMarkdown(data);
console.log(markdown);
}
run();
All (5.8 MB)
static/chunks/pages/_app-080c77e111409ccf344c.js (3.55 MB)
static/chunks/e881dd8a.aaabad384ef2dd6439e9.js (939.47 KB)
static/chunks/48d18726.2f59de87fa2aedcb1498.js (388.16 KB)
static/chunks/a48ca981.bdc7c53768d4798b9d15.js (229.56 KB)
static/chunks/895b5e10.4b5760a4d28bdeb6b2b2.js (136.05 KB)
static/chunks/framework.3519294afa6a9ab53973.js (126.21 KB)
static/chunks/styles.68029285599d666b2795.js (102.92 KB)
static/chunks/polyfills-dca1b5a048b3a0e31e64.js (90.02 KB)
static/chunks/2ca616f1.cd0818a071e817fd846e.js (76.72 KB)
static/chunks/0f1ac474.621d2ab38f9a717aa56b.js (70.96 KB)
static/chunks/8313d723.8c0b0bb88b23a35736ed.js (55.09 KB)
static/chunks/4f149b58.17486bb937b4246f3e79.js (40.53 KB)
static/chunks/d1edcd509e188af8a3b11bf83ac465d1fbfeb83c.5430ff9842d7e64d56e3.js (23.55 KB)
static/chunks/main-6c6004c60ac0c2336b18.js (14.56 KB)
static/chunks/pages/_error-0131dd55816b8301f155.js (7.73 KB)
static/chunks/webpack-7bd1a373f3b01d510bde.js (2.37 KB)
static/chunks/pages/[...index]-56225315d3fa5ef4a3a3.js (432 B)
static/chunks/18.0a7cc9f0537c413437d6.js (427 B)
static/chunks/pages/index-5eab8ab0e603d341c697.js (422 B)
---
All (4.24 MB)
static/chunks/pages/_app-612c700ab77ed90ec7e1.js (2.06 MB)
static/chunks/e881dd8a.1155fcfe5c80df89a716.js (939.47 KB)
static/chunks/48d18726.664a8c4aeed362960d23.js (388.16 KB)
static/chunks/a48ca981.975808537899b456f308.js (229.56 KB)
static/chunks/895b5e10.92a4d6d86157057f626b.js (136.05 KB)
static/chunks/framework.3519294afa6a9ab53973.js (126.21 KB)
static/chunks/styles.7451907771067f2e0c7b.js (102.92 KB)
static/chunks/polyfills-c8cc3525c437fef2c42f.js (90.02 KB)
static/chunks/0f1ac474.71ee40e286ebcca351c0.js (70.96 KB)
static/chunks/8313d723.b781d686a875b4b6e5b3.js (55.09 KB)
static/chunks/4f149b58.47f6f9cb8f554b494ae7.js (40.53 KB)
static/chunks/d1edcd509e188af8a3b11bf83ac465d1fbfeb83c.5430ff9842d7e64d56e3.js (23.55 KB)
static/chunks/main-291ce000fe6e5cb7c8b4.js (14.56 KB)
static/chunks/pages/_error-92d2a152b77934e80423.js (7.73 KB)
static/chunks/webpack-898126f19adf8cd83d35.js (2.37 KB)
static/chunks/pages/[...index]-b6ab3b2770240c203545.js (432 B)
static/chunks/17.5e77338df8bbdf71a40d.js (427 B)
static/chunks/pages/index-2108d97d88461fe75fe6.js (422 B)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment