Home / Function/ createBuilder() — vite Function Reference

createBuilder() — vite Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  8fb400c0_eabf_b57a_2905_d2afc7644da6["createBuilder()"]
  45981d85_cbdd_e969_8c88_c17072ea0eda["build.ts"]
  8fb400c0_eabf_b57a_2905_d2afc7644da6 -->|defined in| 45981d85_cbdd_e969_8c88_c17072ea0eda
  bbded320_f805_2b6e_3109_088d194024a2["build()"]
  bbded320_f805_2b6e_3109_088d194024a2 -->|calls| 8fb400c0_eabf_b57a_2905_d2afc7644da6
  3dbc9dfb_0893_5792_6f30_598af5407825["resolveConfigToBuild()"]
  8fb400c0_eabf_b57a_2905_d2afc7644da6 -->|calls| 3dbc9dfb_0893_5792_6f30_598af5407825
  33d28a11_2362_24d7_29f2_e11a703b1f7b["resolveBuilderOptions()"]
  8fb400c0_eabf_b57a_2905_d2afc7644da6 -->|calls| 33d28a11_2362_24d7_29f2_e11a703b1f7b
  58719e23_70bd_67e0_1ed8_f61c19ec725b["getHookHandler()"]
  8fb400c0_eabf_b57a_2905_d2afc7644da6 -->|calls| 58719e23_70bd_67e0_1ed8_f61c19ec725b
  bbded320_f805_2b6e_3109_088d194024a2["build()"]
  8fb400c0_eabf_b57a_2905_d2afc7644da6 -->|calls| bbded320_f805_2b6e_3109_088d194024a2
  94ad7f99_b02e_152e_c5a9_749d6de8d3c0["buildEnvironment()"]
  8fb400c0_eabf_b57a_2905_d2afc7644da6 -->|calls| 94ad7f99_b02e_152e_c5a9_749d6de8d3c0
  2b805bd3_afe5_06fa_d723_ae5cd9934c6e["init()"]
  8fb400c0_eabf_b57a_2905_d2afc7644da6 -->|calls| 2b805bd3_afe5_06fa_d723_ae5cd9934c6e
  8127cae8_510b_1333_1a76_2d21b503c3a6["error()"]
  8fb400c0_eabf_b57a_2905_d2afc7644da6 -->|calls| 8127cae8_510b_1333_1a76_2d21b503c3a6
  style 8fb400c0_eabf_b57a_2905_d2afc7644da6 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/build.ts lines 1769–1915

export async function createBuilder(
  inlineConfig: InlineConfig = {},
  useLegacyBuilder: null | boolean = false,
): Promise<ViteBuilder> {
  const patchConfig = (resolved: ResolvedConfig) => {
    if (!(useLegacyBuilder ?? !resolved.builder)) return

    // Until the ecosystem updates to use `environment.config.build` instead of `config.build`,
    // we need to make override `config.build` for the current environment.
    // We can deprecate `config.build` in ResolvedConfig and push everyone to upgrade, and later
    // remove the default values that shouldn't be used at all once the config is resolved
    const environmentName = resolved.build.ssr ? 'ssr' : 'client'
    ;(resolved.build as ResolvedBuildOptions) = {
      ...resolved.environments[environmentName].build,
    }
  }
  const config = await resolveConfigToBuild(inlineConfig, patchConfig)
  useLegacyBuilder ??= !config.builder
  const configBuilder = config.builder ?? resolveBuilderOptions({})!

  const environments: Record<string, BuildEnvironment> = {}

  const builder: ViteBuilder = {
    environments,
    config,
    async buildApp() {
      const pluginContext = new BasicMinimalPluginContext(
        { ...basePluginContextMeta, watchMode: false },
        config.logger,
      )

      // order 'pre' and 'normal' hooks are run first, then config.builder.buildApp, then 'post' hooks
      let configBuilderBuildAppCalled = false
      for (const p of config.getSortedPlugins('buildApp')) {
        const hook = p.buildApp
        if (
          !configBuilderBuildAppCalled &&
          typeof hook === 'object' &&
          hook.order === 'post'
        ) {
          configBuilderBuildAppCalled = true
          await configBuilder.buildApp(builder)
        }
        const handler = getHookHandler(hook)
        await handler.call(pluginContext, builder)
      }
      if (!configBuilderBuildAppCalled) {
        await configBuilder.buildApp(builder)
      }
      // fallback to building all environments if no environments have been built
      if (
        Object.values(builder.environments).every(
          (environment) => !environment.isBuilt,
        )
      ) {
        for (const environment of Object.values(builder.environments)) {
          await builder.build(environment)
        }
      }
    },
    async build(
      environment: BuildEnvironment,
    ): Promise<RolldownOutput | RolldownOutput[] | RolldownWatcher> {
      const output = await buildEnvironment(environment)
      environment.isBuilt = true
      return output
    },
    async runDevTools() {
      const devtoolsConfig = config.devtools
      if (devtoolsConfig.enabled) {
        try {
          const { start } = await import(`@vitejs/devtools/cli-commands`)
          await start(devtoolsConfig.config)
        } catch (e) {
          config.logger.error(
            colors.red(`Failed to run Vite DevTools: ${e.message || e.stack}`),
            { error: e },
          )
        }
      }
    },

Domain

Subdomains

Called By

Frequently Asked Questions

What does createBuilder() do?
createBuilder() is a function in the vite codebase, defined in packages/vite/src/node/build.ts.
Where is createBuilder() defined?
createBuilder() is defined in packages/vite/src/node/build.ts at line 1769.
What does createBuilder() call?
createBuilder() calls 7 function(s): build, buildEnvironment, error, getHookHandler, init, resolveBuilderOptions, resolveConfigToBuild.
What calls createBuilder()?
createBuilder() is called by 1 function(s): build.

Analyze Your Own Codebase

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

Try Supermodel Free