# leaferjs/leaferjs architecture

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

- Interactive diagram: https://gitdiagram.com/leaferjs/leaferjs
- Repository: https://github.com/leaferjs/leaferjs

## Overview

##### Project Type and Purpose
- LeaferJS is an open‑source, frontend Canvas rendering engine and interactive UI library.
- The monorepo uses pnpm workspaces, Rollup for bundling, Vite for development, and shell scripts for build automation.

##### File Structure
- Top‑level config and scripts:
- rollup.config.js, vite.config.ts – build tool configs
- scripts/*.sh – tasks: all.sh (all builds), clear.sh (clean), dts.sh (generate TypeScript defs), update.sh
- pnpm-workspace.yaml, package.json, tsconfig.json – monorepo, package, and TS config
- public/ – static assets for dev server
- index.ts – entry point for demo or custom code
- src/
- README.md – submodule overview

##### Key README Insights
- Requires cloning with --recurse‑submodules to populate src/* from external repos.
- Scripts to install, start a local dev server (Vite on port 10101), and to build “core,” “web,” or “all” bundles via npm run commands.
- MIT‑licensed, targeted at front‑end developers needing high‑performance Canvas and UI building blocks.

##### Main Components
a. Core Engine (“leafer”)
b. Sub‑packages:
- UI layer (leafer-ui)
- Input handling (leafer-in)
- Game abstractions (leafer-game)
- Editor features (leafer-editor)
c. Build Toolchain:
- Rollup bundler
- Vite dev server
- Shell scripts (build, clean, generate typings)
d. Consumer Code:
- index.ts demo or user application
- public assets served by Vite
e. External Interfaces:
- Browser Canvas API
- DOM / Event system

##### Relationships and Interactions
- The Consumer Code (index.ts) imports modules from the sub‑packages.
- Input events flow from the browser through leafer-in to the core engine, then to UI components.
- The UI and editor modules sit atop the core drawing engine. Game logic uses both drawing and input modules.
- During development, Vite serves public/ and index.ts, hot‑reloads code.
- Rollup consumes sub‑packages, outputs bundles (core, web, all) and generates .d.ts files via scripts.

##### Architectural Patterns and Design Principles
- Monorepo with modular sub‑packages (plugin‑style extensibility).
- Clear separation: Core engine vs. UI vs. Input vs. Extensions (game, editor).
- Build‑time bundling vs. runtime dev server.
- Convention over configuration: standardized folder layout, scripts, and configs.

##### Detail Level
- Sub‑package as its own node.
- Shared dependencies (e.g., TypeScript types, shared utils).
- Shell scripts as automation blocks tied to the Rollup configuration.
- Do not over‑complicate: illustrate major components and their interactions clearly.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Monorepo: src"
        core["Core Engine (leafer)"]:::pkg
        ui["UI Module (leafer-ui)"]:::pkg
        input["Input Module (leafer-in)"]:::pkg
        draw["Drawing Module (leafer-draw)"]:::pkg
        game["Game Module (leafer-game)"]:::pkg
        editor["Editor Module (leafer-editor)"]:::pkg
    end

    consumer["Consumer Entry Point (index.ts)"]:::run
    public["Public Assets (public/)"]:::run

    vite["Vite Dev Server (vite.config.ts)"]:::build
    rollup["Rollup Bundler (rollup.config.js)"]:::build

    subgraph "Shell Scripts"
        all["all.sh"]:::script
        clear["clear.sh"]:::script
        dts["dts.sh"]:::script
        update["update.sh"]:::script
    end

    bundles["Output Bundles (core, web, all)"]:::artifact

    browser["Browser"]:::runtime
    canvas["Canvas API"]:::runtime
    dom["DOM/Event System"]:::runtime

    %% Development Workflow
    consumer -->|"served by"| vite
    public --> vite
    vite --> browser

    %% Runtime Interactions
    browser --> dom
    dom --> input
    input --> core
    core --> ui
    core --> draw
    core --> game
    core --> editor
    game --> draw
    game --> input
    editor --> ui
    editor --> draw

    %% Consumer Imports
    consumer --> core
    consumer --> ui
    consumer --> input
    consumer --> draw
    consumer --> game
    consumer --> editor

    %% Build Workflow
    core --> rollup
    ui --> rollup
    input --> rollup
    draw --> rollup
    game --> rollup
    editor --> rollup
    rollup --> bundles
    all --> rollup
    clear --> rollup
    dts --> rollup
    update --> rollup

    %% Click Events
    click core "https://github.com/leaferjs/leaferjs/tree/main/src/leafer"
    click ui "https://github.com/leaferjs/leaferjs/tree/main/src/ui"
    click input "https://github.com/leaferjs/leaferjs/tree/main/src/in"
    click draw "https://github.com/leaferjs/leaferjs/tree/main/src/draw"
    click game "https://github.com/leaferjs/leaferjs/tree/main/src/game"
    click editor "https://github.com/leaferjs/leaferjs/tree/main/src/editor"
    click rollup "https://github.com/leaferjs/leaferjs/blob/main/rollup.config.js"
    click vite "https://github.com/leaferjs/leaferjs/blob/main/vite.config.ts"
    click all "https://github.com/leaferjs/leaferjs/blob/main/scripts/all.sh"
    click clear "https://github.com/leaferjs/leaferjs/blob/main/scripts/clear.sh"
    click dts "https://github.com/leaferjs/leaferjs/blob/main/scripts/dts.sh"
    click update "https://github.com/leaferjs/leaferjs/blob/main/scripts/update.sh"
    click consumer "https://github.com/leaferjs/leaferjs/blob/main/index.ts"
    click public "https://github.com/leaferjs/leaferjs/tree/main/public/"

    %% Styles
    classDef pkg fill:#E3F2FD,stroke:#1E88E5,stroke-width:1px
    classDef build fill:#FFF3E0,stroke:#FB8C00,stroke-width:1px
    classDef script fill:#FFEBEE,stroke:#E53935,stroke-width:1px
    classDef run fill:#E8F5E9,stroke:#43A047,stroke-width:1px
    classDef runtime fill:#F3E5F5,stroke:#8E24AA,stroke-width:1px
    classDef artifact fill:#FFFDE7,stroke:#FDD835,stroke-width:1px
```
