Home / Function/ init() — vite Function Reference

init() — vite Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  6d5b7f61_22fe_5498_a292_5c478890d5f3["init()"]
  ebed723b_f143_390f_439d_dfaa680d6d16["index.ts"]
  6d5b7f61_22fe_5498_a292_5c478890d5f3 -->|defined in| ebed723b_f143_390f_439d_dfaa680d6d16
  b1829b03_3777_b6dc_6cc5_7b563a6cc38a["formatTargetDir()"]
  6d5b7f61_22fe_5498_a292_5c478890d5f3 -->|calls| b1829b03_3777_b6dc_6cc5_7b563a6cc38a
  a6146459_906f_248b_c457_d3cd3c77815f["pkgFromUserAgent()"]
  6d5b7f61_22fe_5498_a292_5c478890d5f3 -->|calls| a6146459_906f_248b_c457_d3cd3c77815f
  3260e851_cf2f_29ed_2cda_04369c8822a5["isEmpty()"]
  6d5b7f61_22fe_5498_a292_5c478890d5f3 -->|calls| 3260e851_cf2f_29ed_2cda_04369c8822a5
  6c169db9_3cef_7932_b3f2_37fb0fbddc40["emptyDir()"]
  6d5b7f61_22fe_5498_a292_5c478890d5f3 -->|calls| 6c169db9_3cef_7932_b3f2_37fb0fbddc40
  0b207296_7c7d_7312_f291_b60ecc8ad151["isValidPackageName()"]
  6d5b7f61_22fe_5498_a292_5c478890d5f3 -->|calls| 0b207296_7c7d_7312_f291_b60ecc8ad151
  7f178f8f_108c_e0bb_ed4e_9ee7b340f995["toValidPackageName()"]
  6d5b7f61_22fe_5498_a292_5c478890d5f3 -->|calls| 7f178f8f_108c_e0bb_ed4e_9ee7b340f995
  15ac042d_7ddf_e44d_028d_9bf4155c4e45["getFullCustomCommand()"]
  6d5b7f61_22fe_5498_a292_5c478890d5f3 -->|calls| 15ac042d_7ddf_e44d_028d_9bf4155c4e45
  95b76a41_cb83_900a_7861_db52445d6d35["getLabel()"]
  6d5b7f61_22fe_5498_a292_5c478890d5f3 -->|calls| 95b76a41_cb83_900a_7861_db52445d6d35
  05b4afba_2d4e_a55b_1789_026344143a2d["copy()"]
  6d5b7f61_22fe_5498_a292_5c478890d5f3 -->|calls| 05b4afba_2d4e_a55b_1789_026344143a2d
  d87846dd_6b47_93d5_33fb_9a71b0f3f131["setupReactSwc()"]
  6d5b7f61_22fe_5498_a292_5c478890d5f3 -->|calls| d87846dd_6b47_93d5_33fb_9a71b0f3f131
  70332fa3_c6e8_f1a8_9fd8_d971f1fc35dd["setupReactCompiler()"]
  6d5b7f61_22fe_5498_a292_5c478890d5f3 -->|calls| 70332fa3_c6e8_f1a8_9fd8_d971f1fc35dd
  c8b19a47_ac83_3632_1e20_c192b06e2cac["install()"]
  6d5b7f61_22fe_5498_a292_5c478890d5f3 -->|calls| c8b19a47_ac83_3632_1e20_c192b06e2cac
  21c23839_d37b_7105_7569_d8c6f394b60d["start()"]
  6d5b7f61_22fe_5498_a292_5c478890d5f3 -->|calls| 21c23839_d37b_7105_7569_d8c6f394b60d
  style 6d5b7f61_22fe_5498_a292_5c478890d5f3 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/create-vite/src/index.ts lines 446–716

async function init() {
  const argTargetDir = argv._[0]
    ? formatTargetDir(String(argv._[0]))
    : undefined
  const argTemplate = argv.template
  const argOverwrite = argv.overwrite
  const argImmediate = argv.immediate
  const argInteractive = argv.interactive

  const help = argv.help
  if (help) {
    console.log(helpMessage)
    return
  }

  const interactive = argInteractive ?? process.stdin.isTTY

  // Detect AI agent environment for better agent experience (AX)
  const { isAgent } = await determineAgent()
  if (isAgent && interactive) {
    console.log(
      '\nTo create in one go, run: create-vite <DIRECTORY> --no-interactive --template <TEMPLATE>\n',
    )
  }

  const pkgInfo = pkgFromUserAgent(process.env.npm_config_user_agent)
  const cancel = () => prompts.cancel('Operation cancelled')

  // 1. Get project name and target dir
  let targetDir = argTargetDir
  if (!targetDir) {
    if (interactive) {
      const projectName = await prompts.text({
        message: 'Project name:',
        defaultValue: defaultTargetDir,
        placeholder: defaultTargetDir,
        validate: (value) => {
          return !value || formatTargetDir(value).length > 0
            ? undefined
            : 'Invalid project name'
        },
      })
      if (prompts.isCancel(projectName)) return cancel()
      targetDir = formatTargetDir(projectName)
    } else {
      targetDir = defaultTargetDir
    }
  }

  // 2. Handle directory if exist and not empty
  if (fs.existsSync(targetDir) && !isEmpty(targetDir)) {
    let overwrite: 'yes' | 'no' | 'ignore' | undefined = argOverwrite
      ? 'yes'
      : undefined
    if (!overwrite) {
      if (interactive) {
        const res = await prompts.select({
          message:
            (targetDir === '.'
              ? 'Current directory'
              : `Target directory "${targetDir}"`) +
            ` is not empty. Please choose how to proceed:`,
          options: [
            {
              label: 'Cancel operation',
              value: 'no',
            },
            {
              label: 'Remove existing files and continue',
              value: 'yes',
            },
            {
              label: 'Ignore files and continue',
              value: 'ignore',
            },
          ],
        })
        if (prompts.isCancel(res)) return cancel()
        overwrite = res
      } else {
        overwrite = 'no'

Subdomains

Frequently Asked Questions

What does init() do?
init() is a function in the vite codebase, defined in packages/create-vite/src/index.ts.
Where is init() defined?
init() is defined in packages/create-vite/src/index.ts at line 446.
What does init() call?
init() calls 15 function(s): copy, emptyDir, formatTargetDir, getFullCustomCommand, getInstallCommand, getLabel, getRunCommand, install, and 7 more.

Analyze Your Own Codebase

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

Try Supermodel Free