How unplugin/unplugin-vue-components works
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)