Skip to content

Commit

Permalink
feat: Allow hiding by remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
exoego committed Aug 14, 2024
1 parent 9a376bd commit 84c18c9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 40 deletions.
4 changes: 2 additions & 2 deletions __tests__/with-base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ describe("examples w/ base analysis", () => {
expect(comment).toMatch(/✅ {2}-\d+/);
expect(comment).toMatch(/✅ {2}No change/i);
if (isEven) {
expect(comment).not.toMatch(/\d bundles with no change are hidden./i);
expect(comment).not.toMatch(/\d bundles are hidden./i);
expect(comment).toMatch(/\(Total\) \| - \|.+⚠️/i);
} else {
expect(comment).toMatch(/\d bundles with no change are hidden./i);
expect(comment).toMatch(/\d bundles are hidden./i);
expect(comment).not.toMatch(/\(Total\) \| - \|.+⚠️/i);
}
expect(comment).toMatch(/🆕 Added/i);
Expand Down
28 changes: 14 additions & 14 deletions dist/index.mjs

Large diffs are not rendered by default.

46 changes: 27 additions & 19 deletions src/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ This analysis was generated by [esbuild-bundle-analyzer](https://github.com/exoe
...currentStats,
baseBytes: baseStats.bytes,
tree,
remark: Math.sign(diff) ? "increased" : "decreased",
remark:
diff === 0 ? "no-change" : Math.sign(diff) ? "increased" : "decreased",
};
});
console.log("Comparison done.", comparison);
Expand All @@ -80,9 +81,10 @@ This analysis was generated by [esbuild-bundle-analyzer](https://github.com/exoe
input.includeSizeComparison,
input.percentExtraAttention,
);
output += noChangesTable(
output += hiddenTable(
comparison,
input.includeSizeComparison.has("no-change"),
input.includeSizeComparison,
input.percentExtraAttention,
);
output += fileSizeTable(comparison, input.topNLargestPaths);
output += detail(input);
Expand Down Expand Up @@ -213,6 +215,7 @@ function buildFileTree(input: Input) {
}

const spacer = " ";

function filesize(bytes: number): string {
const sign = bytes < 0 ? "-" : "";
const n = Math.abs(bytes);
Expand Down Expand Up @@ -264,7 +267,7 @@ function markdownTable(
: [];

const individualRows = data.filter((d) =>
shouldShowBundle(d, sizeComparisonFilters.has("no-change")),
shouldShowBundle(d, sizeComparisonFilters.has(d.remark)),
);
const rows = [...totalRows, ...individualRows]
.map((d) => {
Expand All @@ -281,24 +284,28 @@ Meta File | Out File | Size (raw) | Note
${rows}`;
}

function noChangesTable(
function hiddenTable(
data: Array<CompareResult>,
showNoChange: boolean,
includeSizeComparison: Set<SizeComparisonFilter>,
redThreshold: number,
): string {
const noChangeBundles = data.filter(
(d) => !shouldShowBundle(d, showNoChange),
const hiddenBundles = data.filter(
(d) => !includeSizeComparison.has(d.remark),
);
const rows = noChangeBundles
const rows = hiddenBundles
.map((d) => {
return `${d.metafile} | ${d.outfile} | ${renderSize(d)} | ✅ No change\n`;
return `${d.metafile} | ${d.outfile} | ${renderSize(d)} | ${renderNote(
d,
redThreshold,
)}\n`;
})
.join("");
if (noChangeBundles.length === 0) {
if (hiddenBundles.length === 0) {
return "";
}
return `
<details>
<summary>${noChangeBundles.length} bundles with no change are hidden.</summary>
<summary>${hiddenBundles.length} bundles are hidden since not listed in include_size_comparison.</summary>
Meta File | Out File | Size (raw) | Note
----------|----------|-----------:|------
Expand Down Expand Up @@ -395,6 +402,7 @@ function renderBar(percent: number, bytes: number): string {
// Block progression is 1/8 = 0.125
const blocks = ["", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█"];
const progression = 1 / (blocks.length - 1);

function progress(value: number, length = 25, vmin = 0.0, vmax = 1.0) {
const v = value * length;
const integerPart = Math.floor(v);
Expand All @@ -416,14 +424,14 @@ function renderNote(d: CompareResult, redThreshold: number): string {
if (d.remark === "added") {
return "🆕 Added";
}
const diff = d.bytes - d.baseBytes;
if (diff !== 0) {
const percentChange = (diff / d.baseBytes) * 100;
return `${renderStatusIndicator(percentChange, redThreshold)}${filesize(
diff,
)} (${sign(percentChange)}${percentChange.toFixed(1)}%)`;
if (d.remark === "no-change") {
return "✅ No change";
}
return "✅ No change";
const diff = d.bytes - d.baseBytes;
const percentChange = (diff / d.baseBytes) * 100;
return `${renderStatusIndicator(percentChange, redThreshold)}${filesize(
diff,
)} (${sign(percentChange)}${percentChange.toFixed(1)}%)`;
}

function sign(num: number): string {
Expand Down
7 changes: 2 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ export interface CompareResult {
outfile: string;
bytes: number;
baseBytes: number;
remark: "added" | "deleted" | "increased" | "decreased";
remark: "added" | "deleted" | "increased" | "decreased" | "no-change";
tree: TreeMapNode | undefined;
}

export type SizeComparisonFilter =
| CompareResult["remark"]
| "total"
| "no-change";
export type SizeComparisonFilter = CompareResult["remark"] | "total";

export interface Input {
name: string;
Expand Down

0 comments on commit 84c18c9

Please sign in to comment.