You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Summary
- Expanded documentation for embedding files in single-file executables
with `with { type: "file" }`
- Added clear explanation of how the import attribute works and path
transformation at build time
- Added examples for reading embedded files with both `Bun.file()` and
Node.js `fs` APIs
- Added practical examples: JSON configs, HTTP static assets, templates,
binary files (WASM, fonts)
- Improved `Bun.embeddedFiles` section with a dynamic asset server
example
## Test plan
- [x] Verified all code examples compile and run correctly with `bun
build --compile`
- [x] Tested `Bun.file()` reads embedded files correctly
- [x] Tested `node:fs` APIs (`readFileSync`, `promises.readFile`,
`stat`) work with embedded files
- [x] Tested `Bun.embeddedFiles` returns correct blob array
- [x] Tested `--asset-naming` flag removes content hashes
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Bot <[email protected]>
Co-authored-by: Claude <[email protected]>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
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.
417
417
418
-
To embed files into an executable with `bun build --compile`, import the file in your code.
418
+
### How it works
419
+
420
+
Use the `with { type: "file" }`[import attribute](https://github.com/tc39/proposal-import-attributes) to embed a file:
421
+
422
+
```ts index.ts icon="/icons/typescript.svg"
423
+
importiconfrom"./icon.png"with { type: "file" };
424
+
425
+
console.log(icon);
426
+
// During development: "./icon.png"
427
+
// After compilation: "$bunfs/icon-a1b2c3d4.png" (internal path)
428
+
```
429
+
430
+
The import returns a **path string** that points to the embedded file. At build time, Bun:
431
+
432
+
1. Reads the file contents
433
+
2. Embeds the data into the executable
434
+
3. Replaces the import with an internal path (prefixed with `$bunfs/`)
435
+
436
+
You can then read this embedded file using `Bun.file()` or Node.js `fs` APIs.
437
+
438
+
### Reading embedded files with Bun.file()
439
+
440
+
`Bun.file()` is the recommended way to read embedded files:
0 commit comments