Home / Function/ loadAndTransform() — vite Function Reference

loadAndTransform() — vite Function Reference

Architecture documentation for the loadAndTransform() function in transformRequest.ts from the vite codebase.

Function typescript ViteCore ConfigEngine calls 20 called by 1

Entity Profile

Dependency Diagram

graph TD
  bdac5327_5085_933f_41df_0fc270134a38["loadAndTransform()"]
  ee4fcff9_3096_e290_234c_be9d1a2c8a4b["transformRequest.ts"]
  bdac5327_5085_933f_41df_0fc270134a38 -->|defined in| ee4fcff9_3096_e290_234c_be9d1a2c8a4b
  9bd7b32c_5b79_ac16_8541_4f64cc8ce19b["doTransform()"]
  9bd7b32c_5b79_ac16_8541_4f64cc8ce19b -->|calls| bdac5327_5085_933f_41df_0fc270134a38
  0850ad90_f980_60a3_ab1f_b17433109b74["prettifyUrl()"]
  bdac5327_5085_933f_41df_0fc270134a38 -->|calls| 0850ad90_f980_60a3_ab1f_b17433109b74
  10b9dea8_362c_1af2_93be_afa4dd9aed9e["cleanUrl()"]
  bdac5327_5085_933f_41df_0fc270134a38 -->|calls| 10b9dea8_362c_1af2_93be_afa4dd9aed9e
  b5f75f2b_ae88_b9ce_360f_813018b7842e["isFileLoadingAllowed()"]
  bdac5327_5085_933f_41df_0fc270134a38 -->|calls| b5f75f2b_ae88_b9ce_360f_813018b7842e
  f094d39d_cd97_2548_86c3_38902c2f3301["slash()"]
  bdac5327_5085_933f_41df_0fc270134a38 -->|calls| f094d39d_cd97_2548_86c3_38902c2f3301
  14c1c6b3_e4b1_62ce_7196_077ecd00ef57["timeFrom()"]
  bdac5327_5085_933f_41df_0fc270134a38 -->|calls| 14c1c6b3_e4b1_62ce_7196_077ecd00ef57
  0a6fbb70_77d3_9873_8417_b1e4ffba2651["ensureWatchedFile()"]
  bdac5327_5085_933f_41df_0fc270134a38 -->|calls| 0a6fbb70_77d3_9873_8417_b1e4ffba2651
  f9e66df7_e2b5_e8d9_c0f5_06f811971ad2["extractSourcemapFromFile()"]
  bdac5327_5085_933f_41df_0fc270134a38 -->|calls| f9e66df7_e2b5_e8d9_c0f5_06f811971ad2
  2aff86e8_0c9d_22cb_6536_c1321e1aaa1d["isObject()"]
  bdac5327_5085_933f_41df_0fc270134a38 -->|calls| 2aff86e8_0c9d_22cb_6536_c1321e1aaa1d
  be351481_35b7_f392_a229_ac14e1fa7efb["checkPublicFile()"]
  bdac5327_5085_933f_41df_0fc270134a38 -->|calls| be351481_35b7_f392_a229_ac14e1fa7efb
  040aa4cd_f868_dc2c_786d_85d3bbd1b9d9["getModuleTypeFromId()"]
  bdac5327_5085_933f_41df_0fc270134a38 -->|calls| 040aa4cd_f868_dc2c_786d_85d3bbd1b9d9
  c42acd29_f3d2_c03f_de55_553d650a5fbf["throwClosedServerError()"]
  bdac5327_5085_933f_41df_0fc270134a38 -->|calls| c42acd29_f3d2_c03f_de55_553d650a5fbf
  1df76a35_b65f_4f0a_d9fb_00d2905ad5a6["_ensureEntryFromUrl()"]
  bdac5327_5085_933f_41df_0fc270134a38 -->|calls| 1df76a35_b65f_4f0a_d9fb_00d2905ad5a6
  style bdac5327_5085_933f_41df_0fc270134a38 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/server/transformRequest.ts lines 236–439

async function loadAndTransform(
  environment: DevEnvironment,
  id: string,
  url: string,
  options: TransformOptionsInternal,
  timestamp: number,
  mod?: EnvironmentModuleNode,
  resolved?: PartialResolvedId,
) {
  const { config, pluginContainer, logger } = environment
  const prettyUrl =
    debugLoad || debugTransform ? prettifyUrl(url, config.root) : ''

  const moduleGraph = environment.moduleGraph

  if (options.allowId && !options.allowId(id)) {
    const err: any = new Error(`Denied ID ${id}`)
    err.code = ERR_DENIED_ID
    err.id = id
    throw err
  }

  let code: string | null = null
  let map: SourceDescription['map'] = null
  let moduleType: ModuleType | undefined

  // load
  const loadStart = debugLoad ? performance.now() : 0
  const loadResult = await pluginContainer.load(id)

  if (loadResult == null) {
    const file = cleanUrl(id)

    // try fallback loading it from fs as string
    // if the file is a binary, there should be a plugin that already loaded it
    // as string
    // only try the fallback if access is allowed, skip for out of root url
    // like /service-worker.js or /api/users
    if (
      environment.config.consumer === 'server' ||
      isFileLoadingAllowed(environment.getTopLevelConfig(), slash(file))
    ) {
      try {
        code = await fsp.readFile(file, 'utf-8')
        debugLoad?.(`${timeFrom(loadStart)} [fs] ${prettyUrl}`)
      } catch (e) {
        if (e.code !== 'ENOENT' && e.code !== 'EISDIR') {
          throw e
        }
      }
      if (code != null && environment.pluginContainer.watcher) {
        ensureWatchedFile(
          environment.pluginContainer.watcher,
          file,
          config.root,
        )
      }
    }
    if (code) {
      try {
        const extracted = await extractSourcemapFromFile(code, file)
        if (extracted) {
          code = extracted.code
          map = extracted.map
        }
      } catch (e) {
        logger.warn(`Failed to load source map for ${file}.\n${e}`, {
          timestamp: true,
        })
      }
    }
  } else {
    debugLoad?.(`${timeFrom(loadStart)} [plugin] ${prettyUrl}`)
    if (isObject(loadResult)) {
      code = loadResult.code
      map = loadResult.map
      moduleType = loadResult.moduleType
    } else {
      code = loadResult
    }
  }

Domain

Subdomains

Called By

Frequently Asked Questions

What does loadAndTransform() do?
loadAndTransform() is a function in the vite codebase, defined in packages/vite/src/node/server/transformRequest.ts.
Where is loadAndTransform() defined?
loadAndTransform() is defined in packages/vite/src/node/server/transformRequest.ts at line 236.
What does loadAndTransform() call?
loadAndTransform() calls 20 function(s): _ensureEntryFromUrl, applySourcemapIgnoreList, checkPublicFile, cleanUrl, ensureWatchedFile, extractSourcemapFromFile, getModuleTypeFromId, injectSourcesContent, and 12 more.
What calls loadAndTransform()?
loadAndTransform() is called by 1 function(s): doTransform.

Analyze Your Own Codebase

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

Try Supermodel Free