How posva/unplugin-vue-router works
To represent unplugin-vue-router accurately, we’ll treat it as an open-source build-time plugin library for Vue/TypeScript. Your goal is to show how it integrates into various bundlers, how it processes source files to generate routes/types at build time, and how it exposes runtime modules for client use. Follow these tailored steps:
Main Components
a. Bundler Integration
• Vite plugin (src/vite.ts)
• Rollup plugin (src/rollup.ts)
• Webpack plugin (src/webpack.ts)
• esbuild plugin (src/esbuild.ts)
b. Build-Time Processing (Codegen & Core)
• Codegen modules (src/codegen/*) for generating route maps, records, and TypeScript declarations
• Core modules (src/core/*) for watching the routes folder, parsing file names, applying options, extending routes
• Data-Loader extensions (src/data-loaders/*) (experimental APIs)
• Volar support for IDE integration (src/volar/*)
c. Runtime Modules
• src/runtime.ts (client-side utilities)
• client.d.ts (type declarations for imports from unplugin-vue-router/client)
• Generated types file (typed-router.d.ts in project root)
d. Documentation Site
• VitePress config (docs/.vitepress/*) and markdown content (docs/guide, docs/data-loaders, etc.)
e. Examples & Tests
• Playground app demonstrating usage (playground/)
• E2E tests (e2e/) and unit tests (tests/)
Relationships & Data Flow
• File System → Bundler plugin entry → Core reads src/pages routes folder → Core emits file list → Codegen generates:
• vue-router/auto-routes module (runtime route array)
• TypeScript declarations (typed-router.d.ts)
• Generated modules are written into the build’s virtual module namespace (virtual:), then passed back to the bundler.
• App Code imports routes from vue-router/auto-routes and calls createRouter → Vue Router runtime.
• Data Loaders may inject code at build time for route-level data fetching.
• Volar hooks into language server via custom blocks for type-safe routing in IDE.
• Docs Site is a separate sub-project built with VitePress, consuming some core modules for live examples.
Architectural Patterns
• Plugin Architecture: each bundler adapter delegates to the same core + codegen logic.
• Code Generation: transforms file-based routes into typed route definitions at build time.
• Virtual Modules: uses the unplugin “virtual module” pattern to export generated code.
• Separation of Concerns:
• Bundler adapters (vite/rollup/webpack/esbuild)
• Core file-watching/parsing
• Codegen and type generation
• Runtime utilities vs. build-time logic
• Bundler Plugins (Vite, Rollup, Webpack, esbuild)
• Core Processor (RoutesFolderWatcher, parser, option handler)
• Codegen Modules (generateRouteMap, generateDTS, etc.)
• Virtual Modules (vue-router/auto-routes, typed-router.d.ts)
• Runtime API (runtime.ts, client.d.ts)
• Application (createRouter, Vue app)
• Data-Loader Extensions
• Volar Integration
• Docs Site & Playground (as consumers)
• Tests (as verification step)
• From Bundler → Core → Codegen → Virtual Modules → Application
• Runtime components in another (e.g., green)
• Consumers (app, docs, playground) in a third (e.g., orange)
Be Detailed but Focused
• Related modules under drop-shadows or containers (e.g., “src/codegen,” “src/core,” “src/data-loaders”).
• Call out important config files (route.schema.json, unplugin-vue-router options).