tyfigo

Bun

Preload tyfigo/bun for bun run, bun test and Bun.build.

Bun imports .toml, .yaml and a few other formats natively, without layering or placeholders. Preloading tyfigo/bun registers a Bun plugin that takes over *.config.* files and the files of a .config directory, and leaves every other .yaml or .toml file to Bun:

bunfig.toml
preload = ["tyfigo/bun"]

[test]
preload = ["tyfigo/bun"]
config/app.config.toml
name = "tyfigo-bun-example"
log_level = "debug"

[server]
port = "${PORT:-3000}"
idle_timeout = 10

[redis]
url = "${REDIS_URL}"
ttl = 60
src/main.ts
import config from '../config/app.config.toml';

const listen: { port: number; idleTimeout: number } = {
	port: config.server.port,
	idleTimeout: config.server.idle_timeout,
};

For types, use "types": ["bun"] and "skipLibCheck": true alongside contentMappers in tsconfig.json, and type-check with tsc --noEmit --runExternalCode.

Bun.build

Pass the plugin to Bun.build:

build.ts
import { tyfigo } from 'tyfigo/bun';

await Bun.build({
	entrypoints: ['src/main.ts'],
	outdir: 'dist',
	target: 'bun',
	plugins: [tyfigo()],
});

A browser target inlines the base and environment files (Browser builds). Other targets inline the layer files of every environment, pick them by NODE_ENV and read .env files and environment variables when the bundle runs (Server bundles). tyfigo({ environment: 'production' }) fixes the environment.

Bun loads .env files into process.env on its own; tyfigo reads them itself either way and never writes process.env. See the bun example.

On this page