---
title: "JavaScript Macros in Bun"
description: "Bun Macros run JavaScript functions at bundle time and inline their return values into the bundle as AST nodes."
url: "https://swe.quest/articles/javascript-macros-in-bun-722478e0ec8ce3ca"
canonical: "https://bun.com/blog/bun-macros"
source: "Bun"
published: "2023-05-31"
difficulty: "medium"
kind: "Announcement"
reading_minutes: 6
tags: ["javascript","build-systems","compilers","runtimes"]
---

# JavaScript Macros in Bun

A post on [Bun](https://swe.quest/sources/bun.md), published 2023-05-31, 6 min read, Announcement. Original: [https://bun.com/blog/bun-macros](https://bun.com/blog/bun-macros)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [JavaScript](https://swe.quest/tags/javascript.md) (Languages)
- [Build systems](https://swe.quest/tags/build-systems.md) (Engineering practices)
- [Compilers](https://swe.quest/tags/compilers.md) (Compilers & runtimes)
- [Runtimes & VMs](https://swe.quest/tags/runtimes.md) (Compilers & runtimes)

## Prerequisites

- JavaScript ES modules ([easy posts on JavaScript](https://swe.quest/tags/javascript.md?difficulty=easy))
- JavaScript bundlers ([easy posts on JavaScript](https://swe.quest/tags/javascript.md?difficulty=easy))

## Keywords

Bun, Bun Macros, import attributes, TC39, bun build, HTMLRewriter

## Related posts

- [Bun v1.0.12](https://swe.quest/articles/bun-v1-0-12-7e77f90bbca6622e.md): Bun · 2023-11-16 · Medium · 12 min read · JavaScript, Runtimes & VMs, Testing, Developer tools
  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.
- [Bun v1.2.4](https://swe.quest/articles/bun-v1-2-4-fb8da361a88490ab.md): Bun · 2025-02-26 · Medium · 7 min read · JavaScript, Runtimes & VMs, Build systems, Performance engineering
  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.
- [Bun v1.2.17](https://swe.quest/articles/bun-v1-2-17-6bfcdfe2dc03cf80.md): Bun · 2025-06-21 · Medium · 10 min read · JavaScript, Runtimes & VMs, Build systems, Performance engineering
  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 v0.1.11](https://swe.quest/articles/bun-v0-1-11-608f71de932fd530.md): Bun · 2022-09-07 · Medium · 4 min read · JavaScript, Runtimes & VMs
  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.
- [Bun v1.1.30](https://swe.quest/articles/bun-v1-1-30-4f8e7c9af8830c50.md): Bun · 2024-10-08 · Medium · 10 min read · JavaScript, Build systems, CSS, Runtimes & VMs, Developer tools
  Bun v1.1.30 adds an experimental CSS parser and bundler, a LightningCSS port rewritten from Rust to Zig.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
