Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Bun 6 min read Announcement mediumAdept

JavaScript Macros in Bun

Bun Macros run JavaScript functions at bundle time and inline their return values into the bundle as AST nodes. They are invoked via imports with the `{ type: 'macro' }` import attribute. Macros execute synchronously during the transpiler's visiting phase, run in parallel across worker threads, await returned Promises, and are followed by dead code elimination. Return values must be serializable, with special handling for Response, Blob and TypedArray, and arguments must be statically known. For security, macros can't be invoked from node_modules and can be disabled with --no-macros.

Related posts

  • Bun v1.0.12

    Bun mediumAdept

    Bun v1.0.12 fixes 24 bugs and adds `bun -e` for evaluating inline scripts (with TypeScript, JSX and top-level await), `--env-file` for choosing which .env files to load, `server.url`, `import.meta.env`, and `expect.unreachable()` in bun:test. Macros can now import builtin modules like node:fs and node:child_process at bundle time, and `mock.module` now handles default exports and re-exports. Two fixes include root-cause explanations: a bun:sqlite `.get()` hang came from a wrong JavaScriptCore JSType tag that misled the DFG JIT, and a `require()` crash came from source code being garbage collected before an exception was thrown. As a result, Bun's debug builds now run JavaScriptCore with assertions enabled.

  • Bun v1.2.4

    Bun mediumAdept

    Bun v1.2.4 makes Bun.build up to 60% faster on macOS by using a dedicated I/O threadpool for file operations on macOS and Windows machines with more than 3 logical CPUs, which reduces filesystem contention. It also fixes Mach-O handling so single-file executables built with `bun build --compile` can be codesigned. The release fixes v1.2.3 regressions affecting Hono default exports, Buffer.indexOf with byte offsets, and invalid JSON imports, along with Node.js compatibility bugs in net.Socket errors, stdin pause hangs and process.argv in eval mode. Smaller changes include net.SocketAddress, PostgreSQL binary type handling, a faster native Array.prototype.includes from WebKit, and SQLite 3.49.1.

  • Bun v1.2.17

    Bun mediumAdept

    Bun v1.2.17 adds ahead-of-time bundling for HTML imports in server code, so `bun build --target=bun` or `--compile` can produce a full-stack build or a single self-contained executable with no runtime bundling. Bun Shell no longer uses the native call stack, which prevents stack overflows in deeply nested scripts. Removing a pointer indirection in Strong references cuts per-timer memory for `setTimeout` and `setImmediate` by 8-15%. Other changes include `columnTypes`/`declaredTypes` in `bun:sqlite`, zstd in `node:zlib`, `--unhandled-rejections` modes, `child_process.fork` `execArgv`, and a JavaScriptCore upgrade bringing JIT string and array optimizations, a zero-copy JSON stringifier and WebAssembly interpreter hardening.

  • Bun v0.1.11

    Bun mediumAdept

    Bun v0.1.11 adds a runtime plugin API, loosely modeled on esbuild's, that lets code import file types Bun has no built-in loader for (such as .svelte, .vue, .yaml, .mdx) and dynamically generate ESM/CJS modules, with examples for MDX and YAML loaders. The release also expands Node compatibility with a native os module, EventEmitter, StringDecoder, Buffer methods and N-API fixes. On macOS, the event loop moves to a more reliable polling mechanism that cuts setTimeout CPU usage from 90% to 33% and fixes fetch race-condition hangs, and React SSR runs about 2x faster.

  • Bun v1.1.30

    Bun mediumAdept

    Bun v1.1.30 adds an experimental CSS parser and bundler, a LightningCSS port rewritten from Rust to Zig. It flattens @import chains and CSS imported from JS/TS module graphs into one file per entrypoint. `bun build --bytecode` precompiles to JavaScriptCore bytecode for about 2x faster startup, at the cost of files up to 8x larger, with source kept as a fallback for async functions, generators and eval. Other changes include `bun publish` as a drop-in for `npm publish` with workspace version rewriting, CommonJS output, BoringSSL-backed RSA functions that run 30x faster, zombie-process cleanup on `bun test` timeouts, and a prototype-pollution mitigation that makes native APIs ignore Object.prototype.

The quest for software engineering knowledge.

Look:
Layout: