Home / Function/ generateCodeFrame() — vite Function Reference

generateCodeFrame() — vite Function Reference

Architecture documentation for the generateCodeFrame() function in utils.ts from the vite codebase.

Function typescript ViteCore ConfigEngine calls 2 called by 12

Entity Profile

Dependency Diagram

graph TD
  310ed049_c1b4_c917_b399_81bab290e5a2["generateCodeFrame()"]
  031bc221_67a8_c579_f2bf_bb30a08beeb2["utils.ts"]
  310ed049_c1b4_c917_b399_81bab290e5a2 -->|defined in| 031bc221_67a8_c579_f2bf_bb30a08beeb2
  bfb7f285_fb76_2283_e68f_d806d799034d["runPostCSS()"]
  bfb7f285_fb76_2283_e68f_d806d799034d -->|calls| 310ed049_c1b4_c917_b399_81bab290e5a2
  297b7917_5996_b011_26d1_5bce9b0e6724["minifyCSS()"]
  297b7917_5996_b011_26d1_5bce9b0e6724 -->|calls| 310ed049_c1b4_c917_b399_81bab290e5a2
  cc40a51d_2143_be43_f128_4841bfa5e9d3["compileLightningCSS()"]
  cc40a51d_2143_be43_f128_4841bfa5e9d3 -->|calls| 310ed049_c1b4_c917_b399_81bab290e5a2
  b8ca3f28_b158_ad31_7286_a8412bc7e4cd["prettifyMessage()"]
  b8ca3f28_b158_ad31_7286_a8412bc7e4cd -->|calls| 310ed049_c1b4_c917_b399_81bab290e5a2
  8471f802_a431_7a1b_3e06_15a27483a128["formatParseError()"]
  8471f802_a431_7a1b_3e06_15a27483a128 -->|calls| 310ed049_c1b4_c917_b399_81bab290e5a2
  a1fc1de5_905b_efe7_d960_3597604fbdfe["importAnalysisPlugin()"]
  a1fc1de5_905b_efe7_d960_3597604fbdfe -->|calls| 310ed049_c1b4_c917_b399_81bab290e5a2
  6d315957_5b5c_845c_10c4_b8cb46bc58eb["buildImportAnalysisPlugin()"]
  6d315957_5b5c_845c_10c4_b8cb46bc58eb -->|calls| 310ed049_c1b4_c917_b399_81bab290e5a2
  047aa0d5_25bc_9eb0_8c41_99a2be202642["transformWithOxc()"]
  047aa0d5_25bc_9eb0_8c41_99a2be202642 -->|calls| 310ed049_c1b4_c917_b399_81bab290e5a2
  087bc308_b1da_3dca_40fd_0cf762502823["terserPlugin()"]
  087bc308_b1da_3dca_40fd_0cf762502823 -->|calls| 310ed049_c1b4_c917_b399_81bab290e5a2
  9282ac90_d29c_cf0b_bdf8_00ac31e9ba20["_formatLog()"]
  9282ac90_d29c_cf0b_bdf8_00ac31e9ba20 -->|calls| 310ed049_c1b4_c917_b399_81bab290e5a2
  d5b72255_236c_e62d_9524_5d6892ba1d7f["ssrManifestPlugin()"]
  d5b72255_236c_e62d_9524_5d6892ba1d7f -->|calls| 310ed049_c1b4_c917_b399_81bab290e5a2
  027164d9_0d90_6487_4866_e21cb9d3e6c1["ssrTransformScript()"]
  027164d9_0d90_6487_4866_e21cb9d3e6c1 -->|calls| 310ed049_c1b4_c917_b399_81bab290e5a2
  c4001e12_0e5a_8a12_00e1_eeea1d98ad3d["posToNumber()"]
  310ed049_c1b4_c917_b399_81bab290e5a2 -->|calls| c4001e12_0e5a_8a12_00e1_eeea1d98ad3d
  style 310ed049_c1b4_c917_b399_81bab290e5a2 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/utils.ts lines 488–566

export function generateCodeFrame(
  source: string,
  start: number | Pos = 0,
  end?: number | Pos,
): string {
  start = Math.max(posToNumber(source, start), 0)
  end = Math.min(
    end !== undefined ? posToNumber(source, end) : start,
    source.length,
  )
  const lastPosLine =
    end !== undefined
      ? numberToPos(source, end).line
      : numberToPos(source, start).line + range
  const lineNumberWidth = Math.max(3, String(lastPosLine).length + 1)
  const lines = source.split(splitRE)
  let count = 0
  const res: string[] = []
  for (let i = 0; i < lines.length; i++) {
    count += lines[i].length
    if (count >= start) {
      for (let j = i - range; j <= i + range || end > count; j++) {
        if (j < 0 || j >= lines.length) continue
        const line = j + 1
        const lineLength = lines[j].length
        const pad = Math.max(start - (count - lineLength), 0)
        const underlineLength = Math.max(
          1,
          end > count ? lineLength - pad : end - start,
        )

        let displayLine = lines[j]
        let underlinePad = pad
        if (lineLength > MAX_DISPLAY_LEN) {
          let startIdx = 0
          if (j === i) {
            if (underlineLength > MAX_DISPLAY_LEN) {
              startIdx = pad
            } else {
              const center = pad + Math.floor(underlineLength / 2)
              startIdx = Math.max(0, center - Math.floor(MAX_DISPLAY_LEN / 2))
            }
            underlinePad =
              Math.max(0, pad - startIdx) + (startIdx > 0 ? ELLIPSIS.length : 0)
          }
          const prefix = startIdx > 0 ? ELLIPSIS : ''
          const suffix = lineLength - startIdx > MAX_DISPLAY_LEN ? ELLIPSIS : ''
          const sliceLen = MAX_DISPLAY_LEN - prefix.length - suffix.length
          displayLine =
            prefix + displayLine.slice(startIdx, startIdx + sliceLen) + suffix
        }
        res.push(
          `${line}${' '.repeat(lineNumberWidth - String(line).length)}|  ${displayLine}`,
        )
        if (j === i) {
          // push underline
          const underline = '^'.repeat(
            Math.min(underlineLength, MAX_DISPLAY_LEN),
          )
          res.push(
            `${' '.repeat(lineNumberWidth)}|  ` +
              ' '.repeat(underlinePad) +
              underline,
          )
        } else if (j > i) {
          if (end > count) {
            const length = Math.max(Math.min(end - count, lineLength), 1)
            const underline = '^'.repeat(Math.min(length, MAX_DISPLAY_LEN))
            res.push(`${' '.repeat(lineNumberWidth)}|  ` + underline)
          }
          count += lineLength + 1
        }
      }
      break
    }
    count++
  }
  return res.join('\n')
}

Domain

Subdomains

Frequently Asked Questions

What does generateCodeFrame() do?
generateCodeFrame() is a function in the vite codebase, defined in packages/vite/src/node/utils.ts.
Where is generateCodeFrame() defined?
generateCodeFrame() is defined in packages/vite/src/node/utils.ts at line 488.
What does generateCodeFrame() call?
generateCodeFrame() calls 2 function(s): numberToPos, posToNumber.
What calls generateCodeFrame()?
generateCodeFrame() is called by 12 function(s): _formatLog, buildImportAnalysisPlugin, compileLightningCSS, formatParseError, importAnalysisPlugin, minifyCSS, prettifyMessage, runPostCSS, and 4 more.

Analyze Your Own Codebase

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

Try Supermodel Free