Home / Function/ _formatLog() — vite Function Reference

_formatLog() — vite Function Reference

Architecture documentation for the _formatLog() function in pluginContainer.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  9282ac90_d29c_cf0b_bdf8_00ac31e9ba20["_formatLog()"]
  c261dae5_9c68_4b7d_8b09_fe13190425fe["PluginContext"]
  9282ac90_d29c_cf0b_bdf8_00ac31e9ba20 -->|defined in| c261dae5_9c68_4b7d_8b09_fe13190425fe
  c7b55e0f_bb87_ef0b_4055_c263cfb95e73["debug()"]
  c7b55e0f_bb87_ef0b_4055_c263cfb95e73 -->|calls| 9282ac90_d29c_cf0b_bdf8_00ac31e9ba20
  6e54f0cf_c38f_43ff_3f57_61498112dbbe["info()"]
  6e54f0cf_c38f_43ff_3f57_61498112dbbe -->|calls| 9282ac90_d29c_cf0b_bdf8_00ac31e9ba20
  00831e67_fe39_7ed4_eed0_e29cb8e15286["warn()"]
  00831e67_fe39_7ed4_eed0_e29cb8e15286 -->|calls| 9282ac90_d29c_cf0b_bdf8_00ac31e9ba20
  fe29bf81_68d5_1c19_144c_b2f9047027e2["error()"]
  fe29bf81_68d5_1c19_144c_b2f9047027e2 -->|calls| 9282ac90_d29c_cf0b_bdf8_00ac31e9ba20
  3c78163f_527c_d054_7003_68d51942d57a["_getCombinedSourcemap()"]
  9282ac90_d29c_cf0b_bdf8_00ac31e9ba20 -->|calls| 3c78163f_527c_d054_7003_68d51942d57a
  7e08b9a5_2650_5312_63d2_5971015349af["numberToPos()"]
  9282ac90_d29c_cf0b_bdf8_00ac31e9ba20 -->|calls| 7e08b9a5_2650_5312_63d2_5971015349af
  310ed049_c1b4_c917_b399_81bab290e5a2["generateCodeFrame()"]
  9282ac90_d29c_cf0b_bdf8_00ac31e9ba20 -->|calls| 310ed049_c1b4_c917_b399_81bab290e5a2
  a4adb1a7_cf54_091f_eb63_8217e684a8e1["normalizePath()"]
  9282ac90_d29c_cf0b_bdf8_00ac31e9ba20 -->|calls| a4adb1a7_cf54_091f_eb63_8217e684a8e1
  fe29bf81_68d5_1c19_144c_b2f9047027e2["error()"]
  9282ac90_d29c_cf0b_bdf8_00ac31e9ba20 -->|calls| fe29bf81_68d5_1c19_144c_b2f9047027e2
  style 9282ac90_d29c_cf0b_bdf8_00ac31e9ba20 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/server/pluginContainer.ts lines 919–1019

  private _formatLog<E extends RollupLog>(
    e: string | E,
    position?: number | { column: number; line: number } | undefined,
  ): E {
    const err = (typeof e === 'string' ? new Error(e) : e) as E
    if (err.pluginCode) {
      return err // The plugin likely called `this.error`
    }
    err.plugin = this._plugin.name
    if (this._activeId && !err.id) err.id = this._activeId
    if (this._activeCode) {
      err.pluginCode = this._activeCode

      // some rollup plugins, e.g. json, sets err.position instead of err.pos
      const pos = position ?? err.pos ?? (err as any).position

      if (pos != null) {
        let errLocation
        try {
          errLocation = numberToPos(this._activeCode, pos)
        } catch (err2) {
          this.environment.logger.error(
            colors.red(
              `Error in error handler:\n${err2.stack || err2.message}\n`,
            ),
            // print extra newline to separate the two errors
            { error: err2 },
          )
          throw err
        }
        err.loc = err.loc || {
          file: err.id,
          ...errLocation,
        }
        err.frame = err.frame || generateCodeFrame(this._activeCode, pos)
      } else if (err.loc) {
        // css preprocessors may report errors in an included file
        if (!err.frame) {
          let code = this._activeCode
          if (err.loc.file) {
            err.id = normalizePath(err.loc.file)
            try {
              code = fs.readFileSync(err.loc.file, 'utf-8')
            } catch {}
          }
          err.frame = generateCodeFrame(code, err.loc)
        }
      } else if ((err as any).line && (err as any).column) {
        err.loc = {
          file: err.id,
          line: (err as any).line,
          column: (err as any).column,
        }
        err.frame = err.frame || generateCodeFrame(this._activeCode, err.loc)
      }

      // TODO: move it to overrides
      if (
        this instanceof TransformPluginContext &&
        typeof err.loc?.line === 'number' &&
        typeof err.loc.column === 'number'
      ) {
        const rawSourceMap = this._getCombinedSourcemap()
        if (rawSourceMap && 'version' in rawSourceMap) {
          const traced = new TraceMap(rawSourceMap as any)
          const { source, line, column } = originalPositionFor(traced, {
            line: Number(err.loc.line),
            column: Number(err.loc.column),
          })
          if (source) {
            err.loc = { file: source, line, column }
          }
        }
      }
    } else if (err.loc) {
      if (!err.frame) {
        let code = err.pluginCode
        if (err.loc.file) {
          err.id = normalizePath(err.loc.file)
          if (!code) {
            try {

Domain

Subdomains

Frequently Asked Questions

What does _formatLog() do?
_formatLog() is a function in the vite codebase, defined in packages/vite/src/node/server/pluginContainer.ts.
Where is _formatLog() defined?
_formatLog() is defined in packages/vite/src/node/server/pluginContainer.ts at line 919.
What does _formatLog() call?
_formatLog() calls 5 function(s): _getCombinedSourcemap, error, generateCodeFrame, normalizePath, numberToPos.
What calls _formatLog()?
_formatLog() is called by 4 function(s): debug, error, info, warn.

Analyze Your Own Codebase

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

Try Supermodel Free