Home / Function/ injectToHead() — vite Function Reference

injectToHead() — vite Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  fb5a3d66_7c54_2dc1_57bd_7bba8ad8fdbf["injectToHead()"]
  f8fe0737_718a_5509_b722_473f207d5906["html.ts"]
  fb5a3d66_7c54_2dc1_57bd_7bba8ad8fdbf -->|defined in| f8fe0737_718a_5509_b722_473f207d5906
  39f26be8_b1d9_a756_3043_474687a6bbb7["buildHtmlPlugin()"]
  39f26be8_b1d9_a756_3043_474687a6bbb7 -->|calls| fb5a3d66_7c54_2dc1_57bd_7bba8ad8fdbf
  979ed295_1d80_37c1_156b_2bbf25fc21e4["applyHtmlTransforms()"]
  979ed295_1d80_37c1_156b_2bbf25fc21e4 -->|calls| fb5a3d66_7c54_2dc1_57bd_7bba8ad8fdbf
  bb1d8b71_73c3_31ff_7d80_88303fc49280["serializeTags()"]
  fb5a3d66_7c54_2dc1_57bd_7bba8ad8fdbf -->|calls| bb1d8b71_73c3_31ff_7d80_88303fc49280
  be79b558_1081_1230_d1d0_4753d396fd49["incrementIndent()"]
  fb5a3d66_7c54_2dc1_57bd_7bba8ad8fdbf -->|calls| be79b558_1081_1230_d1d0_4753d396fd49
  3df96a2a_db85_aa8f_52e1_204c48938b88["prependInjectFallback()"]
  fb5a3d66_7c54_2dc1_57bd_7bba8ad8fdbf -->|calls| 3df96a2a_db85_aa8f_52e1_204c48938b88
  style fb5a3d66_7c54_2dc1_57bd_7bba8ad8fdbf fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/plugins/html.ts lines 1453–1487

function injectToHead(
  html: string,
  tags: HtmlTagDescriptor[],
  prepend = false,
) {
  if (tags.length === 0) return html

  if (prepend) {
    // inject as the first element of head
    if (headPrependInjectRE.test(html)) {
      return html.replace(
        headPrependInjectRE,
        (match, p1) => `${match}\n${serializeTags(tags, incrementIndent(p1))}`,
      )
    }
  } else {
    // inject before head close
    if (headInjectRE.test(html)) {
      // respect indentation of head tag
      return html.replace(
        headInjectRE,
        (match, p1) => `${serializeTags(tags, incrementIndent(p1))}${match}`,
      )
    }
    // try to inject before the body tag
    if (bodyPrependInjectRE.test(html)) {
      return html.replace(
        bodyPrependInjectRE,
        (match, p1) => `${serializeTags(tags, p1)}\n${match}`,
      )
    }
  }
  // if no head tag is present, we prepend the tag for both prepend and append
  return prependInjectFallback(html, tags)
}

Domain

Subdomains

Frequently Asked Questions

What does injectToHead() do?
injectToHead() is a function in the vite codebase, defined in packages/vite/src/node/plugins/html.ts.
Where is injectToHead() defined?
injectToHead() is defined in packages/vite/src/node/plugins/html.ts at line 1453.
What does injectToHead() call?
injectToHead() calls 3 function(s): incrementIndent, prependInjectFallback, serializeTags.
What calls injectToHead()?
injectToHead() is called by 2 function(s): applyHtmlTransforms, buildHtmlPlugin.

Analyze Your Own Codebase

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

Try Supermodel Free