How originjs/vite-plugin-federation works
• 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.