# originjs/vite-plugin-federation architecture

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

- Interactive diagram: https://gitdiagram.com/originjs/vite-plugin-federation
- Repository: https://github.com/originjs/vite-plugin-federation

## Overview

- A “packages/lib” folder that holds the core library code (including separate folders for development and production use and utilities for processing semver, HTML, etc.).
- “packages/examples” contains many sub-projects or demos that illustrate host–remote configurations using various technology stacks (Vite with ESM, Rollup with systemjs/esm, Webpack with systemjs/var, as well as React and Vue demos).
- Additional top-level files and directories (e.g., .github workflows, ESLint/Prettier configurations, tests) support quality and continuous integration.

##### Main Components
- Core Plugin Library:
– Located in “packages/lib”, this is the heart of the module federation logic.
– Contains separate logic for development (using Vite’s bundleless dev mode) and production (packaged code via Rollup/Webpack).
– Provides shared utilities (e.g. semver, HTML utilities) and type declarations.
- Example Projects:
– Found under “packages/examples”, these illustrate various usage scenarios (basic-host-remote, react-in-vue, simple-react-esm/systemjs, vue2/3 demos, webpack hybrid setups, etc.).
– Serve as isolated instances that act as either “host” or “remote” in a module federation environment.
- Configuration & Build Tools:
– There are multiple configuration files (vite.config.js, rollup.config.mjs, webpack.config.js) in different examples which show how the plugin is integrated into bundler pipelines.
– The CI/CD workflows (.github/workflows) and test setups (various Vitest config files) indicate automated testing for different modes (dev mode, build&serve mode).

##### Relationships and Interactions
- At a high level, the “Core Plugin Library” is integrated into host and remote applications via bundler configuration files:
– Remotes are configured to expose specific modules (as outlined in the README “exposes” section).
– Hosts define “remotes” to fetch remote entry files, with shared dependencies (e.g., vue, react).
– Remote Side: Module exposure – “exposes” configuration from the core library builds the module federation “entry file” (e.g., remoteEntry.js). This is then served by a remote app.
– Host Side: Module consumption – Uses dynamic or static imports to load modules from the remote side. It may use the “virtual:__federation__” runtime methods to add or get remotes dynamically.
- In addition, include connections to external systems such as GitHub (workflow files) and dependency managers (pnpm-workspace.yaml, package.json files).

##### Architectural Patterns & Design Principles
- The design heavily employs modularity and separation of concerns:
– Core plugin logic is decoupled from example applications.
– Each example serves as a “use case” that illustrates integration with a unique bundler strategy or frontend framework.
- It also leverages the micro-frontend design pattern via module federation:
– Clear boundaries exist between host and remote applications.
– Shared dependencies are handled explicitly in the configuration to optimize bundle size.
- The plugin supports both static and dynamic loading:
– Show how the runtime API (“virtual:__federation__” with methods such as __federation_method_setRemote, __federation_method_getRemote, etc.) fits into the overall design.

##### Relevant Technologies, Frameworks, & Tools
- Bundlers: Vite, Rollup, Webpack.
- Frameworks: Vue (Vue2 and Vue3), React.
- Testing: Vitest for both e2e and unit tests.
- CI/CD: GitHub workflows for CI, code quality, and security scorecards.
- Supporting Tools: ESLint, Prettier, Husky for Git hooks.

##### Detail the Essential Architectural Elements
– Plugin Core (handling module federation logic, exposing shared methods and utilities).
– Host and Remote Examples (each with a configuration file that integrates the plugin and demonstrates how they load each other’s modules).
– The pipeline for testing and deployment with CI/CD (indicating GitHub workflows and scripts for dev, build, serve, and stop processes).
– Dynamic remote registration via the “virtual:__federation__” API.

- The separation between the core plugin library and multiple example applications.
- The integration points (configuration files, build scripts) that inject the plugin into host and remote applications.
- The role of shared dependencies and runtime remote addition via the virtual module API.

## Diagram (Mermaid)

```mermaid
graph TD
    A["Monorepo: vite-plugin-federation"]:::monorepo
    A --> B["Core Plugin Library"]:::core
    A --> C["Example Projects"]:::examples
    A --> D["Config & Build Tools / CI/CD"]:::config

    subgraph "Core Plugin Library"
        B1["Dev Logic (src/dev)"]:::core_sub
        B2["Prod Logic (src/prod)"]:::core_sub
        B3["Utilities (src/utils)"]:::core_sub
        B4["Types"]:::core_sub
        B5["Runtime API (public.ts)"]:::runtime
        B --> B1
        B --> B2
        B --> B3
        B --> B4
        B --> B5
    end

    subgraph "Example Projects"
        C1["Host Examples"]:::host
        C2["Remote Examples"]:::remote
        C --> C1
        C --> C2
    end

    subgraph "Config & Build Tools / CI/CD"
        D1["Bundler Configs (vite, rollup, webpack)"]:::config_sub
        D2["CI/CD Workflows (.github/workflows)"]:::config_sub
        D3["Quality Tools (.eslintrc, .prettierrc, pnpm-workspace.yaml)"]:::config_sub
        D --> D1
        D --> D2
        D --> D3
    end

    %% Interactions
    C1 -->|"dynamic_remote_registration"| B5
    B5 -->|"module_exposure"| C2
    D1 -->|"plugin_integration"| B

    %% Styling Classes
    classDef monorepo fill:#FFECB3,stroke:#FF9800,stroke-width:2px;
    classDef core fill:#BBDEFB,stroke:#0D47A1,stroke-width:2px;
    classDef core_sub fill:#E3F2FD,stroke:#0D47A1,stroke-width:1px;
    classDef runtime fill:#FFF9C4,stroke:#FBC02D,stroke-width:2px;
    classDef examples fill:#C8E6C9,stroke:#388E3C,stroke-width:2px;
    classDef host fill:#DCEDC8,stroke:#689F38,stroke-width:1px,rx:10,ry:10;
    classDef remote fill:#FFCDD2,stroke:#C62828,stroke-width:1px,rx:10,ry:10;
    classDef config fill:#D1C4E9,stroke:#512DA8,stroke-width:2px;
    classDef config_sub fill:#EDE7F6,stroke:#512DA8,stroke-width:1px;

    %% Click Events
    click B "https://github.com/originjs/vite-plugin-federation/tree/main/packages/lib"
    click B5 "https://github.com/originjs/vite-plugin-federation/blob/main/packages/lib/src/public.ts"
    click C "https://github.com/originjs/vite-plugin-federation/tree/main/packages/examples"
    click D2 "https://github.com/originjs/vite-plugin-federation/tree/main/.github/workflows"
```
