Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/few-lights-ask.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@arkenv/bun-plugin": minor
---

Include the `NODE_ENV` environment variable as one of the publicly exposed environment variables so it's possible to type it. Bun already exposes `process.env.NODE_ENV` to the frontend and now it's possible to get it correctly typed.
4 changes: 3 additions & 1 deletion packages/bun-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
- Build-time validation - app won't start if environment variables are invalid
- Typesafe environment variables backed by TypeScript
- Access to ArkType's powerful type system
- Automatic filtering of client-exposed variables (defaults to `BUN_PUBLIC_*`)
- Automatic filtering of client-exposed variables (defaults to `BUN_PUBLIC_*` and `NODE_ENV`)

> [!IMPORTANT]
> This plugin requires `arktype` to be installed in your project.
Expand Down Expand Up @@ -73,6 +73,7 @@ import { type } from 'arkenv';
export default type({
BUN_PUBLIC_API_URL: 'string',
BUN_PUBLIC_DEBUG: 'boolean',
NODE_ENV: '"development" | "test" | "production" = "development"',
});
```

Expand Down Expand Up @@ -112,6 +113,7 @@ await Bun.build({
arkenv(type({
BUN_PUBLIC_API_URL: 'string',
BUN_PUBLIC_DEBUG: 'boolean',
NODE_ENV: '"development" | "test" | "production" = "development"',
})),
],
});
Expand Down
12 changes: 12 additions & 0 deletions packages/bun-plugin/src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,16 @@ describe("Bun Plugin Utils", () => {
expect(envMap.has("PORT")).toBe(false);
expect(envMap.has("DATABASE_URL")).toBe(false);
});

it("should not filter out NODE_ENV", () => {
process.env.NODE_ENV = "development";

const envMap = processEnvSchema({
NODE_ENV: "'development' | 'test' | 'production'",
} as const);

// Check that NODE_ENV is present
expect(envMap.has("NODE_ENV")).toBe(true);
expect(envMap.get("NODE_ENV")).toBe(JSON.stringify("development"));
});
});
5 changes: 4 additions & 1 deletion packages/bun-plugin/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ export function processEnvSchema<T extends SchemaShape>(
env: config?.env ?? process.env,
});
const prefix = "BUN_PUBLIC_";
const allowed = new Set(["NODE_ENV"]);
const filteredEnv = Object.fromEntries(
Object.entries(env).filter(([key]) => key.startsWith(prefix)),
Object.entries(env).filter(
([key]) => allowed.has(key) || key.startsWith(prefix),
),
);
const envMap = new Map<string, string>();
for (const [key, value] of Object.entries(filteredEnv)) {
Expand Down
Loading