99 */
1010import { existsSync } from "node:fs" ;
1111import { spawnSync } from "node:child_process" ;
12+ import { resolve } from "node:path" ;
13+ import { fileURLToPath } from "node:url" ;
1214
1315function parseArgs ( argv ) {
1416 let entry = null ;
@@ -34,11 +36,14 @@ function parseArgs(argv) {
3436 return { entry, outfile, target } ;
3537}
3638
37- const { entry, outfile, target } = parseArgs ( process . argv . slice ( 2 ) ) ;
38-
39- const result = spawnSync (
40- "bun" ,
41- [
39+ /**
40+ * Bun ≤1.3.14 inlines `process.env.NODE_ENV` during `bun build --compile`.
41+ * Unset on the build machine becomes the string `"development"`, which
42+ * `detectEnv()` would report as dev. Pin production here, and keep the
43+ * compiled-marker define so the runtime check still wins if that default changes.
44+ */
45+ export function bunCompileArguments ( entry , outfile , target ) {
46+ return [
4247 "build" ,
4348 entry ,
4449 "--compile" ,
@@ -48,18 +53,30 @@ const result = spawnSync(
4853 target ,
4954 "--define" ,
5055 'process.env.BAILIAN_COMPILED="1"' ,
51- ] ,
52- { encoding : "utf-8" , stdio : [ "ignore" , "pipe" , "pipe" ] } ,
53- ) ;
56+ "--define" ,
57+ 'process.env.NODE_ENV="production"' ,
58+ ] ;
59+ }
5460
55- if ( result . stdout ) process . stdout . write ( result . stdout ) ;
56- if ( result . stderr ) process . stderr . write ( result . stderr ) ;
61+ function compileBinary ( { entry, outfile, target } ) {
62+ const result = spawnSync ( "bun" , bunCompileArguments ( entry , outfile , target ) , {
63+ encoding : "utf-8" ,
64+ stdio : [ "ignore" , "pipe" , "pipe" ] ,
65+ } ) ;
5766
58- if ( result . status !== 0 ) {
59- process . exit ( result . status ?? 1 ) ;
67+ if ( result . stdout ) process . stdout . write ( result . stdout ) ;
68+ if ( result . stderr ) process . stderr . write ( result . stderr ) ;
69+
70+ if ( result . status !== 0 ) {
71+ process . exit ( result . status ?? 1 ) ;
72+ }
73+
74+ if ( ! existsSync ( outfile ) ) {
75+ console . error ( `bun build --compile exited 0 but outfile missing: ${ outfile } ` ) ;
76+ process . exit ( 1 ) ;
77+ }
6078}
6179
62- if ( ! existsSync ( outfile ) ) {
63- console . error ( `bun build --compile exited 0 but outfile missing: ${ outfile } ` ) ;
64- process . exit ( 1 ) ;
80+ if ( resolve ( process . argv [ 1 ] ?? "" ) === fileURLToPath ( import . meta. url ) ) {
81+ compileBinary ( parseArgs ( process . argv . slice ( 2 ) ) ) ;
6582}
0 commit comments