# Build-time code with Bun macros

A **macro** is a function that Bun runs while it bundles, and whose return value Bun writes into the code in place of the call. The function's source never reaches the bundle. Bun owns the feature; Hamur adds nothing to it, and `hamur build` runs it in both bundles.

A macro is an ordinary module that exports a function:

````ts [macros-build-time.ts]
export function buildTime(): string {
  return new Date().toISOString()
}
````

You mark the import, not the function, with an import attribute:

````ts [macros-page.ts]
import { buildTime } from "./macros-build-time.ts" with { type: "macro" }

export default function Home(): string {
  return `<main><h1>Macros</h1><p id="built">${buildTime()}</p></main>`
}
````

The same macro works in a server route:

````ts [macros-route.ts]
import { defineHandler } from "hamur/server"
import { buildTime } from "./macros-build-time.ts" with { type: "macro" }

export const getApiBuildTime = defineHandler({
  route: "/api/build-time",
  method: "GET",
  raw: true,
  handler() {
    return { builtAt: buildTime() }
  },
})
````

As every handler, the route is served once `server/index.ts` exports it ([Server](/server)).

After `hamur build`, the client chunk of the page holds the literal, such as `"2026-11-21T09:30:12.481Z"`, and neither bundle imports the macro file or holds its body. `tests/macros.test.ts` builds the fixture `tests/fixtures/macros-html` (the three files above, with the macro under `macros/` and `server/index.ts` exporting the route), and checks that the client chunks and the server bundle each contain an ISO date literal, no `macros/build-time` path and no marker string that only the macro body holds, and that the page and the route answer with such a literal.

## Rules from Bun

- A macro import has `with { type: "macro" }` (`assert { type: "macro" }` also works). Without the attribute the function is ordinary code and runs at run time.
- The result must be something Bun can write as code: any JSON-compatible value, a `Promise` of one (Bun awaits it), a `Response` or a `Blob` (serialized by its `Content-Type`). A function or a class instance is not.
- An argument must be known at build time: a literal, a constant, or the result of another macro. `buildTime(userInput)` is refused.
- Bun does not let a package under `node_modules` call a macro, for security. Your own code can import a macro from a package and call it.
- With `minify`, Bun removes the code a macro result makes unreachable, such as the branch after `if (flag())` when `flag()` returns `false`.

## Do not read secrets into client code

A macro runs in your build with every privilege your build has: it can read the environment, the disk, a database, the network. Whatever it returns is a literal in the file the build writes. A macro that returns `process.env.API_KEY` puts the key into every file that calls it, and a call in a page puts it into `.output/public/_hamur/`, which anyone can download. Hamur has no check on what a macro returns, and the check that stops client code from reading `process.env` does not see a literal that a macro produced.

- Call a macro that reads anything private only from server code (`server/` or a `.server.ts` file), and expect the value to be in the server bundle in plain text.
- In a page or component, a macro may return what you would be happy to see in a view-source: a build time, a commit hash, a version, the text of a Markdown file.

## The client and the server are two builds

Hamur bundles the client and the server separately, so Bun runs the macro once for each. A macro whose value changes between runs, like `buildTime()` above, gives the server HTML one value and the browser bundle another, and a UI library that hydrates the page then sees text that differs from the markup. Return something both builds agree on, such as a commit hash, a package version, or a date without the time, and render a changing value in a handler (the route above is fine) rather than in a hydrated page.

## What is not covered

The test builds with `hamur build` and the bun preset. Macros under `hamur dev`, where Bun's dev bundler builds the client, are not tested, and nothing in Hamur tells the dev server that a macro's file, or a file it reads, changed. For a value that comes from a file, `import.meta.glob` with `query: "?raw"` and the [data-file imports](/directory-structure#importing-data-files) are build-time reads that `hamur dev` does follow.
