Home / File/ utils.ts — vite Source File

utils.ts — vite Source File

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

File typescript HMRClient HotRuntime 1 imports 56 dependents 12 functions

Entity Profile

Dependency Diagram

graph LR
  abfc9e70_3c15_b3f0_a595_3cf27afb7e64["utils.ts"]
  ffe942a0_fdea_ef58_0f68_6f1fec25f285["constants.ts"]
  abfc9e70_3c15_b3f0_a595_3cf27afb7e64 --> ffe942a0_fdea_ef58_0f68_6f1fec25f285
  d957f785_adef_de7a_92dc_045f724e6d34["createImportMeta.ts"]
  d957f785_adef_de7a_92dc_045f724e6d34 --> abfc9e70_3c15_b3f0_a595_3cf27afb7e64
  18923f93_cfa8_2d16_b71c_25bd600b0c6a["esmEvaluator.ts"]
  18923f93_cfa8_2d16_b71c_25bd600b0c6a --> abfc9e70_3c15_b3f0_a595_3cf27afb7e64
  9f67d7a4_f300_a592_a5b0_c8f97c2d3564["evaluatedModules.ts"]
  9f67d7a4_f300_a592_a5b0_c8f97c2d3564 --> abfc9e70_3c15_b3f0_a595_3cf27afb7e64
  569b12a9_015e_564e_efd3_205cedee54dd["hmrHandler.ts"]
  569b12a9_015e_564e_efd3_205cedee54dd --> abfc9e70_3c15_b3f0_a595_3cf27afb7e64
  29e248d2_9983_1037_00e6_8bcd9ee87840["runner.ts"]
  29e248d2_9983_1037_00e6_8bcd9ee87840 --> abfc9e70_3c15_b3f0_a595_3cf27afb7e64
  95cae2f2_ad8c_91c1_5a74_93d939dbc47b["interceptor.ts"]
  95cae2f2_ad8c_91c1_5a74_93d939dbc47b --> abfc9e70_3c15_b3f0_a595_3cf27afb7e64
  81939ddb_795d_a69b_d1ea_fb1af459ccc2["utils.ts"]
  81939ddb_795d_a69b_d1ea_fb1af459ccc2 --> abfc9e70_3c15_b3f0_a595_3cf27afb7e64
  b126a12b_207a_1dea_fbf4_d8a321104f39["dev.spec.ts"]
  b126a12b_207a_1dea_fbf4_d8a321104f39 --> abfc9e70_3c15_b3f0_a595_3cf27afb7e64
  bd61be4f_3dcc_2549_1583_12f4a8f96cd1["parse.spec.ts"]
  bd61be4f_3dcc_2549_1583_12f4a8f96cd1 --> abfc9e70_3c15_b3f0_a595_3cf27afb7e64
  3fd9950b_bcb7_a128_9aed_592d2aca94e6["hooks.spec.ts"]
  3fd9950b_bcb7_a128_9aed_592d2aca94e6 --> abfc9e70_3c15_b3f0_a595_3cf27afb7e64
  8e5be4f8_47ed_25d2_b54a_787696793bf1["runnerImport.spec.ts"]
  8e5be4f8_47ed_25d2_b54a_787696793bf1 --> abfc9e70_3c15_b3f0_a595_3cf27afb7e64
  979f8150_4348_7be4_3132_a56e679f89db["utils.spec.ts"]
  979f8150_4348_7be4_3132_a56e679f89db --> abfc9e70_3c15_b3f0_a595_3cf27afb7e64
  7da774f9_eca5_d54e_6e01_6bee7d460a2b["config.ts"]
  7da774f9_eca5_d54e_6e01_6bee7d460a2b --> abfc9e70_3c15_b3f0_a595_3cf27afb7e64
  style abfc9e70_3c15_b3f0_a595_3cf27afb7e64 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { NULL_BYTE_PLACEHOLDER, VALID_ID_PREFIX } from './constants'

export const isWindows: boolean =
  typeof process !== 'undefined' && process.platform === 'win32'

/**
 * Prepend `/@id/` and replace null byte so the id is URL-safe.
 * This is prepended to resolved ids that are not valid browser
 * import specifiers by the importAnalysis plugin.
 */
export function wrapId(id: string): string {
  return id.startsWith(VALID_ID_PREFIX)
    ? id
    : VALID_ID_PREFIX + id.replace('\0', NULL_BYTE_PLACEHOLDER)
}

/**
 * Undo {@link wrapId}'s `/@id/` and null byte replacements.
 */
export function unwrapId(id: string): string {
  return id.startsWith(VALID_ID_PREFIX)
    ? id.slice(VALID_ID_PREFIX.length).replace(NULL_BYTE_PLACEHOLDER, '\0')
    : id
}

const windowsSlashRE = /\\/g
export function slash(p: string): string {
  return p.replace(windowsSlashRE, '/')
}

const postfixRE = /[?#].*$/
export function cleanUrl(url: string): string {
  return url.replace(postfixRE, '')
}

export function splitFileAndPostfix(path: string): {
  file: string
  postfix: string
} {
  const file = cleanUrl(path)
  return { file, postfix: path.slice(file.length) }
}

export function isPrimitive(value: unknown): boolean {
  return !value || (typeof value !== 'object' && typeof value !== 'function')
}

export function withTrailingSlash(path: string): string {
  if (path[path.length - 1] !== '/') {
    return `${path}/`
  }
  return path
}

// eslint-disable-next-line @typescript-eslint/no-empty-function
export const AsyncFunction = async function () {}.constructor as typeof Function

// https://github.com/nodejs/node/issues/43047#issuecomment-1564068099
let asyncFunctionDeclarationPaddingLineCount: number | undefined

export function getAsyncFunctionDeclarationPaddingLineCount(): number {
  if (typeof asyncFunctionDeclarationPaddingLineCount === 'undefined') {
    const body = '/*code*/'
    const source = new AsyncFunction('a', 'b', body).toString()
    asyncFunctionDeclarationPaddingLineCount =
      source.slice(0, source.indexOf(body)).split('\n').length - 1
  }
  return asyncFunctionDeclarationPaddingLineCount
}

export interface PromiseWithResolvers<T> {
  promise: Promise<T>
  resolve: (value: T | PromiseLike<T>) => void
  reject: (reason?: any) => void
}
export function promiseWithResolvers<T>(): PromiseWithResolvers<T> {
  let resolve: any
  let reject: any
  const promise = new Promise<T>((_resolve, _reject) => {
    resolve = _resolve
    reject = _reject
  })
  return { promise, resolve, reject }
}

Domain

Subdomains

Dependencies

Imported By

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 HMRClient domain, HotRuntime subdomain.
What functions are defined in utils.ts?
utils.ts defines 12 function(s): AsyncFunction, cleanUrl, getAsyncFunctionDeclarationPaddingLineCount, isPrimitive, promiseWithResolvers, reason, slash, splitFileAndPostfix, unwrapId, value, and 2 more.
What does utils.ts depend on?
utils.ts imports 1 module(s): constants.ts.
What files import utils.ts?
utils.ts is imported by 56 file(s): asset.ts, assetImportMetaUrl.ts, base.ts, clientInjections.ts, config.ts, createImportMeta.ts, css.ts, dev.spec.ts, and 48 more.
Where is utils.ts in the architecture?
utils.ts is located at packages/vite/src/shared/utils.ts (domain: HMRClient, subdomain: HotRuntime, directory: packages/vite/src/shared).

Analyze Your Own Codebase

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

Try Supermodel Free