How vitejs/vite-plugin-react works
Project type and purpose
• This is an open-source monorepo hosting two Vite plugins—@vitejs/plugin-react (Babel-based) and @vitejs/plugin-react-swc (SWC-based)—plus shared utilities, playgrounds for examples/tests, build scripts, and CI/CD workflows.
• Its purpose is to extend the Vite bundler with React support: JSX transforms, fast refresh HMR, SSR hooks, etc.
High-level file-structure analysis
• Root (pnpm workspace): orchestrates installs, builds, releases. Contains global config (.eslintrc, tsconfig, GitHub workflows).
• packages/common: shared runtime code and helper utilities for fast refresh, warning, runtime injection.
• packages/plugin-react: Babel-based plugin; exposes a Vite plugin object, uses common refresh logic, has copy-runtime scripts.
• playground/: manual/demo apps (single‐package Vite projects) for end-to-end verification.
• scripts/: toolbelt for building CJS/ESM bundles, publishing, patching.
• .github/workflows: CI pipelines (lint, test, publish).
Key insights from README
• Directs readers to the two plugin READMEs for feature details (JSX, fast refresh, SSR).
• Emphasizes support for both Babel and SWC engines under a unified Vite plugin interface.
a. Monorepo orchestrator (pnpm workspace + root scripts + CI/CD)
b. packages/common module (shared runtime & utils)
c. plugin-react package (Babel transform + Vite plugin hooks)
d. plugin-react-swc package (SWC transform + Vite plugin hooks + playground tests)
e. Playground projects (real-world Vite projects consuming the plugins)
f. External services/dependencies: Vite core, Babel, SWC, Node.js ecosystem, npm registry/GitHub
Relationships and interactions
• The Monorepo Orchestrator installs dependencies and invokes build scripts.
• Both plugin packages import from common to apply refresh runtime injection.
• Each plugin exports a Vite plugin object implementing hooks (config, resolveId, load, transform) that:
• Receive source files from the user’s Vite build pipeline
• Apply Babel or SWC transforms (JSX, refresh annotations)
• Inject runtime code (copied from common)
• Return transformed code to Vite for bundling/HMR
• Playground projects import and configure the plugin, triggering the plugin’s hooks during their local dev/build.
• CI/CD (GitHub Actions) runs unit tests, playground e2e tests, publishes to npm.
Architectural patterns & design principles
• Monorepo with workspace-level dependency management for code sharing & atomic releases.
• Separation of concerns: common runtime code extracted to packages/common; two adapter packages layer on top (one for Babel, one for SWC).
• Plugin pattern: implementing Vite’s plugin interface to hook into the compiler lifecycle.
• Declarative build configuration via build.config.ts and tsconfig.json.
Essential elements
• Module boundaries for root, common, plugin-react, plugin-react-swc, playgrounds
• Vite plugin lifecycle: config → load → transform → handle HMR
• Build & release pipeline (scripts + GitHub Actions)
• External dependencies (Vite core, Babel, SWC, Node/npm registry)