Home / File/ prepareOutDir.ts — vite Source File

prepareOutDir.ts — vite Source File

Architecture documentation for prepareOutDir.ts, a typescript file in the vite codebase. 16 imports, 1 dependents.

File typescript PluginSystem LegacySupport 16 imports 1 dependents 3 functions

Entity Profile

Dependency Diagram

graph LR
  4c658b37_85c2_95c4_7b25_9e1e2258e2a4["prepareOutDir.ts"]
  5abb8c87_ffcb_f2d4_7421_e36705d9e5c7["plugin.ts"]
  4c658b37_85c2_95c4_7b25_9e1e2258e2a4 --> 5abb8c87_ffcb_f2d4_7421_e36705d9e5c7
  1dc2cf7d_5d97_c778_8c33_6449a7607aef["Plugin"]
  4c658b37_85c2_95c4_7b25_9e1e2258e2a4 --> 1dc2cf7d_5d97_c778_8c33_6449a7607aef
  b4c4dcfb_d52b_008a_a6fc_ed4f2bf1f714["watch.ts"]
  4c658b37_85c2_95c4_7b25_9e1e2258e2a4 --> b4c4dcfb_d52b_008a_a6fc_ed4f2bf1f714
  841e9f5e_2979_644d_f5ee_6e634536197c["getResolvedOutDirs"]
  4c658b37_85c2_95c4_7b25_9e1e2258e2a4 --> 841e9f5e_2979_644d_f5ee_6e634536197c
  dd84c207_766d_3923_500c_1abe3b21a31e["resolveEmptyOutDir"]
  4c658b37_85c2_95c4_7b25_9e1e2258e2a4 --> dd84c207_766d_3923_500c_1abe3b21a31e
  0c33ff62_54e9_5c90_902b_b26728e71fca["environment.ts"]
  4c658b37_85c2_95c4_7b25_9e1e2258e2a4 --> 0c33ff62_54e9_5c90_902b_b26728e71fca
  7e6f7c83_3515_4e0d_5362_4eb9515b4a86["Environment"]
  4c658b37_85c2_95c4_7b25_9e1e2258e2a4 --> 7e6f7c83_3515_4e0d_5362_4eb9515b4a86
  031bc221_67a8_c579_f2bf_bb30a08beeb2["utils.ts"]
  4c658b37_85c2_95c4_7b25_9e1e2258e2a4 --> 031bc221_67a8_c579_f2bf_bb30a08beeb2
  f90acca0_7045_11b1_e750_7498f272812e["copyDir"]
  4c658b37_85c2_95c4_7b25_9e1e2258e2a4 --> f90acca0_7045_11b1_e750_7498f272812e
  3fbde17a_4cd2_7c18_fa87_ea844479d120["emptyDir"]
  4c658b37_85c2_95c4_7b25_9e1e2258e2a4 --> 3fbde17a_4cd2_7c18_fa87_ea844479d120
  a4adb1a7_cf54_091f_eb63_8217e684a8e1["normalizePath"]
  4c658b37_85c2_95c4_7b25_9e1e2258e2a4 --> a4adb1a7_cf54_091f_eb63_8217e684a8e1
  abfc9e70_3c15_b3f0_a595_3cf27afb7e64["utils.ts"]
  4c658b37_85c2_95c4_7b25_9e1e2258e2a4 --> abfc9e70_3c15_b3f0_a595_3cf27afb7e64
  1a3bec7b_1a11_316f_5831_a0535b829bbf["withTrailingSlash"]
  4c658b37_85c2_95c4_7b25_9e1e2258e2a4 --> 1a3bec7b_1a11_316f_5831_a0535b829bbf
  e6032fbc_44cf_58d6_868d_dd15106c18c5["node:fs"]
  4c658b37_85c2_95c4_7b25_9e1e2258e2a4 --> e6032fbc_44cf_58d6_868d_dd15106c18c5
  style 4c658b37_85c2_95c4_7b25_9e1e2258e2a4 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import fs from 'node:fs'
import path from 'node:path'
import colors from 'picocolors'
import type { Plugin } from '../plugin'
import { getResolvedOutDirs, resolveEmptyOutDir } from '../watch'
import type { Environment } from '../environment'
import { copyDir, emptyDir, normalizePath } from '../utils'
import { withTrailingSlash } from '../../shared/utils'

export function prepareOutDirPlugin(): Plugin {
  const rendered = new Set<Environment>()
  return {
    name: 'vite:prepare-out-dir',
    options() {
      rendered.delete(this.environment)
    },
    renderStart: {
      order: 'pre',
      handler() {
        if (rendered.has(this.environment)) {
          return
        }
        rendered.add(this.environment)

        const { config } = this.environment
        if (config.build.write) {
          const { root, build: options } = config
          const resolvedOutDirs = getResolvedOutDirs(
            root,
            options.outDir,
            options.rollupOptions.output,
          )
          const emptyOutDir = resolveEmptyOutDir(
            options.emptyOutDir,
            root,
            resolvedOutDirs,
            this.environment.logger,
          )
          prepareOutDir(resolvedOutDirs, emptyOutDir, this.environment)
        }
      },
    },
  }
}

function prepareOutDir(
  outDirs: Set<string>,
  emptyOutDir: boolean | null,
  environment: Environment,
) {
  const { publicDir } = environment.config
  const outDirsArray = [...outDirs]
  for (const outDir of outDirs) {
    if (emptyOutDir !== false && fs.existsSync(outDir)) {
      // skip those other outDirs which are nested in current outDir
      const skipDirs = outDirsArray
        .map((dir) => {
          const relative = path.relative(outDir, dir)
          if (
            relative &&
            !relative.startsWith('..') &&
            !path.isAbsolute(relative)
          ) {
            return relative
          }
          return ''
        })
        .filter(Boolean)
      emptyDir(outDir, [...skipDirs, '.git'])
    }
    if (
      environment.config.build.copyPublicDir &&
      publicDir &&
      fs.existsSync(publicDir)
    ) {
      if (!areSeparateFolders(outDir, publicDir)) {
        environment.logger.warn(
          colors.yellow(
            `\n${colors.bold(
              `(!)`,
            )} The public directory feature may not work correctly. outDir ${colors.white(
              colors.dim(outDir),
            )} and publicDir ${colors.white(
              colors.dim(publicDir),
            )} are not separate folders.\n`,
          ),
        )
      }
      copyDir(publicDir, outDir)
    }
  }
}

function areSeparateFolders(a: string, b: string) {
  const na = normalizePath(a)
  const nb = normalizePath(b)
  return (
    na !== nb &&
    !na.startsWith(withTrailingSlash(nb)) &&
    !nb.startsWith(withTrailingSlash(na))
  )
}

Domain

Subdomains

Frequently Asked Questions

What does prepareOutDir.ts do?
prepareOutDir.ts is a source file in the vite codebase, written in typescript. It belongs to the PluginSystem domain, LegacySupport subdomain.
What functions are defined in prepareOutDir.ts?
prepareOutDir.ts defines 3 function(s): areSeparateFolders, prepareOutDir, prepareOutDirPlugin.
What does prepareOutDir.ts depend on?
prepareOutDir.ts imports 16 module(s): Environment, Plugin, copyDir, emptyDir, environment.ts, getResolvedOutDirs, node:fs, node:path, and 8 more.
What files import prepareOutDir.ts?
prepareOutDir.ts is imported by 1 file(s): build.ts.
Where is prepareOutDir.ts in the architecture?
prepareOutDir.ts is located at packages/vite/src/node/plugins/prepareOutDir.ts (domain: PluginSystem, subdomain: LegacySupport, directory: packages/vite/src/node/plugins).

Analyze Your Own Codebase

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

Try Supermodel Free