Skip to content

Commit a5014a6

Browse files
afc163claude
andauthored
ci: add npm pack size detection workflow and badge (#39)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a42aa46 commit a5014a6

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

.github/workflows/pkg-size.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Pkg Size
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
permissions:
10+
pull-requests: write
11+
12+
jobs:
13+
pkg-size:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
- uses: actions/setup-node@v4
18+
with:
19+
node-version: 22
20+
cache: npm
21+
22+
- run: npm ci
23+
- run: npm run build
24+
25+
- name: Get pack size
26+
id: size
27+
run: |
28+
PACK=$(npm pack --dry-run --json 2>&1 | sed -n '/^\[/,/^\]/p' | jq '.[0]')
29+
echo "size=$(echo "$PACK" | jq '.size')" >> "$GITHUB_OUTPUT"
30+
echo "unpack_size=$(echo "$PACK" | jq '.unpackedSize')" >> "$GITHUB_OUTPUT"
31+
32+
- name: Get base branch size
33+
if: github.event_name == 'pull_request'
34+
id: base_size
35+
run: |
36+
git fetch origin ${{ github.base_ref }} --depth=1
37+
git checkout FETCH_HEAD -- . 2>/dev/null || true
38+
npm ci
39+
npm run build
40+
PACK=$(npm pack --dry-run --json 2>&1 | sed -n '/^\[/,/^\]/p' | jq '.[0]')
41+
echo "size=$(echo "$PACK" | jq '.size')" >> "$GITHUB_OUTPUT"
42+
echo "unpack_size=$(echo "$PACK" | jq '.unpackedSize')" >> "$GITHUB_OUTPUT"
43+
44+
- name: Comment on PR
45+
if: github.event_name == 'pull_request'
46+
uses: actions/github-script@v7
47+
with:
48+
script: |
49+
const run = require('./scripts/pkg-size-comment.cjs');
50+
await run({
51+
github, context,
52+
size: parseInt('${{ steps.size.outputs.size }}'),
53+
unpackSize: parseInt('${{ steps.size.outputs.unpack_size }}'),
54+
baseSize: parseInt('${{ steps.base_size.outputs.size }}') || parseInt('${{ steps.size.outputs.size }}'),
55+
baseUnpackSize: parseInt('${{ steps.base_size.outputs.unpack_size }}') || parseInt('${{ steps.size.outputs.unpack_size }}'),
56+
});

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Query component knowledge, analyze project usage, and guide migrations — fully
1414
[![npm version](https://img.shields.io/npm/v/@ant-design/cli?color=blue&label=npm)](https://www.npmjs.com/package/@ant-design/cli)
1515
[![npm downloads](https://img.shields.io/npm/dm/@ant-design/cli?color=blue)](https://www.npmjs.com/package/@ant-design/cli)
1616
[![CI](https://github.com/ant-design/ant-design-cli/actions/workflows/ci.yml/badge.svg)](https://github.com/ant-design/ant-design-cli/actions/workflows/ci.yml)
17+
[![install size](https://packagephobia.com/badge?p=@ant-design/cli)](https://packagephobia.com/result?p=@ant-design/cli)
1718
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](./LICENSE)
1819

1920
[English](./README.md) · [中文](./README.zh-CN.md) · [Changelog](./CHANGELOG.md)

README.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
[![npm version](https://img.shields.io/npm/v/@ant-design/cli?color=blue&label=npm)](https://www.npmjs.com/package/@ant-design/cli)
1515
[![npm downloads](https://img.shields.io/npm/dm/@ant-design/cli?color=blue)](https://www.npmjs.com/package/@ant-design/cli)
1616
[![CI](https://github.com/ant-design/ant-design-cli/actions/workflows/ci.yml/badge.svg)](https://github.com/ant-design/ant-design-cli/actions/workflows/ci.yml)
17+
[![install size](https://packagephobia.com/badge?p=@ant-design/cli)](https://packagephobia.com/result?p=@ant-design/cli)
1718
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](./LICENSE)
1819

1920
[English](./README.md) · [中文](./README.zh-CN.md) · [更新日志](./CHANGELOG.zh-CN.md)

scripts/pkg-size-comment.cjs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Post a package size report comment on a PR.
3+
* Called by the pkg-size GitHub Actions workflow via actions/github-script.
4+
*
5+
* @param {object} params
6+
* @param {import('@octokit/rest').Octokit} params.github
7+
* @param {object} params.context
8+
* @param {number} params.size - packed size in bytes
9+
* @param {number} params.unpackSize - unpacked size in bytes
10+
* @param {number} params.baseSize - base branch packed size in bytes
11+
* @param {number} params.baseUnpackSize - base branch unpacked size in bytes
12+
*/
13+
module.exports = async ({ github, context, size, unpackSize, baseSize, baseUnpackSize }) => {
14+
function formatBytes(bytes) {
15+
if (bytes >= 1048576) return (bytes / 1048576).toFixed(2) + ' MB';
16+
return (bytes / 1024).toFixed(2) + ' KB';
17+
}
18+
19+
function formatDiff(current, base) {
20+
const diff = current - base;
21+
if (diff === 0) return '±0';
22+
const sign = diff > 0 ? '+' : '';
23+
return `${sign}${formatBytes(Math.abs(diff))} (${sign}${((diff / base) * 100).toFixed(1)}%)`;
24+
}
25+
26+
const body = [
27+
'## 📦 Package Size Report',
28+
'',
29+
'| Metric | Size | Diff |',
30+
'| --- | --- | --- |',
31+
`| Packed | ${formatBytes(size)} | ${formatDiff(size, baseSize)} |`,
32+
`| Unpacked | ${formatBytes(unpackSize)} | ${formatDiff(unpackSize, baseUnpackSize)} |`,
33+
].join('\n');
34+
35+
const { data: comments } = await github.rest.issues.listComments({
36+
owner: context.repo.owner,
37+
repo: context.repo.repo,
38+
issue_number: context.issue.number,
39+
});
40+
const existing = comments.find((c) => c.body.includes('📦 Package Size Report'));
41+
42+
if (existing) {
43+
await github.rest.issues.updateComment({
44+
owner: context.repo.owner,
45+
repo: context.repo.repo,
46+
comment_id: existing.id,
47+
body,
48+
});
49+
} else {
50+
await github.rest.issues.createComment({
51+
owner: context.repo.owner,
52+
repo: context.repo.repo,
53+
issue_number: context.issue.number,
54+
body,
55+
});
56+
}
57+
};

0 commit comments

Comments
 (0)