Home / Function/ viteLegacyPlugin() — vite Function Reference

viteLegacyPlugin() — vite Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  cf21e8d8_cce6_30a7_7732_696ed5cdf103["viteLegacyPlugin()"]
  9776ecb6_00c0_b3b9_b3f7_177fedabeacc["index.ts"]
  cf21e8d8_cce6_30a7_7732_696ed5cdf103 -->|defined in| 9776ecb6_00c0_b3b9_b3f7_177fedabeacc
  05be5ca5_2f63_2c99_c05a_ce1bdff8d1ab["isLegacyBundle()"]
  cf21e8d8_cce6_30a7_7732_696ed5cdf103 -->|calls| 05be5ca5_2f63_2c99_c05a_ce1bdff8d1ab
  49d850e2_8fd6_07a7_8f83_58cf64932a71["buildPolyfillChunk()"]
  cf21e8d8_cce6_30a7_7732_696ed5cdf103 -->|calls| 49d850e2_8fd6_07a7_8f83_58cf64932a71
  8f96a660_631f_d882_8813_44cfe1cdb28f["detectPolyfills()"]
  cf21e8d8_cce6_30a7_7732_696ed5cdf103 -->|calls| 8f96a660_631f_d882_8813_44cfe1cdb28f
  0064f6ab_988c_7c5f_d7b9_1668a42f0a59["isLegacyChunk()"]
  cf21e8d8_cce6_30a7_7732_696ed5cdf103 -->|calls| 0064f6ab_988c_7c5f_d7b9_1668a42f0a59
  b52618ce_8781_f4f2_b33b_cd2b4ed2b715["loadBabel()"]
  cf21e8d8_cce6_30a7_7732_696ed5cdf103 -->|calls| b52618ce_8781_f4f2_b33b_cd2b4ed2b715
  9b43ba2f_f840_d828_0465_b829e6574d45["recordAndRemovePolyfillBabelPlugin()"]
  cf21e8d8_cce6_30a7_7732_696ed5cdf103 -->|calls| 9b43ba2f_f840_d828_0465_b829e6574d45
  471bd328_d308_54b1_7f54_dc8e088c38f7["replaceLegacyEnvBabelPlugin()"]
  cf21e8d8_cce6_30a7_7732_696ed5cdf103 -->|calls| 471bd328_d308_54b1_7f54_dc8e088c38f7
  f458c82a_ff5e_9390_e984_cbbb9a2719e1["replaceModernEnvBabelPlugin()"]
  cf21e8d8_cce6_30a7_7732_696ed5cdf103 -->|calls| f458c82a_ff5e_9390_e984_cbbb9a2719e1
  48fb5018_fd41_a067_fb87_1ef4d68f67fb["wrapIIFEBabelPlugin()"]
  cf21e8d8_cce6_30a7_7732_696ed5cdf103 -->|calls| 48fb5018_fd41_a067_fb87_1ef4d68f67fb
  e4d2379b_4e77_cb47_c192_09b680b7570b["toAssetPathFromHtml()"]
  cf21e8d8_cce6_30a7_7732_696ed5cdf103 -->|calls| e4d2379b_4e77_cb47_c192_09b680b7570b
  style cf21e8d8_cce6_30a7_7732_696ed5cdf103 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/plugin-legacy/src/index.ts lines 145–786

function viteLegacyPlugin(options: Options = {}): Plugin[] {
  let config: ResolvedConfig
  let targets: Options['targets']
  const modernTargets: Options['modernTargets'] =
    options.modernTargets || modernTargetsBabel

  const genLegacy = options.renderLegacyChunks !== false
  const genModern = options.renderModernChunks !== false
  if (!genLegacy && !genModern) {
    throw new Error(
      '`renderLegacyChunks` and `renderModernChunks` cannot be both false',
    )
  }

  const debugFlags = (process.env.DEBUG || '').split(',')
  const isDebug =
    debugFlags.includes('vite:*') || debugFlags.includes('vite:legacy')

  const assumptions = options.assumptions || {}

  const facadeToLegacyChunkMap = new Map()
  const facadeToLegacyPolyfillMap = new Map()
  const facadeToModernPolyfillMap = new Map()
  const modernPolyfills = new Set<string>()
  const legacyPolyfills = new Set<string>()
  // When discovering polyfills in `renderChunk`, the hook may be non-deterministic, so we group the
  // modern and legacy polyfills in a sorted chunks map for each rendered outputs before merging them.
  const outputToChunkFileNameToPolyfills = new WeakMap<
    Rollup.NormalizedOutputOptions,
    Map<string, { modern: Set<string>; legacy: Set<string> }> | null
  >()

  if (Array.isArray(options.modernPolyfills) && genModern) {
    options.modernPolyfills.forEach((i) => {
      modernPolyfills.add(
        i.includes('/') ? `core-js/${i}` : `core-js/modules/${i}.js`,
      )
    })
  }
  if (Array.isArray(options.additionalModernPolyfills)) {
    options.additionalModernPolyfills.forEach((i) => {
      modernPolyfills.add(i)
    })
  }
  if (Array.isArray(options.polyfills)) {
    options.polyfills.forEach((i) => {
      if (i.startsWith(`regenerator`)) {
        legacyPolyfills.add(`regenerator-runtime/runtime.js`)
      } else {
        legacyPolyfills.add(
          i.includes('/') ? `core-js/${i}` : `core-js/modules/${i}.js`,
        )
      }
    })
  }
  if (Array.isArray(options.additionalLegacyPolyfills)) {
    options.additionalLegacyPolyfills.forEach((i) => {
      legacyPolyfills.add(i)
    })
  }

  let overriddenBuildTarget = false
  let overriddenBuildTargetOnlyModern = false
  let overriddenDefaultModernTargets = false
  const legacyConfigPlugin: Plugin = {
    name: 'vite:legacy-config',

    async config(config, env) {
      if (env.command === 'build' && !config.build?.ssr) {
        if (!config.build) {
          config.build = {}
        }

        if (genLegacy && !config.build.cssTarget) {
          // Hint for esbuild that we are targeting legacy browsers when minifying CSS.
          // Full CSS compat table available at https://github.com/evanw/esbuild/blob/78e04680228cf989bdd7d471e02bbc2c8d345dc9/internal/compat/css_table.go
          // But note that only the `HexRGBA` feature affects the minify outcome.
          // HSL & rebeccapurple values will be minified away regardless the target.
          // So targeting `chrome61` suffices to fix the compatibility issue.
          config.build.cssTarget = 'chrome61'
        }

Domain

Subdomains

Frequently Asked Questions

What does viteLegacyPlugin() do?
viteLegacyPlugin() is a function in the vite codebase, defined in packages/plugin-legacy/src/index.ts.
Where is viteLegacyPlugin() defined?
viteLegacyPlugin() is defined in packages/plugin-legacy/src/index.ts at line 145.
What does viteLegacyPlugin() call?
viteLegacyPlugin() calls 10 function(s): buildPolyfillChunk, detectPolyfills, isLegacyBundle, isLegacyChunk, loadBabel, recordAndRemovePolyfillBabelPlugin, replaceLegacyEnvBabelPlugin, replaceModernEnvBabelPlugin, and 2 more.

Analyze Your Own Codebase

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

Try Supermodel Free