Home / Function/ htmlEnvHook() — vite Function Reference

htmlEnvHook() — vite Function Reference

Architecture documentation for the htmlEnvHook() function in html.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  74ff6a26_1c5b_a015_895e_a968330cb049["htmlEnvHook()"]
  f8fe0737_718a_5509_b722_473f207d5906["html.ts"]
  74ff6a26_1c5b_a015_895e_a968330cb049 -->|defined in| f8fe0737_718a_5509_b722_473f207d5906
  39f26be8_b1d9_a756_3043_474687a6bbb7["buildHtmlPlugin()"]
  39f26be8_b1d9_a756_3043_474687a6bbb7 -->|calls| 74ff6a26_1c5b_a015_895e_a968330cb049
  7a53712a_b13b_4bc2_85fc_b4de5cb705fb["createDevHtmlTransformFn()"]
  7a53712a_b13b_4bc2_85fc_b4de5cb705fb -->|calls| 74ff6a26_1c5b_a015_895e_a968330cb049
  3683cd63_6033_ad37_3392_8ecf602fefea["resolveEnvPrefix()"]
  74ff6a26_1c5b_a015_895e_a968330cb049 -->|calls| 3683cd63_6033_ad37_3392_8ecf602fefea
  a4adb1a7_cf54_091f_eb63_8217e684a8e1["normalizePath()"]
  74ff6a26_1c5b_a015_895e_a968330cb049 -->|calls| a4adb1a7_cf54_091f_eb63_8217e684a8e1
  style 74ff6a26_1c5b_a015_895e_a968330cb049 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/plugins/html.ts lines 1209–1253

export function htmlEnvHook(config: ResolvedConfig): IndexHtmlTransformHook {
  const pattern = /%(\S+?)%/g
  const envPrefix = resolveEnvPrefix({ envPrefix: config.envPrefix })
  const env: Record<string, any> = { ...config.env }

  // account for user env defines
  for (const key in config.define) {
    if (key.startsWith(`import.meta.env.`)) {
      const val = config.define[key]
      if (typeof val === 'string') {
        try {
          const parsed = JSON.parse(val)
          env[key.slice(16)] = typeof parsed === 'string' ? parsed : val
        } catch {
          env[key.slice(16)] = val
        }
      } else {
        env[key.slice(16)] = JSON.stringify(val)
      }
    }
  }
  return (html, ctx) => {
    return html.replace(pattern, (text, key) => {
      if (key in env) {
        return env[key]
      } else {
        if (envPrefix.some((prefix) => key.startsWith(prefix))) {
          const relativeHtml = normalizePath(
            path.relative(config.root, ctx.filename),
          )
          config.logger.warn(
            colors.yellow(
              colors.bold(
                `(!) ${text} is not defined in env variables found in /${relativeHtml}. ` +
                  `Is the variable mistyped?`,
              ),
            ),
          )
        }

        return text
      }
    })
  }
}

Domain

Subdomains

Frequently Asked Questions

What does htmlEnvHook() do?
htmlEnvHook() is a function in the vite codebase, defined in packages/vite/src/node/plugins/html.ts.
Where is htmlEnvHook() defined?
htmlEnvHook() is defined in packages/vite/src/node/plugins/html.ts at line 1209.
What does htmlEnvHook() call?
htmlEnvHook() calls 2 function(s): normalizePath, resolveEnvPrefix.
What calls htmlEnvHook()?
htmlEnvHook() is called by 2 function(s): buildHtmlPlugin, createDevHtmlTransformFn.

Analyze Your Own Codebase

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

Try Supermodel Free