Home / File/ publicDir.ts — vite Source File

publicDir.ts — vite Source File

Architecture documentation for publicDir.ts, a typescript file in the vite codebase. 10 imports, 7 dependents.

File typescript ViteCore ConfigEngine 10 imports 7 dependents 3 functions

Entity Profile

Dependency Diagram

graph LR
  c6b6e85a_866b_a3e1_08e0_f9aa4550a2d3["publicDir.ts"]
  abfc9e70_3c15_b3f0_a595_3cf27afb7e64["utils.ts"]
  c6b6e85a_866b_a3e1_08e0_f9aa4550a2d3 --> abfc9e70_3c15_b3f0_a595_3cf27afb7e64
  10b9dea8_362c_1af2_93be_afa4dd9aed9e["cleanUrl"]
  c6b6e85a_866b_a3e1_08e0_f9aa4550a2d3 --> 10b9dea8_362c_1af2_93be_afa4dd9aed9e
  1a3bec7b_1a11_316f_5831_a0535b829bbf["withTrailingSlash"]
  c6b6e85a_866b_a3e1_08e0_f9aa4550a2d3 --> 1a3bec7b_1a11_316f_5831_a0535b829bbf
  7da774f9_eca5_d54e_6e01_6bee7d460a2b["config.ts"]
  c6b6e85a_866b_a3e1_08e0_f9aa4550a2d3 --> 7da774f9_eca5_d54e_6e01_6bee7d460a2b
  eb5604c2_58e1_1c00_5a1a_5d97ea5236ad["ResolvedConfig"]
  c6b6e85a_866b_a3e1_08e0_f9aa4550a2d3 --> eb5604c2_58e1_1c00_5a1a_5d97ea5236ad
  031bc221_67a8_c579_f2bf_bb30a08beeb2["utils.ts"]
  c6b6e85a_866b_a3e1_08e0_f9aa4550a2d3 --> 031bc221_67a8_c579_f2bf_bb30a08beeb2
  a4adb1a7_cf54_091f_eb63_8217e684a8e1["normalizePath"]
  c6b6e85a_866b_a3e1_08e0_f9aa4550a2d3 --> a4adb1a7_cf54_091f_eb63_8217e684a8e1
  ed12cde8_6daa_948e_e774_02217b503b77["recursiveReaddir"]
  c6b6e85a_866b_a3e1_08e0_f9aa4550a2d3 --> ed12cde8_6daa_948e_e774_02217b503b77
  a9bd45ce_8339_2b77_7543_41c306ebdb02["tryStatSync"]
  c6b6e85a_866b_a3e1_08e0_f9aa4550a2d3 --> a9bd45ce_8339_2b77_7543_41c306ebdb02
  51e96894_3556_ed5c_1ede_97d449867adf["node:path"]
  c6b6e85a_866b_a3e1_08e0_f9aa4550a2d3 --> 51e96894_3556_ed5c_1ede_97d449867adf
  e71b94ef_3010_e358_13d8_f3b3acb0a268["asset.ts"]
  e71b94ef_3010_e358_13d8_f3b3acb0a268 --> c6b6e85a_866b_a3e1_08e0_f9aa4550a2d3
  c3eb47df_971b_0616_6c9f_29b3ded72224["css.ts"]
  c3eb47df_971b_0616_6c9f_29b3ded72224 --> c6b6e85a_866b_a3e1_08e0_f9aa4550a2d3
  f8fe0737_718a_5509_b722_473f207d5906["html.ts"]
  f8fe0737_718a_5509_b722_473f207d5906 --> c6b6e85a_866b_a3e1_08e0_f9aa4550a2d3
  5a7b98e4_4eb1_dfca_508b_2d43e2a077e6["importAnalysis.ts"]
  5a7b98e4_4eb1_dfca_508b_2d43e2a077e6 --> c6b6e85a_866b_a3e1_08e0_f9aa4550a2d3
  style c6b6e85a_866b_a3e1_08e0_f9aa4550a2d3 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import path from 'node:path'
import { cleanUrl, withTrailingSlash } from '../shared/utils'
import type { ResolvedConfig } from './config'
import {
  ERR_SYMLINK_IN_RECURSIVE_READDIR,
  normalizePath,
  recursiveReaddir,
  tryStatSync,
} from './utils'

const publicFilesMap = new WeakMap<ResolvedConfig, Set<string>>()

export async function initPublicFiles(
  config: ResolvedConfig,
): Promise<Set<string> | undefined> {
  let fileNames: string[]
  try {
    fileNames = await recursiveReaddir(config.publicDir)
  } catch (e) {
    if (e.code === ERR_SYMLINK_IN_RECURSIVE_READDIR) {
      return
    }
    throw e
  }
  const publicFiles = new Set(
    fileNames.map((fileName) => fileName.slice(config.publicDir.length)),
  )
  publicFilesMap.set(config, publicFiles)
  return publicFiles
}

function getPublicFiles(config: ResolvedConfig): Set<string> | undefined {
  return publicFilesMap.get(config)
}

export function checkPublicFile(
  url: string,
  config: ResolvedConfig,
): string | undefined {
  // note if the file is in /public, the resolver would have returned it
  // as-is so it's not going to be a fully resolved path.
  const { publicDir } = config
  if (!publicDir || url[0] !== '/') {
    return
  }

  const fileName = cleanUrl(url)

  // short-circuit if we have an in-memory publicFiles cache
  const publicFiles = getPublicFiles(config)
  if (publicFiles) {
    return publicFiles.has(fileName)
      ? normalizePath(path.join(publicDir, fileName))
      : undefined
  }

  const publicFile = normalizePath(path.join(publicDir, fileName))
  if (!publicFile.startsWith(withTrailingSlash(publicDir))) {
    // can happen if URL starts with '../'
    return
  }

  return tryStatSync(publicFile)?.isFile() ? publicFile : undefined
}

Domain

Subdomains

Frequently Asked Questions

What does publicDir.ts do?
publicDir.ts is a source file in the vite codebase, written in typescript. It belongs to the ViteCore domain, ConfigEngine subdomain.
What functions are defined in publicDir.ts?
publicDir.ts defines 3 function(s): checkPublicFile, getPublicFiles, initPublicFiles.
What does publicDir.ts depend on?
publicDir.ts imports 10 module(s): ResolvedConfig, cleanUrl, config.ts, node:path, normalizePath, recursiveReaddir, tryStatSync, utils.ts, and 2 more.
What files import publicDir.ts?
publicDir.ts is imported by 7 file(s): asset.ts, css.ts, html.ts, importAnalysis.ts, index.ts, indexHtml.ts, transformRequest.ts.
Where is publicDir.ts in the architecture?
publicDir.ts is located at packages/vite/src/node/publicDir.ts (domain: ViteCore, subdomain: ConfigEngine, directory: packages/vite/src/node).

Analyze Your Own Codebase

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

Try Supermodel Free