@lukeed05: In celebration of @rolldown_rs 1.0 Announcing `comptime` — a Zig-inspired build-time evaluation primitive, exposed as V…
Summary
Luke Edwards announces `comptime`, a Zig-inspired build-time evaluation primitive for Vite and Rolldown, allowing code execution during the build process to replace call sites with evaluated values.
View Cached Full Text
Cached at: 05/09/26, 04:16 PM
In celebration of @rolldown_rs 1.0 🎉
Announcing comptime — a Zig-inspired build-time evaluation primitive, exposed as Vite and Rolldown plugins
This allows you run code at build time, replacing the call site(s) with the evaluated output value.
https://t.co/myKeJMvDBP https://t.co/VhvUTegO6P
lukeed/comptime
Source: https://github.com/lukeed/comptime
comptime
A Zig-inspired build-time evaluation primitive, exposed as Vite and Rolldown plugins.
import { comptime } from "comptime";
import { fibonacci } from "./math";
export const value = comptime(() => fibonacci(10));
With the plugin enabled, the call is evaluated during the build and replaced with a serialized expression:
export const value = 55;
If the plugin is not enabled, the runtime helper throws so missed transforms fail loudly.
Install
# via rolldown
bun add --dev comptime rolldown
# via vite
bun add --dev comptime vite
Vite
import { defineConfig } from "vite";
import { comptime } from "comptime/vite";
export default defineConfig({
plugins: [comptime()],
});
Rolldown
import { defineConfig } from "rolldown";
import { comptime } from "comptime/rolldown";
export default defineConfig({
input: "src/app.ts",
plugins: [comptime()],
});
API
import { comptime } from "comptime";
let value = comptime(() => expensivePureWork());
comptime<T>(fn: () => T | Promise<T>): T is typed as an identity helper. The plugin requires a single zero-argument arrow function or function expression.
Supported behavior:
- Imported
comptimebindings from"comptime", including aliases. - Shadowed local bindings are ignored.
- Referenced value imports are captured into virtual modules with absolute import paths.
- Referenced top-level declarations from the origin module are copied into the virtual module.
- Promise-returning bodies are awaited.
- Values are serialized with
devalue. - Build errors include the original call-site location.
- Vite dev uses
server.ssrLoadModule; Vite build and Rolldown build use an internal Rolldown evaluator.
Options
type ComptimeOptions = {
include?: string | string[];
exclude?: string | string[];
timeout?: number;
env?: string[] | "all" | "declared";
serializers?: Array<{
test: (value: unknown) => boolean;
serialize: (value: unknown) => string;
}>;
};
Defaults:
timeout:10_000env:"all"
When env is a string list, static process.env.KEY reads must be listed. Dynamic env reads are rejected unless env is "all".
Common Errors
These fail during transform:
comptime(1);
// comptime() requires a single arrow function with no parameters
comptime((value) => value);
// comptime() requires a single arrow function with no parameters
comptime(() => () => 1);
// comptime returned a value that cannot be serialized
comptime(() => {
throw new Error("something happened");
});
// build fails
Limits
This package does not add browser-side evaluation, disk caching, Webpack support, or esbuild standalone support.
License
MIT © Luke Edwards
Similar Articles
Conditionally Disabling Code with Comptime in Zig
Mitchell Hashimoto explains how to use Zig's comptime feature to conditionally disable code at compile time, comparing it to approaches in C and Go.
@voidzerodev: Rolldown is now available as Rust crate and will be published continuously there in addition to npm.
Rolldown, a build tool bundler, is now available as a Rust crate and will be published continuously there in addition to npm.
Zig Builds Are Getting Faster
Zig 0.15 shows significant compile-time improvements over 0.14, with build script compilation dropping from ~7s to ~1.7s and full builds from 41s to 32s, even while still using LLVM. The article highlights progress toward self-hosted backends and incremental compilation.
Tagged Union Subsets with Comptime in Zig
Mitchell Hashimoto demonstrates how to use Zig's comptime to create subset types of tagged unions, enabling compile-time safety without exhaustive case handling.
@webfansplz: In the latest Vite DevTools with Rolldown v1, we’ve fixed performance issues in large projects. Here’s a snapshot of it…
The latest Vite DevTools with Rolldown v1 fixes performance issues in large projects, as demonstrated on LobeHub with 30,000+ modules.