Home / File/ env.ts — vite Source File

env.ts — vite Source File

Architecture documentation for env.ts, a typescript file in the vite codebase. 13 imports, 6 dependents.

File typescript ViteCore ConfigEngine 13 imports 6 dependents 3 functions

Entity Profile

Dependency Diagram

graph LR
  7fa76fc1_cb1b_cf98_0900_1217276f6616["env.ts"]
  031bc221_67a8_c579_f2bf_bb30a08beeb2["utils.ts"]
  7fa76fc1_cb1b_cf98_0900_1217276f6616 --> 031bc221_67a8_c579_f2bf_bb30a08beeb2
  19ce2051_6a74_4b8b_104d_ec006cd7075f["arraify"]
  7fa76fc1_cb1b_cf98_0900_1217276f6616 --> 19ce2051_6a74_4b8b_104d_ec006cd7075f
  23a2e685_f919_9578_27ba_bde71c122058["createDebugger"]
  7fa76fc1_cb1b_cf98_0900_1217276f6616 --> 23a2e685_f919_9578_27ba_bde71c122058
  a4adb1a7_cf54_091f_eb63_8217e684a8e1["normalizePath"]
  7fa76fc1_cb1b_cf98_0900_1217276f6616 --> a4adb1a7_cf54_091f_eb63_8217e684a8e1
  a9bd45ce_8339_2b77_7543_41c306ebdb02["tryStatSync"]
  7fa76fc1_cb1b_cf98_0900_1217276f6616 --> a9bd45ce_8339_2b77_7543_41c306ebdb02
  7da774f9_eca5_d54e_6e01_6bee7d460a2b["config.ts"]
  7fa76fc1_cb1b_cf98_0900_1217276f6616 --> 7da774f9_eca5_d54e_6e01_6bee7d460a2b
  fe7f5962_2950_2a13_2dfe_8f14ff3bb0d8["UserConfig"]
  7fa76fc1_cb1b_cf98_0900_1217276f6616 --> fe7f5962_2950_2a13_2dfe_8f14ff3bb0d8
  650c6af9_c8d9_b67f_9149_9fa38a8587ab["UserConfig"]
  7fa76fc1_cb1b_cf98_0900_1217276f6616 --> 650c6af9_c8d9_b67f_9149_9fa38a8587ab
  e6032fbc_44cf_58d6_868d_dd15106c18c5["node:fs"]
  7fa76fc1_cb1b_cf98_0900_1217276f6616 --> e6032fbc_44cf_58d6_868d_dd15106c18c5
  51e96894_3556_ed5c_1ede_97d449867adf["node:path"]
  7fa76fc1_cb1b_cf98_0900_1217276f6616 --> 51e96894_3556_ed5c_1ede_97d449867adf
  10809968_066c_58db_f8b4_cb0464da805e["node:util"]
  7fa76fc1_cb1b_cf98_0900_1217276f6616 --> 10809968_066c_58db_f8b4_cb0464da805e
  b07972ac_8c4d_b15f_6d10_c9b1d678ed16["dotenv-expand"]
  7fa76fc1_cb1b_cf98_0900_1217276f6616 --> b07972ac_8c4d_b15f_6d10_c9b1d678ed16
  bff4f846_ab01_b5ba_74d4_c1608e434d2c["picocolors"]
  7fa76fc1_cb1b_cf98_0900_1217276f6616 --> bff4f846_ab01_b5ba_74d4_c1608e434d2c
  58760808_888f_c5b0_2f67_88fd0b1efc86["config.spec.ts"]
  58760808_888f_c5b0_2f67_88fd0b1efc86 --> 7fa76fc1_cb1b_cf98_0900_1217276f6616
  style 7fa76fc1_cb1b_cf98_0900_1217276f6616 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import fs from 'node:fs'
import path from 'node:path'
import { parseEnv } from 'node:util'
import { type DotenvPopulateInput, expand } from 'dotenv-expand'
import colors from 'picocolors'
import { arraify, createDebugger, normalizePath, tryStatSync } from './utils'
import type { UserConfig } from './config'

const debug = createDebugger('vite:env')

export function getEnvFilesForMode(
  mode: string,
  envDir: string | false,
): string[] {
  if (envDir !== false) {
    return [
      /** default file */ `.env`,
      /** local file */ `.env.local`,
      /** mode file */ `.env.${mode}`,
      /** mode local file */ `.env.${mode}.local`,
    ].map((file) => normalizePath(path.join(envDir, file)))
  }

  return []
}

export function loadEnv(
  mode: string,
  envDir: string | false,
  prefixes: string | string[] = 'VITE_',
): Record<string, string> {
  const start = performance.now()
  const getTime = () => `${(performance.now() - start).toFixed(2)}ms`

  if (mode === 'local') {
    throw new Error(
      `"local" cannot be used as a mode name because it conflicts with ` +
        `the .local postfix for .env files.`,
    )
  }
  prefixes = arraify(prefixes)
  const env: Record<string, string> = {}
  const envFiles = getEnvFilesForMode(mode, envDir)

  debug?.(`loading env files: %O`, envFiles)

  const parsed = Object.fromEntries(
    envFiles.flatMap((filePath) => {
      const stat = tryStatSync(filePath)
      // Support FIFOs (named pipes) for apps like 1Password
      if (!stat || (!stat.isFile() && !stat.isFIFO())) return []

      const parsedEnv = parseEnv(fs.readFileSync(filePath, 'utf-8'))
      return Object.entries(parsedEnv as Record<string, string>)
    }),
  )

  debug?.(`env files loaded in ${getTime()}`)

  // test NODE_ENV override before expand as otherwise process.env.NODE_ENV would override this
  if (parsed.NODE_ENV && process.env.VITE_USER_NODE_ENV === undefined) {
    process.env.VITE_USER_NODE_ENV = parsed.NODE_ENV
  }
  // support BROWSER and BROWSER_ARGS env variables
  if (parsed.BROWSER && process.env.BROWSER === undefined) {
    process.env.BROWSER = parsed.BROWSER
  }
  if (parsed.BROWSER_ARGS && process.env.BROWSER_ARGS === undefined) {
    process.env.BROWSER_ARGS = parsed.BROWSER_ARGS
  }

  // let environment variables use each other. make a copy of `process.env` so that `dotenv-expand`
  // doesn't re-assign the expanded values to the global `process.env`.
  const processEnv = { ...process.env } as DotenvPopulateInput
  expand({ parsed, processEnv })

  // only keys that start with prefix are exposed to client
  for (const [key, value] of Object.entries(parsed)) {
    if (prefixes.some((prefix) => key.startsWith(prefix))) {
      env[key] = value
    }
  }

  // check if there are actual env variables starting with VITE_*
  // these are typically provided inline and should be prioritized
  for (const key in process.env) {
    if (prefixes.some((prefix) => key.startsWith(prefix))) {
      env[key] = process.env[key] as string
    }
  }

  debug?.(`using resolved env: %O`, env)

  return env
}

export function resolveEnvPrefix({
  envPrefix = 'VITE_',
}: UserConfig): string[] {
  envPrefix = arraify(envPrefix)
  if (envPrefix.includes('')) {
    throw new Error(
      `envPrefix option contains value '', which could lead unexpected exposure of sensitive information.`,
    )
  }
  if (envPrefix.some((prefix) => /\s/.test(prefix))) {
    // eslint-disable-next-line no-console
    console.warn(
      colors.yellow(
        `[vite] Warning: envPrefix option contains values with whitespace, which does not work in practice.`,
      ),
    )
  }
  return envPrefix
}

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does env.ts do?
env.ts is a source file in the vite codebase, written in typescript. It belongs to the ViteCore domain, ConfigEngine subdomain.
What functions are defined in env.ts?
env.ts defines 3 function(s): getEnvFilesForMode, loadEnv, resolveEnvPrefix.
What does env.ts depend on?
env.ts imports 13 module(s): UserConfig, UserConfig, arraify, config.ts, createDebugger, dotenv-expand, node:fs, node:path, and 5 more.
What files import env.ts?
env.ts is imported by 6 file(s): config.spec.ts, config.ts, env.spec.ts, hmr.ts, html.ts, index.ts.
Where is env.ts in the architecture?
env.ts is located at packages/vite/src/node/env.ts (domain: ViteCore, subdomain: ConfigEngine, directory: packages/vite/src/node).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free