Repository navigation
feat: recommend ViteHub Env on Nuxt 5 #45
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
10 commits
Select commit
Hold shift + click to select a range
cc52888
feat!: remove the Shelve integration
onmax 83528c1
docs: add the logo, a README banner and working site config
onmax a28338a
docs: animated landing hero and banner with Vue Bits FaultyTerminal
onmax b740709
docs: drop the Nuxt 3 claim from the landing hero
onmax c6954d1
docs: support light mode in the landing hero
onmax e205678
feat: recommend ViteHub Env on Nuxt 5
onmax 4a27290
fix(docs): resolve main conflicts and stop paused hero rendering
vite-hub[bot] 8f0463d
fix: refresh packed runtime check and document migration Node require…
vite-hub[bot] cc06b12
chore: resolve base merge while retaining runtime package verification
vite-hub[bot] 60765cf
chore: resolve main merge while preserving Nuxt 5 migration tip
vite-hub[bot] 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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <script setup lang="ts"> | ||
| import DocusHeader from 'docus/app/components/app/AppHeader.vue' | ||
| </script> | ||
|
|
||
| <template> | ||
| <UBanner | ||
| id="vitehub-env-nuxt-5" | ||
| icon="i-ph-arrow-circle-up-right" | ||
| title="On Nuxt 5? Move to @vite-hub/env with one command." | ||
| to="/guide/migrate-to-vitehub-env" | ||
| :actions="[{ label: 'Migration guide', to: '/guide/migrate-to-vitehub-env', color: 'neutral', variant: 'outline', trailingIcon: 'i-ph-arrow-right' }]" | ||
| close | ||
| /> | ||
| <DocusHeader /> | ||
| </template> |
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 |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| --- | ||
| title: Migrate to ViteHub Env | ||
| description: On Nuxt 5, move from this module to @vite-hub/env with one command. | ||
| navigation.icon: i-ph-arrow-circle-up-right | ||
| --- | ||
|
|
||
| On Nuxt 5, use [ViteHub Env](https://vitehub.dev/docs/env) (`@vite-hub/env`) instead of this module. You still declare each value once and read it typed. What changes: | ||
|
|
||
| - **One API for Nuxt, Vite and Nitro.** Server code reads `useServerEnv(event)` and app code reads `usePublicEnv()`. | ||
| - **Secrets stay sealed.** A secret is a `SecretEnv` that prints as `<redacted>` until you call `.unseal()`. | ||
| - **The same `.env.example`.** Env reads the same [`env-spec` profile](/guide/env-schema), so an `envSchema` app moves without changes to the file. | ||
| - **Inspection from the CLI.** `vitehub env check` lists every declared variable and its status, and never prints values. | ||
|
|
||
| ## Run the codemod | ||
|
|
||
| Upgrade to **Node.js 24.15 or newer** before running the codemod or installing the migrated dependencies. `vite-hub` requires it, even if your Nuxt 5 app currently runs on Node.js 22. | ||
|
|
||
| ::steps | ||
|
|
||
| ### Preview the changes | ||
|
|
||
| ```bash | ||
| npx @vite-hub/codemods nuxt-safe-runtime-config --dry-run | ||
| ``` | ||
|
|
||
| `--dry-run` prints the diff of every file and a report, and writes nothing. | ||
|
|
||
| ### Apply them | ||
|
|
||
| ```bash | ||
| npx @vite-hub/codemods nuxt-safe-runtime-config | ||
| ``` | ||
|
|
||
| Pass a directory as the last argument when the app is not in the current directory. | ||
|
|
||
| ### Install and check | ||
|
|
||
| ```bash | ||
| pnpm install | ||
| npx nuxi prepare | ||
| ``` | ||
|
|
||
| Then type-check the app and work through the follow-ups in the report. | ||
|
|
||
| :: | ||
|
|
||
| ## What it changes | ||
|
|
||
| In `nuxt.config.ts`, the codemod replaces `nuxt-safe-runtime-config` with `vite-hub/nuxt` in `modules`. It moves the declarations to `vitehub.env` and removes `safeRuntimeConfig`. `package.json` drops this module and adds `vite-hub`. | ||
|
|
||
| ### With `envSchema` | ||
|
|
||
| The `.env.example` stays as it is. `prefix: 'NUXT_'` keeps the same variable names and the same `public` split. | ||
|
|
||
| ```ts [nuxt.config.ts] | ||
| import { envSchema } from '@vite-hub/env/vite' | ||
|
|
||
| export default defineNuxtConfig({ | ||
| modules: ['vite-hub/nuxt'], | ||
| vitehub: { | ||
| preset: 'node', | ||
| env: envSchema(new URL('./.env.example', import.meta.url), { prefix: 'NUXT_' }), | ||
| }, | ||
| }) | ||
| ``` | ||
|
|
||
| ### With `$schema` | ||
|
|
||
| The codemod loads your schema, converts it to JSON Schema, and writes one Env declaration per value. Each value keeps its `NUXT_*` variable name, and literal defaults stay. | ||
|
|
||
| ::code-group | ||
|
|
||
| ```ts [Before] | ||
| export default defineNuxtConfig({ | ||
| modules: ['nuxt-safe-runtime-config'], | ||
| runtimeConfig: { | ||
| apiSecret: '', | ||
| maxRetries: 3, | ||
| featureEnabled: false, | ||
| public: { apiBase: 'https://api.example.com' }, | ||
| }, | ||
| safeRuntimeConfig: { | ||
| $schema: v.object({ | ||
| apiSecret: v.pipe(v.string(), v.minLength(16)), | ||
| maxRetries: v.number(), | ||
| featureEnabled: v.boolean(), | ||
| public: v.object({ apiBase: v.pipe(v.string(), v.url()) }), | ||
| }), | ||
| validateAtRuntime: true, | ||
| }, | ||
| }) | ||
| ``` | ||
|
|
||
| ```ts [After] | ||
| import { env } from 'vite-hub/env' | ||
|
|
||
| export default defineNuxtConfig({ | ||
| modules: ['vite-hub/nuxt'], | ||
| vitehub: { | ||
| preset: 'node', | ||
| env: { | ||
| public: { | ||
| apiBase: env({ default: 'https://api.example.com', mode: 'build', source: env.source('NUXT_PUBLIC_API_BASE') }), | ||
| }, | ||
| server: { | ||
| apiSecret: env({ secret: true, source: env.source('NUXT_API_SECRET') }), | ||
| maxRetries: env.number({ default: 3, source: env.source('NUXT_MAX_RETRIES') }), | ||
| featureEnabled: env.boolean({ default: false, source: env.source('NUXT_FEATURE_ENABLED') }), | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| ``` | ||
|
|
||
| :: | ||
|
|
||
| Server fields with credential-like names (`secret`, `token`, `password`, `key`, database URLs) become `secret: true`. | ||
|
|
||
| ### In your code | ||
|
|
||
| ```ts [server/api/config.get.ts] | ||
| // Before | ||
| const config = useSafeRuntimeConfig(event) | ||
|
|
||
| // After | ||
| import { useServerEnv } from '#vitehub/env/server' | ||
| const config = useServerEnv(event) | ||
| ``` | ||
|
|
||
| In app code, `useSafeRuntimeConfig()` becomes `usePublicEnv()` from `#vitehub/env/public`, and `config.public.apiBase` becomes `config.apiBase`. | ||
|
|
||
| ## Follow-ups | ||
|
|
||
| The report lists each change the codemod could not make, with `file:line`: | ||
|
|
||
| - **Secret reads.** `config.apiSecret` is now a `SecretEnv<string>`. Call `.unseal()` where the code needs the raw value. | ||
| - **Lost checks.** Code-first Env declarations do not keep minimum lengths, patterns, URL formats, transforms or refinements. Check those values in code, or move to a `.env.example` with `envSchema()`, which keeps length, range, integer and URL checks. | ||
| - **JSON arrays, objects and unions.** These stay in `runtimeConfig`. Read them with `useRuntimeConfig()`, or declare them in a `.env.example`. | ||
| - **The deployment preset.** `vite-hub/nuxt` needs one. The codemod copies `nitro.preset` when it can, and otherwise sets `node`. | ||
| - **Call sites it did not rewrite.** For example, a config object passed to another function, or `useRuntimeConfig()` reads of keys that moved. | ||
| - **The ESLint rule.** Remove `nuxt-safe-runtime-config/eslint` from your ESLint config. | ||
|
|
||
| ::warning | ||
| Public Env values are fixed at build time. Nuxt applied `NUXT_PUBLIC_*` overrides when the server started; with Env, set them before you build. Server Env values are still read when the server runs. | ||
| :: | ||
|
|
||
| Shelve and the [standalone Nitro module](/guide/nitro) are not part of the codemod. | ||
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 |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import type { Nuxt } from '@nuxt/schema' | ||
| import { describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const warn = vi.hoisted(() => vi.fn()) | ||
| vi.mock('@nuxt/kit', async importOriginal => ({ | ||
| ...await importOriginal<typeof import('@nuxt/kit')>(), | ||
| useLogger: () => ({ warn, error: vi.fn(), info: vi.fn(), success: vi.fn() }), | ||
| })) | ||
|
|
||
| const { default: module } = await import('../src/module') | ||
|
|
||
| function nuxtWithVersion(version: string): Nuxt { | ||
| return { _version: version, options: {} } as unknown as Nuxt | ||
| } | ||
|
|
||
| describe('viteHub Env recommendation', () => { | ||
| it('stays quiet on Nuxt 4', async () => { | ||
| warn.mockClear() | ||
| await module.onInstall?.(nuxtWithVersion('4.5.0')) | ||
| expect(warn).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('names the codemod on Nuxt 5 install and upgrade', async () => { | ||
| warn.mockClear() | ||
| await module.onInstall?.(nuxtWithVersion('5.0.0')) | ||
| await module.onUpgrade?.(nuxtWithVersion('5.0.0'), {}, '0.3.2') | ||
| expect(warn).toHaveBeenCalledTimes(2) | ||
| const message = String(warn.mock.calls[0]![0]) | ||
| expect(message.split('\n')).toHaveLength(2) | ||
| expect(message).toContain('npx @vite-hub/codemods nuxt-safe-runtime-config') | ||
| }) | ||
| }) |
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.