Check if a feature flag is enabled at compile time.
This function is replaced with a boolean literal (true or false) at bundle time, enabling dead-code elimination. The bundler will remove unreachable branches.
module
Check if a feature flag is enabled at compile time.
This function is replaced with a boolean literal (true or false) at bundle time, enabling dead-code elimination. The bundler will remove unreachable branches.
The name of the feature flag to check
true if the flag was passed via --feature=FLAG, false otherwise
import { feature } from "bun:bundle";
// With --feature=DEBUG, this becomes: if (true) { ... }
// Without --feature=DEBUG, this becomes: if (false) { ... }
if (feature("DEBUG")) {
console.log("Debug mode enabled");
}
Registry for type-safe feature flags.
Augment this interface to get autocomplete and type checking for your feature flags:
// env.d.ts
declare module "bun:bundle" {
interface Registry {
features: "DEBUG" | "PREMIUM" | "BETA";
}
}
Now feature() only accepts "DEBUG", "PREMIUM", or "BETA":
feature("DEBUG"); // OK
feature("TYPO"); // Type error