# imvu/imvujs architecture

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

- Interactive diagram: https://gitdiagram.com/imvu/imvujs
- Repository: https://github.com/imvu/imvujs

## Overview

##### Project Type and Purpose
- imvujs is an open-source JavaScript “standard library and toolchain” for writing modular JS, combining/minifying modules, and running unit tests both in Node.js and browsers.
- Key features: optional module loader, module combiner, build/minify pipeline, unit-testing framework, bundled common libs (jQuery, Backbone, Underscore), support for headless browsers (PhantomJS, Leprechaun).

##### High-Level Component Analysis
A. Core Library (src/)
- src/*.js and .d.ts: JavaScript core (Promise, EventLoop, Module system, ServiceProvider, Timer, etc.)
- imvujstest sub-directory: the in-house unit testing framework modules (Runner, Reporter, Fixture, AssertionError, etc.)
B. Build & CLI Tools
- SConstruct and bin/: entry points for SCons and small command-line utilities (combine, fix-imports, scan-dependencies, test runners).
- s/ directory: convenience shell scripts that wrap common tasks (build, runtests, browsertest, install hooks).
- scons-tools/: custom SCons extension modules (closure.py, uglify.py, module_combine.py).
C. External Dependencies
- ext/: pre-bundled libraries (jQuery, Underscore, Backbone, esprima).
- third-party/: larger external binaries and libs (Closure Compiler, UglifyJS, Node distributions, RequireJS).
D. Tests
- tests/: integration and browser-based tests, organized by functionality (module resolution, combiners, async, CSS, plugin).
- fakes/: fake implementations (FakeTimer, FakeXMLHttpRequestFactory, FakeEventLoop) to support unit tests.

1. “Invoke Build / Test Scripts” → s/ shell scripts or bin/*.js
2. “Import Library” → end users consuming the combined library in Browser or Node.
- Build & Test Path:
- s/ scripts / bin/ tools
↓
- SCons Engine (SConstruct + scons-tools/*)
↓
- Module Combiner & Minifier (module_combine.py + closure.py/uglify.py)
↓
- Output: dist/imvujs.js (minified bundle)
- Test Path:
- s/runtests or bin/node-runner.js / bin/leprechaun-runner.js
↓
- imvujstest framework (src/imvujstest)
↓
- Test adapters: Node.js, PhantomJS, Leprechaun
↓
- Test results (console or HTML reporter)
- Library Runtime:
- Core Modules (src/*.js) ←→ Module Loader (src/module-common.js / web-module.js / node-module.js)
↓
- ServiceProvider (DOM or Node)
↓
- Application code (user modules)
- External Services:
- Closure Compiler / UglifyJS (via scons-tools)
- RequireJS (optional loader)
- Git hooks (githooks/pre-commit)

##### Architectural Patterns & Principles
- Modular design: each feature in its own file under src/, resolved via a custom AMD-like loader.
- Pluggable build pipeline: SCons with custom extension modules driving combine/minify.
- Environment abstraction: ServiceProvider real vs fake for browser/Node test isolation.
- Test sandboxing: fakes/ to isolate global timers, XHR, randomness.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Actors
    Developer["Developer"]:::external

    %% Build & Test Scripts
    subgraph "CLI Tools & Scripts"
        direction TB
        S_Scripts["Shell Wrappers (s/)"]:::cliTools
        BinTools["Bin Utilities (bin/)"]:::cliTools
    end

    %% SCons Engine
    SConstructNode["SCons Engine\n(SConstruct + scons-tools/)"]:::cliTools

    %% Module Combiner & Minifier
    ModuleCombiner["Module Combiner & Minifier\n(module_combine.py + closure.py/uglify.py)"]:::cliTools

    %% Output
    Bundle["Bundle Output\ndist/imvujs.js"]:::output

    %% Test Runner Path
    subgraph "Test Runner"
        direction TB
        TestRunner["Test Runner Scripts\n(bin/node-runner.js, bin/leprechaun-runner.js)"]:::test
        Imvujstest["imvujstest Framework\n(src/imvujstest/)"]:::test
        Adapters["Adapters\n(Node.js, PhantomJS, Leprechaun)"]:::test
        TestResults["Test Results\n(Console/HTML)"]:::output
    end

    %% Core Library Runtime
    subgraph "Library Runtime"
        direction TB
        CoreLibrary["imvujs Core Library\n(src/)"]:::coreLib
        ModuleLoader["Module Loader\n(module-common.js, web-module.js, node-module.js)"]:::coreLib
        ServiceProvider["ServiceProvider Abstraction\n(real & fake)"]:::coreLib
        AppCode["Application Code\n(User Modules)"]:::coreLib
    end

    %% External Tools & Dependencies
    subgraph "External Tools"
        direction TB
        ExtLibs["Bundled Libraries\n(ext/)"]:::external
        ThirdParty["Third-Party Binaries\n(third-party/)"]:::external
        RequireJS["RequireJS (optional)"]:::external
        GitHooks["Git Hooks\n(githooks/pre-commit)"]:::external
    end

    %% Developer flows
    Developer -->|"Invoke Build/Test"| S_Scripts
    Developer -->|"Invoke Build/Test"| BinTools
    Developer -->|"Import Library"| CoreLibrary

    %% Build flow
    S_Scripts --> SConstructNode
    BinTools --> SConstructNode
    SConstructNode --> ModuleCombiner
    ModuleCombiner --> Bundle

    %% Test flow
    BinTools --> TestRunner
    TestRunner --> Imvujstest
    Imvujstest --> Adapters
    Adapters --> TestResults

    %% Runtime flow
    CoreLibrary <--> ModuleLoader
    ModuleLoader --> ServiceProvider
    ServiceProvider --> AppCode

    %% External integrations
    ModuleCombiner --> ExtLibs
    ModuleCombiner --> ThirdParty
    ModuleCombiner --> RequireJS
    SConstructNode --> GitHooks

    %% Click Events
    click CoreLibrary "https://github.com/imvu/imvujs/tree/master/src/"
    click Imvujstest "https://github.com/imvu/imvujs/tree/master/src/imvujstest/"
    click ModuleLoader "https://github.com/imvu/imvujs/blob/master/src/module-common.js"
    click ModuleLoader "https://github.com/imvu/imvujs/blob/master/src/web-module.js"
    click ModuleLoader "https://github.com/imvu/imvujs/blob/master/src/node-module.js"
    click ServiceProvider "https://github.com/imvu/imvujs/blob/master/src/ServiceProvider.fake.js"
    click ServiceProvider "https://github.com/imvu/imvujs/blob/master/src/ServiceProvider.real.domtest.js"
    click ServiceProvider "https://github.com/imvu/imvujs/blob/master/src/ServiceProvider.real.js"
    click Promise "https://github.com/imvu/imvujs/blob/master/src/Promise.js"
    click EventLoop "https://github.com/imvu/imvujs/blob/master/src/EventLoop.js"
    click Timer "https://github.com/imvu/imvujs/blob/master/src/Timer.js"
    click CPS "https://github.com/imvu/imvujs/blob/master/src/cps.js"
    click SConstructNode "https://github.com/imvu/imvujs/tree/master/SConstruct"
    click BinTools "https://github.com/imvu/imvujs/tree/master/bin/"
    click S_Scripts "https://github.com/imvu/imvujs/tree/master/s/"
    click SConsTools "https://github.com/imvu/imvujs/tree/master/scons-tools/"
    click ExtLibs "https://github.com/imvu/imvujs/tree/master/ext/"
    click ThirdParty "https://github.com/imvu/imvujs/tree/master/third-party/"
    click TestsIntegration "https://github.com/imvu/imvujs/tree/master/tests/"
    click Fakes "https://github.com/imvu/imvujs/tree/master/fakes/"
    click GitHooks "https://github.com/imvu/imvujs/tree/master/githooks/pre-commit"

    %% Styles
    classDef cliTools fill:#D0E4F5,stroke:#333,stroke-width:1px
    classDef coreLib fill:#D8F5D0,stroke:#333,stroke-width:1px
    classDef external fill:#E0E0E0,stroke:#333,stroke-width:1px
    classDef output fill:#FFF5B1,stroke:#333,stroke-width:1px
    classDef test fill:#F2D0F5,stroke:#333,stroke-width:1px
```
