How matinzd/rozenite-preview works
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