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-apple-darwin" : "rspack.darwin-arm64.node" ,
9+ "x86_64-pc-windows-msvc" : "rspack.win32-x64-msvc.node"
10+ } ;
11+
12+ const PLATFORM_LABELS = {
13+ "x86_64-unknown-linux-gnu" : "Linux x64 (glibc)" ,
14+ "aarch64-apple-darwin" : "macOS arm64" ,
15+ "x86_64-pc-windows-msvc" : "Windows x64"
16+ } ;
17+
318/**
4- * @param {import("@octokit/rest") } github
5- * @param {Number } limit
19+ * @param {{ github: import('@octokit/rest'), context: any, limit: number, platform?: string } } options
620 */
7- module . exports = async function action ( { github, context, limit } ) {
21+ async function run ( { github, context, limit, platform } ) {
22+ const target = platform || DEFAULT_PLATFORM ;
823 const commits = await github . rest . repos . listCommits ( {
924 owner : context . repo . owner ,
1025 repo : context . repo . repo ,
@@ -18,10 +33,13 @@ module.exports = async function action({ github, context, limit }) {
1833 console . log ( commit . sha ) ;
1934 try {
2035 const data = await fetchDataBySha ( commit . sha ) ;
21- if ( data ?. size ) {
36+ const size = data ?. sizes ?. [ target ] ?? data ?. size ;
37+ if ( typeof size === "number" ) {
2238 baseCommit = commit ;
23- baseSize = data . size ;
24- console . log ( `Commit ${ commit . sha } has binary size: ${ data . size } ` ) ;
39+ baseSize = size ;
40+ console . log (
41+ `Commit ${ commit . sha } has binary size (${ target } ): ${ size } `
42+ ) ;
2543 break ;
2644 }
2745 } catch ( e ) {
@@ -35,48 +53,49 @@ module.exports = async function action({ github, context, limit }) {
3553 throw new Error ( error ) ;
3654 }
3755
38- const headSize = fs . statSync (
39- "./crates/node_binding/rspack.linux-x64-gnu.node"
40- ) . size ;
56+ const file = getBinaryPath ( target ) ;
57+ console . log ( `Checking binary size for ${ file } ` ) ;
58+ const headSize = fs . statSync ( file ) . size ;
4159
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- }
60+ console . log ( `Base commit size (${ target } ): ${ baseSize } ` ) ;
61+ console . log ( `Head commit size (${ target } ): ${ headSize } ` ) ;
5262
5363 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- } ;
64+ return {
65+ platform : target ,
66+ baseSize,
67+ headSize,
68+ increasedSize,
69+ exceeded : increasedSize > limit ,
70+ comment : compareBinarySize ( headSize , baseSize , context , baseCommit )
71+ } ;
72+ }
73+
74+ module . exports = run ;
75+ module . exports . commentToPullRequest = commentToPullRequest ;
76+ module . exports . formatReport = formatReport ;
77+ module . exports . getBinaryPath = getBinaryPath ;
78+ module . exports . SIZE_LIMIT_HEADING = SIZE_LIMIT_HEADING ;
6079
61- async function commentToPullRequest ( github , context , comment ) {
80+ async function commentToPullRequest ( github , context , body ) {
6281 const { data : comments } = await github . rest . issues . listComments ( {
6382 owner : context . repo . owner ,
6483 repo : context . repo . repo ,
6584 issue_number : context . payload . number
6685 } ) ;
6786
68- const prevComment = comments . filter (
87+ const prevComment = comments . find (
6988 comment =>
7089 comment . user . login === "github-actions[bot]" &&
7190 comment . body . startsWith ( SIZE_LIMIT_HEADING )
72- ) [ 0 ] ;
91+ ) ;
7392
7493 if ( prevComment ) {
7594 await github . rest . issues . updateComment ( {
7695 owner : context . repo . owner ,
7796 repo : context . repo . repo ,
7897 comment_id : prevComment . id ,
79- body : ` ${ SIZE_LIMIT_HEADING } \n ${ comment } `
98+ body
8099 } ) ;
81100 return ;
82101 }
@@ -85,8 +104,19 @@ async function commentToPullRequest(github, context, comment) {
85104 owner : context . repo . owner ,
86105 repo : context . repo . repo ,
87106 issue_number : context . payload . number ,
88- body : `${ SIZE_LIMIT_HEADING } \n${ comment } `
107+ body
108+ } ) ;
109+ }
110+
111+ function formatReport ( entries ) {
112+ const ordered = [ ...entries ] . sort ( ( a , b ) =>
113+ a . platform . localeCompare ( b . platform )
114+ ) ;
115+ const sections = ordered . map ( entry => {
116+ const title = PLATFORM_LABELS [ entry . platform ] || entry . platform ;
117+ return `### ${ title } \n${ entry . comment } ` ;
89118 } ) ;
119+ return `${ SIZE_LIMIT_HEADING } \n\n${ sections . join ( "\n\n" ) } ` . trim ( ) ;
90120}
91121
92122function fetchDataBySha ( sha ) {
@@ -95,8 +125,6 @@ function fetchDataBySha(sha) {
95125 return fetch ( dataUrl ) . then ( res => res . json ( ) ) ;
96126}
97127
98- const SIZE_LIMIT_HEADING = "## 📦 Binary Size-limit" ;
99-
100128const DATA_URL_BASE =
101129 "https://raw.githubusercontent.com/web-infra-dev/rspack-ecosystem-benchmark/data" ;
102130
@@ -118,6 +146,15 @@ function compareBinarySize(headSize, baseSize, context, baseCommit) {
118146 return `${ info } 🙈 Size remains the same at ${ toHumanReadable ( headSize ) } ` ;
119147}
120148
149+ function getBinaryPath ( platform ) {
150+ const target = platform || DEFAULT_PLATFORM ;
151+ const filename = BINARY_PATHS [ target ] ;
152+ if ( ! filename ) {
153+ throw new Error ( `Unsupported platform: ${ target } ` ) ;
154+ }
155+ return `./crates/node_binding/${ filename } ` ;
156+ }
157+
121158function toHumanReadable ( size ) {
122159 if ( size < 1024 ) {
123160 return `${ size } bytes` ;
0 commit comments