|
| 1 | +# ArkEnv + TanStack Start (Rsbuild) Example |
| 2 | + |
| 3 | +This example demonstrates `@arkenv/rsbuild-plugin` with [TanStack Start](https://tanstack.com/start) on Rsbuild: |
| 4 | + |
| 5 | +- A single `src/env.ts` is the typed source of truth (`import { env } from "./env"`) |
| 6 | +- **Client graph**: plugin inlines coerced `PUBLIC_*` literals and guards server-only keys |
| 7 | +- **SSR graph**: `env.ts` runs as-is → boot-time validation against the real environment, including inside `createServerFn` handlers |
| 8 | +- Reading `env.DATABASE_URL` in the browser throws (try the button on the home page) |
| 9 | + |
| 10 | +## Setup |
| 11 | + |
| 12 | +```ts title="src/env.ts" |
| 13 | +import arkenv from "@arkenv/core"; |
| 14 | + |
| 15 | +export const env = arkenv({ |
| 16 | + DATABASE_URL: "string = 'postgres://localhost:5432/tanstackstartrsbuild'", |
| 17 | + PORT: "number.port = 3000", |
| 18 | + PUBLIC_API_URL: "string = 'https://api.example.com'", |
| 19 | + NODE_ENV: "'development' | 'production' | 'test' = 'development'", |
| 20 | +}); |
| 21 | +``` |
| 22 | + |
| 23 | +```ts title="rsbuild.config.ts" |
| 24 | +import { pluginReact } from "@rsbuild/plugin-react"; |
| 25 | +import { tanstackStart } from "@tanstack/react-start/plugin/rsbuild"; |
| 26 | +import { arkenvRsbuildPlugin } from "@arkenv/rsbuild-plugin"; |
| 27 | +import { defineConfig } from "@rsbuild/core"; |
| 28 | + |
| 29 | +export default defineConfig({ |
| 30 | + server: { |
| 31 | + port: 3000, |
| 32 | + }, |
| 33 | + plugins: [ |
| 34 | + tanstackStart({ srcDirectory: "src" }), |
| 35 | + pluginReact(), |
| 36 | + arkenvRsbuildPlugin(), |
| 37 | + ], |
| 38 | +}); |
| 39 | +``` |
| 40 | + |
| 41 | +## Usage |
| 42 | + |
| 43 | +```tsx |
| 44 | +import { createServerFn } from "@tanstack/react-start"; |
| 45 | +import { env } from "./env"; |
| 46 | + |
| 47 | +const readDatabaseUrl = createServerFn({ method: "GET" }).handler(() => { |
| 48 | + return env.DATABASE_URL; // server-only: real value, validated at boot |
| 49 | +}); |
| 50 | + |
| 51 | +env.PUBLIC_API_URL; // string (inlined on the client) |
| 52 | +env.DATABASE_URL; // throws in the browser; works in SSR and server functions |
| 53 | +``` |
| 54 | + |
| 55 | +## Running the Example |
| 56 | + |
| 57 | +```bash |
| 58 | +# Install dependencies |
| 59 | +pnpm install |
| 60 | + |
| 61 | +# Start dev server |
| 62 | +pnpm dev |
| 63 | + |
| 64 | +# Build for production |
| 65 | +pnpm build |
| 66 | + |
| 67 | +# Start production server |
| 68 | +pnpm start |
| 69 | +``` |
| 70 | + |
| 71 | +## Documentation |
| 72 | + |
| 73 | +- [TanStack Start guide](https://github.com/yamcodes/arkenv/blob/v1/apps/www/content/docs/frameworks/tanstack-start.mdx) (`/docs/frameworks/tanstack-start` on the v1 docs site) |
| 74 | +- [Rsbuild plugin docs](https://github.com/yamcodes/arkenv/blob/v1/apps/www/content/docs/reference/rsbuild-plugin.mdx) (`/docs/reference/rsbuild-plugin` on the v1 docs site) |
0 commit comments