bun-types 1.3.4 → 1.3.5-canary.20251218T140753

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.
@@ -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
- <CodeGroup>
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
- ```bash icon="terminal" terminal
41
- bun build --compile --target=bun-linux-x64 ./index.ts --outfile myapp
42
-
43
- # To support CPUs from before 2013, use the baseline version (nehalem)
44
- bun build --compile --target=bun-linux-x64-baseline ./index.ts --outfile myapp
45
-
46
- # To explicitly only support CPUs from 2013 and later, use the modern version (haswell)
47
- # modern is faster, but baseline is more compatible.
48
- bun build --compile --target=bun-linux-x64-modern ./index.ts --outfile myapp
49
- ```
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
+ });
74
+
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
+ });
83
+
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
- ```bash icon="terminal" terminal
54
- # Note: the default architecture is x64 if no architecture is specified.
55
- bun build --compile --target=bun-linux-arm64 ./index.ts --outfile myapp
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
- ```bash icon="terminal" terminal
61
- bun build --compile --target=bun-windows-x64 ./path/to/my/app.ts --outfile myapp
62
-
63
- # To support CPUs from before 2013, use the baseline version (nehalem)
64
- bun build --compile --target=bun-windows-x64-baseline ./path/to/my/app.ts --outfile myapp
65
-
66
- # To explicitly only support CPUs from 2013 and later, use the modern version (haswell)
67
- bun build --compile --target=bun-windows-x64-modern ./path/to/my/app.ts --outfile myapp
68
-
69
- # note: if no .exe extension is provided, Bun will automatically add it for Windows executables
70
- ```
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
+ });
146
+
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
+ ```
156
+
157
+ </Tab>
158
+ </Tabs>
71
159
 
72
160
  To build for macOS arm64:
73
161
 
74
- ```bash icon="terminal" terminal
75
- bun build --compile --target=bun-darwin-arm64 ./path/to/my/app.ts --outfile myapp
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
- ```bash icon="terminal" terminal
81
- bun build --compile --target=bun-darwin-x64 ./path/to/my/app.ts --outfile myapp
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
- ```bash icon="terminal" terminal
114
- bun build --compile --define BUILD_VERSION='"1.2.3"' --define BUILD_TIME='"2024-01-15T10:30:00Z"' src/cli.ts --outfile mycli
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
- ```bash icon="terminal" terminal
137
- bun build --compile --minify --sourcemap ./path/to/my/app.ts --outfile myapp
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
- ```bash icon="terminal" terminal
145
- bun build --compile --minify --sourcemap --bytecode ./path/to/my/app.ts --outfile myapp
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
- ```bash icon="terminal" terminal
176
- bun build --compile --compile-exec-argv="--smol --user-agent=MyBot" ./app.ts --outfile myapp
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
@@ -183,32 +367,69 @@ console.log(process.execArgv); // ["--smol", "--user-agent=MyBot"]
183
367
 
184
368
  ---
185
369
 
186
- ## Disabling automatic config loading
370
+ ## Automatic config loading
371
+
372
+ Standalone executables can automatically load configuration files from the directory where they are run. By default:
373
+
374
+ - **`tsconfig.json`** and **`package.json`** loading is **disabled** — these are typically only needed at development time, and the bundler already uses them when compiling
375
+ - **`.env`** and **`bunfig.toml`** loading is **enabled** — these often contain runtime configuration that may vary per deployment
376
+
377
+ <Note>
378
+ In a future version of Bun, `.env` and `bunfig.toml` may also be disabled by default for more deterministic behavior.
379
+ </Note>
380
+
381
+ ### Enabling config loading at runtime
187
382
 
188
- By default, standalone executables look for `.env` and `bunfig.toml` files in the directory where the executable is run. You can disable this behavior at build time for deterministic execution regardless of the user's working directory.
383
+ If your executable needs to read `tsconfig.json` or `package.json` at runtime, you can opt in with the new CLI flags:
189
384
 
190
385
  ```bash icon="terminal" terminal
191
- # Disable .env loading
192
- bun build --compile --no-compile-autoload-dotenv ./app.ts --outfile myapp
386
+ # Enable runtime loading of tsconfig.json
387
+ bun build --compile --compile-autoload-tsconfig ./app.ts --outfile myapp
193
388
 
194
- # Disable bunfig.toml loading
195
- bun build --compile --no-compile-autoload-bunfig ./app.ts --outfile myapp
389
+ # Enable runtime loading of package.json
390
+ bun build --compile --compile-autoload-package-json ./app.ts --outfile myapp
196
391
 
197
- # Disable both
198
- bun build --compile --no-compile-autoload-dotenv --no-compile-autoload-bunfig ./app.ts --outfile myapp
392
+ # Enable both
393
+ bun build --compile --compile-autoload-tsconfig --compile-autoload-package-json ./app.ts --outfile myapp
199
394
  ```
200
395
 
201
- You can also configure this via the JavaScript API:
396
+ ### Disabling config loading at runtime
202
397
 
203
- ```ts
204
- await Bun.build({
205
- entrypoints: ["./app.ts"],
206
- compile: {
207
- autoloadDotenv: false, // Disable .env loading
208
- autoloadBunfig: false, // Disable bunfig.toml loading
209
- },
210
- });
211
- ```
398
+ To disable `.env` or `bunfig.toml` loading for deterministic execution:
399
+
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
+ ```
430
+
431
+ </Tab>
432
+ </Tabs>
212
433
 
213
434
  ---
214
435
 
@@ -307,9 +528,23 @@ body {
307
528
 
308
529
  To build this into a single executable:
309
530
 
310
- ```bash terminal icon="terminal"
311
- bun build --compile ./server.ts --outfile myapp
312
- ```
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>
313
548
 
314
549
  This creates a self-contained binary that includes:
315
550
 
@@ -332,11 +567,25 @@ For more details on building full-stack applications with Bun, see the [full-sta
332
567
 
333
568
  ## Worker
334
569
 
335
- To use workers in a standalone executable, add the worker's entrypoint to the CLI arguments:
336
-
337
- ```bash terminal icon="terminal"
338
- bun build --compile ./index.ts ./my-worker.ts --outfile myapp
339
- ```
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>
340
589
 
341
590
  Then, reference the worker in your code:
342
591
 
@@ -380,34 +629,141 @@ cd /home/me/Desktop
380
629
 
381
630
  ## Embed assets & files
382
631
 
383
- Standalone executables support embedding files.
632
+ Standalone executables support embedding files directly into the binary. This lets you ship a single executable that contains images, JSON configs, templates, or any other assets your application needs.
633
+
634
+ ### How it works
635
+
636
+ Use the `with { type: "file" }` [import attribute](https://github.com/tc39/proposal-import-attributes) to embed a file:
637
+
638
+ ```ts index.ts icon="/icons/typescript.svg"
639
+ import icon from "./icon.png" with { type: "file" };
640
+
641
+ console.log(icon);
642
+ // During development: "./icon.png"
643
+ // After compilation: "$bunfs/icon-a1b2c3d4.png" (internal path)
644
+ ```
645
+
646
+ The import returns a **path string** that points to the embedded file. At build time, Bun:
647
+
648
+ 1. Reads the file contents
649
+ 2. Embeds the data into the executable
650
+ 3. Replaces the import with an internal path (prefixed with `$bunfs/`)
384
651
 
385
- To embed files into an executable with `bun build --compile`, import the file in your code.
652
+ You can then read this embedded file using `Bun.file()` or Node.js `fs` APIs.
653
+
654
+ ### Reading embedded files with Bun.file()
655
+
656
+ `Bun.file()` is the recommended way to read embedded files:
386
657
 
387
658
  ```ts index.ts icon="/icons/typescript.svg"
388
- // this becomes an internal file path
389
659
  import icon from "./icon.png" with { type: "file" };
390
660
  import { file } from "bun";
391
661
 
662
+ // Get file contents as different types
663
+ const bytes = await file(icon).arrayBuffer(); // ArrayBuffer
664
+ const text = await file(icon).text(); // string (for text files)
665
+ const blob = file(icon); // Blob
666
+
667
+ // Stream the file in a response
392
668
  export default {
393
669
  fetch(req) {
394
- // Embedded files can be streamed from Response objects
395
- return new Response(file(icon));
670
+ return new Response(file(icon), {
671
+ headers: { "Content-Type": "image/png" },
672
+ });
396
673
  },
397
674
  };
398
675
  ```
399
676
 
400
- Embedded files can be read using `Bun.file`'s functions or the Node.js `fs.readFile` function (in `"node:fs"`).
677
+ ### Reading embedded files with Node.js fs
401
678
 
402
- For example, to read the contents of the embedded file:
679
+ Embedded files work seamlessly with Node.js file system APIs:
403
680
 
404
681
  ```ts index.ts icon="/icons/typescript.svg"
405
682
  import icon from "./icon.png" with { type: "file" };
683
+ import config from "./config.json" with { type: "file" };
684
+ import { readFileSync, promises as fs } from "node:fs";
685
+
686
+ // Synchronous read
687
+ const iconBuffer = readFileSync(icon);
688
+
689
+ // Async read
690
+ const configData = await fs.readFile(config, "utf-8");
691
+ const parsed = JSON.parse(configData);
692
+
693
+ // Check file stats
694
+ const stats = await fs.stat(icon);
695
+ console.log(`Icon size: ${stats.size} bytes`);
696
+ ```
697
+
698
+ ### Practical examples
699
+
700
+ #### Embedding a JSON config file
701
+
702
+ ```ts index.ts icon="/icons/typescript.svg"
703
+ import configPath from "./default-config.json" with { type: "file" };
406
704
  import { file } from "bun";
407
705
 
408
- const bytes = await file(icon).arrayBuffer();
409
- // await fs.promises.readFile(icon)
410
- // fs.readFileSync(icon)
706
+ // Load the embedded default configuration
707
+ const defaultConfig = await file(configPath).json();
708
+
709
+ // Merge with user config if it exists
710
+ const userConfig = await file("./user-config.json")
711
+ .json()
712
+ .catch(() => ({}));
713
+ const config = { ...defaultConfig, ...userConfig };
714
+ ```
715
+
716
+ #### Serving static assets in an HTTP server
717
+
718
+ Use `static` routes in `Bun.serve()` for efficient static file serving:
719
+
720
+ ```ts server.ts icon="/icons/typescript.svg"
721
+ import favicon from "./favicon.ico" with { type: "file" };
722
+ import logo from "./logo.png" with { type: "file" };
723
+ import styles from "./styles.css" with { type: "file" };
724
+ import { file, serve } from "bun";
725
+
726
+ serve({
727
+ static: {
728
+ "/favicon.ico": file(favicon),
729
+ "/logo.png": file(logo),
730
+ "/styles.css": file(styles),
731
+ },
732
+ fetch(req) {
733
+ return new Response("Not found", { status: 404 });
734
+ },
735
+ });
736
+ ```
737
+
738
+ Bun automatically handles Content-Type headers and caching for static routes.
739
+
740
+ #### Embedding templates
741
+
742
+ ```ts index.ts icon="/icons/typescript.svg"
743
+ import templatePath from "./email-template.html" with { type: "file" };
744
+ import { file } from "bun";
745
+
746
+ async function sendWelcomeEmail(user: { name: string; email: string }) {
747
+ const template = await file(templatePath).text();
748
+ const html = template.replace("{{name}}", user.name).replace("{{email}}", user.email);
749
+
750
+ // Send email with the rendered template...
751
+ }
752
+ ```
753
+
754
+ #### Embedding binary files
755
+
756
+ ```ts index.ts icon="/icons/typescript.svg"
757
+ import wasmPath from "./processor.wasm" with { type: "file" };
758
+ import fontPath from "./font.ttf" with { type: "file" };
759
+ import { file } from "bun";
760
+
761
+ // Load a WebAssembly module
762
+ const wasmBytes = await file(wasmPath).arrayBuffer();
763
+ const wasmModule = await WebAssembly.instantiate(wasmBytes);
764
+
765
+ // Read binary font data
766
+ const fontData = await file(fontPath).bytes();
411
767
  ```
412
768
 
413
769
  ### Embed SQLite databases
@@ -450,11 +806,32 @@ Unfortunately, if you're using `@mapbox/node-pre-gyp` or other similar tools, yo
450
806
 
451
807
  ### Embed directories
452
808
 
453
- To embed a directory with `bun build --compile`, use a shell glob in your `bun build` command:
454
-
455
- ```bash terminal icon="terminal"
456
- bun build --compile ./index.ts ./public/**/*.png
457
- ```
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
+ ```
832
+
833
+ </Tab>
834
+ </Tabs>
458
835
 
459
836
  Then, you can reference the files in your code:
460
837
 
@@ -474,47 +851,174 @@ This is honestly a workaround, and we expect to improve this in the future with
474
851
 
475
852
  ### Listing embedded files
476
853
 
477
- To get a list of all embedded files, use `Bun.embeddedFiles`:
854
+ `Bun.embeddedFiles` gives you access to all embedded files as `Blob` objects:
478
855
 
479
856
  ```ts index.ts icon="/icons/typescript.svg"
480
857
  import "./icon.png" with { type: "file" };
858
+ import "./data.json" with { type: "file" };
859
+ import "./template.html" with { type: "file" };
481
860
  import { embeddedFiles } from "bun";
482
861
 
483
- console.log(embeddedFiles[0].name); // `icon-${hash}.png`
862
+ // List all embedded files
863
+ for (const blob of embeddedFiles) {
864
+ console.log(`${blob.name} - ${blob.size} bytes`);
865
+ }
866
+ // Output:
867
+ // icon-a1b2c3d4.png - 4096 bytes
868
+ // data-e5f6g7h8.json - 256 bytes
869
+ // template-i9j0k1l2.html - 1024 bytes
484
870
  ```
485
871
 
486
- `Bun.embeddedFiles` returns an array of `Blob` objects which you can use to get the size, contents, and other properties of the files.
872
+ Each item in `Bun.embeddedFiles` is a `Blob` with a `name` property:
487
873
 
488
874
  ```ts
489
- embeddedFiles: Blob[]
875
+ embeddedFiles: ReadonlyArray<Blob>;
490
876
  ```
491
877
 
492
- The list of embedded files excludes bundled source code like `.ts` and `.js` files.
878
+ This is useful for dynamically serving all embedded assets using `static` routes:
879
+
880
+ ```ts server.ts icon="/icons/typescript.svg"
881
+ import "./public/favicon.ico" with { type: "file" };
882
+ import "./public/logo.png" with { type: "file" };
883
+ import "./public/styles.css" with { type: "file" };
884
+ import { embeddedFiles, serve } from "bun";
885
+
886
+ // Build static routes from all embedded files
887
+ const staticRoutes: Record<string, Blob> = {};
888
+ for (const blob of embeddedFiles) {
889
+ // Remove hash from filename: "icon-a1b2c3d4.png" -> "icon.png"
890
+ const name = blob.name.replace(/-[a-f0-9]+\./, ".");
891
+ staticRoutes[`/${name}`] = blob;
892
+ }
893
+
894
+ serve({
895
+ static: staticRoutes,
896
+ fetch(req) {
897
+ return new Response("Not found", { status: 404 });
898
+ },
899
+ });
900
+ ```
901
+
902
+ <Note>
903
+ `Bun.embeddedFiles` excludes bundled source code (`.ts`, `.js`, etc.) to help protect your application's source.
904
+ </Note>
493
905
 
494
906
  #### Content hash
495
907
 
496
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:
497
909
 
498
- To disable the content hash, pass `--asset-naming` to `bun build --compile` like this:
499
-
500
- ```bash terminal icon="terminal"
501
- bun build --compile --asset-naming="[name].[ext]" ./index.ts
502
- ```
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>
503
932
 
504
933
  ---
505
934
 
506
935
  ## Minification
507
936
 
508
- To trim down the size of the executable a little, pass `--minify` to `bun build --compile`. 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.
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.
509
973
 
510
974
  ---
511
975
 
512
976
  ## Windows-specific flags
513
977
 
514
- When compiling a standalone executable on Windows, there are two platform-specific options that can be used to customize metadata on the generated `.exe` file:
515
-
516
- - `--windows-icon=path/to/icon.ico` to customize the executable file icon.
517
- - `--windows-hide-console` to disable the background terminal, which can be used for applications that do not need a TTY.
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:
1014
+
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
518
1022
 
519
1023
  <Warning>These flags currently cannot be used when cross-compiling because they depend on Windows APIs.</Warning>
520
1024
 
@@ -571,9 +1075,23 @@ codesign -vvv --verify ./myapp
571
1075
 
572
1076
  Standalone executables support code splitting. Use `--compile` with `--splitting` to create an executable that loads code-split chunks at runtime.
573
1077
 
574
- ```bash
575
- bun build --compile --splitting ./src/entry.ts --outdir ./build
576
- ```
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>
577
1095
 
578
1096
  <CodeGroup>
579
1097
 
@@ -602,6 +1120,50 @@ Lazy module loaded
602
1120
 
603
1121
  ---
604
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
+
605
1167
  ## Unsupported CLI arguments
606
1168
 
607
1169
  Currently, the `--compile` flag can only accept a single entrypoint at a time and does not support the following flags:
@@ -610,3 +1172,106 @@ Currently, the `--compile` flag can only accept a single entrypoint at a time an
610
1172
  - `--public-path`
611
1173
  - `--target=node` or `--target=browser`
612
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
+ ```