@@ -7,10 +7,11 @@ At the time of writing (~2025-05-31), the bundles are created using Bun as the r
77When node stabilizes SEA (https://nodejs.org/api/single-executable-applications.html) [and supports ESM -.-], this code can be adapted to build it using that instead (but cross-platform will be a CI experience)
88*/
99
10+ import { readFileSync } from 'node:fs' ;
1011import { readFile , rm , writeFile } from 'node:fs/promises' ;
1112import { basename } from 'node:path' ;
1213
13- import { $ , fileURLToPath } from 'bun' ;
14+ import { $ , type Build , build , fileURLToPath } from 'bun' ;
1415
1516import { version } from '../package.json' with { type : 'json' } ;
1617
@@ -30,13 +31,14 @@ const targets = (() => {
3031 'bun-darwin-arm64-baseline' ,
3132 'bun-linux-x64-musl' ,
3233 'bun-linux-arm64-musl' ,
33- 'bun-linux-x64-musl-baseline' ,
34- 'bun-linux-arm64-musl-baseline' ,
35- ] ;
34+ // TODO: when adding native windows arm64 builds, remove these too
35+ 'bun-linux-x64-musl-baseline' as never ,
36+ 'bun-linux-arm64-musl-baseline' as never ,
37+ ] satisfies Build . CompileTarget [ ] ;
3638 }
3739
3840 if ( process . platform === 'win32' ) {
39- return [ 'bun-windows-x64' , 'bun-windows-x64-baseline' ] ;
41+ return [ 'bun-windows-x64' , 'bun-windows-x64-baseline' ] satisfies Build . CompileTarget [ ] ;
4042 }
4143
4244 return [
@@ -50,9 +52,9 @@ const targets = (() => {
5052 'bun-darwin-arm64-baseline' ,
5153 'bun-linux-x64-musl' ,
5254 'bun-linux-arm64-musl' ,
53- 'bun-linux-x64-musl-baseline' ,
54- 'bun-linux-arm64-musl-baseline' ,
55- ] ;
55+ 'bun-linux-x64-musl-baseline' as never ,
56+ 'bun-linux-arm64-musl-baseline' as never ,
57+ ] satisfies Build . CompileTarget [ ] ;
5658} ) ( ) ;
5759
5860const entryPoints = [
@@ -77,6 +79,35 @@ await writeFile(metadataFile, newContent);
7779for ( const entryPoint of entryPoints ) {
7880 const cliName = basename ( entryPoint , '.ts' ) ;
7981
82+ const lines = readFileSync ( entryPoint , 'utf-8' ) . split ( '\n' ) ;
83+ lines . splice ( 1 , 0 , 'import "proxy-agent";' ) ;
84+
85+ // Step 1: create one fat JS file with node resolver to ensure no imports point to non-node export conditions
86+ const result = await build ( {
87+ entrypoints : [ entryPoint ] ,
88+ files : {
89+ [ entryPoint ] : lines . join ( '\n' ) ,
90+ } ,
91+ outdir : fileURLToPath ( new URL ( `../bundles/fat-clis` , import . meta. url ) ) ,
92+ conditions : 'node' ,
93+ target : 'bun' ,
94+ sourcemap : 'none' ,
95+ } ) ;
96+
97+ const entrypointResultFilePath = result . outputs [ 0 ] ! . path ;
98+
99+ // Fix apify client js (it now lazy loads proxy-agent, which makes bun skip it from the bundle)
100+ {
101+ const entrypointResultFileContent = await result . outputs [ 0 ] ! . text ( ) ;
102+
103+ const newEntrypointResultFileContent = entrypointResultFileContent . replace (
104+ `(0, utils_1.dynamicNodeImport)("proxy-agent")` ,
105+ `Promise.resolve().then(() => import_proxy_agent)` ,
106+ ) ;
107+
108+ await writeFile ( entrypointResultFilePath , newEntrypointResultFileContent ) ;
109+ }
110+
80111 for ( const target of targets ) {
81112 // eslint-disable-next-line prefer-const -- somehow it cannot tell that os and arch cannot be "const" while the rest are let
82113 let [ , os , arch , musl , baseline ] = target . split ( '-' ) ;
@@ -88,6 +119,7 @@ for (const entryPoint of entryPoints) {
88119
89120 // If we are building on Windows ARM64, even though the target is x64, we mark it as "arm64" (there are some weird errors when compiling on x64
90121 // and running on arm64). Hopefully bun will get arm64 native builds
122+ // TODO: Vlad remove this in a subsequent PR as Bun now has native arm64 windows builds
91123 if ( os === 'windows' && process . platform === 'win32' ) {
92124 const systemType = await $ `pwsh -c "(Get-CimInstance Win32_ComputerSystem).SystemType"` . text ( ) ;
93125
@@ -108,8 +140,21 @@ for (const entryPoint of entryPoints) {
108140 const outFile = fileURLToPath ( new URL ( `../bundles/${ fileName } ` , import . meta. url ) ) ;
109141
110142 console . log ( `Building ${ cliName } for ${ target } (result: ${ fileName } )...` ) ;
111- // TODO: --sourcemap crashes for w/e reason and --bytecode doesn't support ESM (TLA to be exact)
112- await $ `bun build --compile --minify --target=${ target } --outfile=${ outFile } ${ entryPoint } ` ;
143+
144+ // Step 2: create the final executable bundle
145+ await build ( {
146+ entrypoints : [ entrypointResultFilePath ] ,
147+ compile : {
148+ outfile : outFile ,
149+ target,
150+ } ,
151+ format : 'esm' ,
152+ minify : {
153+ identifiers : true ,
154+ keepNames : true ,
155+ } ,
156+ bytecode : true ,
157+ } ) ;
113158
114159 // Remove the arch override
115160 await writeFile ( metadataFile , newContent ) ;
0 commit comments