Home / Function/ fetchModule() — vite Function Reference

fetchModule() — vite Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  18626e4c_2392_d64e_6551_5d524a5d0253["fetchModule()"]
  4b293f94_dc8f_b625_ac09_35b43008310b["fetchModule.ts"]
  18626e4c_2392_d64e_6551_5d524a5d0253 -->|defined in| 4b293f94_dc8f_b625_ac09_35b43008310b
  be964a75_40a0_c608_ed3a_1a730415cb0f["fetchModule()"]
  be964a75_40a0_c608_ed3a_1a730415cb0f -->|calls| 18626e4c_2392_d64e_6551_5d524a5d0253
  be964a75_40a0_c608_ed3a_1a730415cb0f["fetchModule()"]
  18626e4c_2392_d64e_6551_5d524a5d0253 -->|calls| be964a75_40a0_c608_ed3a_1a730415cb0f
  5d2580c9_3b65_7396_afe1_ab2aa13f8ed9["isBuiltin()"]
  18626e4c_2392_d64e_6551_5d524a5d0253 -->|calls| 5d2580c9_3b65_7396_afe1_ab2aa13f8ed9
  23257aa1_5e3f_7e6e_1b43_875b139e4ec5["isExternalUrl()"]
  18626e4c_2392_d64e_6551_5d524a5d0253 -->|calls| 23257aa1_5e3f_7e6e_1b43_875b139e4ec5
  310b19b4_2716_b45b_716d_edfca3064152["tryNodeResolve()"]
  18626e4c_2392_d64e_6551_5d524a5d0253 -->|calls| 310b19b4_2716_b45b_716d_edfca3064152
  a3a231c1_e1f6_f7c0_6d09_1d3c7fbf6ac6["isFilePathESM()"]
  18626e4c_2392_d64e_6551_5d524a5d0253 -->|calls| a3a231c1_e1f6_f7c0_6d09_1d3c7fbf6ac6
  795dec35_22f9_80f5_ccab_9c2d170af0d3["unwrapId()"]
  18626e4c_2392_d64e_6551_5d524a5d0253 -->|calls| 795dec35_22f9_80f5_ccab_9c2d170af0d3
  c99812b1_1fd5_5046_98fa_678c5c80c0fb["transformRequest()"]
  18626e4c_2392_d64e_6551_5d524a5d0253 -->|calls| c99812b1_1fd5_5046_98fa_678c5c80c0fb
  37c7e77b_d40c_df6e_62db_77d0ac97c34a["inlineSourceMap()"]
  18626e4c_2392_d64e_6551_5d524a5d0253 -->|calls| 37c7e77b_d40c_df6e_62db_77d0ac97c34a
  style 18626e4c_2392_d64e_6551_5d524a5d0253 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/ssr/fetchModule.ts lines 24–116

export async function fetchModule(
  environment: DevEnvironment,
  url: string,
  importer?: string,
  options: FetchModuleOptions = {},
): Promise<FetchResult> {
  if (
    url.startsWith('data:') ||
    isBuiltin(environment.config.resolve.builtins, url)
  ) {
    return { externalize: url, type: 'builtin' }
  }

  // handle file urls from not statically analyzable dynamic import
  const isFileUrl = url.startsWith('file://')

  if (isExternalUrl(url) && !isFileUrl) {
    return { externalize: url, type: 'network' }
  }

  // if there is no importer, the file is an entry point
  // entry points are always internalized
  if (!isFileUrl && importer && url[0] !== '.' && url[0] !== '/') {
    const { isProduction, root } = environment.config
    const { externalConditions, dedupe, preserveSymlinks } =
      environment.config.resolve

    const resolved = tryNodeResolve(url, importer, {
      mainFields: ['main'],
      conditions: externalConditions,
      externalConditions,
      external: [],
      noExternal: [],
      extensions: ['.js', '.cjs', '.json'],
      dedupe,
      preserveSymlinks,
      tsconfigPaths: false,
      isBuild: false,
      isProduction,
      root,
      packageCache: environment.config.packageCache,
      builtins: environment.config.resolve.builtins,
    })
    if (!resolved) {
      const err: any = new Error(
        `Cannot find module '${url}' imported from '${importer}'`,
      )
      err.code = 'ERR_MODULE_NOT_FOUND'
      throw err
    }
    const file = pathToFileURL(resolved.id).toString()
    const type = isFilePathESM(resolved.id, environment.config.packageCache)
      ? 'module'
      : 'commonjs'
    return { externalize: file, type }
  }

  url = unwrapId(url)

  const mod = await environment.moduleGraph.ensureEntryFromUrl(url)
  const cached = !!mod.transformResult

  // if url is already cached, we can just confirm it's also cached on the server
  if (options.cached && cached) {
    return { cache: true }
  }

  let result = await environment.transformRequest(url)

  if (!result) {
    throw new Error(
      `[vite] transform failed for module '${url}'${
        importer ? ` imported from '${importer}'` : ''
      }.`,
    )
  }

  if (options.inlineSourceMap !== false) {
    result = inlineSourceMap(mod, result, options.startOffset)
  }

Domain

Subdomains

Called By

Frequently Asked Questions

What does fetchModule() do?
fetchModule() is a function in the vite codebase, defined in packages/vite/src/node/ssr/fetchModule.ts.
Where is fetchModule() defined?
fetchModule() is defined in packages/vite/src/node/ssr/fetchModule.ts at line 24.
What does fetchModule() call?
fetchModule() calls 8 function(s): fetchModule, inlineSourceMap, isBuiltin, isExternalUrl, isFilePathESM, transformRequest, tryNodeResolve, unwrapId.
What calls fetchModule()?
fetchModule() is called by 1 function(s): fetchModule.

Analyze Your Own Codebase

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

Try Supermodel Free