Home / Function/ buildHtmlPlugin() — vite Function Reference

buildHtmlPlugin() — vite Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  39f26be8_b1d9_a756_3043_474687a6bbb7["buildHtmlPlugin()"]
  f8fe0737_718a_5509_b722_473f207d5906["html.ts"]
  39f26be8_b1d9_a756_3043_474687a6bbb7 -->|defined in| f8fe0737_718a_5509_b722_473f207d5906
  b1b40b5b_3e43_2197_dea0_d36389d312a1["resolvePlugins()"]
  b1b40b5b_3e43_2197_dea0_d36389d312a1 -->|calls| 39f26be8_b1d9_a756_3043_474687a6bbb7
  f0ebd795_2bfe_7b80_5eb1_60aa49511704["resolveHtmlTransforms()"]
  39f26be8_b1d9_a756_3043_474687a6bbb7 -->|calls| f0ebd795_2bfe_7b80_5eb1_60aa49511704
  0819abd4_be39_9b71_5bc7_55025fbeb47e["injectCspNonceMetaTagHook()"]
  39f26be8_b1d9_a756_3043_474687a6bbb7 -->|calls| 0819abd4_be39_9b71_5bc7_55025fbeb47e
  f2e3401e_edbf_08a2_0460_cc85afbcd839["preImportMapHook()"]
  39f26be8_b1d9_a756_3043_474687a6bbb7 -->|calls| f2e3401e_edbf_08a2_0460_cc85afbcd839
  74ff6a26_1c5b_a015_895e_a968330cb049["htmlEnvHook()"]
  39f26be8_b1d9_a756_3043_474687a6bbb7 -->|calls| 74ff6a26_1c5b_a015_895e_a968330cb049
  7514180d_f3bd_4f07_5c4e_5911c2a70a4c["injectNonceAttributeTagHook()"]
  39f26be8_b1d9_a756_3043_474687a6bbb7 -->|calls| 7514180d_f3bd_4f07_5c4e_5911c2a70a4c
  6c4a81f2_0b84_113e_b706_96c3d99e6151["postImportMapHook()"]
  39f26be8_b1d9_a756_3043_474687a6bbb7 -->|calls| 6c4a81f2_0b84_113e_b706_96c3d99e6151
  16a00926_f0e9_60f1_3006_9132a6d78745["perEnvironmentState()"]
  39f26be8_b1d9_a756_3043_474687a6bbb7 -->|calls| 16a00926_f0e9_60f1_3006_9132a6d78745
  23257aa1_5e3f_7e6e_1b43_875b139e4ec5["isExternalUrl()"]
  39f26be8_b1d9_a756_3043_474687a6bbb7 -->|calls| 23257aa1_5e3f_7e6e_1b43_875b139e4ec5
  298b86d6_4810_9884_d5fa_3e3077e2206a["isDataUrl()"]
  39f26be8_b1d9_a756_3043_474687a6bbb7 -->|calls| 298b86d6_4810_9884_d5fa_3e3077e2206a
  a4adb1a7_cf54_091f_eb63_8217e684a8e1["normalizePath()"]
  39f26be8_b1d9_a756_3043_474687a6bbb7 -->|calls| a4adb1a7_cf54_091f_eb63_8217e684a8e1
  28465a50_606a_e9ed_0f86_7578ef3217b6["getBaseInHTML()"]
  39f26be8_b1d9_a756_3043_474687a6bbb7 -->|calls| 28465a50_606a_e9ed_0f86_7578ef3217b6
  979ed295_1d80_37c1_156b_2bbf25fc21e4["applyHtmlTransforms()"]
  39f26be8_b1d9_a756_3043_474687a6bbb7 -->|calls| 979ed295_1d80_37c1_156b_2bbf25fc21e4
  style 39f26be8_b1d9_a756_3043_474687a6bbb7 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/plugins/html.ts lines 352–1045

export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
  const [preHooks, normalHooks, postHooks] = resolveHtmlTransforms(
    config.plugins,
  )
  preHooks.unshift(injectCspNonceMetaTagHook(config))
  preHooks.unshift(preImportMapHook(config))
  preHooks.push(htmlEnvHook(config))
  postHooks.push(injectNonceAttributeTagHook(config))
  postHooks.push(postImportMapHook())
  const processedHtml = perEnvironmentState(() => new Map<string, string>())

  const isExcludedUrl = (url: string) =>
    url[0] === '#' || isExternalUrl(url) || isDataUrl(url)

  // Same reason with `htmlInlineProxyPlugin`
  isAsyncScriptMap.set(config, new Map())

  return {
    name: 'vite:build-html',

    transform: {
      filter: { id: /\.html$/ },
      async handler(html, id) {
        id = normalizePath(id)
        const relativeUrlPath = normalizePath(path.relative(config.root, id))
        const publicPath = `/${relativeUrlPath}`
        const publicBase = getBaseInHTML(relativeUrlPath, config)

        const publicToRelative = (filename: string) => publicBase + filename
        const toOutputPublicFilePath = (url: string) =>
          toOutputFilePathInHtml(
            url.slice(1),
            'public',
            relativeUrlPath,
            'html',
            config,
            publicToRelative,
          )
        // Determines true start position for the node, either the < character
        // position, or the newline at the end of the previous line's node.
        const nodeStartWithLeadingWhitespace = (
          node: DefaultTreeAdapterMap['node'],
        ) => {
          const startOffset = node.sourceCodeLocation!.startOffset
          if (startOffset === 0) return 0

          // Gets the offset for the start of the line including the
          // newline trailing the previous node
          const lineStartOffset =
            startOffset - node.sourceCodeLocation!.startCol

          // <previous-line-node></previous-line-node>
          // <target-node></target-node>
          //
          // Here we want to target the newline at the end of the previous line
          // as the start position for our target.
          //
          // <previous-node></previous-node>
          // <doubled-up-node></doubled-up-node><target-node></target-node>
          //
          // However, if there is content between our target node start and the
          // previous newline, we cannot strip it out without risking content deletion.
          let isLineEmpty = false
          try {
            const line = s.slice(Math.max(0, lineStartOffset), startOffset)
            isLineEmpty = !line.trim()
          } catch {
            // magic-string may throw if there's some content removed in the sliced string,
            // which we ignore and assume the line is not empty
          }

          return isLineEmpty ? lineStartOffset : startOffset
        }

        // pre-transform
        html = await applyHtmlTransforms(html, preHooks, this, {
          path: publicPath,
          filename: id,
        })

        let js = ''

Domain

Subdomains

Called By

Frequently Asked Questions

What does buildHtmlPlugin() do?
buildHtmlPlugin() is a function in the vite codebase, defined in packages/vite/src/node/plugins/html.ts.
Where is buildHtmlPlugin() defined?
buildHtmlPlugin() is defined in packages/vite/src/node/plugins/html.ts at line 352.
What does buildHtmlPlugin() call?
buildHtmlPlugin() calls 36 function(s): addToHTMLProxyCache, applyHtmlTransforms, checkPublicFile, cleanUrl, decodeURIIfPossible, encodeURIPath, extractImportExpressionFromClassicScript, findNeedTransformStyleAttribute, and 28 more.
What calls buildHtmlPlugin()?
buildHtmlPlugin() is called by 1 function(s): resolvePlugins.

Analyze Your Own Codebase

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

Try Supermodel Free