11const fs = require ( "node:fs" ) ;
22
3+ const SIZE_LIMIT_HEADING = "## 📦 Binary Size-limit" ;
4+ const DEFAULT_PLATFORM = "x86_64-unknown-linux-gnu" ;
5+
6+ const BINARY_PATHS = {
7+ "x86_64-unknown-linux-gnu" : "rspack.linux-x64-gnu.node" ,
8+ "aarch64-unknown-linux-gnu" : "rspack.linux-arm64-gnu.node" ,
9+ "x86_64-unknown-linux-musl" : "rspack.linux-x64-musl.node" ,
10+ "aarch64-unknown-linux-musl" : "rspack.linux-arm64-musl.node" ,
11+ "x86_64-apple-darwin" : "rspack.darwin-x64.node" ,
12+ "aarch64-apple-darwin" : "rspack.darwin-arm64.node" ,
13+ "x86_64-pc-windows-msvc" : "rspack.win32-x64-msvc.node" ,
14+ "i686-pc-windows-msvc" : "rspack.win32-ia32-msvc.node" ,
15+ "aarch64-pc-windows-msvc" : "rspack.win32-arm64-msvc.node"
16+ } ;
17+
18+ const PLATFORM_LABELS = {
19+ "x86_64-unknown-linux-gnu" : "Linux x64 (glibc)" ,
20+ "aarch64-unknown-linux-gnu" : "Linux arm64 (glibc)" ,
21+ "x86_64-unknown-linux-musl" : "Linux x64 (musl)" ,
22+ "aarch64-unknown-linux-musl" : "Linux arm64 (musl)" ,
23+ "x86_64-apple-darwin" : "macOS x64" ,
24+ "aarch64-apple-darwin" : "macOS arm64" ,
25+ "x86_64-pc-windows-msvc" : "Windows x64" ,
26+ "i686-pc-windows-msvc" : "Windows x86" ,
27+ "aarch64-pc-windows-msvc" : "Windows arm64"
28+ } ;
29+
330/**
4- * @param {import("@octokit/rest") } github
5- * @param {Number } limit
31+ * @param {{ github: import('@octokit/rest'), context: any, limit: number, platform?: string } } options
632 */
7- module . exports = async function action ( { github, context, limit } ) {
33+ async function run ( { github, context, limit, platform } ) {
34+ const target = platform || DEFAULT_PLATFORM ;
835 const commits = await github . rest . repos . listCommits ( {
936 owner : context . repo . owner ,
1037 repo : context . repo . repo ,
@@ -18,10 +45,13 @@ module.exports = async function action({ github, context, limit }) {
1845 console . log ( commit . sha ) ;
1946 try {
2047 const data = await fetchDataBySha ( commit . sha ) ;
21- if ( data ?. size ) {
48+ const size = data ?. sizes ?. [ target ] ?? data ?. size ;
49+ if ( typeof size === "number" ) {
2250 baseCommit = commit ;
23- baseSize = data . size ;
24- console . log ( `Commit ${ commit . sha } has binary size: ${ data . size } ` ) ;
51+ baseSize = size ;
52+ console . log (
53+ `Commit ${ commit . sha } has binary size (${ target } ): ${ size } `
54+ ) ;
2555 break ;
2656 }
2757 } catch ( e ) {
@@ -35,48 +65,49 @@ module.exports = async function action({ github, context, limit }) {
3565 throw new Error ( error ) ;
3666 }
3767
38- const headSize = fs . statSync (
39- "./crates/node_binding/rspack.linux-x64-gnu.node"
40- ) . size ;
68+ const file = getBinaryPath ( target ) ;
69+ console . log ( `Checking binary size for ${ file } ` ) ;
70+ const headSize = fs . statSync ( file ) . size ;
4171
42- console . log ( `Base commit size: ${ baseSize } ` ) ;
43- console . log ( `Head commit size: ${ headSize } ` ) ;
44-
45- const comment = compareBinarySize ( headSize , baseSize , context , baseCommit ) ;
46-
47- try {
48- await commentToPullRequest ( github , context , comment ) ;
49- } catch ( e ) {
50- console . error ( "Failed to comment on pull request:" , e ) ;
51- }
72+ console . log ( `Base commit size (${ target } ): ${ baseSize } ` ) ;
73+ console . log ( `Head commit size (${ target } ): ${ headSize } ` ) ;
5274
5375 const increasedSize = headSize - baseSize ;
54- if ( increasedSize > limit ) {
55- throw new Error (
56- `Binary size increased by ${ increasedSize } bytes, exceeding the limit of ${ limit } bytes`
57- ) ;
58- }
59- } ;
76+ return {
77+ platform : target ,
78+ baseSize,
79+ headSize,
80+ increasedSize,
81+ exceeded : increasedSize > limit ,
82+ comment : compareBinarySize ( headSize , baseSize , context , baseCommit )
83+ } ;
84+ }
85+
86+ module . exports = run ;
87+ module . exports . commentToPullRequest = commentToPullRequest ;
88+ module . exports . formatReport = formatReport ;
89+ module . exports . getBinaryPath = getBinaryPath ;
90+ module . exports . SIZE_LIMIT_HEADING = SIZE_LIMIT_HEADING ;
6091
61- async function commentToPullRequest ( github , context , comment ) {
92+ async function commentToPullRequest ( github , context , body ) {
6293 const { data : comments } = await github . rest . issues . listComments ( {
6394 owner : context . repo . owner ,
6495 repo : context . repo . repo ,
6596 issue_number : context . payload . number
6697 } ) ;
6798
68- const prevComment = comments . filter (
99+ const prevComment = comments . find (
69100 comment =>
70101 comment . user . login === "github-actions[bot]" &&
71102 comment . body . startsWith ( SIZE_LIMIT_HEADING )
72- ) [ 0 ] ;
103+ ) ;
73104
74105 if ( prevComment ) {
75106 await github . rest . issues . updateComment ( {
76107 owner : context . repo . owner ,
77108 repo : context . repo . repo ,
78109 comment_id : prevComment . id ,
79- body : ` ${ SIZE_LIMIT_HEADING } \n ${ comment } `
110+ body
80111 } ) ;
81112 return ;
82113 }
@@ -85,8 +116,19 @@ async function commentToPullRequest(github, context, comment) {
85116 owner : context . repo . owner ,
86117 repo : context . repo . repo ,
87118 issue_number : context . payload . number ,
88- body : `${ SIZE_LIMIT_HEADING } \n${ comment } `
119+ body
120+ } ) ;
121+ }
122+
123+ function formatReport ( entries ) {
124+ const ordered = [ ...entries ] . sort ( ( a , b ) =>
125+ a . platform . localeCompare ( b . platform )
126+ ) ;
127+ const sections = ordered . map ( entry => {
128+ const title = PLATFORM_LABELS [ entry . platform ] || entry . platform ;
129+ return `### ${ title } \n${ entry . comment } ` ;
89130 } ) ;
131+ return `${ SIZE_LIMIT_HEADING } \n\n${ sections . join ( "\n\n" ) } ` . trim ( ) ;
90132}
91133
92134function fetchDataBySha ( sha ) {
@@ -95,8 +137,6 @@ function fetchDataBySha(sha) {
95137 return fetch ( dataUrl ) . then ( res => res . json ( ) ) ;
96138}
97139
98- const SIZE_LIMIT_HEADING = "## 📦 Binary Size-limit" ;
99-
100140const DATA_URL_BASE =
101141 "https://raw.githubusercontent.com/web-infra-dev/rspack-ecosystem-benchmark/data" ;
102142
@@ -108,7 +148,8 @@ function compareBinarySize(headSize, baseSize, context, baseCommit) {
108148 const info = `> Comparing [\`${ headSha . slice ( 0 , 7 ) } \`](${ context . payload . repository . html_url } /commit/${ headSha } ) to [${ message } by ${ author } ](${ baseCommit . html_url } )\n\n` ;
109149
110150 const diff = headSize - baseSize ;
111- const percentage = ( Math . abs ( diff / baseSize ) * 100 ) . toFixed ( 2 ) ;
151+ const percentage =
152+ baseSize === 0 ? "0.00" : ( Math . abs ( diff / baseSize ) * 100 ) . toFixed ( 2 ) ;
112153 if ( diff > 0 ) {
113154 return `${ info } ❌ Size increased by ${ toHumanReadable ( diff ) } from ${ toHumanReadable ( baseSize ) } to ${ toHumanReadable ( headSize ) } (⬆️${ percentage } %)` ;
114155 }
@@ -118,6 +159,15 @@ function compareBinarySize(headSize, baseSize, context, baseCommit) {
118159 return `${ info } 🙈 Size remains the same at ${ toHumanReadable ( headSize ) } ` ;
119160}
120161
162+ function getBinaryPath ( platform ) {
163+ const target = platform || DEFAULT_PLATFORM ;
164+ const filename = BINARY_PATHS [ target ] ;
165+ if ( ! filename ) {
166+ throw new Error ( `Unsupported platform: ${ target } ` ) ;
167+ }
168+ return `./crates/node_binding/${ filename } ` ;
169+ }
170+
121171function toHumanReadable ( size ) {
122172 if ( size < 1024 ) {
123173 return `${ size } bytes` ;
0 commit comments