Skip to content

Bun ​

Run, test, and bundle SAFE-TS projects natively on Bun with bun run, bun test, and Bun.build.

In short

  • Preload in both sections: configure @evobug_com/safe-ts-transform/bun at the top of bunfig.toml and again under [test].
  • Bundling: CLI bun build doesn't accept plugins—call Bun.build({ plugins: [safeTsBun()] }) from a build script instead.
  • Different loader from Node: --import @evobug_com/safe-ts-transform/register is Node-only and does not run in Bun.
  • Zero-transform helpers: match, is, and variant imported directly work everywhere without a plugin.

Setup ​

sh
bun add -d @evobug_com/safe-ts-transform @evobug_com/safe-ts-prelude
bun add @evobug_com/safe-ts-std
toml
# bunfig.toml
preload = ["@evobug_com/safe-ts-transform/bun"]

[test]
preload = ["@evobug_com/safe-ts-transform/bun"]

Both lines are needed: bun run reads the first, bun test reads only its own section. For a single command, bun --preload @evobug_com/safe-ts-transform/bun src/main.ts does the same.

The preload registers the Bun plugin with Bun.plugin. It makes the same rewrite as the Node loader and the Vite plugin: q(...), match(...), parseJson<T>, validate<T>, fromFields<T>, arbitrary<T>(), loops, @inline and prelude names, in .ts, .tsx, .mts and .cts files outside node_modules. It also behaves like the Node loader:

  • Dev checks are on unless NODE_ENV=production; SAFE_TS_DEV_CHECKS=1 or =0 overrides that.
  • Each named type's validator is generated once, in a module every file using it imports (One validator per type); SAFE_TS_SHARED_VALIDATORS=0 gives every file its own copies.
  • Rewritten files are kept in the same disk cache, with the same SAFE_TS_CACHE=0 and SAFE_TS_CACHE_DIR (the cache).
  • It doesn't run the rules: safe-ts check and the oxlint plugin do that.

bun --watch and bun --hot restart when a file the program loads changes. A file imported only for its types (import type { User } from "./types.ts") isn't loaded, so editing a type behind parseJson<User> doesn't restart the program: save a file that is loaded, or restart it.

bun build ​

The bun build command takes no plugins, and the preload doesn't apply to it: a bundle built that way still contains q(...) and prelude names, and fails when it runs. Build with a script instead:

ts
// build.ts: run with `bun build.ts`
import { safeTsBun } from "@evobug_com/safe-ts-transform";

const result = await Bun.build({
  entrypoints: ["src/main.ts"],
  outdir: "dist",
  target: "bun",
  plugins: [safeTsBun()],
});
if (!result.success) process.exit(1);

safeTsBun() takes the Vite plugin's options, with the defaults of vite build:

OptionDefault in Bun.build
check"warn"run the rules on every file it rewrites: "warn" prints the problems, "error" fails the build, false turns it off
devChecksfalseadd dev checks
sharedValidatorstrueone validator module per named type, or a copy per file (false)

Passed to Bun.plugin(safeTsBun(options)) in a preload of your own, the same options apply at runtime; there they default to what the preload above does (no rules, dev checks unless NODE_ENV=production).

The Node loader under Bun ​

--import @evobug_com/safe-ts-transform/register is the Node loader. It needs module.registerHooks, which Bun doesn't have, so under Bun (bun --import, bun --preload) it stops with a message that names the Bun preload. loadTransformed(...) in a Vite config works in both: under bunx --bun vite it registers the Bun plugin instead.

Without the plugin: safe-ts emit ​

safe-ts emit out src/main.ts writes the program, rewritten, as plain TypeScript, and bun out/src/main.ts runs it as it is (Running without the loader). That is the fallback where you can't preload the plugin, for example a build that must not depend on @evobug_com/safe-ts-transform. bun build out/src/main.ts bundles it.

What needs no transform ​

match, is and variant are ordinary functions: imported by name, they run in plain bun, without the plugin (the transform only makes match faster, as a switch). So are constructors, err, isErr, mapErr, unwrapOr, unsafe and the other helpers of @evobug_com/safe-ts-std.

✓ Accepted0 diagnostics
ts
import { is, match, variant, constructors, type Enum } from "@evobug_com/safe-ts-std/enum";

type Job = Enum<{ Queued: { id: string }; Done: { id: string; ms: number } }>;
const Job = constructors<Job>();

const job: Job = Job.Done({ id: "b", ms: 12 });
console.log(match(job, { Queued: ({ id }) => `${id} queued`, Done: ({ id, ms }) => `${id} took ${ms} ms` }));
console.log(is(job, Job.Done), variant(job, Job.Done)?.ms);
ts
import { is, match, variant, constructors, type Enum } from "@evobug_com/safe-ts-std/enum";

type Job = Enum<{ Queued: { id: string }; Done: { id: string; ms: number } }>;
const Job = constructors<Job>();

const job: Job = ({ tag: "Done", id: "b", ms: 12 } satisfies Job as Job);
console.log(match(job, { Queued: ({ id }) => `${id} queued`, Done: ({ id, ms }) => `${id} took ${ms} ms` }));
console.log((job?.tag === "Done"), (job?.tag === "Done" ? job : undefined)?.ms);

What does need it: q(...) (it throws "q() was not compiled"), parseJson<T>, validate<T>, fromFields<T>, arbitrary<T>() and variants<T>(), which need a type turned into code, and every prelude name, whose import the transform adds (ReferenceError: match is not defined without it).

A Bun project inside another project ​

A folder with its own package.json and tsconfig.json (a CLI next to a Vue app, say) is a project of its own, and needs its own setup:

  • The std library, bun add @evobug_com/safe-ts-std in that folder. The rewritten files import it, and those imports resolve from the folder like any other. A copy in a parent's node_modules is found too, but then the folder's package.json doesn't list it, and a build or deploy of the folder alone fails.
  • The prelude entry in the folder's own tsconfig.json: "types": ["bun", "@evobug_com/safe-ts-prelude"]. A types list replaces the parent's, and the transform reads only the nearest tsconfig.json above each file (not one it extends). Without the entry there, prelude names get no imports: ReferenceError: q is not defined.
  • A bunfig.toml with the preload: Bun reads the one in the directory it runs in.

.vue files aren't for the Bun plugin, since Bun doesn't compile Vue components. Build the app with Vite, where safeTs() rewrites them; Vite running under Bun (bunx --bun vite build) runs it the same way.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra