Skip to content
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

Allow validating environment vars without prefix #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path'
import { cwd } from 'node:process'
import { createConfigLoader as createLoader } from 'unconfig'
import { type ConfigEnv, type Plugin, type UserConfig } from 'vite'
import { type ConfigEnv, type Plugin, type UserConfig, resolveEnvPrefix } from 'vite'

import { initUi, type UI } from './ui.js'
import { zodValidation } from './validators/zod/index.js'
Expand Down Expand Up @@ -93,13 +93,18 @@ async function validateEnv(
? normalizePath(path.resolve(resolvedRoot, userConfig.envDir))
: resolvedRoot

const env = loadEnv(envConfig.mode, envDir, userConfig.envPrefix)
const env = loadEnv(envConfig.mode, envDir, '')

const options = await loadOptions(rootDir, inlineOptions)
const variables = await validateAndLog(ui, env, options)

const envPrefix = resolveEnvPrefix(userConfig)
const prefixedVariables = variables.filter(({ key }) =>
envPrefix.some((prefix) => key.startsWith(prefix)),
)

return {
define: variables.reduce(
define: prefixedVariables.reduce(
(acc, { key, value }) => {
acc[`import.meta.env.${key}`] = JSON.stringify(value)
return acc
Expand Down
Loading