Conversation
- Updated unit tests to mock environment variable creation and validate transformed values. - Added tests for handling various data types, including null, undefined, and special characters. - Improved the plugin's configuration hook to expose transformed environment variables through Vite's define option. These changes enhance the robustness of the Vite plugin and ensure accurate handling of environment variables.
🦋 Changeset detectedLatest commit: 1635a99 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
WalkthroughThe Vite plugin’s config hook now collects transformed env values from createEnv and returns a Vite define mapping to expose them under import.meta.env.KEY. Added unit tests verify define serialization, key preservation, empty/env edge cases, and error propagation. A changeset entry documents the patch release. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Dev as vite.config.ts
participant Vite as Vite Build
participant Plugin as @arkenv/vite-plugin
participant Ark as createEnv()
Dev->>Vite: start build / dev
Vite->>Plugin: config(config, env)
Plugin->>Ark: createEnv(mode, schema, loaders)
Ark-->>Plugin: transformedEnv
Plugin->>Vite: return { define: { "import.meta.env.KEY": JSON.stringify(value), ... } }
Note right of Vite: Vite inlines define mappings
Vite-->>Dev: import.meta.env.KEY available with morphed values
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
commit: |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/vite-plugin/src/index.ts (1)
20-22: Filter out undefined values in Vite define entries
Vite’s define option passes values to esbuild and requires valid expressions or JSON-serializable values;JSON.stringify(undefined)yieldsundefined(not a string), producing invalid replacements. Exclude undefined entries:const define = Object.fromEntries( - Object.entries(<Record<string, unknown>>env).map(([key, value]) => [ - `import.meta.env.${key}`, - JSON.stringify(value), - ]), + Object.entries(<Record<string, unknown>>env) + .filter(([, value]) => value !== undefined) + .map(([key, value]) => [ + `import.meta.env.${key}`, + JSON.stringify(value), + ]), );
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
.changeset/tidy-worlds-fetch.md(1 hunks)packages/vite-plugin/src/index.test.ts(3 hunks)packages/vite-plugin/src/index.ts(1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.test.ts
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.test.ts: Place tests alongside source files and use the .test.ts suffix (e.g., create-env.test.ts, types.test.ts, errors.test.ts, utils.test.ts)
Use Vitest's describe/it structure in tests
Test both success and failure cases for environment validation and utilities
Mock process.env to cover different environment scenarios in tests
In tests, save and restore process.env in setup/teardown (beforeEach/afterEach) to avoid leakage between cases
Verify both runtime behavior and TypeScript types in tests
Files:
packages/vite-plugin/src/index.test.ts
🧠 Learnings (2)
📚 Learning: 2025-09-12T06:23:45.463Z
Learnt from: CR
PR: yamcodes/arkenv#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-09-12T06:23:45.463Z
Learning: Applies to **/*.test.ts : Mock process.env to cover different environment scenarios in tests
Applied to files:
packages/vite-plugin/src/index.test.ts
📚 Learning: 2025-09-12T06:23:45.463Z
Learnt from: CR
PR: yamcodes/arkenv#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-09-12T06:23:45.463Z
Learning: Applies to **/*.test.ts : Test both success and failure cases for environment validation and utilities
Applied to files:
packages/vite-plugin/src/index.test.ts
🧬 Code graph analysis (2)
packages/vite-plugin/src/index.ts (2)
packages/arkenv/src/create-env.ts (1)
createEnv(21-34)packages/arkenv/src/index.ts (1)
createEnv(13-13)
packages/vite-plugin/src/index.test.ts (1)
packages/vite-plugin/src/index.ts (1)
config(15-27)
🔇 Additional comments (9)
packages/vite-plugin/src/index.ts (1)
16-26: LGTM! The implementation correctly exposes morphed environment variables.The config hook now properly integrates createEnv results with Vite's define option, ensuring schema transformations are respected in
import.meta.env.packages/vite-plugin/src/index.test.ts (7)
42-43: LGTM! Proper mock setup for fixture test.The mock correctly returns the environment variables from the fixture configuration.
102-133: LGTM! Thorough test of createEnv invocation during config hook.The test properly verifies that createEnv is called with the expected schema and environment object, using a complete mock context.
135-182: LGTM! Comprehensive test of define object generation.The test thoroughly verifies that transformed values are correctly serialized and exposed through the define object with proper import.meta.env keys.
184-230: LGTM! Excellent coverage of edge cases.The test thoroughly covers various data types including edge cases like null, undefined, empty string, zero, and false. The explicit expectation at line 225 that
JSON.stringify(undefined)returnsundefineddocuments the current behavior.Note: This test confirms the undefined behavior I flagged in the implementation. Consider adding a comment or integration test to verify that Vite handles undefined values in the define object correctly.
232-259: LGTM! Good edge case coverage.The test correctly verifies that an empty environment object produces an empty define object.
261-304: LGTM! Thorough test of key name preservation.The test ensures that various key formats (uppercase, lowercase, numeric prefixes, special characters) are preserved exactly in the define output.
306-338: LGTM! Proper error propagation test.The test correctly verifies that errors thrown by createEnv are propagated through the config hook, ensuring failures are not silently swallowed.
Based on learnings: The test follows the coding guideline to "Test both success and failure cases for environment validation and utilities."
.changeset/tidy-worlds-fetch.md (1)
1-11: LGTM! Clear and accurate changelog entry.The changeset properly documents the fix, explaining both the previous problematic behavior and the new solution using Vite's define option to expose morphed environment variables.
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## @arkenv/vite-plugin@0.0.14 ### Patch Changes - #### Support array defaults using `type().default()` syntax _[`#224`](#224) [`ecf9b64`](ecf9b64) [@yamcodes](https://github.com/yamcodes)_ Fix to an issue where `type("array[]").default(() => [...])` syntax was not accepted by the plugin due to overly restrictive type constraints. The plugin now accepts any string-keyed record while still maintaining type safety through ArkType's validation system. ##### New Features - Array defaults to empty using `type("string[]").default(() => [])` syntax - Support for complex array types with defaults - Mixed schemas combining string-based and type-based defaults ##### Example ```typescript // vite.config.ts import arkenv from "@arkenv/vite-plugin"; import { type } from "arkenv"; export default defineConfig({ plugins: [ arkenv({ ALLOWED_ORIGINS: type("string[]").default(() => ["localhost"]), FEATURE_FLAGS: type("string[]").default(() => []), PORT: "number.port", }), ], }); ``` > [!NOTE] > This is the same fix as in [`arkenv@0.7.2` (the core library)](https://github.com/yamcodes/arkenv/releases/tag/arkenv%400.7.2), but for the Vite plugin. - #### Fix `import.meta.env` not respecting morphed environment variables _[`#227`](#227) [`d41878f`](d41878f) [@yamcodes](https://github.com/yamcodes)_ The Vite plugin now properly exposes transformed environment variables through `import.meta.env`. Previously, type transformations (`string → number`, `string → boolean`) and default values were lost because the plugin only called `createEnv()` without integrating the results with Vite's environment system. Now the plugin uses Vite's `define` option to expose the morphed values, ensuring all schema transformations are respected. Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This PR contains the following updates: | Package | Change | Age | Confidence | |---|---|---|---| | [@arkenv/vite-plugin](https://arkenv.js.org) ([source](https://redirect.github.com/yamcodes/arkenv)) | [`^0.0.10` -> `^0.0.14`](https://renovatebot.com/diffs/npm/@arkenv%2fvite-plugin/0.0.10/0.0.14) | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>yamcodes/arkenv (@​arkenv/vite-plugin)</summary> ### [`v0.0.14`](https://redirect.github.com/yamcodes/arkenv/releases/tag/%40arkenv/vite-plugin%400.0.14) [Compare Source](https://redirect.github.com/yamcodes/arkenv/compare/@arkenv/vite-plugin@0.0.13...@arkenv/vite-plugin@0.0.14) ##### Patch Changes - #### Support array defaults using `type().default()` syntax *[`#224`](https://redirect.github.com/yamcodes/arkenv/pull/224) [`ecf9b64`](https://redirect.github.com/yamcodes/arkenv/commit/ecf9b64a680d3af5c5786b288fda35608590f7a9) [@​yamcodes](https://redirect.github.com/yamcodes)* Fix to an issue where `type("array[]").default(() => [...])` syntax was not accepted by the plugin due to overly restrictive type constraints. The plugin now accepts any string-keyed record while still maintaining type safety through ArkType's validation system. ##### New Features - Array defaults to empty using `type("string[]").default(() => [])` syntax - Support for complex array types with defaults - Mixed schemas combining string-based and type-based defaults ##### Example ```typescript // vite.config.ts import arkenv from "@​arkenv/vite-plugin"; import { type } from "arkenv"; export default defineConfig({ plugins: [ arkenv({ ALLOWED_ORIGINS: type("string[]").default(() => ["localhost"]), FEATURE_FLAGS: type("string[]").default(() => []), PORT: "number.port", }), ], }); ``` > \[!NOTE] > This is the same fix as in [`arkenv@0.7.2` (the core library)](https://redirect.github.com/yamcodes/arkenv/releases/tag/arkenv%400.7.2), but for the Vite plugin. - #### Fix `import.meta.env` not respecting morphed environment variables *[`#227`](https://redirect.github.com/yamcodes/arkenv/pull/227) [`d41878f`](https://redirect.github.com/yamcodes/arkenv/commit/d41878fe9cc2524f06ac2f0ef35f2f5ba58ee06b) [@​yamcodes](https://redirect.github.com/yamcodes)* The Vite plugin now properly exposes transformed environment variables through `import.meta.env`. Previously, type transformations (`string → number`, `string → boolean`) and default values were lost because the plugin only called `createEnv()` without integrating the results with Vite's environment system. Now the plugin uses Vite's `define` option to expose the morphed values, ensuring all schema transformations are respected. ### [`v0.0.13`](https://redirect.github.com/yamcodes/arkenv/releases/tag/%40arkenv/vite-plugin%400.0.13) [Compare Source](https://redirect.github.com/yamcodes/arkenv/compare/@arkenv/vite-plugin@0.0.12...@arkenv/vite-plugin@0.0.13) ##### Patch Changes - #### Support Vite 2.x *[`#212`](https://redirect.github.com/yamcodes/arkenv/pull/212) [`bfe08f6`](https://redirect.github.com/yamcodes/arkenv/commit/bfe08f6d9f21352186420f0f68611840e164da52) [@​yamcodes](https://redirect.github.com/yamcodes)* Extended the supported Vite versions to include **2.9.18** through **7.x** (inclusive). Also, we've added the `vite-plugin` keyword to the `package.json`, and a section in the `README.md` explaining why this plugin is a Vite only plugin (and not a Rollup plugin). <details><summary>Updated 1 dependency</summary> <small> [`e554e2b`](https://redirect.github.com/yamcodes/arkenv/commit/e554e2b41aab1b8e29d873982ea587c069f4732d) </small> - `arkenv@0.7.3` </details> ### [`v0.0.12`](https://redirect.github.com/yamcodes/arkenv/releases/tag/%40arkenv/vite-plugin%400.0.12) [Compare Source](https://redirect.github.com/yamcodes/arkenv/compare/@arkenv/vite-plugin@0.0.11...@arkenv/vite-plugin@0.0.12) ##### Patch Changes <details><summary>Updated 1 dependency</summary> <small> [`e50dba1`](https://redirect.github.com/yamcodes/arkenv/commit/e50dba1f19418f8fc007dc786df1172067e3d07c) </small> - `arkenv@0.7.2` </details> ### [`v0.0.11`](https://redirect.github.com/yamcodes/arkenv/releases/tag/%40arkenv/vite-plugin%400.0.11) [Compare Source](https://redirect.github.com/yamcodes/arkenv/compare/@arkenv/vite-plugin@0.0.10...@arkenv/vite-plugin@0.0.11) ##### Patch Changes <details><summary>Updated 1 dependency</summary> <small> [`221f9ef`](https://redirect.github.com/yamcodes/arkenv/commit/221f9efdef65691b0c5155b12ec460404dddbe82) [`221f9ef`](https://redirect.github.com/yamcodes/arkenv/commit/221f9efdef65691b0c5155b12ec460404dddbe82) </small> - `arkenv@0.7.1` </details> </details> --- ### Configuration 📅 **Schedule**: Branch creation - "on friday" (UTC), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR was generated by [Mend Renovate](https://mend.io/renovate/). View the [repository job log](https://developer.mend.io/github/yamcodes/arkenv). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MS4xNzMuMSIsInVwZGF0ZWRJblZlciI6IjQxLjE3My4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119--> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
These changes enhance the robustness of the Vite plugin and ensure accurate handling of environment variables.
Closes #226
Summary by CodeRabbit
Bug Fixes
Tests
Chores