# unplugin/unplugin-vue-components architecture

> Architecture diagram of the GitHub repository unplugin/unplugin-vue-components, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-09-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/unplugin/unplugin-vue-components
- Repository: https://github.com/unplugin/unplugin-vue-components

## Overview

##### Project type and purpose
- This is an open-source “unplugin” library for Vue that auto-imports components and directives on demand. It integrates with various bundlers and build tools (Vite, Rollup, Webpack, Rspack, esbuild, Vue CLI, Nuxt) by exposing adapter entry points, and implements a shared core engine under `src/core` for file scanning, resolution, AST transformation, and type declaration generation.

##### File-structure highlights
- Top-level “examples” directories demonstrate usage with different Vue setups.
- `src/` contains:
- `core/` – shared logic:
– `fs/glob.ts` for file matching
– `options.ts` for CLI/plugin config
– `resolvers/` – built-in resolvers per UI library
– `transformer.ts` + `transforms/` for AST injection of imports
– `type-imports/` for generating `components.d.ts`
– `unplugin.ts` – glue code that wires into unplugin framework
- Bundler adapters: `vite.ts`, `rollup.ts`, `webpack.ts`, `rspack.ts`, `esbuild.ts`, `nuxt.ts`
- Entry points: `index.ts`, `resolvers.ts`
- Build and test configs are present (`rollup.ts`, `vitest.config.ts`, etc.).

##### README insights
- Describes features (Vue 2/3, tree-shaking, directives, types) and shows how to install per bundler.
- Lists built-in resolvers for UI libraries (e.g. Ant Design Vue, Element Plus).

a. Main components (blocks):
- User Source Code (Vue SFCs, `.vue`, `.md`)
- Bundler (Vite / Rollup / Webpack / Rspack / esbuild / Vue CLI / Nuxt)
- Unplugin Adapter Layer (one block per adapter entrypoint)
- Plugin Core Engine
- Config Loader
- FileSystem Scanner (glob + dirs)
- Resolver Engine (built-in + custom)
- AST Transformer (component & directive transforms)
- Type Declaration Generator (`components.d.ts`)
- External UI Libraries (AntD, Element-Plus, Vant, etc.)
- Output: Transformed Code + Generated Types

b. Relationships & Interactions:
- Adapter Layer delegates to the Plugin Core Engine.
- Core Engine reads configuration, scans file system for components, calls Resolver Engine (which may consult External UI Libraries), then passes component paths to the AST Transformer.
- Transformer injects import statements into the module AST, then returns code to Bundler.
- Core Engine emits or updates `components.d.ts`.
- Bundler continues with its normal build pipeline.

c. Architectural patterns / principles:
- Adapter Pattern: separate entrypoints per bundler that share a common core.
- Plugin Framework: uses “unplugin” conventions for hooking into multiple build systems.
- Strategy Pattern: resolvers are pluggable functions for different UI libraries.
- Single Source of Truth: central options object flows through scanner, resolver, transformer.

d. Key technologies / libraries:
- unplugin (core plugin framework)
- glob / file-system utilities
- AST parsing (Babel parser or similar)
- TypeScript for type declarations
- Bundler-specific plugin APIs (Vite, Rollup, Webpack, Rspack, esbuild)

## Diagram (Mermaid)

```mermaid
graph TD
    %% User input and bundler
    UserSource["User Source Code\n(.vue, .md)"]:::input
    Bundler["Bundler\n(Vite / Rollup / Webpack / Rspack / esbuild / Vue CLI / Nuxt)"]:::bundler

    UserSource -->|"SourceFiles"| Bundler

    %% Adapter Layer
    subgraph "Adapter Layer"
        direction TB
        Vite["Vite Adapter"]:::adapter
        Rollup["Rollup Adapter"]:::adapter
        Webpack["Webpack Adapter"]:::adapter
        Rspack["Rspack Adapter"]:::adapter
        Esbuild["esbuild Adapter"]:::adapter
        Nuxt["Nuxt Adapter"]:::adapter
    end

    Bundler -->|"plugin hooks"| Vite
    Bundler --> Rollup
    Bundler --> Webpack
    Bundler --> Rspack
    Bundler --> Esbuild
    Bundler --> Nuxt

    %% Core Engine
    subgraph "Plugin Core Engine"
        direction TB
        Unplugin["Unplugin Glue Code"]:::core
        Config["Config Loader"]:::core
        Scanner["FileSystem Scanner\n(glob)"]:::core
        Resolver["Resolver Engine"]:::core
        Transformer["AST Transformer"]:::core
        CompTrans["Component Transform"]:::core
        DirTrans["Directive Transforms"]:::core
        Detect["Type Detection Helper"]:::core
        TypeGen["Type Declaration Generator"]:::core
        EntryMain["Entry Point\nsrc/index.ts"]:::core
        EntryRes["Resolver Utilities\nsrc/resolvers.ts"]:::core
    end

    Vite --> Unplugin
    Rollup --> Unplugin
    Webpack --> Unplugin
    Rspack --> Unplugin
    Esbuild --> Unplugin
    Nuxt --> Unplugin

    Unplugin --> Config
    Config --> Scanner
    Scanner --> Resolver
    Resolver --> Transformer
    Transformer -->|"ImportStatements"| Bundler

    %% Integrate transforms
    Transformer --> CompTrans
    Transformer --> DirTrans

    Resolver --> Detect
    Detect --> TypeGen

    %% External UI Libraries
    subgraph "External UI Libraries"
        EUI["UI Library Resolvers\n(AntD, Element-Plus, Vant, etc.)"]:::external
    end

    Resolver -->|"consults"| EUI

    %% Outputs
    subgraph "Outputs"
        Transformed["Transformed Code"]:::output
        Types["components.d.ts"]:::output
    end

    Transformer -->|"TransformedCode"| Transformed
    TypeGen -->|"GeneratedTypes"| Types

    %% Click Events
    click UserSource "https://github.com/unplugin/unplugin-vue-components/tree/main/examples/"
    click Vite "https://github.com/unplugin/unplugin-vue-components/blob/main/src/vite.ts"
    click Rollup "https://github.com/unplugin/unplugin-vue-components/blob/main/src/rollup.ts"
    click Webpack "https://github.com/unplugin/unplugin-vue-components/blob/main/src/webpack.ts"
    click Rspack "https://github.com/unplugin/unplugin-vue-components/blob/main/src/rspack.ts"
    click Esbuild "https://github.com/unplugin/unplugin-vue-components/blob/main/src/esbuild.ts"
    click Nuxt "https://github.com/unplugin/unplugin-vue-components/blob/main/src/nuxt.ts"
    click Unplugin "https://github.com/unplugin/unplugin-vue-components/blob/main/src/core/unplugin.ts"
    click Config "https://github.com/unplugin/unplugin-vue-components/blob/main/src/core/options.ts"
    click Scanner "https://github.com/unplugin/unplugin-vue-components/blob/main/src/core/fs/glob.ts"
    click Resolver "https://github.com/unplugin/unplugin-vue-components/blob/main/src/core/resolvers/index.ts"
    click CompTrans "https://github.com/unplugin/unplugin-vue-components/blob/main/src/core/transforms/component.ts"
    click DirTrans "https://github.com/unplugin/unplugin-vue-components/blob/main/src/core/transforms/directive/index.ts"
    click Transformer "https://github.com/unplugin/unplugin-vue-components/blob/main/src/core/transformer.ts"
    click Detect "https://github.com/unplugin/unplugin-vue-components/blob/main/src/core/type-imports/detect.ts"
    click TypeGen "https://github.com/unplugin/unplugin-vue-components/blob/main/src/core/type-imports/index.ts"
    click EntryMain "https://github.com/unplugin/unplugin-vue-components/blob/main/src/index.ts"
    click EntryRes "https://github.com/unplugin/unplugin-vue-components/blob/main/src/resolvers.ts"

    %% Styles
    classDef adapter fill:#bbdefb,stroke:#1e88e5,color:#0d47a1
    classDef core fill:#c8e6c9,stroke:#388e3c,color:#1b5e20
    classDef external fill:#ffe0b2,stroke:#f57c00,color:#e65100
    classDef bundler fill:#d1c4e9,stroke:#7e57c2,color:#4a148c
    classDef input fill:#f0f4c3,stroke:#9e9d24,color:#827717
    classDef output fill:#b2dfdb,stroke:#00796b,color:#004d40
```
