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
21 changes: 19 additions & 2 deletions .changeset/few-lights-ask.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
---
"@arkenv/bun-plugin": minor
"@arkenv/bun-plugin": patch
---

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.
#### Support `NODE_ENV` in schema

When `NODE_ENV` is included in your schema, it is now validated at startup and correctly typed.

```ts
// src/env.ts
import { type } from "arkenv";

export default type({
BUN_PUBLIC_API_URL: "string.url",
NODE_ENV: "'development' | 'production' | 'test'",
});
```

```tsx
// process.env.NODE_ENV is now typed as "development" | "production" | "test"
<p>Mode: {process.env.NODE_ENV}</p>
```
3 changes: 3 additions & 0 deletions apps/playgrounds/bun-react/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BUN_PUBLIC_API_URL=https://api.example.com
BUN_PUBLIC_DEBUG=true
NODE_ENV=development
1 change: 1 addition & 0 deletions apps/playgrounds/bun-react/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import { type } from "arkenv";
export default type({
BUN_PUBLIC_API_URL: "string.url",
BUN_PUBLIC_DEBUG: "boolean",
NODE_ENV: "'development' | 'production' | 'test'",
});
2 changes: 1 addition & 1 deletion apps/www/content/docs/bun-plugin/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ await Bun.build({

- **Static Analysis**: Automatically replaces `process.env.VARIABLE` with validated values during the build.
- **Typesafety**: Ensures your code only accesses variables defined in your schema.
- **Secure Defaults**: Only exposes variables prefixed with `BUN_PUBLIC_` to the client bundle. You can customize this prefix by setting the `env` option in your `Bun.build()` configuration (e.g., `env: "MY_PUBLIC_*"` to use `MY_PUBLIC_` instead). See [Bun's bundler documentation](https://bun.sh/docs/bundler#env) for more details.
- **Secure Defaults**: Only exposes variables prefixed with `BUN_PUBLIC_` to the client bundle. `NODE_ENV` is also exposed when defined in your schema. You can customize this prefix by setting the `env` option in your `Bun.build()` configuration (e.g., `env: "MY_PUBLIC_*"` to use `MY_PUBLIC_` instead). See [Bun's bundler documentation](https://bun.sh/docs/bundler#env) for more details.

## Installation

Expand Down
17 changes: 16 additions & 1 deletion apps/www/content/docs/bun-plugin/typing-process-env.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,27 @@ const apiUrl = process.env.BUN_PUBLIC_API_URL; // ✅ Typesafe
const debug = process.env.BUN_PUBLIC_DEBUG; // ✅ Typesafe
```

`NODE_ENV` is also supported — when defined in your schema, it is validated and correctly typed:

```ts title="src/env.ts"
import { type } from "arkenv";

export default type({
BUN_PUBLIC_API_URL: "string.url",
NODE_ENV: "'development' | 'production' | 'test'",
});
```

```ts title="src/app.tsx"
process.env.NODE_ENV; // ✅ "development" | "production" | "test"
```

## How it works

The `ProcessEnvAugmented` type:

1. Extracts the inferred type from your schema (the result of `type()` from arkenv)
2. Filters to only include variables matching the Bun prefix (defaults to `"BUN_PUBLIC_"`)
2. Filters to only include variables matching the Bun prefix (defaults to `"BUN_PUBLIC_"`) and `NODE_ENV`
3. Makes them available on `process.env` with full type safety

Server-only variables (like `PORT`) are automatically excluded from the client bundle and won't appear in the augmented `process.env` type.
Expand Down
8 changes: 4 additions & 4 deletions examples/basic-js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/basic-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"clean": "rimraf dist node_modules"
},
"dependencies": {
"arkenv": "^0.9.3",
"arkenv": "^0.10.0",
"arktype": "^2.1.29"
},
"engines": {
Expand Down
8 changes: 4 additions & 4 deletions examples/basic/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"clean": "rimraf dist node_modules"
},
"dependencies": {
"arkenv": "^0.9.3",
"arkenv": "^0.10.0",
"arktype": "^2.1.29",
"zod": "^4.3.5"
},
Expand Down
3 changes: 3 additions & 0 deletions examples/with-bun-react/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BUN_PUBLIC_API_URL=https://api.example.com
BUN_PUBLIC_DEBUG=true
NODE_ENV=development
8 changes: 4 additions & 4 deletions examples/with-bun-react/bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/with-bun-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"clean": "rimraf dist node_modules"
},
"dependencies": {
"@arkenv/bun-plugin": "^0.1.3",
"arkenv": "^0.9.3",
"@arkenv/bun-plugin": "^0.1.4",
"arkenv": "^0.10.0",
"arktype": "^2.1.29",
"react": "^19.2.3",
"react-dom": "^19.2.3"
Expand Down
1 change: 1 addition & 0 deletions examples/with-bun-react/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import { type } from "arkenv";
export default type({
BUN_PUBLIC_API_URL: "string.url",
BUN_PUBLIC_DEBUG: "boolean",
NODE_ENV: "'development' | 'production' | 'test'",
});
4 changes: 2 additions & 2 deletions examples/with-bun/bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/with-bun/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"clean": "rimraf dist node_modules"
},
"dependencies": {
"arkenv": "^0.9.3",
"arkenv": "^0.10.0",
"arktype": "^2.1.29"
},
"devDependencies": {
Expand Down
18 changes: 9 additions & 9 deletions examples/with-solid-start/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/with-solid-start/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"start": "vinxi start"
},
"dependencies": {
"@arkenv/vite-plugin": "^0.0.30",
"@arkenv/vite-plugin": "^0.0.31",
"@solidjs/start": "1.2.1",
"arkenv": "^0.9.3",
"arkenv": "^0.10.0",
"arktype": "^2.1.29",
"solid-js": "1.9.11",
"vinxi": "0.5.11"
Expand Down
8 changes: 4 additions & 4 deletions examples/with-standard-schema/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/with-standard-schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"clean": "rimraf dist node_modules"
},
"dependencies": {
"arkenv": "^0.9.3",
"arkenv": "^0.10.0",
"arktype": "^2.1.29",
"zod": "^4.3.5"
},
Expand Down
18 changes: 9 additions & 9 deletions examples/with-vite-react/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/with-vite-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"clean": "rimraf dist node_modules"
},
"dependencies": {
"@arkenv/vite-plugin": "^0.0.30",
"arkenv": "^0.9.3",
"@arkenv/vite-plugin": "^0.0.31",
"arkenv": "^0.10.0",
"arktype": "^2.1.29",
"react": "^19.2.3",
"react-dom": "^19.2.3"
Expand Down
8 changes: 6 additions & 2 deletions packages/bun-plugin/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import type { type } from "arktype";
* based on the schema validator.
*
* This type extracts the inferred type from the schema (result of `type()` from arkenv),
* filters it to only include variables matching the Bun prefix (defaults to "BUN_PUBLIC_"),
* filters by the Bun prefix (defaults to "BUN_PUBLIC_") and always includes `NODE_ENV`,
* and makes them available on `process.env`.
*
* @template TSchema - The environment variable schema (result of `type()` from arkenv)
* @template Prefix - The prefix to filter by (defaults to "BUN_PUBLIC_")
* @template AllowedKeys - Additional keys to include regardless of prefix (defaults to "NODE_ENV")
*
* @example
* ```ts
Expand All @@ -21,6 +22,7 @@ import type { type } from "arktype";
* export const Env = type({
* BUN_PUBLIC_API_URL: 'string',
* BUN_PUBLIC_DEBUG: 'boolean',
* NODE_ENV: "'development' | 'production' | 'test'", // Included via AllowedKeys
* PORT: 'number.port', // Server-only, won't be in ProcessEnvAugmented
* });
*
Expand All @@ -39,6 +41,7 @@ import type { type } from "arktype";
*
* declare global {
* namespace NodeJS {
* // process.env.BUN_PUBLIC_* and NODE_ENV are now typesafe
* interface ProcessEnv extends ProcessEnvAugmented<typeof Env> {}
* }
* }
Expand All @@ -47,4 +50,5 @@ import type { type } from "arktype";
export type ProcessEnvAugmented<
TSchema extends type.Any,
Prefix extends string = "BUN_PUBLIC_",
> = FilterByPrefix<InferType<TSchema>, Prefix>;
AllowedKeys extends string = "NODE_ENV",
> = FilterByPrefix<InferType<TSchema>, Prefix, AllowedKeys>;
Loading
Loading