Home / File/ utils.ts — vite Source File

utils.ts — vite Source File

Architecture documentation for utils.ts, a typescript file in the vite codebase. 2 imports, 5 dependents.

File typescript ModuleRunner SSRRuntime 2 imports 5 dependents 6 functions

Entity Profile

Dependency Diagram

graph LR
  81939ddb_795d_a69b_d1ea_fb1af459ccc2["utils.ts"]
  abfc9e70_3c15_b3f0_a595_3cf27afb7e64["utils.ts"]
  81939ddb_795d_a69b_d1ea_fb1af459ccc2 --> abfc9e70_3c15_b3f0_a595_3cf27afb7e64
  df30b98b_a7cb_f91e_d760_0d3022bfa053["pathe"]
  81939ddb_795d_a69b_d1ea_fb1af459ccc2 --> df30b98b_a7cb_f91e_d760_0d3022bfa053
  d957f785_adef_de7a_92dc_045f724e6d34["createImportMeta.ts"]
  d957f785_adef_de7a_92dc_045f724e6d34 --> 81939ddb_795d_a69b_d1ea_fb1af459ccc2
  9f67d7a4_f300_a592_a5b0_c8f97c2d3564["evaluatedModules.ts"]
  9f67d7a4_f300_a592_a5b0_c8f97c2d3564 --> 81939ddb_795d_a69b_d1ea_fb1af459ccc2
  29e248d2_9983_1037_00e6_8bcd9ee87840["runner.ts"]
  29e248d2_9983_1037_00e6_8bcd9ee87840 --> 81939ddb_795d_a69b_d1ea_fb1af459ccc2
  d4f6d186_baf8_38ee_e63e_b58f4d86f016["decoder.ts"]
  d4f6d186_baf8_38ee_e63e_b58f4d86f016 --> 81939ddb_795d_a69b_d1ea_fb1af459ccc2
  95cae2f2_ad8c_91c1_5a74_93d939dbc47b["interceptor.ts"]
  95cae2f2_ad8c_91c1_5a74_93d939dbc47b --> 81939ddb_795d_a69b_d1ea_fb1af459ccc2
  style 81939ddb_795d_a69b_d1ea_fb1af459ccc2 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import * as pathe from 'pathe'
import { isWindows } from '../shared/utils'

export const decodeBase64: typeof atob =
  typeof atob !== 'undefined'
    ? atob
    : (str: string) => Buffer.from(str, 'base64').toString('utf-8')

const CHAR_FORWARD_SLASH = 47
const CHAR_BACKWARD_SLASH = 92

const percentRegEx = /%/g
const backslashRegEx = /\\/g
const newlineRegEx = /\n/g
const carriageReturnRegEx = /\r/g
const tabRegEx = /\t/g
const questionRegex = /\?/g
const hashRegex = /#/g

function encodePathChars(filepath: string) {
  if (filepath.indexOf('%') !== -1)
    filepath = filepath.replace(percentRegEx, '%25')
  // In posix, backslash is a valid character in paths:
  if (!isWindows && filepath.indexOf('\\') !== -1)
    filepath = filepath.replace(backslashRegEx, '%5C')
  if (filepath.indexOf('\n') !== -1)
    filepath = filepath.replace(newlineRegEx, '%0A')
  if (filepath.indexOf('\r') !== -1)
    filepath = filepath.replace(carriageReturnRegEx, '%0D')
  if (filepath.indexOf('\t') !== -1)
    filepath = filepath.replace(tabRegEx, '%09')
  return filepath
}

export const posixDirname: (path: string) => string = pathe.dirname
export const posixResolve: (...paths: string[]) => string = pathe.resolve

export function posixPathToFileHref(posixPath: string): string {
  let resolved = posixResolve(posixPath)
  // path.resolve strips trailing slashes so we must add them back
  const filePathLast = posixPath.charCodeAt(posixPath.length - 1)
  if (
    (filePathLast === CHAR_FORWARD_SLASH ||
      (isWindows && filePathLast === CHAR_BACKWARD_SLASH)) &&
    resolved[resolved.length - 1] !== '/'
  )
    resolved += '/'

  // Call encodePathChars first to avoid encoding % again for ? and #.
  resolved = encodePathChars(resolved)

  // Question and hash character should be included in pathname.
  // Therefore, encoding is required to eliminate parsing them in different states.
  // This is done as an optimization to not creating a URL instance and
  // later triggering pathname setter, which impacts performance
  if (resolved.indexOf('?') !== -1)
    resolved = resolved.replace(questionRegex, '%3F')
  if (resolved.indexOf('#') !== -1)
    resolved = resolved.replace(hashRegex, '%23')
  return new URL(`file://${resolved}`).href
}

export function toWindowsPath(path: string): string {
  return path.replace(/\//g, '\\')
}

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does utils.ts do?
utils.ts is a source file in the vite codebase, written in typescript. It belongs to the ModuleRunner domain, SSRRuntime subdomain.
What functions are defined in utils.ts?
utils.ts defines 6 function(s): decodeBase64, encodePathChars, path, paths, posixPathToFileHref, toWindowsPath.
What does utils.ts depend on?
utils.ts imports 2 module(s): pathe, utils.ts.
What files import utils.ts?
utils.ts is imported by 5 file(s): createImportMeta.ts, decoder.ts, evaluatedModules.ts, interceptor.ts, runner.ts.
Where is utils.ts in the architecture?
utils.ts is located at packages/vite/src/module-runner/utils.ts (domain: ModuleRunner, subdomain: SSRRuntime, directory: packages/vite/src/module-runner).

Analyze Your Own Codebase

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

Try Supermodel Free