Home / Function/ shimDepsPlugin() — vite Function Reference

shimDepsPlugin() — vite Function Reference

Architecture documentation for the shimDepsPlugin() function in rolldown.config.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  8c80e3d1_f111_8342_d8a7_3a0d3c801e64["shimDepsPlugin()"]
  095867e3_0044_bba5_61f7_c60c3c9e56ce["rolldown.config.ts"]
  8c80e3d1_f111_8342_d8a7_3a0d3c801e64 -->|defined in| 095867e3_0044_bba5_61f7_c60c3c9e56ce
  style 8c80e3d1_f111_8342_d8a7_3a0d3c801e64 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/rolldown.config.ts lines 221–291

function shimDepsPlugin(deps: Record<string, ShimOptions[]>): Plugin {
  const transformed: Record<string, boolean> = {}

  return {
    name: 'shim-deps',
    transform: {
      filter: {
        id: new RegExp(`(?:${Object.keys(deps).join('|')})$`),
      },
      handler(code, id) {
        const file = Object.keys(deps).find((file) =>
          id.replace(/\\/g, '/').endsWith(file),
        )
        if (!file) return

        for (const { src, replacement, pattern } of deps[file]) {
          const magicString = new MagicString(code)

          if (src) {
            const pos = code.indexOf(src)
            if (pos < 0) {
              this.error(
                `Could not find expected src "${src}" in file "${file}"`,
              )
            }
            transformed[file] = true
            magicString.overwrite(pos, pos + src.length, replacement)
          }

          if (pattern) {
            let match
            while ((match = pattern.exec(code))) {
              transformed[file] = true
              const start = match.index
              const end = start + match[0].length
              let _replacement = replacement
              for (let i = 1; i <= match.length; i++) {
                _replacement = _replacement.replace(`$${i}`, match[i] || '')
              }
              magicString.overwrite(start, end, _replacement)
            }
            if (!transformed[file]) {
              this.error(
                `Could not find expected pattern "${pattern}" in file "${file}"`,
              )
            }
          }

          code = magicString.toString()
        }

        console.log(`shimmed: ${file}`)

        return code
      },
    },
    buildEnd(err) {
      if (this.meta.watchMode) return

      if (!err) {
        for (const file in deps) {
          if (!transformed[file]) {
            this.error(
              `Did not find "${file}" which is supposed to be shimmed, was the file renamed?`,
            )
          }
        }
      }
    },
  }
}

Domain

Subdomains

Frequently Asked Questions

What does shimDepsPlugin() do?
shimDepsPlugin() is a function in the vite codebase, defined in packages/vite/rolldown.config.ts.
Where is shimDepsPlugin() defined?
shimDepsPlugin() is defined in packages/vite/rolldown.config.ts at line 221.

Analyze Your Own Codebase

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

Try Supermodel Free