Home / Function/ devHtmlHook() — vite Function Reference

devHtmlHook() — vite Function Reference

Architecture documentation for the devHtmlHook() function in indexHtml.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  c7929a5b_9791_180a_9c0b_4f479fb4cf3f["devHtmlHook()"]
  3f56d5b2_9fca_532f_3bfc_6bfb2be77015["indexHtml.ts"]
  c7929a5b_9791_180a_9c0b_4f479fb4cf3f -->|defined in| 3f56d5b2_9fca_532f_3bfc_6bfb2be77015
  0c17692e_44e4_448d_4a57_1d0018af1182["wrapId()"]
  c7929a5b_9791_180a_9c0b_4f479fb4cf3f -->|calls| 0c17692e_44e4_448d_4a57_1d0018af1182
  c9db8630_93b3_267d_8e26_8b62626a11ca["joinUrlSegments()"]
  c7929a5b_9791_180a_9c0b_4f479fb4cf3f -->|calls| c9db8630_93b3_267d_8e26_8b62626a11ca
  10b9dea8_362c_1af2_93be_afa4dd9aed9e["cleanUrl()"]
  c7929a5b_9791_180a_9c0b_4f479fb4cf3f -->|calls| 10b9dea8_362c_1af2_93be_afa4dd9aed9e
  a4adb1a7_cf54_091f_eb63_8217e684a8e1["normalizePath()"]
  c7929a5b_9791_180a_9c0b_4f479fb4cf3f -->|calls| a4adb1a7_cf54_091f_eb63_8217e684a8e1
  bf28c3ed_1de7_8026_dca5_48657723b46c["addToHTMLProxyCache()"]
  c7929a5b_9791_180a_9c0b_4f479fb4cf3f -->|calls| bf28c3ed_1de7_8026_dca5_48657723b46c
  29f999b6_18cf_5f75_7b27_cf12b0774ee0["preTransformRequest()"]
  c7929a5b_9791_180a_9c0b_4f479fb4cf3f -->|calls| 29f999b6_18cf_5f75_7b27_cf12b0774ee0
  5800888e_ad6f_73a4_812a_c831c6a9eebf["traverseHtml()"]
  c7929a5b_9791_180a_9c0b_4f479fb4cf3f -->|calls| 5800888e_ad6f_73a4_812a_c831c6a9eebf
  0a5ccc35_5940_e97a_b8cf_fa02cbb6f117["nodeIsElement()"]
  c7929a5b_9791_180a_9c0b_4f479fb4cf3f -->|calls| 0a5ccc35_5940_e97a_b8cf_fa02cbb6f117
  a37f0802_91ab_f008_65a6_1e79c811c37f["getScriptInfo()"]
  c7929a5b_9791_180a_9c0b_4f479fb4cf3f -->|calls| a37f0802_91ab_f008_65a6_1e79c811c37f
  4a1bdd32_bdc1_e08e_2015_af6c39dd7bd4["removeViteIgnoreAttr()"]
  c7929a5b_9791_180a_9c0b_4f479fb4cf3f -->|calls| 4a1bdd32_bdc1_e08e_2015_af6c39dd7bd4
  98ce5fcf_1d4d_c6f5_6eed_3bde652f7be3["processNodeUrl()"]
  c7929a5b_9791_180a_9c0b_4f479fb4cf3f -->|calls| 98ce5fcf_1d4d_c6f5_6eed_3bde652f7be3
  370852c5_4762_1633_45af_f5ae49f7fc4e["overwriteAttrValue()"]
  c7929a5b_9791_180a_9c0b_4f479fb4cf3f -->|calls| 370852c5_4762_1633_45af_f5ae49f7fc4e
  454378ea_32bf_8bfb_be5d_8095a717ea09["extractImportExpressionFromClassicScript()"]
  c7929a5b_9791_180a_9c0b_4f479fb4cf3f -->|calls| 454378ea_32bf_8bfb_be5d_8095a717ea09
  style c7929a5b_9791_180a_9c0b_4f479fb4cf3f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/server/middlewares/indexHtml.ts lines 201–440

const devHtmlHook: IndexHtmlTransformHook = async (
  html,
  { path: htmlPath, filename, server, originalUrl },
) => {
  const { config, watcher } = server!
  const base = config.base || '/'
  const decodedBase = config.decodedBase || '/'

  let proxyModulePath: string
  let proxyModuleUrl: string

  const trailingSlash = htmlPath.endsWith('/')
  if (!trailingSlash && fs.existsSync(filename)) {
    proxyModulePath = htmlPath
    proxyModuleUrl = proxyModulePath
  } else {
    // There are users of vite.transformIndexHtml calling it with url '/'
    // for SSR integrations #7993, filename is root for this case
    // A user may also use a valid name for a virtual html file
    // Mark the path as virtual in both cases so sourcemaps aren't processed
    // and ids are properly handled
    const validPath = `${htmlPath}${trailingSlash ? 'index.html' : ''}`
    proxyModulePath = `\0${validPath}`
    proxyModuleUrl = wrapId(proxyModulePath)
  }
  proxyModuleUrl = joinUrlSegments(decodedBase, proxyModuleUrl)

  const s = new MagicString(html)
  let inlineModuleIndex = -1
  // The key to the proxyHtml cache is decoded, as it will be compared
  // against decoded URLs by the HTML plugins.
  const proxyCacheUrl = decodeURI(
    cleanUrl(proxyModulePath).replace(normalizePath(config.root), ''),
  )
  const styleUrl: AssetNode[] = []
  const inlineStyles: InlineStyleAttribute[] = []
  const inlineModulePaths: string[] = []

  const addInlineModule = (
    node: DefaultTreeAdapterMap['element'],
    ext: 'js',
  ) => {
    inlineModuleIndex++

    const contentNode = node.childNodes[0] as DefaultTreeAdapterMap['textNode']

    const code = contentNode.value

    let map: SourceMapInput | undefined
    if (proxyModulePath[0] !== '\0') {
      map = new MagicString(html)
        .snip(
          contentNode.sourceCodeLocation!.startOffset,
          contentNode.sourceCodeLocation!.endOffset,
        )
        .generateMap({ hires: 'boundary' })
      map.sources = [filename]
      map.file = filename
    }

    // add HTML Proxy to Map
    addToHTMLProxyCache(config, proxyCacheUrl, inlineModuleIndex, { code, map })

    // inline js module. convert to src="proxy" (dev only, base is never relative)
    const modulePath = `${proxyModuleUrl}?html-proxy&index=${inlineModuleIndex}.${ext}`
    inlineModulePaths.push(modulePath)

    s.update(
      node.sourceCodeLocation!.startOffset,
      node.sourceCodeLocation!.endOffset,
      `<script type="module" src="${modulePath}"></script>`,
    )
    preTransformRequest(server!, modulePath, decodedBase)
  }

  await traverseHtml(html, filename, config.logger.warn, (node) => {
    if (!nodeIsElement(node)) {
      return
    }

    // script tags

Domain

Subdomains

Frequently Asked Questions

What does devHtmlHook() do?
devHtmlHook() is a function in the vite codebase, defined in packages/vite/src/node/server/middlewares/indexHtml.ts.
Where is devHtmlHook() defined?
devHtmlHook() is defined in packages/vite/src/node/server/middlewares/indexHtml.ts at line 201.
What does devHtmlHook() call?
devHtmlHook() calls 22 function(s): addToHTMLProxyCache, cleanUrl, ensureWatchedFile, extractImportExpressionFromClassicScript, findNeedTransformStyleAttribute, get, getCodeWithSourcemap, getHash, and 14 more.

Analyze Your Own Codebase

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

Try Supermodel Free