Skip to content

Commit

Permalink
Added show-no-change option
Browse files Browse the repository at this point in the history
  • Loading branch information
exoego committed Jul 30, 2024
1 parent c53cafc commit 7d73c95
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ Please check the above setup example to use this action with `pull_request_targe
| `include_extensions` | `.js,.cjs,.mjs` | A comma-separated list of file extension to be included in the analysis table. |
| `percent_extra_attention` | `20` | If an out file size has increased more than this percent, display a "‼️" to draw extra attention to the change. |
| `show_details` | `true` | If `true`, a collapsed "details" section is rendered. It explains the details of the numbers provided and icons. |
| `show_no_change` | `false` | If `true`, all bundles are shown in the analysis regardless of size change. If `false`, only bundles with size changes are shown. |
| `top_n_largest_paths` | `20` | The number of largest paths (e.g.) `node_modules/foo`) to be collected. If 0 or lower, skipped. |

## Action outputs
Expand Down
2 changes: 2 additions & 0 deletions __tests__/no-base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ describe("examples w/o base analysis", () => {
metafiles: ["out/**/meta.json"],
name: "test",
showDetails: false,
showNoChange: true,
topNLargestPaths: 10,
};

beforeEach(() => {
Expand Down
12 changes: 11 additions & 1 deletion __tests__/with-base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,25 @@ describe("examples w/ base analysis", () => {
"examples-with-base",
);

for (const example of getExampleDirectories(fixturesPath)) {
for (const [index, example] of getExampleDirectories(
fixturesPath,
).entries()) {
describe(`example ${example.name}`, () => {
const metafiles =
example.name === "multi-metafiles"
? ["out/meta1.json", "out/meta2.json"]
: ["out/meta.json"];

const isEven = index % 2 === 0;
const input: Input = {
analyzerDirectory: ".analyzer",
percentExtraAttention: 20,
includeExtensions: [".js", ".mjs", ".cjs"],
metafiles,
name: "test",
showDetails: false,
showNoChange: isEven,
topNLargestPaths: 10,
};

beforeEach(() => {
Expand All @@ -53,6 +58,11 @@ describe("examples w/ base analysis", () => {
expect(comment).toMatch(/⚠️ \+\d+/);
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);
} else {
expect(comment).toMatch(/\d bundles with no change are hidden./i);
}
expect(comment).toMatch(/🆕 Added/i);
expect(comment).toMatch(/🗑️ Deleted/i);
});
Expand Down
6 changes: 6 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ inputs:
default: "true"
description: |
If `true`, a collapsed "details" section is rendered. It explains the details of the numbers provided and icons.
show_no_change_bundles:
required: false
default: "true"
description: |
If `true`, all bundles are shown in the analysis regardless of size change. If `false`, only bundles with size changes are shown.
top_n_largest_paths:
required: false
default: "20"
Expand Down Expand Up @@ -83,6 +88,7 @@ runs:
INPUT_INCLUDE_EXTENSIONS: ${{ inputs.include_extensions }}
INPUT_PERCENT_EXTRA_ATTENTION: ${{ inputs.percent_extra_attention }}
INPUT_SHOW_DETAILS: ${{ inputs.show_details }}
INPUT_SHOW_NO_CHANGE: ${{ inputs.show_no_change }}
INPUT_TOP_N_LARGEST_PATHS: ${{ inputs.top_n_largest_paths }}
run: |
node ${{ github.action_path }}/dist/index.mjs
Expand Down
28 changes: 19 additions & 9 deletions dist/index.mjs

Large diffs are not rendered by default.

39 changes: 38 additions & 1 deletion src/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ This analysis was generated by [esbuild-bundle-analyzer](https://github.com/exoe
console.log("Comparison done.", comparison);

if (hasAnyChange) {
output += markdownTable(comparison, input.percentExtraAttention);
output += markdownTable(
comparison,
input.percentExtraAttention,
input.showNoChange,
);
output += noChangesTable(comparison, input.showNoChange);
output += fileSizeTable(comparison, input.topNLargestPaths);
output += detail(input);
} else {
Expand Down Expand Up @@ -215,11 +220,16 @@ function filesize(bytes: number): string {
throw new Error("Too large file size!! Are you sure?");
}

const shouldShowBundle = (d: CompareResult, showNoChange: boolean) =>
showNoChange || d.bytes - d.baseBytes !== 0;

function markdownTable(
data: Array<CompareResult>,
redThreshold: number,
showNoChange: boolean,
): string {
const rows = data
.filter((d) => shouldShowBundle(d, showNoChange))
.map((d) => {
return `${d.metafile} | ${d.outfile} | ${renderSize(d)} | ${renderNote(
d,
Expand All @@ -234,6 +244,33 @@ Meta File | Out File | Size (raw) | Note
${rows}`;
}

function noChangesTable(
data: Array<CompareResult>,
showNoChange: boolean,
): string {
const noChangeBundles = data.filter(
(d) => !shouldShowBundle(d, showNoChange),
);
const rows = noChangeBundles
.map((d) => {
return `${d.metafile} | ${d.outfile} | ${renderSize(d)} | ✅ No change\n`;
})
.join("");
if (noChangeBundles.length === 0) {
return "";
}
return `
<details>
<summary>${noChangeBundles.length} bundles with no change are hidden.</summary>
Meta File | Out File | Size (raw) | Note
----------|----------|-----------:|------
${rows}
</details>
`;
}

/**
* Find the top N largest nodes in root tree.
* Dig nodes until the depth of 3.
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ function getInput(): Input {
showDetails: ["true", "True", "TRUE"].includes(
getSingleInput("show_details") || "true",
),
showNoChange: ["true", "True", "TRUE"].includes(
getSingleInput("show_no_change") || "true",
),
topNLargestPaths: Number.parseInt(
getSingleInput("top_n_largest_paths") || "20",
10,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface Input {
analyzerDirectory: string;
percentExtraAttention: number;
showDetails: boolean;
showNoChange: boolean;
}

export interface TreeMapNode {
Expand Down

0 comments on commit 7d73c95

Please sign in to comment.