# surjitsahoo/mf-cra architecture

> Architecture diagram of the GitHub repository surjitsahoo/mf-cra, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-10-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/surjitsahoo/mf-cra
- Repository: https://github.com/surjitsahoo/mf-cra

## Overview

##### Project type and purpose
- This is an open-source, Lerna-managed monorepo containing:
- A CLI/library package (“mf-cra”) that injects Webpack Module Federation support into Create-React-App projects without ejecting
- Several example CRA micro-frontend apps (shell + remotes) demonstrating both static and dynamic federation

##### High-level components
- Monorepo root
- lerna.json (workspace orchestration)
- package.json (dev dependencies, scripts)
- packages/mf-cra (the core library/CLI)
- bin/mf-cra.js (CLI entry)
- scripts/overrides/webpack-config.js (patches CRA webpack config to add ModuleFederationPlugin)
- dynamicModuleFederation/* (runtime helpers: getOrLoadRemote, useFederatedComponent)
- examples/ (sample CRA apps)
- shell-app (host)
- app1, app2, redux-store (remotes)
- Each has moduleFederation.config.js, public/ + src/, bootstrap.tsx, index.ts

##### Core architectural patterns
- Monorepo (Lerna) for simultaneous versioning & local linking of library + samples
- Micro-frontend pattern via Webpack Module Federation:
- Build-time: mf-cra CLI reads moduleFederation.config.js → injects ModuleFederationPlugin into CRA’s webpack config → emits remoteEntry.js
- Runtime: host (shell-app) dynamically loads remoteEntry.js from remote apps and mounts exposed components (static import or via useFederatedComponent)
- Separation of concerns:
- CLI tooling (packages/mf-cra)
- Build configuration (moduleFederation.config.js in examples)
- Runtime federation logic (dynamicModuleFederation helpers)

##### Relationships and data flows
a. Developer invokes “mf-cra start/build” in an example app
b. CLI loads moduleFederation.config.js → patches CRA webpack config
c. Webpack builds each app, producing:
- host bundle + dynamic import hooks
- for each remote: remoteEntry.js + chunked modules
d. At runtime, host fetches remoteEntry.js from remotes → resolves exposed modules → renders federated components

- Optional: version badges or environment (NODE_ENV, PUBLIC_URL) callouts

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Monorepo Tooling Layer
    subgraph "Monorepo Tooling"
        direction TB
        M_Lerna["lerna.json"]:::monorepo
        M_Pkg["package.json"]:::monorepo
    end

    %% Library Package Layer
    subgraph "Library Package (mf-cra)"
        direction TB
        CLI["mf-cra CLI\n(bin/mf-cra.js)"]:::library
        Override["webpack-config.js\n(patch CRA webpack)"]:::library
        subgraph "Runtime Federation Helpers"
            direction TB
            GetOr["getOrLoadRemote.ts"]:::library
            UseFedComp["useFederatedComponent.ts"]:::library
            Loader["componentLoaderFromWebpackContainer.ts"]:::library
            DynIndex["index.ts"]:::library
            DynTypes["types.ts"]:::library
        end
        Dist["dist/*"]:::artifact
    end

    %% Example Apps Layer
    subgraph "Example Apps"
        direction TB

        subgraph "Host (shell-app)" 
            direction TB
            HostConfig["moduleFederation.config.js"]:::example
            HostIndex["index.ts"]:::example
            HostBootstrap["bootstrap.tsx"]:::example
            HostApp["App.tsx"]:::example
            HostMeta["remoteApps.json"]:::example
            HostRemoteComp["remoteApp.tsx"]:::example
            HostBundle["bundle.js"]:::artifact
        end

        subgraph "Remotes"
            direction TB

            subgraph "app1"
                direction TB
                App1Config["moduleFederation.config.js"]:::example
                App1Index["index.ts"]:::example
                App1Bootstrap["bootstrap.tsx"]:::example
                App1Routes["routes.tsx"]:::example
                App1Hello["hello.tsx"]:::example
                App1RemoteEntry["remoteEntry.js"]:::artifact
            end

            subgraph "app2"
                direction TB
                App2Config["moduleFederation.config.js"]:::example
                App2Index["index.ts"]:::example
                App2Bootstrap["bootstrap.tsx"]:::example
                App2Routes["routes.tsx"]:::example
                App2Hello["hello.tsx"]:::example
                App2Store["reduxStore/*"]:::example
                App2RemoteEntry["remoteEntry.js"]:::artifact
            end

            subgraph "redux-store"
                direction TB
                RSConfig["moduleFederation.config.js"]:::example
                RSIndex["index.ts"]:::example
                RSBootstrap["bootstrap.tsx"]:::example
                RSStore["reduxStore/*"]:::example
                RSRemoteEntry["remoteEntry.js"]:::artifact
            end

        end
    end

    %% Relationships - Build-time
    M_Lerna -->|links| CLI
    M_Lerna -->|links| HostConfig
    M_Lerna -->|links| App1Config
    M_Lerna -->|links| App2Config
    M_Lerna -->|links| RSConfig
    M_Pkg -->|scripts| CLI

    CLI -->|"injects MF plugin"| Override
    Override -->|patches CRA webpack| Webpack[("Webpack")]:::library
    Webpack -->|builds host| HostBundle
    Webpack -->|builds remote| App1RemoteEntry
    Webpack -->|builds remote| App2RemoteEntry
    Webpack -->|builds remote| RSRemoteEntry
    Webpack -->|outputs dist| Dist

    %% Relationships - Package Runtime
    Webpack -.->|uses helpers| GetOr
    Webpack -.->|uses helpers| UseFedComp
    Webpack -.->|uses helpers| Loader
    Webpack -.->|uses helpers| DynIndex
    Webpack -.->|uses helpers| DynTypes

    %% Relationships - Runtime
    HostApp -->|"runtime load"| App1RemoteEntry
    HostApp -->|"runtime load"| App2RemoteEntry
    HostApp -->|"runtime load"| RSRemoteEntry

    %% Click Events
    click M_Lerna "https://github.com/surjitsahoo/mf-cra/blob/main/lerna.json"
    click M_Pkg "https://github.com/surjitsahoo/mf-cra/blob/main/package.json"
    click CLI "https://github.com/surjitsahoo/mf-cra/blob/main/packages/mf-cra/bin/mf-cra.js"
    click Override "https://github.com/surjitsahoo/mf-cra/blob/main/packages/mf-cra/scripts/overrides/webpack-config.js"
    click GetOr "https://github.com/surjitsahoo/mf-cra/blob/main/packages/mf-cra/dynamicModuleFederation/getOrLoadRemote.ts"
    click UseFedComp "https://github.com/surjitsahoo/mf-cra/blob/main/packages/mf-cra/dynamicModuleFederation/useFederatedComponent.ts"
    click Loader "https://github.com/surjitsahoo/mf-cra/blob/main/packages/mf-cra/dynamicModuleFederation/componentLoaderFromWebpackContainer.ts"
    click DynIndex "https://github.com/surjitsahoo/mf-cra/blob/main/packages/mf-cra/dynamicModuleFederation/index.ts"
    click DynTypes "https://github.com/surjitsahoo/mf-cra/blob/main/packages/mf-cra/dynamicModuleFederation/types.ts"
    click Dist "https://github.com/surjitsahoo/mf-cra/tree/main/packages/mf-cra/dist/"
    click HostConfig "https://github.com/surjitsahoo/mf-cra/blob/main/examples/shell-app/moduleFederation.config.js"
    click HostIndex "https://github.com/surjitsahoo/mf-cra/blob/main/examples/shell-app/src/index.ts"
    click HostBootstrap "https://github.com/surjitsahoo/mf-cra/blob/main/examples/shell-app/src/bootstrap.tsx"
    click HostApp "https://github.com/surjitsahoo/mf-cra/blob/main/examples/shell-app/src/App.tsx"
    click HostMeta "https://github.com/surjitsahoo/mf-cra/blob/main/examples/shell-app/src/remoteApps.json"
    click HostRemoteComp "https://github.com/surjitsahoo/mf-cra/blob/main/examples/shell-app/src/remoteApp.tsx"
    click App1Config "https://github.com/surjitsahoo/mf-cra/blob/main/examples/app1/moduleFederation.config.js"
    click App1Index "https://github.com/surjitsahoo/mf-cra/blob/main/examples/app1/src/index.ts"
    click App1Bootstrap "https://github.com/surjitsahoo/mf-cra/blob/main/examples/app1/src/bootstrap.tsx"
    click App1Routes "https://github.com/surjitsahoo/mf-cra/blob/main/examples/app1/src/mf/routes.tsx"
    click App1Hello "https://github.com/surjitsahoo/mf-cra/blob/main/examples/app1/src/mf/hello.tsx"
    click App2Config "https://github.com/surjitsahoo/mf-cra/blob/main/examples/app2/moduleFederation.config.js"
    click App2Index "https://github.com/surjitsahoo/mf-cra/blob/main/examples/app2/src/index.ts"
    click App2Bootstrap "https://github.com/surjitsahoo/mf-cra/blob/main/examples/app2/src/bootstrap.tsx"
    click App2Routes "https://github.com/surjitsahoo/mf-cra/blob/main/examples/app2/src/mf/routes.tsx"
    click App2Hello "https://github.com/surjitsahoo/mf-cra/blob/main/examples/app2/src/mf/hello.tsx"
    click App2Store "https://github.com/surjitsahoo/mf-cra/tree/main/examples/app2/src/reduxStore/"
    click RSConfig "https://github.com/surjitsahoo/mf-cra/blob/main/examples/redux-store/moduleFederation.config.js"
    click RSIndex "https://github.com/surjitsahoo/mf-cra/blob/main/examples/redux-store/src/index.ts"
    click RSBootstrap "https://github.com/surjitsahoo/mf-cra/blob/main/examples/redux-store/src/bootstrap.tsx"
    click RSStore "https://github.com/surjitsahoo/mf-cra/tree/main/examples/redux-store/src/reduxStore/"

    %% Styles
    classDef monorepo fill:#ddd,stroke:#333,stroke-width:1px
    classDef library fill:#bbf,stroke:#333,stroke-width:1px
    classDef example fill:#bfb,stroke:#333,stroke-width:1px
    classDef artifact fill:#999,stroke:#333,stroke-width:1px,stroke-dasharray: 2 2
```
