Skip to content

Commit

Permalink
Allow validating environment vars without prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
Kim Biesbjerg committed Apr 18, 2024
1 parent 1d9bb47 commit 2b15a14
Showing 1 changed file with 8 additions and 3 deletions.
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

0 comments on commit 2b15a14

Please sign in to comment.