# matinzd/rozenite-preview architecture

> Architecture diagram of the GitHub repository matinzd/rozenite-preview, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-08-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/matinzd/rozenite-preview
- Repository: https://github.com/matinzd/rozenite-preview

## Overview

##### Project Type and Purpose
- This is an open-source Rozenite plugin package for React Native DevTools, not a standalone app. Its goal is to let developers register React Native components in their app and preview them live in the simulator via a custom DevTools panel.
- Key features: a Babel plugin to instrument code, a PreviewHost component in the app, a DevTools UI panel, and a messaging layer tying them together.

##### File Structure Analysis
- Top-level:
- babel-plugin/… – the code that runs at build time to add metadata/hooks into user code
- react-native.ts – entry point that exports React Native plugin APIs
- src/
– react-native/… – PreviewHost, setup-plugin, and registry that lives in the mobile app
– ui/… – the Preview panel UI (panel layout and CSS) that runs inside DevTools
– shared/… – common types and messaging definitions
– utils/… – helper modules (e.g., signal-map for managing subscriptions)
- Configs and build:
- tsconfig.json, tsconfig.babel-plugin.json – TypeScript setups for plugin and library
- vite.config.ts, vite.babel-plugin.config.ts – bundle the DevTools panel and Babel plugin
- rozenite.config.ts – Rozenite’s plugin manifest describing entry points

##### Core Components
a. Babel Plugin
- Runs at build/transpile time
- Injects registration calls (`registerPreview`) and metadata into user components
b. React Native Host
- PreviewHost React component wraps the app
- preview-registry.ts holds the map of registered previews
- setup-plugin.ts wires the host to Rozenite’s messaging runtime
c. DevTools Panel
- Built with Vite from src/ui/preview-panel.tsx and .css
- Exposes a “Preview” tab inside React Native DevTools
- Sends requests (select, render) to the Host, receives snapshots or errors
d. Messaging Layer
- src/shared/messaging.ts defines message types and channel protocols
- Bi-directional communication between Host (simulator) and Panel (DevTools)
e. Rozenite Core
- The underlying plugin framework that loads this plugin into DevTools
- Invoked via rozenite.config.ts

##### Relationships and Interactions
- Build time: Babel plugin transforms user code → embeds preview hooks
- Runtime (Simulator side):
- PreviewHost initializes messaging client
- User registers components via the API (`registerPreview`)
- preview-registry stores component factories
- Runtime (DevTools side):
- DevTools loads the plugin UI bundle
- Panel UI sends “list previews” and “render preview” commands
- Host listens and responds with rendered output or asset updates
- All messages flow over a channel managed by shared/messaging.ts

##### Important Patterns and Principles
- Plugin-based architecture: clear separation between DevTools UI (panel) and app host
- Publish/subscribe (signal-map) for event management
- Code instrumentation via Babel for minimal runtime overhead
- Type-safe messaging contract across host/panel

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Build-Time Tools
    subgraph "Build Tools"
        direction TB
        BP["Babel Plugin"]:::build
        RH["react-helper.ts"]:::build
        TSBP["tsconfig.babel-plugin.json"]:::build
        VBP["vite.babel-plugin.config.ts"]:::build
        VITE["Vite Bundler"]:::build
    end

    %% React Native App Host
    subgraph "React Native App" 
        direction TB
        PH["PreviewHost"]:::app
        PR["Preview Registry"]:::app
        SP["Setup Plugin"]:::app
        UTIL["signal-map.ts"]:::shared
    end

    %% DevTools UI Panel
    subgraph "DevTools Panel" 
        direction TB
        PP["PreviewPanel UI"]:::ui
        CSS["preview-panel.css"]:::ui
        VITEC["vite.config.ts"]:::build
    end

    %% Shared Messaging
    M["Messaging Layer"]:::shared
    T["types.ts"]:::shared

    %% Rozenite Core
    subgraph "Rozenite Core"
        direction TB
        RC["rozenite.config.ts"]:::core
        RNENTRY["react-native.ts"]:::core
    end

    %% Connections
    BP -->|"inject registerPreview"| PH
    RH --> BP
    TSBP --> BP
    VBP --> BP
    VBP --> VITE
    VITEC --> PP

    PP -->|"PREVIEW_REQUEST"| M
    M -->|"deliver to Host"| SP
    SP -->|"load registry"| PR
    PR --> PH
    PH -->|"PREVIEW_RESPONSE"| M
    M -->|"deliver to Panel"| PP

    RC --> PP
    RNENTRY --> SP

    UTIL --> SP
    UTIL --> PR

    %% Click Events
    click BP "https://github.com/matinzd/rozenite-preview/blob/beta/babel-plugin/preview-babel-plugin.ts"
    click RH "https://github.com/matinzd/rozenite-preview/blob/beta/babel-plugin/react-helper.ts"
    click TSBP "https://github.com/matinzd/rozenite-preview/blob/beta/tsconfig.babel-plugin.json"
    click VBP "https://github.com/matinzd/rozenite-preview/blob/beta/vite.babel-plugin.config.ts"
    click PH "https://github.com/matinzd/rozenite-preview/blob/beta/src/react-native/preview-host.tsx"
    click PR "https://github.com/matinzd/rozenite-preview/blob/beta/src/react-native/preview-registry.ts"
    click SP "https://github.com/matinzd/rozenite-preview/blob/beta/src/react-native/setup-plugin.ts"
    click PP "https://github.com/matinzd/rozenite-preview/blob/beta/src/ui/preview-panel.tsx"
    click CSS "https://github.com/matinzd/rozenite-preview/blob/beta/src/ui/preview-panel.css"
    click VITEC "https://github.com/matinzd/rozenite-preview/blob/beta/vite.config.ts"
    click M "https://github.com/matinzd/rozenite-preview/blob/beta/src/shared/messaging.ts"
    click T "https://github.com/matinzd/rozenite-preview/blob/beta/src/shared/types.ts"
    click RC "https://github.com/matinzd/rozenite-preview/blob/beta/rozenite.config.ts"
    click RNENTRY "https://github.com/matinzd/rozenite-preview/blob/beta/react-native.ts"
    click UTIL "https://github.com/matinzd/rozenite-preview/blob/beta/src/utils/signal-map.ts"

    %% Styles
    classDef build fill:#D0E6FF,stroke:#0066CC,color:#003366
    classDef app fill:#E6FFEA,stroke:#00AA33,color:#007700
    classDef ui fill:#FFE6CC,stroke:#FF6600,color:#CC3300
    classDef shared fill:#F2F2F2,stroke:#999999,color:#333333
    classDef core fill:#DDDDDD,stroke:#777777,color:#555555
```
