extforge/env
extforge/env loads .env files at build time and inlines variables prefixed EXTFORGE_PUBLIC_ into your bundles via esbuild’s define. It mirrors the conventions Vite users expect.
Load order
Section titled “Load order”Files load in this precedence (later overrides earlier):
.env.env.local.env.<mode>— where<mode>isproductionordevelopment.env.<mode>.localprocess.env— overlays last
mode defaults to production. The dev server passes mode: 'development'.
Public vs. private keys
Section titled “Public vs. private keys”Only keys starting with EXTFORGE_PUBLIC_ are inlined. Everything else stays in the build-process environment (available to plugins and config files) but is NOT shipped to the user’s browser.
EXTFORGE_PUBLIC_API_BASE=https://api.example.com # ← inlined into bundlesEXTFORGE_BACKEND_TOKEN=secret-do-not-ship # ← never inlined// User codeconst url = import.meta.env.EXTFORGE_PUBLIC_API_BASE;// ^? "https://api.example.com" (string literal at build time)Both import.meta.env.<KEY> and process.env.<KEY> are populated for compatibility with code that reads either form.
Available metadata
Section titled “Available metadata”The import.meta.env object also carries:
| Key | Value |
|---|---|
MODE | 'production' or 'development' |
PROD | 'true' in prod, 'false' in dev |
DEV | inverse of PROD |
Programmatic API
Section titled “Programmatic API”The builder calls loadEnv automatically. You only need this if you’re writing a plugin or custom build script.
import { loadEnv, publicEnvToDefine } from 'extforge/env';
const { raw, publicEnv, files } = loadEnv({ cwd: process.cwd(), mode: 'production' });// `raw` = every key/value (including non-public)// `publicEnv` = only EXTFORGE_PUBLIC_*// `files` = files actually read, in precedence order
const define = publicEnvToDefine(publicEnv, 'production');// define is the esbuild `define` map: { 'import.meta.env': '...', 'process.env.X': '"..."' }Typing your env
Section titled “Typing your env”extforge/env doesn’t generate .d.ts files. Add your own ambient declaration:
interface ImportMetaEnv { readonly EXTFORGE_PUBLIC_API_BASE: string;}
interface ImportMeta { readonly env: ImportMetaEnv;}