# yashpatel45/stylex architecture

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

- Interactive diagram: https://gitdiagram.com/yashpatel45/stylex
- Repository: https://github.com/yashpatel45/stylex

## Overview

##### Project type and purpose
- This is a JavaScript monorepo hosting StyleX, a CSS-in-JS library plus a suite of related packages (Babel plugin, Webpack/Rollup plugins, ESLint plugin, Next.js integration, a “dev runtime,” and a collection of open-props).
- It also contains example “apps” (documentation site, Next.js demo, Rollup demo, Webpack demo) that consume these packages to show integration points.

##### High-level file-structure analysis
- Top level
- CI/configuration files (.github workflows, .eslintrc, .prettierignore, vercel.json)
- package.json & npm-workspace setup
- “apps/” folder with:
– docs (Docusaurus-based site)
– nextjs-example (Next.js + stylex)
– rollup-example
– webpack-example
- “packages/” folder with individual packages:
- stylex (core runtime + API)
- shared (core CSS-generation & utility functions)
- babel-plugin (compile-time stylex transforms)
- dev-runtime (in-browser helper for stylex)
- eslint-plugin (lint rules for stylex usage)
- nextjs-plugin (Next.js custom loader + plugin)
- rollup-plugin (Rollup plugin for stylex assets)
- webpack-plugin (Webpack loader + plugin)
- open-props (design tokens packaged as stylex styles)

##### Relevant insights from README
- Confirms monorepo structure: “apps” are example consumers, “packages” are published modules.
- Emphasizes build/test tasks per package.
- Mentions architectural principles (e.g., compile-time transforms + runtime injection).

##### Main components
A. Monorepo Root
– NPM Workspaces & Lerna (or similar) orchestrator
– CI (GitHub Actions workflows for tests, performance, typos)
B. Package Group
1. Core Layer
- shared (common CSS gen, preprocess rules)
- stylex (public API: create/apply, runtime injection)
- dev-runtime (hot-reload & metadata in dev env)
2. Compiler/Build Plugins
- babel-plugin (AST transforms of stylex calls → CSS metadata)
- eslint-plugin (linting rules for stylex patterns)
3. Bundler Integrations
- webpack-plugin (loader+plugin to handle stylex CSS at bundle time)
- rollup-plugin (similar integration for Rollup)
- nextjs-plugin (glue for Next.js’ Webpack config + SSR)
4. Token Library
- open-props (preset design tokens, packaged as stylex definitions)
C. Consumer Apps
– docs (Docusaurus site uses stylex packages + open-props)
– nextjs-example (Next.js app integrating via nextjs-plugin)
– rollup-example & webpack-example (demo bundler setups)
D. External Services
– npm registry (publishing each package)
– GitHub (source, issues, CI)
– Vercel (hosting docs / examples)

##### Relationships & data flow
- shared → stylex → dev-runtime (stylex imports shared; dev-runtime wraps stylex)
- stylex & shared form the foundation; all plugins depend on them.
- babel-plugin processes user code at compile time, emitting CSS metadata read by runtime.
- bundler plugins (webpack/rollup/nextjs) hook into the build pipeline to collect and emit final CSS assets.
- eslint-plugin runs statically on user code.
- open-props is just a dependency of stylex or of user code; it provides ready-made style definitions.
- Consumer apps import stylex (and possibly open-props) directly, or rely on nextjs-plugin/babel-plugin at build time.
- CI invokes build/test scripts across all packages and apps, then publishes to npm.

##### Architectural patterns & design principles
- Monorepo with NPM Workspaces – single source of truth, shared versioning.
- Clear separation of concerns: core runtime vs. compile-time transforms vs. bundler integrations vs. linting.
- Plugin-based architecture: each package is small, focused on one integration point.
- “Shared” package centralizes common logic to avoid duplication.
- Example apps demonstrate “contracts” with each plugin.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Monorepo Root
    MR["Monorepo Root\n(Workspaces)"]:::internal
    CI["CI & Workflows"]:::internal
    MR --> CI
    click MR "https://github.com/yashpatel45/stylex/blob/main/package.json"
    click CI "https://github.com/yashpatel45/stylex/blob/main/.github/workflows/tests.yml"

    %% Packages
    subgraph "packages/"
        subgraph "Core Layer" 
            Shared["shared"]:::core
            Stylex["stylex"]:::core
            DevRuntime["dev-runtime"]:::core
        end
        subgraph "Compiler/Build Plugins"
            Babel["babel-plugin"]:::plugins
            ESLint["eslint-plugin"]:::plugins
        end
        subgraph "Bundler Integrations"
            WebpackPlugin["webpack-plugin"]:::plugins
            RollupPlugin["rollup-plugin"]:::plugins
            NextjsPlugin["nextjs-plugin"]:::plugins
        end
        subgraph "Token Library"
            OpenProps["open-props"]:::core
        end
    end

    %% Consumer Apps
    subgraph "apps/"
        Docs["docs\n(Docusaurus)"]:::apps
        NextApp["nextjs-example"]:::apps
        RollupApp["rollup-example"]:::apps
        WebpackApp["webpack-example"]:::apps
    end

    %% External Services
    NPM["npm Registry"]:::external
    GitHub["GitHub"]:::external
    Vercel["Vercel"]:::external

    %% Core dependencies
    Shared -->|"dependency"| Stylex
    Stylex -->|"wraps"| DevRuntime
    OpenProps -->|"design tokens"| Stylex

    %% Plugins dependencies
    Stylex -->|"compile-time transform"| Babel
    Stylex -->|"lint rules"| ESLint
    Stylex -->|"bundle integration"| WebpackPlugin
    Stylex -->|"bundle integration"| RollupPlugin
    Stylex -->|"bundle integration"| NextjsPlugin

    %% Consumer apps usage
    Docs -->|"uses"| Stylex
    Docs -->|"uses"| OpenProps
    NextApp -->|"uses"| Stylex
    NextApp -->|"uses"| NextjsPlugin
    RollupApp -->|"uses"| RollupPlugin
    WebpackApp -->|"uses"| WebpackPlugin

    %% CI pipeline
    CI -->|"build/test"| Shared
    CI -->|"build/test"| Stylex
    CI -->|"build/test"| DevRuntime
    CI -->|"build/test"| Babel
    CI -->|"build/test"| ESLint
    CI -->|"build/test"| WebpackPlugin
    CI -->|"build/test"| RollupPlugin
    CI -->|"build/test"| NextjsPlugin
    CI -->|"build/test"| OpenProps
    CI -->|"build/test"| Docs
    CI -->|"build/test"| NextApp
    CI -->|"build/test"| RollupApp
    CI -->|"build/test"| WebpackApp

    %% Publishing
    Shared -->|"publish"| NPM
    Stylex -->|"publish"| NPM
    DevRuntime -->|"publish"| NPM
    Babel -->|"publish"| NPM
    ESLint -->|"publish"| NPM
    WebpackPlugin -->|"publish"| NPM
    RollupPlugin -->|"publish"| NPM
    NextjsPlugin -->|"publish"| NPM
    OpenProps -->|"publish"| NPM

    %% External service flows
    GitHub -->|"source & CI"| MR
    Vercel -->|"hosts"| Docs
    Vercel -->|"hosts"| NextApp

    %% Click Events
    click Shared "https://github.com/yashpatel45/stylex/tree/main/packages/shared"
    click Stylex "https://github.com/yashpatel45/stylex/tree/main/packages/stylex"
    click DevRuntime "https://github.com/yashpatel45/stylex/tree/main/packages/dev-runtime"
    click Babel "https://github.com/yashpatel45/stylex/tree/main/packages/babel-plugin"
    click ESLint "https://github.com/yashpatel45/stylex/tree/main/packages/eslint-plugin"
    click WebpackPlugin "https://github.com/yashpatel45/stylex/tree/main/packages/webpack-plugin"
    click RollupPlugin "https://github.com/yashpatel45/stylex/tree/main/packages/rollup-plugin"
    click NextjsPlugin "https://github.com/yashpatel45/stylex/tree/main/packages/nextjs-plugin"
    click OpenProps "https://github.com/yashpatel45/stylex/tree/main/packages/open-props"
    click Docs "https://github.com/yashpatel45/stylex/tree/main/apps/docs"
    click NextApp "https://github.com/yashpatel45/stylex/tree/main/apps/nextjs-example"
    click RollupApp "https://github.com/yashpatel45/stylex/tree/main/apps/rollup-example"
    click WebpackApp "https://github.com/yashpatel45/stylex/tree/main/apps/webpack-example"

    %% Styles
    classDef core fill:#cce5ff,stroke:#004085
    classDef plugins fill:#d4edda,stroke:#155724
    classDef apps fill:#fff3cd,stroke:#856404
    classDef external fill:#e2e3e5,stroke:#6c757d
    classDef internal stroke-dasharray: 5 5,fill:#f8f9fa,stroke:#6c757d
```
