bun-types 1.3.5-canary.20251209T140747 → 1.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bun.d.ts +225 -0
- package/bundle.d.ts +74 -0
- package/docs/bundler/esbuild.mdx +1 -0
- package/docs/bundler/executables.mdx +588 -98
- package/docs/bundler/fullstack.mdx +2 -2
- package/docs/bundler/index.mdx +78 -0
- package/docs/guides/install/trusted.mdx +3 -1
- package/docs/guides/process/os-signals.mdx +0 -10
- package/docs/pm/lifecycle.mdx +7 -0
- package/docs/runtime/child-process.mdx +131 -4
- package/globals.d.ts +31 -28
- package/index.d.ts +1 -0
- package/overrides.d.ts +18 -3
- package/package.json +1 -1
- package/test.d.ts +10 -8
|
@@ -5,18 +5,28 @@ description: "Generate standalone executables from TypeScript or JavaScript file
|
|
|
5
5
|
|
|
6
6
|
Bun's bundler implements a `--compile` flag for generating a standalone binary from a TypeScript or JavaScript file.
|
|
7
7
|
|
|
8
|
-
<
|
|
9
|
-
|
|
10
|
-
```bash terminal icon="terminal"
|
|
11
|
-
bun build ./cli.ts --compile --outfile mycli
|
|
12
|
-
```
|
|
8
|
+
<Tabs>
|
|
9
|
+
<Tab title="CLI">
|
|
10
|
+
```bash terminal icon="terminal"
|
|
11
|
+
bun build ./cli.ts --compile --outfile mycli
|
|
12
|
+
```
|
|
13
|
+
</Tab>
|
|
14
|
+
<Tab title="JavaScript">
|
|
15
|
+
```ts build.ts icon="/icons/typescript.svg"
|
|
16
|
+
await Bun.build({
|
|
17
|
+
entrypoints: ["./cli.ts"],
|
|
18
|
+
compile: {
|
|
19
|
+
outfile: "./mycli",
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
```
|
|
23
|
+
</Tab>
|
|
24
|
+
</Tabs>
|
|
13
25
|
|
|
14
26
|
```ts cli.ts icon="/icons/typescript.svg"
|
|
15
27
|
console.log("Hello world!");
|
|
16
28
|
```
|
|
17
29
|
|
|
18
|
-
</CodeGroup>
|
|
19
|
-
|
|
20
30
|
This bundles `cli.ts` into an executable that can be executed directly:
|
|
21
31
|
|
|
22
32
|
```bash terminal icon="terminal"
|
|
@@ -37,49 +47,157 @@ The `--target` flag lets you compile your standalone executable for a different
|
|
|
37
47
|
|
|
38
48
|
To build for Linux x64 (most servers):
|
|
39
49
|
|
|
40
|
-
|
|
41
|
-
|
|
50
|
+
<Tabs>
|
|
51
|
+
<Tab title="CLI">
|
|
52
|
+
```bash icon="terminal" terminal
|
|
53
|
+
bun build --compile --target=bun-linux-x64 ./index.ts --outfile myapp
|
|
54
|
+
|
|
55
|
+
# To support CPUs from before 2013, use the baseline version (nehalem)
|
|
56
|
+
bun build --compile --target=bun-linux-x64-baseline ./index.ts --outfile myapp
|
|
57
|
+
|
|
58
|
+
# To explicitly only support CPUs from 2013 and later, use the modern version (haswell)
|
|
59
|
+
# modern is faster, but baseline is more compatible.
|
|
60
|
+
bun build --compile --target=bun-linux-x64-modern ./index.ts --outfile myapp
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
</Tab>
|
|
64
|
+
<Tab title="JavaScript">
|
|
65
|
+
```ts build.ts icon="/icons/typescript.svg"
|
|
66
|
+
// Standard Linux x64
|
|
67
|
+
await Bun.build({
|
|
68
|
+
entrypoints: ["./index.ts"],
|
|
69
|
+
compile: {
|
|
70
|
+
target: "bun-linux-x64",
|
|
71
|
+
outfile: "./myapp",
|
|
72
|
+
},
|
|
73
|
+
});
|
|
42
74
|
|
|
43
|
-
|
|
44
|
-
|
|
75
|
+
// Baseline (pre-2013 CPUs)
|
|
76
|
+
await Bun.build({
|
|
77
|
+
entrypoints: ["./index.ts"],
|
|
78
|
+
compile: {
|
|
79
|
+
target: "bun-linux-x64-baseline",
|
|
80
|
+
outfile: "./myapp",
|
|
81
|
+
},
|
|
82
|
+
});
|
|
45
83
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
84
|
+
// Modern (2013+ CPUs, faster)
|
|
85
|
+
await Bun.build({
|
|
86
|
+
entrypoints: ["./index.ts"],
|
|
87
|
+
compile: {
|
|
88
|
+
target: "bun-linux-x64-modern",
|
|
89
|
+
outfile: "./myapp",
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
</Tab>
|
|
95
|
+
</Tabs>
|
|
50
96
|
|
|
51
97
|
To build for Linux ARM64 (e.g. Graviton or Raspberry Pi):
|
|
52
98
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
99
|
+
<Tabs>
|
|
100
|
+
<Tab title="CLI">
|
|
101
|
+
```bash icon="terminal" terminal
|
|
102
|
+
# Note: the default architecture is x64 if no architecture is specified.
|
|
103
|
+
bun build --compile --target=bun-linux-arm64 ./index.ts --outfile myapp
|
|
104
|
+
```
|
|
105
|
+
</Tab>
|
|
106
|
+
<Tab title="JavaScript">
|
|
107
|
+
```ts build.ts icon="/icons/typescript.svg"
|
|
108
|
+
await Bun.build({
|
|
109
|
+
entrypoints: ["./index.ts"],
|
|
110
|
+
compile: {
|
|
111
|
+
target: "bun-linux-arm64",
|
|
112
|
+
outfile: "./myapp",
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
</Tab>
|
|
117
|
+
</Tabs>
|
|
57
118
|
|
|
58
119
|
To build for Windows x64:
|
|
59
120
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
121
|
+
<Tabs>
|
|
122
|
+
<Tab title="CLI">
|
|
123
|
+
```bash icon="terminal" terminal
|
|
124
|
+
bun build --compile --target=bun-windows-x64 ./path/to/my/app.ts --outfile myapp
|
|
125
|
+
|
|
126
|
+
# To support CPUs from before 2013, use the baseline version (nehalem)
|
|
127
|
+
bun build --compile --target=bun-windows-x64-baseline ./path/to/my/app.ts --outfile myapp
|
|
128
|
+
|
|
129
|
+
# To explicitly only support CPUs from 2013 and later, use the modern version (haswell)
|
|
130
|
+
bun build --compile --target=bun-windows-x64-modern ./path/to/my/app.ts --outfile myapp
|
|
131
|
+
|
|
132
|
+
# note: if no .exe extension is provided, Bun will automatically add it for Windows executables
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
</Tab>
|
|
136
|
+
<Tab title="JavaScript">
|
|
137
|
+
```ts build.ts icon="/icons/typescript.svg"
|
|
138
|
+
// Standard Windows x64
|
|
139
|
+
await Bun.build({
|
|
140
|
+
entrypoints: ["./path/to/my/app.ts"],
|
|
141
|
+
compile: {
|
|
142
|
+
target: "bun-windows-x64",
|
|
143
|
+
outfile: "./myapp", // .exe added automatically
|
|
144
|
+
},
|
|
145
|
+
});
|
|
65
146
|
|
|
66
|
-
|
|
67
|
-
|
|
147
|
+
// Baseline or modern variants
|
|
148
|
+
await Bun.build({
|
|
149
|
+
entrypoints: ["./path/to/my/app.ts"],
|
|
150
|
+
compile: {
|
|
151
|
+
target: "bun-windows-x64-baseline",
|
|
152
|
+
outfile: "./myapp",
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
```
|
|
68
156
|
|
|
69
|
-
|
|
70
|
-
|
|
157
|
+
</Tab>
|
|
158
|
+
</Tabs>
|
|
71
159
|
|
|
72
160
|
To build for macOS arm64:
|
|
73
161
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
```
|
|
162
|
+
<Tabs>
|
|
163
|
+
<Tab title="CLI">
|
|
164
|
+
```bash icon="terminal" terminal
|
|
165
|
+
bun build --compile --target=bun-darwin-arm64 ./path/to/my/app.ts --outfile myapp
|
|
166
|
+
```
|
|
167
|
+
</Tab>
|
|
168
|
+
<Tab title="JavaScript">
|
|
169
|
+
```ts build.ts icon="/icons/typescript.svg"
|
|
170
|
+
await Bun.build({
|
|
171
|
+
entrypoints: ["./path/to/my/app.ts"],
|
|
172
|
+
compile: {
|
|
173
|
+
target: "bun-darwin-arm64",
|
|
174
|
+
outfile: "./myapp",
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
```
|
|
178
|
+
</Tab>
|
|
179
|
+
</Tabs>
|
|
77
180
|
|
|
78
181
|
To build for macOS x64:
|
|
79
182
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
```
|
|
183
|
+
<Tabs>
|
|
184
|
+
<Tab title="CLI">
|
|
185
|
+
```bash icon="terminal" terminal
|
|
186
|
+
bun build --compile --target=bun-darwin-x64 ./path/to/my/app.ts --outfile myapp
|
|
187
|
+
```
|
|
188
|
+
</Tab>
|
|
189
|
+
<Tab title="JavaScript">
|
|
190
|
+
```ts build.ts icon="/icons/typescript.svg"
|
|
191
|
+
await Bun.build({
|
|
192
|
+
entrypoints: ["./path/to/my/app.ts"],
|
|
193
|
+
compile: {
|
|
194
|
+
target: "bun-darwin-x64",
|
|
195
|
+
outfile: "./myapp",
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
```
|
|
199
|
+
</Tab>
|
|
200
|
+
</Tabs>
|
|
83
201
|
|
|
84
202
|
### Supported targets
|
|
85
203
|
|
|
@@ -110,9 +228,27 @@ The order of the `--target` flag does not matter, as long as they're delimited b
|
|
|
110
228
|
|
|
111
229
|
Use the `--define` flag to inject build-time constants into your executable, such as version numbers, build timestamps, or configuration values:
|
|
112
230
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
```
|
|
231
|
+
<Tabs>
|
|
232
|
+
<Tab title="CLI">
|
|
233
|
+
```bash icon="terminal" terminal
|
|
234
|
+
bun build --compile --define BUILD_VERSION='"1.2.3"' --define BUILD_TIME='"2024-01-15T10:30:00Z"' src/cli.ts --outfile mycli
|
|
235
|
+
```
|
|
236
|
+
</Tab>
|
|
237
|
+
<Tab title="JavaScript">
|
|
238
|
+
```ts build.ts icon="/icons/typescript.svg"
|
|
239
|
+
await Bun.build({
|
|
240
|
+
entrypoints: ["./src/cli.ts"],
|
|
241
|
+
compile: {
|
|
242
|
+
outfile: "./mycli",
|
|
243
|
+
},
|
|
244
|
+
define: {
|
|
245
|
+
BUILD_VERSION: JSON.stringify("1.2.3"),
|
|
246
|
+
BUILD_TIME: JSON.stringify("2024-01-15T10:30:00Z"),
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
```
|
|
250
|
+
</Tab>
|
|
251
|
+
</Tabs>
|
|
116
252
|
|
|
117
253
|
These constants are embedded directly into your compiled binary at build time, providing zero runtime overhead and enabling dead code elimination optimizations.
|
|
118
254
|
|
|
@@ -133,17 +269,50 @@ With compiled executables, you can move that cost from runtime to build-time.
|
|
|
133
269
|
|
|
134
270
|
When deploying to production, we recommend the following:
|
|
135
271
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
```
|
|
272
|
+
<Tabs>
|
|
273
|
+
<Tab title="CLI">
|
|
274
|
+
```bash icon="terminal" terminal
|
|
275
|
+
bun build --compile --minify --sourcemap ./path/to/my/app.ts --outfile myapp
|
|
276
|
+
```
|
|
277
|
+
</Tab>
|
|
278
|
+
<Tab title="JavaScript">
|
|
279
|
+
```ts build.ts icon="/icons/typescript.svg"
|
|
280
|
+
await Bun.build({
|
|
281
|
+
entrypoints: ["./path/to/my/app.ts"],
|
|
282
|
+
compile: {
|
|
283
|
+
outfile: "./myapp",
|
|
284
|
+
},
|
|
285
|
+
minify: true,
|
|
286
|
+
sourcemap: "linked",
|
|
287
|
+
});
|
|
288
|
+
```
|
|
289
|
+
</Tab>
|
|
290
|
+
</Tabs>
|
|
139
291
|
|
|
140
292
|
### Bytecode compilation
|
|
141
293
|
|
|
142
294
|
To improve startup time, enable bytecode compilation:
|
|
143
295
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
```
|
|
296
|
+
<Tabs>
|
|
297
|
+
<Tab title="CLI">
|
|
298
|
+
```bash icon="terminal" terminal
|
|
299
|
+
bun build --compile --minify --sourcemap --bytecode ./path/to/my/app.ts --outfile myapp
|
|
300
|
+
```
|
|
301
|
+
</Tab>
|
|
302
|
+
<Tab title="JavaScript">
|
|
303
|
+
```ts build.ts icon="/icons/typescript.svg"
|
|
304
|
+
await Bun.build({
|
|
305
|
+
entrypoints: ["./path/to/my/app.ts"],
|
|
306
|
+
compile: {
|
|
307
|
+
outfile: "./myapp",
|
|
308
|
+
},
|
|
309
|
+
minify: true,
|
|
310
|
+
sourcemap: "linked",
|
|
311
|
+
bytecode: true,
|
|
312
|
+
});
|
|
313
|
+
```
|
|
314
|
+
</Tab>
|
|
315
|
+
</Tabs>
|
|
147
316
|
|
|
148
317
|
Using bytecode compilation, `tsc` starts 2x faster:
|
|
149
318
|
|
|
@@ -172,9 +341,24 @@ The `--bytecode` argument enables bytecode compilation. Every time you run JavaS
|
|
|
172
341
|
|
|
173
342
|
**`--compile-exec-argv="args"`** - Embed runtime arguments that are available via `process.execArgv`:
|
|
174
343
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
```
|
|
344
|
+
<Tabs>
|
|
345
|
+
<Tab title="CLI">
|
|
346
|
+
```bash icon="terminal" terminal
|
|
347
|
+
bun build --compile --compile-exec-argv="--smol --user-agent=MyBot" ./app.ts --outfile myapp
|
|
348
|
+
```
|
|
349
|
+
</Tab>
|
|
350
|
+
<Tab title="JavaScript">
|
|
351
|
+
```ts build.ts icon="/icons/typescript.svg"
|
|
352
|
+
await Bun.build({
|
|
353
|
+
entrypoints: ["./app.ts"],
|
|
354
|
+
compile: {
|
|
355
|
+
execArgv: ["--smol", "--user-agent=MyBot"],
|
|
356
|
+
outfile: "./myapp",
|
|
357
|
+
},
|
|
358
|
+
});
|
|
359
|
+
```
|
|
360
|
+
</Tab>
|
|
361
|
+
</Tabs>
|
|
178
362
|
|
|
179
363
|
```ts app.ts icon="/icons/typescript.svg"
|
|
180
364
|
// In the compiled app
|
|
@@ -213,35 +397,39 @@ bun build --compile --compile-autoload-tsconfig --compile-autoload-package-json
|
|
|
213
397
|
|
|
214
398
|
To disable `.env` or `bunfig.toml` loading for deterministic execution:
|
|
215
399
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
```ts
|
|
232
|
-
await Bun.build({
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
400
|
+
<Tabs>
|
|
401
|
+
<Tab title="CLI">
|
|
402
|
+
```bash icon="terminal" terminal
|
|
403
|
+
# Disable .env loading
|
|
404
|
+
bun build --compile --no-compile-autoload-dotenv ./app.ts --outfile myapp
|
|
405
|
+
|
|
406
|
+
# Disable bunfig.toml loading
|
|
407
|
+
bun build --compile --no-compile-autoload-bunfig ./app.ts --outfile myapp
|
|
408
|
+
|
|
409
|
+
# Disable all config loading
|
|
410
|
+
bun build --compile --no-compile-autoload-dotenv --no-compile-autoload-bunfig ./app.ts --outfile myapp
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
</Tab>
|
|
414
|
+
<Tab title="JavaScript">
|
|
415
|
+
```ts build.ts icon="/icons/typescript.svg"
|
|
416
|
+
await Bun.build({
|
|
417
|
+
entrypoints: ["./app.ts"],
|
|
418
|
+
compile: {
|
|
419
|
+
// tsconfig.json and package.json are disabled by default
|
|
420
|
+
autoloadTsconfig: true, // Enable tsconfig.json loading
|
|
421
|
+
autoloadPackageJson: true, // Enable package.json loading
|
|
422
|
+
|
|
423
|
+
// .env and bunfig.toml are enabled by default
|
|
424
|
+
autoloadDotenv: false, // Disable .env loading
|
|
425
|
+
autoloadBunfig: false, // Disable bunfig.toml loading
|
|
426
|
+
outfile: "./myapp",
|
|
427
|
+
},
|
|
428
|
+
});
|
|
429
|
+
```
|
|
238
430
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
autoloadBunfig: false, // Disable bunfig.toml loading
|
|
242
|
-
},
|
|
243
|
-
});
|
|
244
|
-
```
|
|
431
|
+
</Tab>
|
|
432
|
+
</Tabs>
|
|
245
433
|
|
|
246
434
|
---
|
|
247
435
|
|
|
@@ -340,9 +528,23 @@ body {
|
|
|
340
528
|
|
|
341
529
|
To build this into a single executable:
|
|
342
530
|
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
```
|
|
531
|
+
<Tabs>
|
|
532
|
+
<Tab title="CLI">
|
|
533
|
+
```bash terminal icon="terminal"
|
|
534
|
+
bun build --compile ./server.ts --outfile myapp
|
|
535
|
+
```
|
|
536
|
+
</Tab>
|
|
537
|
+
<Tab title="JavaScript">
|
|
538
|
+
```ts build.ts icon="/icons/typescript.svg"
|
|
539
|
+
await Bun.build({
|
|
540
|
+
entrypoints: ["./server.ts"],
|
|
541
|
+
compile: {
|
|
542
|
+
outfile: "./myapp",
|
|
543
|
+
},
|
|
544
|
+
});
|
|
545
|
+
```
|
|
546
|
+
</Tab>
|
|
547
|
+
</Tabs>
|
|
346
548
|
|
|
347
549
|
This creates a self-contained binary that includes:
|
|
348
550
|
|
|
@@ -365,11 +567,25 @@ For more details on building full-stack applications with Bun, see the [full-sta
|
|
|
365
567
|
|
|
366
568
|
## Worker
|
|
367
569
|
|
|
368
|
-
To use workers in a standalone executable, add the worker's entrypoint to the
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
```
|
|
570
|
+
To use workers in a standalone executable, add the worker's entrypoint to the build:
|
|
571
|
+
|
|
572
|
+
<Tabs>
|
|
573
|
+
<Tab title="CLI">
|
|
574
|
+
```bash terminal icon="terminal"
|
|
575
|
+
bun build --compile ./index.ts ./my-worker.ts --outfile myapp
|
|
576
|
+
```
|
|
577
|
+
</Tab>
|
|
578
|
+
<Tab title="JavaScript">
|
|
579
|
+
```ts build.ts icon="/icons/typescript.svg"
|
|
580
|
+
await Bun.build({
|
|
581
|
+
entrypoints: ["./index.ts", "./my-worker.ts"],
|
|
582
|
+
compile: {
|
|
583
|
+
outfile: "./myapp",
|
|
584
|
+
},
|
|
585
|
+
});
|
|
586
|
+
```
|
|
587
|
+
</Tab>
|
|
588
|
+
</Tabs>
|
|
373
589
|
|
|
374
590
|
Then, reference the worker in your code:
|
|
375
591
|
|
|
@@ -590,11 +806,32 @@ Unfortunately, if you're using `@mapbox/node-pre-gyp` or other similar tools, yo
|
|
|
590
806
|
|
|
591
807
|
### Embed directories
|
|
592
808
|
|
|
593
|
-
To embed a directory with `bun build --compile`,
|
|
809
|
+
To embed a directory with `bun build --compile`, include file patterns in your build:
|
|
810
|
+
|
|
811
|
+
<Tabs>
|
|
812
|
+
<Tab title="CLI">
|
|
813
|
+
```bash terminal icon="terminal"
|
|
814
|
+
bun build --compile ./index.ts ./public/**/*.png
|
|
815
|
+
```
|
|
816
|
+
</Tab>
|
|
817
|
+
<Tab title="JavaScript">
|
|
818
|
+
```ts build.ts icon="/icons/typescript.svg"
|
|
819
|
+
import { Glob } from "bun";
|
|
820
|
+
|
|
821
|
+
// Expand glob pattern to file list
|
|
822
|
+
const glob = new Glob("./public/**/*.png");
|
|
823
|
+
const pngFiles = Array.from(glob.scanSync("."));
|
|
824
|
+
|
|
825
|
+
await Bun.build({
|
|
826
|
+
entrypoints: ["./index.ts", ...pngFiles],
|
|
827
|
+
compile: {
|
|
828
|
+
outfile: "./myapp",
|
|
829
|
+
},
|
|
830
|
+
});
|
|
831
|
+
```
|
|
594
832
|
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
```
|
|
833
|
+
</Tab>
|
|
834
|
+
</Tabs>
|
|
598
835
|
|
|
599
836
|
Then, you can reference the files in your code:
|
|
600
837
|
|
|
@@ -670,26 +907,118 @@ serve({
|
|
|
670
907
|
|
|
671
908
|
By default, embedded files have a content hash appended to their name. This is useful for situations where you want to serve the file from a URL or CDN and have fewer cache invalidation issues. But sometimes, this is unexpected and you might want the original name instead:
|
|
672
909
|
|
|
673
|
-
To disable the content hash,
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
```
|
|
910
|
+
To disable the content hash, configure asset naming:
|
|
911
|
+
|
|
912
|
+
<Tabs>
|
|
913
|
+
<Tab title="CLI">
|
|
914
|
+
```bash terminal icon="terminal"
|
|
915
|
+
bun build --compile --asset-naming="[name].[ext]" ./index.ts
|
|
916
|
+
```
|
|
917
|
+
</Tab>
|
|
918
|
+
<Tab title="JavaScript">
|
|
919
|
+
```ts build.ts icon="/icons/typescript.svg"
|
|
920
|
+
await Bun.build({
|
|
921
|
+
entrypoints: ["./index.ts"],
|
|
922
|
+
compile: {
|
|
923
|
+
outfile: "./myapp",
|
|
924
|
+
},
|
|
925
|
+
naming: {
|
|
926
|
+
asset: "[name].[ext]",
|
|
927
|
+
},
|
|
928
|
+
});
|
|
929
|
+
```
|
|
930
|
+
</Tab>
|
|
931
|
+
</Tabs>
|
|
678
932
|
|
|
679
933
|
---
|
|
680
934
|
|
|
681
935
|
## Minification
|
|
682
936
|
|
|
683
|
-
To trim down the size of the executable
|
|
937
|
+
To trim down the size of the executable, enable minification:
|
|
938
|
+
|
|
939
|
+
<Tabs>
|
|
940
|
+
<Tab title="CLI">
|
|
941
|
+
```bash terminal icon="terminal"
|
|
942
|
+
bun build --compile --minify ./index.ts --outfile myapp
|
|
943
|
+
```
|
|
944
|
+
</Tab>
|
|
945
|
+
<Tab title="JavaScript">
|
|
946
|
+
```ts build.ts icon="/icons/typescript.svg"
|
|
947
|
+
await Bun.build({
|
|
948
|
+
entrypoints: ["./index.ts"],
|
|
949
|
+
compile: {
|
|
950
|
+
outfile: "./myapp",
|
|
951
|
+
},
|
|
952
|
+
minify: true, // Enable all minification
|
|
953
|
+
});
|
|
954
|
+
|
|
955
|
+
// Or granular control:
|
|
956
|
+
await Bun.build({
|
|
957
|
+
entrypoints: ["./index.ts"],
|
|
958
|
+
compile: {
|
|
959
|
+
outfile: "./myapp",
|
|
960
|
+
},
|
|
961
|
+
minify: {
|
|
962
|
+
whitespace: true,
|
|
963
|
+
syntax: true,
|
|
964
|
+
identifiers: true,
|
|
965
|
+
},
|
|
966
|
+
});
|
|
967
|
+
```
|
|
968
|
+
|
|
969
|
+
</Tab>
|
|
970
|
+
</Tabs>
|
|
971
|
+
|
|
972
|
+
This uses Bun's minifier to reduce the code size. Overall though, Bun's binary is still way too big and we need to make it smaller.
|
|
684
973
|
|
|
685
974
|
---
|
|
686
975
|
|
|
687
976
|
## Windows-specific flags
|
|
688
977
|
|
|
689
|
-
When compiling a standalone executable on Windows, there are
|
|
978
|
+
When compiling a standalone executable on Windows, there are platform-specific options to customize metadata on the generated `.exe` file:
|
|
979
|
+
|
|
980
|
+
<Tabs>
|
|
981
|
+
<Tab title="CLI">
|
|
982
|
+
```bash terminal icon="terminal"
|
|
983
|
+
# Custom icon
|
|
984
|
+
bun build --compile --windows-icon=path/to/icon.ico ./app.ts --outfile myapp
|
|
985
|
+
|
|
986
|
+
# Hide console window (for GUI apps)
|
|
987
|
+
bun build --compile --windows-hide-console ./app.ts --outfile myapp
|
|
988
|
+
```
|
|
989
|
+
|
|
990
|
+
</Tab>
|
|
991
|
+
<Tab title="JavaScript">
|
|
992
|
+
```ts build.ts icon="/icons/typescript.svg"
|
|
993
|
+
await Bun.build({
|
|
994
|
+
entrypoints: ["./app.ts"],
|
|
995
|
+
compile: {
|
|
996
|
+
outfile: "./myapp",
|
|
997
|
+
windows: {
|
|
998
|
+
icon: "./path/to/icon.ico",
|
|
999
|
+
hideConsole: true,
|
|
1000
|
+
// Additional Windows metadata:
|
|
1001
|
+
title: "My Application",
|
|
1002
|
+
publisher: "My Company",
|
|
1003
|
+
version: "1.0.0",
|
|
1004
|
+
description: "A standalone Windows application",
|
|
1005
|
+
copyright: "Copyright 2024",
|
|
1006
|
+
},
|
|
1007
|
+
},
|
|
1008
|
+
});
|
|
1009
|
+
```
|
|
1010
|
+
</Tab>
|
|
1011
|
+
</Tabs>
|
|
1012
|
+
|
|
1013
|
+
Available Windows options:
|
|
690
1014
|
|
|
691
|
-
-
|
|
692
|
-
-
|
|
1015
|
+
- `icon` - Path to `.ico` file for the executable icon
|
|
1016
|
+
- `hideConsole` - Disable the background terminal (for GUI apps)
|
|
1017
|
+
- `title` - Application title in file properties
|
|
1018
|
+
- `publisher` - Publisher name in file properties
|
|
1019
|
+
- `version` - Version string in file properties
|
|
1020
|
+
- `description` - Description in file properties
|
|
1021
|
+
- `copyright` - Copyright notice in file properties
|
|
693
1022
|
|
|
694
1023
|
<Warning>These flags currently cannot be used when cross-compiling because they depend on Windows APIs.</Warning>
|
|
695
1024
|
|
|
@@ -746,9 +1075,23 @@ codesign -vvv --verify ./myapp
|
|
|
746
1075
|
|
|
747
1076
|
Standalone executables support code splitting. Use `--compile` with `--splitting` to create an executable that loads code-split chunks at runtime.
|
|
748
1077
|
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
```
|
|
1078
|
+
<Tabs>
|
|
1079
|
+
<Tab title="CLI">
|
|
1080
|
+
```bash terminal icon="terminal"
|
|
1081
|
+
bun build --compile --splitting ./src/entry.ts --outdir ./build
|
|
1082
|
+
```
|
|
1083
|
+
</Tab>
|
|
1084
|
+
<Tab title="JavaScript">
|
|
1085
|
+
```ts build.ts icon="/icons/typescript.svg"
|
|
1086
|
+
await Bun.build({
|
|
1087
|
+
entrypoints: ["./src/entry.ts"],
|
|
1088
|
+
compile: true,
|
|
1089
|
+
splitting: true,
|
|
1090
|
+
outdir: "./build",
|
|
1091
|
+
});
|
|
1092
|
+
```
|
|
1093
|
+
</Tab>
|
|
1094
|
+
</Tabs>
|
|
752
1095
|
|
|
753
1096
|
<CodeGroup>
|
|
754
1097
|
|
|
@@ -777,6 +1120,50 @@ Lazy module loaded
|
|
|
777
1120
|
|
|
778
1121
|
---
|
|
779
1122
|
|
|
1123
|
+
## Using plugins
|
|
1124
|
+
|
|
1125
|
+
Plugins work with standalone executables, allowing you to transform files during the build process:
|
|
1126
|
+
|
|
1127
|
+
```ts build.ts icon="/icons/typescript.svg"
|
|
1128
|
+
import type { BunPlugin } from "bun";
|
|
1129
|
+
|
|
1130
|
+
const envPlugin: BunPlugin = {
|
|
1131
|
+
name: "env-loader",
|
|
1132
|
+
setup(build) {
|
|
1133
|
+
build.onLoad({ filter: /\.env\.json$/ }, async args => {
|
|
1134
|
+
// Transform .env.json files into validated config objects
|
|
1135
|
+
const env = await Bun.file(args.path).json();
|
|
1136
|
+
|
|
1137
|
+
return {
|
|
1138
|
+
contents: `export default ${JSON.stringify(env)};`,
|
|
1139
|
+
loader: "js",
|
|
1140
|
+
};
|
|
1141
|
+
});
|
|
1142
|
+
},
|
|
1143
|
+
};
|
|
1144
|
+
|
|
1145
|
+
await Bun.build({
|
|
1146
|
+
entrypoints: ["./cli.ts"],
|
|
1147
|
+
compile: {
|
|
1148
|
+
outfile: "./mycli",
|
|
1149
|
+
},
|
|
1150
|
+
plugins: [envPlugin],
|
|
1151
|
+
});
|
|
1152
|
+
```
|
|
1153
|
+
|
|
1154
|
+
Example use case - embedding environment config at build time:
|
|
1155
|
+
|
|
1156
|
+
```ts cli.ts icon="/icons/typescript.svg"
|
|
1157
|
+
import config from "./config.env.json";
|
|
1158
|
+
|
|
1159
|
+
console.log(`Running in ${config.environment} mode`);
|
|
1160
|
+
console.log(`API endpoint: ${config.apiUrl}`);
|
|
1161
|
+
```
|
|
1162
|
+
|
|
1163
|
+
Plugins can perform any transformation: compile YAML/TOML configs, inline SQL queries, generate type-safe API clients, or preprocess templates. Refer to the [plugin documentation](/docs/bundler/plugins) for more details.
|
|
1164
|
+
|
|
1165
|
+
---
|
|
1166
|
+
|
|
780
1167
|
## Unsupported CLI arguments
|
|
781
1168
|
|
|
782
1169
|
Currently, the `--compile` flag can only accept a single entrypoint at a time and does not support the following flags:
|
|
@@ -785,3 +1172,106 @@ Currently, the `--compile` flag can only accept a single entrypoint at a time an
|
|
|
785
1172
|
- `--public-path`
|
|
786
1173
|
- `--target=node` or `--target=browser`
|
|
787
1174
|
- `--no-bundle` - we always bundle everything into the executable.
|
|
1175
|
+
|
|
1176
|
+
---
|
|
1177
|
+
|
|
1178
|
+
## API reference
|
|
1179
|
+
|
|
1180
|
+
The `compile` option in `Bun.build()` accepts three forms:
|
|
1181
|
+
|
|
1182
|
+
```ts title="types" icon="/icons/typescript.svg"
|
|
1183
|
+
interface BuildConfig {
|
|
1184
|
+
entrypoints: string[];
|
|
1185
|
+
compile: boolean | Bun.Build.Target | CompileBuildOptions;
|
|
1186
|
+
// ... other BuildConfig options (minify, sourcemap, define, plugins, etc.)
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
interface CompileBuildOptions {
|
|
1190
|
+
target?: Bun.Build.Target; // Cross-compilation target
|
|
1191
|
+
outfile?: string; // Output executable path
|
|
1192
|
+
execArgv?: string[]; // Runtime arguments (process.execArgv)
|
|
1193
|
+
autoloadTsconfig?: boolean; // Load tsconfig.json (default: false)
|
|
1194
|
+
autoloadPackageJson?: boolean; // Load package.json (default: false)
|
|
1195
|
+
autoloadDotenv?: boolean; // Load .env files (default: true)
|
|
1196
|
+
autoloadBunfig?: boolean; // Load bunfig.toml (default: true)
|
|
1197
|
+
windows?: {
|
|
1198
|
+
icon?: string; // Path to .ico file
|
|
1199
|
+
hideConsole?: boolean; // Hide console window
|
|
1200
|
+
title?: string; // Application title
|
|
1201
|
+
publisher?: string; // Publisher name
|
|
1202
|
+
version?: string; // Version string
|
|
1203
|
+
description?: string; // Description
|
|
1204
|
+
copyright?: string; // Copyright notice
|
|
1205
|
+
};
|
|
1206
|
+
}
|
|
1207
|
+
```
|
|
1208
|
+
|
|
1209
|
+
Usage forms:
|
|
1210
|
+
|
|
1211
|
+
```ts icon="/icons/typescript.svg"
|
|
1212
|
+
// Simple boolean - compile for current platform (uses entrypoint name as output)
|
|
1213
|
+
compile: true
|
|
1214
|
+
|
|
1215
|
+
// Target string - cross-compile (uses entrypoint name as output)
|
|
1216
|
+
compile: "bun-linux-x64"
|
|
1217
|
+
|
|
1218
|
+
// Full options object - specify outfile and other options
|
|
1219
|
+
compile: {
|
|
1220
|
+
target: "bun-linux-x64",
|
|
1221
|
+
outfile: "./myapp",
|
|
1222
|
+
}
|
|
1223
|
+
```
|
|
1224
|
+
|
|
1225
|
+
### Supported targets
|
|
1226
|
+
|
|
1227
|
+
```ts title="Bun.Build.Target" icon="/icons/typescript.svg"
|
|
1228
|
+
type Target =
|
|
1229
|
+
| "bun-darwin-x64"
|
|
1230
|
+
| "bun-darwin-x64-baseline"
|
|
1231
|
+
| "bun-darwin-arm64"
|
|
1232
|
+
| "bun-linux-x64"
|
|
1233
|
+
| "bun-linux-x64-baseline"
|
|
1234
|
+
| "bun-linux-x64-modern"
|
|
1235
|
+
| "bun-linux-arm64"
|
|
1236
|
+
| "bun-linux-x64-musl"
|
|
1237
|
+
| "bun-linux-arm64-musl"
|
|
1238
|
+
| "bun-windows-x64"
|
|
1239
|
+
| "bun-windows-x64-baseline"
|
|
1240
|
+
| "bun-windows-x64-modern";
|
|
1241
|
+
```
|
|
1242
|
+
|
|
1243
|
+
### Complete example
|
|
1244
|
+
|
|
1245
|
+
```ts build.ts icon="/icons/typescript.svg"
|
|
1246
|
+
import type { BunPlugin } from "bun";
|
|
1247
|
+
|
|
1248
|
+
const myPlugin: BunPlugin = {
|
|
1249
|
+
name: "my-plugin",
|
|
1250
|
+
setup(build) {
|
|
1251
|
+
// Plugin implementation
|
|
1252
|
+
},
|
|
1253
|
+
};
|
|
1254
|
+
|
|
1255
|
+
const result = await Bun.build({
|
|
1256
|
+
entrypoints: ["./src/cli.ts"],
|
|
1257
|
+
compile: {
|
|
1258
|
+
target: "bun-linux-x64",
|
|
1259
|
+
outfile: "./dist/mycli",
|
|
1260
|
+
execArgv: ["--smol"],
|
|
1261
|
+
autoloadDotenv: false,
|
|
1262
|
+
autoloadBunfig: false,
|
|
1263
|
+
},
|
|
1264
|
+
minify: true,
|
|
1265
|
+
sourcemap: "linked",
|
|
1266
|
+
bytecode: true,
|
|
1267
|
+
define: {
|
|
1268
|
+
"process.env.NODE_ENV": JSON.stringify("production"),
|
|
1269
|
+
VERSION: JSON.stringify("1.0.0"),
|
|
1270
|
+
},
|
|
1271
|
+
plugins: [myPlugin],
|
|
1272
|
+
});
|
|
1273
|
+
|
|
1274
|
+
if (result.success) {
|
|
1275
|
+
console.log("Build successful:", result.outputs[0].path);
|
|
1276
|
+
}
|
|
1277
|
+
```
|