Home / File/ warmup.ts — vite Source File

warmup.ts — vite Source File

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

File typescript ViteCore ConfigEngine 10 imports 1 dependents 5 functions

Entity Profile

Dependency Diagram

graph LR
  c4f5fac4_8500_5acd_037b_a0062bdbd152["warmup.ts"]
  545df65b_7f67_94d3_e2e8_a592d5e64b8f["constants.ts"]
  c4f5fac4_8500_5acd_037b_a0062bdbd152 --> 545df65b_7f67_94d3_e2e8_a592d5e64b8f
  031bc221_67a8_c579_f2bf_bb30a08beeb2["utils.ts"]
  c4f5fac4_8500_5acd_037b_a0062bdbd152 --> 031bc221_67a8_c579_f2bf_bb30a08beeb2
  a4adb1a7_cf54_091f_eb63_8217e684a8e1["normalizePath"]
  c4f5fac4_8500_5acd_037b_a0062bdbd152 --> a4adb1a7_cf54_091f_eb63_8217e684a8e1
  8377ae20_ffba_2f9c_bded_58742b7f1c3b["index.ts"]
  c4f5fac4_8500_5acd_037b_a0062bdbd152 --> 8377ae20_ffba_2f9c_bded_58742b7f1c3b
  7916c84f_5621_2b3b_d220_a171ebce997f["environment.ts"]
  c4f5fac4_8500_5acd_037b_a0062bdbd152 --> 7916c84f_5621_2b3b_d220_a171ebce997f
  f634223d_ed8e_a65b_08f8_a839ec17994a["DevEnvironment"]
  c4f5fac4_8500_5acd_037b_a0062bdbd152 --> f634223d_ed8e_a65b_08f8_a839ec17994a
  a09ff191_7c83_bdcd_30f1_b4e129910bf6["promises"]
  c4f5fac4_8500_5acd_037b_a0062bdbd152 --> a09ff191_7c83_bdcd_30f1_b4e129910bf6
  51e96894_3556_ed5c_1ede_97d449867adf["node:path"]
  c4f5fac4_8500_5acd_037b_a0062bdbd152 --> 51e96894_3556_ed5c_1ede_97d449867adf
  bff4f846_ab01_b5ba_74d4_c1608e434d2c["picocolors"]
  c4f5fac4_8500_5acd_037b_a0062bdbd152 --> bff4f846_ab01_b5ba_74d4_c1608e434d2c
  f768205a_533a_2be3_ec95_10f7f47d71ce["tinyglobby"]
  c4f5fac4_8500_5acd_037b_a0062bdbd152 --> f768205a_533a_2be3_ec95_10f7f47d71ce
  7916c84f_5621_2b3b_d220_a171ebce997f["environment.ts"]
  7916c84f_5621_2b3b_d220_a171ebce997f --> c4f5fac4_8500_5acd_037b_a0062bdbd152
  style c4f5fac4_8500_5acd_037b_a0062bdbd152 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import fs from 'node:fs/promises'
import path from 'node:path'
import colors from 'picocolors'
import { glob, isDynamicPattern } from 'tinyglobby'
import { FS_PREFIX } from '../constants'
import { normalizePath } from '../utils'
import type { ViteDevServer } from '../index'
import type { DevEnvironment } from './environment'

export function warmupFiles(
  server: ViteDevServer,
  environment: DevEnvironment,
): void {
  const { root } = server.config
  mapFiles(environment.config.dev.warmup, root).then((files) => {
    for (const file of files) {
      warmupFile(server, environment, file)
    }
  })
}

async function warmupFile(
  server: ViteDevServer,
  environment: DevEnvironment,
  file: string,
) {
  // transform html with the `transformIndexHtml` hook as Vite internals would
  // pre-transform the imported JS modules linked. this may cause `transformIndexHtml`
  // plugins to be executed twice, but that's probably fine.
  if (file.endsWith('.html')) {
    const url = htmlFileToUrl(file, server.config.root)
    if (url) {
      try {
        const html = await fs.readFile(file, 'utf-8')
        await server.transformIndexHtml(url, html)
      } catch (e) {
        // Unexpected error, log the issue but avoid an unhandled exception
        environment.logger.error(
          `Pre-transform error (${colors.cyan(file)}): ${e.message}`,
          {
            error: e,
            timestamp: true,
          },
        )
      }
    }
  }
  // for other files, pass it through `transformRequest` with warmup
  else {
    const url = fileToUrl(file, server.config.root)
    await environment.warmupRequest(url)
  }
}

function htmlFileToUrl(file: string, root: string) {
  const url = path.relative(root, file)
  // out of root, ignore file
  if (url[0] === '.') return
  // file within root, create root-relative url
  return '/' + normalizePath(url)
}

function fileToUrl(file: string, root: string) {
  const url = path.relative(root, file)
  // out of root, use /@fs/ prefix
  if (url[0] === '.') {
    return path.posix.join(FS_PREFIX, normalizePath(file))
  }
  // file within root, create root-relative url
  return '/' + normalizePath(url)
}

async function mapFiles(files: string[], root: string) {
  if (!files.length) return []

  const result: string[] = []
  const globs: string[] = []
  for (const file of files) {
    if (isDynamicPattern(file)) {
      globs.push(file)
    } else {
      if (path.isAbsolute(file)) {
        result.push(file)
      } else {
        result.push(path.resolve(root, file))
      }
    }
  }
  if (globs.length) {
    result.push(
      ...(await glob(globs, {
        absolute: true,
        cwd: root,
        expandDirectories: false,
        ignore: ['**/.git/**', '**/node_modules/**'],
      })),
    )
  }
  return result
}

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does warmup.ts do?
warmup.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 warmup.ts?
warmup.ts defines 5 function(s): fileToUrl, htmlFileToUrl, mapFiles, warmupFile, warmupFiles.
What does warmup.ts depend on?
warmup.ts imports 10 module(s): DevEnvironment, constants.ts, environment.ts, index.ts, node:path, normalizePath, picocolors, promises, and 2 more.
What files import warmup.ts?
warmup.ts is imported by 1 file(s): environment.ts.
Where is warmup.ts in the architecture?
warmup.ts is located at packages/vite/src/node/server/warmup.ts (domain: ViteCore, subdomain: ConfigEngine, directory: packages/vite/src/node/server).

Analyze Your Own Codebase

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

Try Supermodel Free