Skip to content

Commit

Permalink
Merge pull request #19 from exoego/fix-diff
Browse files Browse the repository at this point in the history
Fix diff calculation
  • Loading branch information
exoego committed May 11, 2024
2 parents 7429f1f + 9927db2 commit 6cf5c4b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
20 changes: 13 additions & 7 deletions dist/index.mjs

Large diffs are not rendered by default.

18 changes: 12 additions & 6 deletions src/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,20 @@ This analysis was generated by [esbuild-bundle-analyzer](https://github.com/exoe

if (!currentStats) {
hasAnyChange = true;
return { ...baseStats, diff: -1, remark: "deleted", tree: undefined };
return {
...baseStats,
baseBytes: -1,
remark: "deleted",
tree: undefined,
};
}

const tree = fileTree.get(
treeKey(currentStats.metafile, currentStats.outfile),
);
if (!baseStats) {
hasAnyChange = true;
return { ...currentStats, diff: -1, remark: "added", tree };
return { ...currentStats, baseBytes: -1, remark: "added", tree };
}

const diff = currentStats.bytes - baseStats.bytes;
Expand All @@ -45,7 +50,7 @@ This analysis was generated by [esbuild-bundle-analyzer](https://github.com/exoe
}
return {
...currentStats,
diff,
baseBytes: baseStats.bytes,
tree,
remark: Math.sign(diff) ? "increased" : "decreased",
};
Expand Down Expand Up @@ -322,10 +327,11 @@ function renderNote(d: CompareResult, redThreshold: number): string {
if (d.remark === "added") {
return "🆕 Added";
}
if (d.diff) {
const percentChange = (d.diff / d.bytes) * 100;
const diff = d.bytes - d.baseBytes;
if (diff !== 0) {
const percentChange = (diff / d.baseBytes) * 100;
return `${renderStatusIndicator(percentChange, redThreshold)}${filesize(
d.diff,
diff,
)} (${sign(percentChange)}${percentChange.toFixed(1)}%)`;
}
return "✅ No change";
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface CompareResult {
metafile: string;
outfile: string;
bytes: number;
diff: number;
baseBytes: number;
remark: "added" | "deleted" | "increased" | "decreased";
tree: TreeMapNode | undefined;
}
Expand Down

0 comments on commit 6cf5c4b

Please sign in to comment.