Skip to content

Commit bd04a12

Browse files
committed
Add CI workflow to compute diff between files dist files
1 parent 320e635 commit bd04a12

File tree

2 files changed

+196
-0
lines changed

2 files changed

+196
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* Generate a markdown table with the difference in size of the dist files between the base and the PR.
3+
*/
4+
5+
/*
6+
Usage:
7+
```shell
8+
BASE_DIST_FILES='{"src/Autocomplete/assets/dist/controller.js":{"size":15382,"size_gz":3716,"size_brotli":3125},"src/Chartjs/assets/dist/controller.js":{"size":2281,"size_gz":771,"size_brotli":642},"src/Cropperjs/assets/dist/controller.js":{"size":1044,"size_gz":475,"size_brotli":371}}' \
9+
PR_DIST_FILES='{"src/Chartjs/assets/dist/controller.js":{"size":2281,"size_gz":771,"size_brotli":642},"src/Cropperjs/assets/dist/controller.js":{"size":1044,"size_gz":475,"size_brotli":371},"src/Cropperjs/assets/dist/style.min.css":{"size":32,"size_gz":66,"size_brotli":34},"src/Dropzone/assets/dist/controller.js":{"size":3199,"size_gz":816,"size_brotli":634}}' \
10+
node .github/generate-dist-files-size-diff.mjs
11+
```
12+
*/
13+
14+
if (!process.env.BASE_DIST_FILES) {
15+
throw new Error('Missing or invalid "BASE_DIST_FILES" env variable.');
16+
}
17+
18+
if (!process.env.PR_DIST_FILES) {
19+
throw new Error('Missing or invalid "PR_DIST_FILES" env variable.');
20+
}
21+
22+
/**
23+
* Adapted from https://gist.github.com/zentala/1e6f72438796d74531803cc3833c039c?permalink_comment_id=4455218#gistcomment-4455218
24+
* @param {number} bytes
25+
* @param {number} digits
26+
* @returns {string}
27+
*/
28+
function formatBytes(bytes, digits = 2) {
29+
if (bytes === 0) {
30+
return '0 B';
31+
}
32+
const sizes = [`B`, 'kB', 'MB'];
33+
const i = Math.floor(Math.log(bytes) / Math.log(1024));
34+
35+
return parseFloat((bytes / Math.pow(1024, i)).toFixed(digits)) + ' ' + sizes[i];
36+
}
37+
38+
/**
39+
* @param {number} from
40+
* @param {number} to
41+
* @returns {number}
42+
*/
43+
function computeDiffPercent(from, to) {
44+
if (from === to) {
45+
return 0;
46+
}
47+
48+
return Number(((from - to) / to * -100).toFixed(2));
49+
}
50+
51+
/**
52+
* @param {number} percent
53+
* @returns {string}
54+
*/
55+
function formatDiffPercent(percent) {
56+
return percent > 0 ? `+${percent}% 📈` : percent < 0 ? `${percent}% 📉` : `${percent}%`;
57+
}
58+
59+
export function main() {
60+
let base = JSON.parse(process.env.BASE_DIST_FILES);
61+
let pr = JSON.parse(process.env.PR_DIST_FILES);
62+
let output = '<h1>📊 Dist files size difference</h1>\n\n';
63+
64+
const files = [...new Set([...Object.keys(pr), ...Object.keys(base)])].sort().reduce((acc, file) => {
65+
const added = !base[file] && pr[file];
66+
const removed = base[file] && !pr[file];
67+
const diff_percent_size = removed ? -100 : (added ? 100 : (computeDiffPercent(base[file].size, pr[file].size)));
68+
const diff_percent_size_gz = removed ? -100 : (added ? 100 : (computeDiffPercent(base[file].size_gz, pr[file].size_gz)));
69+
const diff_percent_size_brotli = removed ? -100 : (added ? 100 : (computeDiffPercent(base[file].size_brotli, pr[file].size_brotli)));
70+
71+
if (diff_percent_size !== 0 && diff_percent_size_gz !== 0 && diff_percent_size_brotli !== 0) {
72+
acc.set(file, {
73+
state: added ? 'added' : (removed ? 'removed' : 'changed'),
74+
diff_percent_size,
75+
diff_percent_size_gz,
76+
diff_percent_size_brotli
77+
});
78+
}
79+
80+
return acc;
81+
}, new Map);
82+
83+
if (files.size === 0) {
84+
output += 'ℹ️ No difference in dist files.\n';
85+
return output;
86+
}
87+
88+
output += 'Thanks for the PR! Here is the difference in size of the dist files between the base and the PR.\n';
89+
output += 'Please review the changes and make sure they are expected.\n\n';
90+
output += `<table>
91+
<thead><tr><th>File</th><th>Diff (B)</th><th>Diff (%)</th></tr></thead>
92+
<tbody>`;
93+
for (const [file, details] of files.entries()) {
94+
output += `<tr>
95+
<td><code>${file}</code> ${details.state === 'added' ? '(new)' : (details.state === 'removed' ? '(deleted)' : '')}</td>
96+
<td>
97+
Size: <code>${formatBytes(base[file]?.size || 0)}</code> → <code>${formatBytes(pr[file]?.size || 0)}</code><br>
98+
Gzip: <code>${formatBytes(base[file]?.size_gz || 0)}</code> → <code>${formatBytes(pr[file]?.size_gz || 0)}</code><br>
99+
Brotli: <code>${formatBytes(base[file]?.size_brotli || 0)}</code> → <code>${formatBytes(pr[file]?.size_brotli || 0)}</code>
100+
</td>
101+
<td align="right">
102+
Size: <b>${formatDiffPercent(details.diff_percent_size)}</b><br>
103+
Gzip: <b>${formatDiffPercent(details.diff_percent_size_gz)}</b><br>
104+
Brotli: <b>${formatDiffPercent(details.diff_percent_size_brotli)}</b>
105+
</td>
106+
</tr>
107+
`;
108+
}
109+
output += `</tbody>
110+
</table>
111+
`;
112+
113+
return output;
114+
}
115+
116+
if (!process.env.CI) {
117+
console.log(main());
118+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: Dist Files Size Diff
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- 'src/*/**'
7+
- '!src/*/doc/**'
8+
- '.github/**'
9+
10+
jobs:
11+
dist-files-size-diff:
12+
runs-on: ubuntu-latest
13+
permissions:
14+
pull-requests: write # for marocchino/sticky-pull-request-comment@v2
15+
steps:
16+
- name: Configure git
17+
run: |
18+
git config --global user.email ""
19+
git config --global user.name "github-action[bot]"
20+
21+
- uses: marocchino/sticky-pull-request-comment@v2
22+
with:
23+
message: |
24+
⏳ The dist files size difference is being calculated...
25+
26+
- uses: actions/checkout@v4
27+
with:
28+
ref: ${{ github.base_ref }}
29+
30+
- name: Get dist files size (from base branch)
31+
id: base-dist-files
32+
run: |
33+
set -e
34+
35+
FILES=$(find src -mindepth 2 -path '*/assets/dist/*' \( -name "*.js" -o -name "*.css" \) -not \( -path '*/tests/*' -o -path '*/public/*' -o -path '*/vendor/*' \) | sort | while read -r file; do
36+
echo "{\"$file\": {\"size\": $(wc -c < "$file"), \"size_gz\": $(gzip -c "$file" | wc -c), \"size_brotli\": $(brotli -c "$file" | wc -c)}}"
37+
done | jq -s 'add' -c)
38+
39+
echo "files=$FILES" >> $GITHUB_OUTPUT
40+
41+
- uses: actions/checkout@v4
42+
43+
- name: Get dist files size (from pull request)
44+
id: pr-dist-files
45+
run: |
46+
set -e
47+
48+
FILES=$(find src -mindepth 2 -path '*/assets/dist/*' \( -name "*.js" -o -name "*.css" \) -not \( -path '*/tests/*' -o -path '*/public/*' -o -path '*/vendor/*' \) | sort | while read -r file; do
49+
echo "{\"$file\": {\"size\": $(wc -c < "$file"), \"size_gz\": $(gzip -c "$file" | wc -c), \"size_brotli\": $(brotli -c "$file" | wc -c)}}"
50+
done | jq -s 'add' -c)
51+
52+
echo "files=$FILES" >> $GITHUB_OUTPUT
53+
54+
- name: Generate the diff
55+
id: diff
56+
uses: actions/github-script@v7
57+
env:
58+
BASE_DIST_FILES: ${{ steps.base-dist-files.outputs.files }}
59+
PR_DIST_FILES: ${{ steps.pr-dist-files.outputs.files }}
60+
with:
61+
result-encoding: string
62+
script: |
63+
const { main } = await import('${{ github.workspace }}/.github/generate-dist-files-size-diff.mjs')
64+
65+
return await main()
66+
67+
- name: Comment on the pull request (if any failure)
68+
if: ${{ failure() }}
69+
uses: marocchino/sticky-pull-request-comment@v2
70+
with:
71+
message: |
72+
❌ The dist files size difference could not be calculated. Please check the logs for more details.
73+
74+
- name: Comment on the pull request (if success)
75+
if: ${{ always() && steps.diff.conclusion == 'success' }}
76+
uses: marocchino/sticky-pull-request-comment@v2
77+
with:
78+
message: ${{ steps.diff.outputs.result }}

0 commit comments

Comments
 (0)