-
Notifications
You must be signed in to change notification settings - Fork 5
ArkEnv in vite.config.ts
#380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
bca5d56
refactor(vite.config.ts): enhance configuration with environment load…
yamcodes 25a3d33
Merge branch 'main' into 365-arkenv-in-viteconfigts
yamcodes a984baa
Update dependencies and improve test setup
yamcodes 999f21f
Merge branch 'main' into 365-arkenv-in-viteconfigts
yamcodes 8c590b2
Add type safety requirements for Vite config usage
yamcodes 7f9e968
Refactor Vite config to enhance environment variable schema handling
yamcodes 499e130
Merge branch 'main' into 365-arkenv-in-viteconfigts
yamcodes ffbadb0
Merge branch 'main' into 365-arkenv-in-viteconfigts
yamcodes 45632dd
Update pnpm-lock.yaml and Vite configuration for improved environment…
yamcodes 0c3aafe
[autofix.ci] apply automated fixes
autofix-ci[bot] 7a22926
Refactor environment schema usage across the project
yamcodes f8bfd5f
Update pnpm-lock.yaml to upgrade Vite to version 7.2.4
yamcodes 093c433
Refactor environment type from `Env` to `EnvSchema` for consistency
yamcodes 7576ce6
Enhance ArkEnv and Vite integration with comprehensive tests and docu…
yamcodes df389e9
[autofix.ci] apply automated fixes
autofix-ci[bot] e09b1d9
Remove outdated design, proposal, tasks, and specs documents for ArkE…
yamcodes f65115d
Update validation tasks for ArkEnv and Vite integration documentation
yamcodes 38b55ab
Update documentation for ArkEnv and Vite integration
yamcodes 1610173
Update ArkEnv in Vite config documentation
yamcodes 34ac14b
Update Quickstart tests to reflect ArkEnv documentation links
yamcodes e1de716
[autofix.ci] apply automated fixes
autofix-ci[bot] 6eaa578
Update documentation links for environment variable setup in README f…
yamcodes e1e0d70
Update documentation navigation tests to reflect new ArkEnv paths
yamcodes 39cd975
Update tests to reflect new ArkEnv documentation paths
yamcodes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,36 @@ | ||
| import arkenv from "@arkenv/vite-plugin"; | ||
| import react from "@vitejs/plugin-react"; | ||
| import { type } from "arkenv"; | ||
| import { defineConfig } from "vite"; | ||
| import arkenvVitePlugin from "@arkenv/vite-plugin"; | ||
| import reactPlugin from "@vitejs/plugin-react"; | ||
| import arkenv, { type } from "arkenv"; | ||
| import { defineConfig, loadEnv } from "vite"; | ||
|
|
||
| // Define the schema once, outside of defineConfig using type() | ||
| // This schema is used for both: | ||
| // 1. Validating unprefixed config variables (PORT) via loadEnv | ||
| // 2. Validating VITE_* variables via the plugin | ||
| const Env = type({ | ||
| PORT: "number.port", | ||
| VITE_MY_VAR: "string", | ||
| VITE_MY_NUMBER: type("string").pipe((str) => Number.parseInt(str, 10)), | ||
| VITE_MY_BOOLEAN: type("string").pipe((str) => str === "true"), | ||
| }); | ||
|
|
||
| // https://vite.dev/config/ | ||
| export default defineConfig({ | ||
| plugins: [ | ||
| react(), | ||
| arkenv({ | ||
| VITE_MY_VAR: "string", | ||
| VITE_MY_NUMBER: type("string").pipe((str) => Number.parseInt(str, 10)), | ||
| VITE_MY_BOOLEAN: type("string").pipe((str) => str === "true"), | ||
| }), | ||
| ], | ||
| export default defineConfig(({ mode }) => { | ||
| // Validate unprefixed config variables (e.g., PORT) using loadEnv | ||
| // These are server-only and not exposed to client code | ||
| // The schema defined with type() can be passed directly to arkenv() | ||
| const env = arkenv(Env, loadEnv(mode, process.cwd(), "")); | ||
|
|
||
| console.log(env.VITE_MY_NUMBER + " " + typeof env.VITE_MY_NUMBER); | ||
| return { | ||
| plugins: [ | ||
| reactPlugin(), | ||
| // The plugin validates VITE_* variables and exposes them to client code | ||
| // The same schema is reused here to avoid duplication | ||
| arkenvVitePlugin(Env), | ||
| ], | ||
| server: { | ||
| port: env.PORT, | ||
| }, | ||
| }; | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "title": "arkenv", | ||
| "description": "The core library", | ||
| "root": true, | ||
| "pages": [ | ||
| "---Introduction---", | ||
| "index", | ||
| "quickstart", | ||
| "examples", | ||
| "---API---", | ||
| "morphs", | ||
| "---Integrations---", | ||
| "[VS Code & Cursor](/docs/arkenv/integrations/vscode)", | ||
| "[JetBrains IDEs](/docs/arkenv/integrations/jetbrains)", | ||
| "---How-to---", | ||
| "[Load environment variables](/docs/arkenv/how-to/load-environment-variables)", | ||
| "[New][Reuse your schema](/docs/arkenv/how-to/reuse-schemas)" | ||
| ] | ||
| } |
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,3 @@ | ||
| { | ||
| "pages": [ | ||
| "---Introduction---", | ||
| "index", | ||
| "quickstart", | ||
| "examples", | ||
| "---API---", | ||
| "morphs", | ||
| "---Integrations---", | ||
| "[VS Code & Cursor](/docs/integrations/vscode)", | ||
| "[JetBrains IDEs](/docs/integrations/jetbrains)", | ||
| "---How-to---", | ||
| "[Load environment variables](/docs/how-to/load-environment-variables)", | ||
| "[New][Reuse your schema](/docs/how-to/reuse-schemas)" | ||
| ] | ||
| "pages": ["arkenv", "vite-plugin"] | ||
| } |
45 changes: 45 additions & 0 deletions
45
apps/www/content/docs/vite-plugin/arkenv-in-viteconfig.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| --- | ||
| title: Using ArkEnv in Vite config | ||
| --- | ||
|
|
||
| import { Globe } from "lucide-react"; | ||
|
|
||
|
|
||
| Vite doesn't automatically load `.env*` files when evaluating your `vite.config.ts` - those variables are only available later in your application code via `import.meta.env`. If you need environment variables in your config (like setting `server.port` or conditionally enabling plugins), you'll need to load them manually using Vite's `loadEnv` helper. | ||
|
|
||
| > [!IMPORTANT] | ||
| > You must have the core `arkenv` package installed in your project for this to work. See [arkenv](/docs/arkenv) for more information. | ||
|
|
||
|
|
||
| Here's how to validate those variables with ArkEnv. The key is defining your schema *once* with ArkEnv's`type()` and reusing it for both server-side config variables and client-exposed `VITE_*` variables: | ||
|
|
||
| ```ts title="vite.config.ts" | ||
| import arkenvVitePlugin from "@arkenv/vite-plugin"; | ||
| import arkenv, { type } from "arkenv"; | ||
| import { defineConfig, loadEnv } from "vite"; | ||
|
|
||
| const Env = type({ | ||
| PORT: "number.port", | ||
| VITE_API_URL: "string", | ||
| }); | ||
|
|
||
| export default defineConfig(({ mode }) => { | ||
| const env = arkenv(Env, loadEnv(mode, process.cwd(), "")); | ||
|
|
||
| return { | ||
| plugins: [arkenvVitePlugin(Env)], | ||
| server: { | ||
| port: env.PORT, | ||
| }, | ||
| }; | ||
| }); | ||
| ``` | ||
|
|
||
| Note how the schema is defined once but used in two different places: | ||
|
|
||
| 1. Environment variables needed for the Vite config (`PORT`) are available via `loadEnv()` and validated by ArkEnv. | ||
| 2. Public `VITE_*` variables that should be available in your client code (like `VITE_API_URL`) are validated by the `@arkenv/vite-plugin`. | ||
|
|
||
| <Cards> | ||
| <Card icon={<Globe aria-hidden="true" />} title="Using Environment Variables in Config (Vite docs)" href="https://vite.dev/config/#using-environment-variables-in-config" /> | ||
| </Cards> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| --- | ||
| title: What is the Vite plugin? | ||
| --- | ||
|
|
||
| This is the Vite plugin for ArkEnv. It validates environment variables at build-time with ArkEnv. | ||
|
|
||
| ```ts title="vite.config.ts" | ||
| import arkenv from "@arkenv/vite-plugin"; | ||
| import { defineConfig } from "vite"; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [ | ||
| arkenv({ | ||
| VITE_API_URL: "string", | ||
| VITE_APP_NAME: "'MyApp' | 'TestApp'", | ||
| "VITE_DEBUG": 'boolean = false' | ||
| }), | ||
| ], | ||
| }); | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "title": "@arkenv/vite-plugin", | ||
| "description": "The vite plugin", | ||
| "root": true, | ||
| "pages": ["index", "arkenv-in-viteconfig"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import type { NextRequest } from "next/server"; | ||
| import { NextResponse } from "next/server"; | ||
| import { source } from "~/lib/source"; | ||
|
|
||
| export function middleware(request: NextRequest) { | ||
| const { pathname } = request.nextUrl; | ||
|
|
||
| // Only handle /docs paths (but not /docs/arkenv/* which should work normally) | ||
| if (!pathname.startsWith("/docs")) { | ||
| return NextResponse.next(); | ||
| } | ||
|
|
||
| // Skip if already under /docs/arkenv | ||
| if (pathname.startsWith("/docs/arkenv")) { | ||
| return NextResponse.next(); | ||
| } | ||
|
|
||
| // Skip the root /docs redirect (handled by next.config.ts) | ||
| if (pathname === "/docs") { | ||
| return NextResponse.next(); | ||
| } | ||
|
|
||
| // Extract the slug from the path | ||
| // e.g., /docs/quickstart -> ["quickstart"] | ||
| // e.g., /docs/how-to/load -> ["how-to", "load"] | ||
| const slug = pathname.replace("/docs", "").split("/").filter(Boolean); | ||
|
|
||
| // Check if the page exists with the original slug | ||
| const page = source.getPage(slug); | ||
| if (page) { | ||
| // Page exists, let it render normally | ||
| return NextResponse.next(); | ||
| } | ||
|
|
||
| // Page doesn't exist, try with "arkenv" prefix | ||
| const arkenvSlug = ["arkenv", ...slug]; | ||
| const arkenvPage = source.getPage(arkenvSlug); | ||
| if (arkenvPage) { | ||
| // Found it under arkenv, redirect there | ||
| const newUrl = request.nextUrl.clone(); | ||
| newUrl.pathname = `/docs/arkenv/${slug.join("/")}`; | ||
| return NextResponse.redirect(newUrl); | ||
| } | ||
|
|
||
| // Not found even under arkenv, let it fall through to 404 | ||
| return NextResponse.next(); | ||
| } | ||
|
|
||
| export const config = { | ||
| matcher: "/docs/:path*", | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.