# web-infra-dev/rsbuild architecture

> Architecture diagram of the GitHub repository web-infra-dev/rsbuild, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/web-infra-dev/rsbuild
- Repository: https://github.com/web-infra-dev/rsbuild

## Overview

##### Main building blocks and their roles
a. CLI & Entry points – The “packages/core/src/cli” and “packages/create‑rsbuild” sections show that Rsbuild has a user‐facing CLI that reads configuration, dispatches commands (e.g., “build”, “dev server”) and triggers the build pipeline.
b. Configuration subsystem – Notice files such as “packages/core/src/config.ts”, “packages/core/src/mergeConfig.ts”, and several config templates. This portion parses user and default configurations, merges them and exposes a semantic API.
d. Build pipeline and integration – There is a “packages/core” layer that includes code for:
- Loaders (“packages/core/src/loader/transformLoader.ts”, etc.)
- Bundler integration with Rspack (see “packages/core/src/rspack” and associated provider modules)
- Build output, caching, performance, and analysis modules (plugins for bundle analyzer, cache, file size, etc.)
e. Development server – The “packages/core/src/server” directory provides the modules for the dev server (supporting HMR, proxy, overlay, static file serving, etc.).
f. Compatibility and ecosystem support – The “packages/compat” and the plugin categories (webpack compatibility plugins) expose that Rsbuild can leverage the existing ecosystem (webpack plugins and even module federation patterns).

##### Interactions and data flow between components
- User input flows from the CLI (or API invocations) into the configuration subsystem to parse and merge user settings.
- The configuration subsystem then initializes the context and passes it to the Plugin Manager.
- The Plugin Manager dynamically loads plugins (each representing a feature like Babel transformation, Sass processing, etc.) which then register hooks into the build pipeline.
- The core build pipeline orchestrates the sequence of tasks. It calls into loaders, transformation and bundling steps through the Rspack integration layer.
- In development mode, the dev server subsystem is activated; it sets up middleware for HMR, file watching, overlay display, and so on.
- Final output (bundled artifacts, static assets) is produced after passing through transformations and optimizations, which may be handled by plugins such as “minimize”, “sri”, and “mergeConfig”.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "CLI & Entry Points"
        CLI1("CLI & Command Dispatcher"):::cli
        SCF("Scaffolding Tool"):::cli
    end

    subgraph "Configuration Subsystem"
        CFG1("Config Parser (config.ts)"):::config
        CFG2("Config Merger (mergeConfig.ts)"):::config
        CFG3("Config Chain Processor (configChain.ts)"):::config
    end

    subgraph "Plugin System"
        PM("Plugin Manager (Hooks&Lifecycle)"):::plugin
        IP("Plugin Initializer"):::plugin
    end

    subgraph "Plugin Ecosystem"
        P_Babel("Babel Plugin"):::plugin
        P_Less("Less Plugin"):::plugin
        P_Preact("Preact Plugin"):::plugin
        P_React("React Plugin"):::plugin
        P_Sass("Sass Plugin"):::plugin
        P_Solid("Solid Plugin"):::plugin
        P_Stylus("Stylus Plugin"):::plugin
        P_Svelte("Svelte Plugin"):::plugin
        P_Svgr("SVGR Plugin"):::plugin
        P_Vue("Vue Plugin"):::plugin
    end

    subgraph "Build Pipeline & Integration"
        Loader("Loaders"):::build
        Rspack("Rspack Integration"):::build
        Provider("Bundler Provider"):::build
    end

    DS("Dev Server"):::dev
    Output("Output Artifacts"):::build
    Compat("Compatibility & Ecosystem Support"):::compat

    %% Connections between subsystems
    CLI1 -->|"readConfig"| CFG1
    SCF -->|"readConfig"| CFG1

    CFG1 -->|"mergeConfig"| CFG2
    CFG2 -->|"chainProcess"| CFG3

    CFG3 -->|"initContext"| PM

    PM -->|"loadPlugins"| IP
    IP -->|"registerHooks"| Loader

    Loader -->|"transform"| Rspack
    Rspack -->|"bundleFiles"| Provider
    Provider -->|"emitArtifacts"| Output
    Provider -->|"devMiddleware"| DS

    PM ---|"ecosystemBridge"| Compat

    %% Plugin Ecosystem hooks into Plugin Manager
    P_Babel -->|"hook"| PM
    P_Less -->|"hook"| PM
    P_Preact -->|"hook"| PM
    P_React -->|"hook"| PM
    P_Sass -->|"hook"| PM
    P_Solid -->|"hook"| PM
    P_Stylus -->|"hook"| PM
    P_Svelte -->|"hook"| PM
    P_Svgr -->|"hook"| PM
    P_Vue -->|"hook"| PM

    %% Click Events
    click CLI1 "https://github.com/web-infra-dev/rsbuild/tree/main/packages/core/src/cli"
    click SCF "https://github.com/web-infra-dev/rsbuild/tree/main/packages/create‑rsbuild"
    click CFG1 "https://github.com/web-infra-dev/rsbuild/blob/main/packages/core/src/config.ts"
    click CFG2 "https://github.com/web-infra-dev/rsbuild/blob/main/packages/core/src/mergeConfig.ts"
    click CFG3 "https://github.com/web-infra-dev/rsbuild/blob/main/packages/core/src/configChain.ts"
    click PM "https://github.com/web-infra-dev/rsbuild/blob/main/packages/core/src/pluginManager.ts"
    click IP "https://github.com/web-infra-dev/rsbuild/blob/main/packages/core/src/initPlugins.ts"
    click Loader "https://github.com/web-infra-dev/rsbuild/tree/main/packages/core/src/loader"
    click Rspack "https://github.com/web-infra-dev/rsbuild/tree/main/packages/core/src/rspack"
    click Provider "https://github.com/web-infra-dev/rsbuild/tree/main/packages/core/src/provider"
    click DS "https://github.com/web-infra-dev/rsbuild/tree/main/packages/core/src/server"
    click Compat "https://github.com/web-infra-dev/rsbuild/tree/main/packages/compat"
    click P_Babel "https://github.com/web-infra-dev/rsbuild/tree/main/packages/plugin‑babel"
    click P_Less "https://github.com/web-infra-dev/rsbuild/tree/main/packages/plugin‑less"
    click P_Preact "https://github.com/web-infra-dev/rsbuild/tree/main/packages/plugin‑preact"
    click P_React "https://github.com/web-infra-dev/rsbuild/tree/main/packages/plugin‑react"
    click P_Sass "https://github.com/web-infra-dev/rsbuild/tree/main/packages/plugin‑sass"
    click P_Solid "https://github.com/web-infra-dev/rsbuild/tree/main/packages/plugin‑solid"
    click P_Stylus "https://github.com/web-infra-dev/rsbuild/tree/main/packages/plugin‑stylus"
    click P_Svelte "https://github.com/web-infra-dev/rsbuild/tree/main/packages/plugin‑svelte"
    click P_Svgr "https://github.com/web-infra-dev/rsbuild/tree/main/packages/plugin‑svgr"
    click P_Vue "https://github.com/web-infra-dev/rsbuild/tree/main/packages/plugin‑vue"

    %% Style Definitions
    classDef cli fill:#f9c74f,stroke:#333,stroke-width:2px;
    classDef config fill:#90be6d,stroke:#333,stroke-width:2px;
    classDef plugin fill:#f94144,stroke:#333,stroke-width:2px;
    classDef build fill:#577590,stroke:#333,stroke-width:2px;
    classDef dev fill:#277da1,stroke:#333,stroke-width:2px;
    classDef compat fill:#f3722c,stroke:#333,stroke-width:2px;
```
