Files
bun.sh/docs/bundler/executables.md
dave caruso 57a06745a4 Progress for Next.js (#4468)
* L

* ipc

* asdfghjkl

* dfghjk

* it works!

* types

* patches for next.js

* sdfghj

* wsdfgn,./

* this

* yolo

* okay loser

* asdfghjk

* add some more APIs

* MESS

* sdfghjkl

* remove native events from streams

* stuff

* remove lazy(primordials) test

* debugging

* okay

* less fake extensions object

* fix `Buffer.toString()` args logic

* fix deserialize

* make tests work

* add test for `Buffer.toString` args

* Update server.zig

* remove test

* update test

* Update spawn-streaming-stdin.test.ts

* fix linux build

* Update fs.test.ts

* cli message improvements

* dfshaj

* Fix fs.watch bug maybe?

* remove

---------

Co-authored-by: Dylan Conway <dylan.conway567@gmail.com>
2023-09-07 04:58:44 -07:00

1.5 KiB

Bun's bundler implements a --compile flag for generating a standalone binary from a TypeScript or JavaScript file.

{% codetabs %}

$ bun build ./cli.ts --compile --outfile mycli
console.log("Hello world!");

{% /codetabs %}

This bundles cli.ts into an executable that can be executed directly:

$ ./mycli
Hello world!

All imported files and packages are bundled into the executable, along with a copy of the Bun runtime. All built-in Bun and Node.js APIs are supported.

{% callout %}

Note — Currently, the --compile flag can only accept a single entrypoint at a time and does not support the following flags:

  • --outdir — use outfile instead.
  • --external
  • --splitting
  • --public-path

{% /callout %}

Embedding files

Standalone executables support embedding files.

To embed files into an executable with bun build --compile, import the file in your code

// this becomes an internal file path
import icon from "./icon.png";

import { file } from "bun";

export default {
  fetch(req) {
    return new Response(file(icon));
  },
};

You may need to specify a --loader for it to be treated as a "file" loader (so you get back a file path).

Embedded files can be read using Bun.file's functions or the Node.js fs.readFile function (in "node:fs").

Minification

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.