Home / Function/ transformWithEsbuild() — vite Function Reference

transformWithEsbuild() — vite Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  bf4a41be_9dbf_35be_2072_4283cd478ae7["transformWithEsbuild()"]
  926e3b98_b813_2ff8_abb3_16447ab95544["esbuild.ts"]
  bf4a41be_9dbf_35be_2072_4283cd478ae7 -->|defined in| 926e3b98_b813_2ff8_abb3_16447ab95544
  2c458bfa_10fc_ba71_1db1_819ac600e615["esbuildPlugin()"]
  2c458bfa_10fc_ba71_1db1_819ac600e615 -->|calls| bf4a41be_9dbf_35be_2072_4283cd478ae7
  58657749_bb5a_f054_a7a7_21e9351ab0f7["buildEsbuildPlugin()"]
  58657749_bb5a_f054_a7a7_21e9351ab0f7 -->|calls| bf4a41be_9dbf_35be_2072_4283cd478ae7
  10b9dea8_362c_1af2_93be_afa4dd9aed9e["cleanUrl()"]
  bf4a41be_9dbf_35be_2072_4283cd478ae7 -->|calls| 10b9dea8_362c_1af2_93be_afa4dd9aed9e
  bf7f9c91_a198_bc30_3101_285b2049912d["loadTsconfigJsonForFile()"]
  bf4a41be_9dbf_35be_2072_4283cd478ae7 -->|calls| bf7f9c91_a198_bc30_3101_285b2049912d
  0a6fbb70_77d3_9873_8417_b1e4ffba2651["ensureWatchedFile()"]
  bf4a41be_9dbf_35be_2072_4283cd478ae7 -->|calls| 0a6fbb70_77d3_9873_8417_b1e4ffba2651
  91493e29_f18b_5b23_936e_c42ebeeb07fc["importEsbuild()"]
  bf4a41be_9dbf_35be_2072_4283cd478ae7 -->|calls| 91493e29_f18b_5b23_936e_c42ebeeb07fc
  b947d693_c472_522f_b5ea_efcd9d72a3b0["warnTransformWithEsbuildUsageOnce()"]
  bf4a41be_9dbf_35be_2072_4283cd478ae7 -->|calls| b947d693_c472_522f_b5ea_efcd9d72a3b0
  cb1210e8_03e9_2eec_ef04_aa15d44d4c08["combineSourcemaps()"]
  bf4a41be_9dbf_35be_2072_4283cd478ae7 -->|calls| cb1210e8_03e9_2eec_ef04_aa15d44d4c08
  b8ca3f28_b158_ad31_7286_a8412bc7e4cd["prettifyMessage()"]
  bf4a41be_9dbf_35be_2072_4283cd478ae7 -->|calls| b8ca3f28_b158_ad31_7286_a8412bc7e4cd
  style bf4a41be_9dbf_35be_2072_4283cd478ae7 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/plugins/esbuild.ts lines 99–278

export async function transformWithEsbuild(
  code: string,
  filename: string,
  options?: EsbuildTransformOptions,
  inMap?: object,
  config?: ResolvedConfig,
  watcher?: FSWatcher,
  /** @internal */
  ignoreEsbuildWarning = false,
): Promise<ESBuildTransformResult> {
  let loader = options?.loader

  if (!loader) {
    // if the id ends with a valid ext, use it (e.g. vue blocks)
    // otherwise, cleanup the query before checking the ext
    const ext = path
      .extname(validExtensionRE.test(filename) ? filename : cleanUrl(filename))
      .slice(1)

    if (ext === 'cjs' || ext === 'mjs') {
      loader = 'js'
    } else if (ext === 'cts' || ext === 'mts') {
      loader = 'ts'
    } else {
      loader = ext as EsbuildLoader
    }
  }

  let tsconfigRaw = options?.tsconfigRaw

  // if options provide tsconfigRaw in string, it takes highest precedence
  if (typeof tsconfigRaw !== 'string') {
    // these fields would affect the compilation result
    // https://esbuild.github.io/content-types/#tsconfig-json
    const meaningfulFields: Array<keyof TSCompilerOptions> = [
      'alwaysStrict',
      'experimentalDecorators',
      'importsNotUsedAsValues',
      'jsx',
      'jsxFactory',
      'jsxFragmentFactory',
      'jsxImportSource',
      'preserveValueImports',
      'target',
      'useDefineForClassFields',
      'verbatimModuleSyntax',
    ]
    const compilerOptionsForFile: TSCompilerOptions = {}
    if (loader === 'ts' || loader === 'tsx') {
      try {
        const { tsconfig: loadedTsconfig, tsconfigFile } =
          await loadTsconfigJsonForFile(filename, config)
        // tsconfig could be out of root, make sure it is watched on dev
        if (watcher && tsconfigFile && config) {
          ensureWatchedFile(watcher, tsconfigFile, config.root)
        }
        const loadedCompilerOptions = loadedTsconfig.compilerOptions ?? {}

        for (const field of meaningfulFields) {
          if (field in loadedCompilerOptions) {
            // @ts-expect-error TypeScript can't tell they are of the same type
            compilerOptionsForFile[field] = loadedCompilerOptions[field]
          }
        }
      } catch (e) {
        if (e instanceof TSConfckParseError) {
          // tsconfig could be out of root, make sure it is watched on dev
          if (watcher && e.tsconfigFile && config) {
            ensureWatchedFile(watcher, e.tsconfigFile, config.root)
          }
        }
        throw e
      }
    }

    const compilerOptions = {
      ...compilerOptionsForFile,
      ...tsconfigRaw?.compilerOptions,
    }

    // esbuild uses `useDefineForClassFields: true` when `tsconfig.compilerOptions.target` isn't declared

Domain

Subdomains

Frequently Asked Questions

What does transformWithEsbuild() do?
transformWithEsbuild() is a function in the vite codebase, defined in packages/vite/src/node/plugins/esbuild.ts.
Where is transformWithEsbuild() defined?
transformWithEsbuild() is defined in packages/vite/src/node/plugins/esbuild.ts at line 99.
What does transformWithEsbuild() call?
transformWithEsbuild() calls 7 function(s): cleanUrl, combineSourcemaps, ensureWatchedFile, importEsbuild, loadTsconfigJsonForFile, prettifyMessage, warnTransformWithEsbuildUsageOnce.
What calls transformWithEsbuild()?
transformWithEsbuild() is called by 2 function(s): buildEsbuildPlugin, esbuildPlugin.

Analyze Your Own Codebase

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

Try Supermodel Free