# comandeer/rollup-lib-bundler architecture

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

- Interactive diagram: https://gitdiagram.com/comandeer/rollup-lib-bundler
- Repository: https://github.com/comandeer/rollup-lib-bundler

## Overview

##### Main Components
- Source Code Input: Represent the “src” folder that holds the library’s source files. Note the special subdirectories like “src/__bin__” for executables.
- Package Metadata Parser: A component (found in files like packageParser.ts and its submodules) that reads and parses package.json, extracting metadata such as name, version, exports, and bin configurations.
- Bundler Core: The central component (evidenced by files like bundler.ts and the “bundler” directory) that configures and runs Rollup bundling operations. This includes handling the determination of input entry points (e.g. src/index.js or src/index.ts, or even additional chunks via subpath exports).
- Babel Transpilation: Indicate that before or during bundling, the code is piped through Babel for transpilation. (Also note that transpilation targets can be derived from the “engines.node” field in package.json.)
- Terser Minification: Represent the minification step using terser that ensures smaller bundle sizes.
- Rollup Plugins: Within the bundler, there are custom plugins (such as those under “src/bundler/rollupPlugins”) that modify or preserve dynamic imports and resolve linked bundles.
- Output & Dist Handling: Show the process where bundled output is written into the “dist” (or alternative) directories. Highlight that the dist directory is purged before bundling takes place.
- CLI & Executable Handling: A flow for processing the CLI command (the “rlb” mentioned in the usage) that triggers the bundling process, including special handling for executables defined in the “bin” field.
- Testing Framework: Although not part of the production pipeline, note the “tests” directory used for validating functionality and bundling correctness.

##### Depicting Relationships & Data Flow
- From there, the source files (from “src/”) are processed by the Bundler Core where Rollup is configured. Within this core, illustrate interactions with:
- The Babel Transpiler: for converting code based on target node version.
- Custom Rollup Plugins: to manage dynamic import preservation and linked bundle resolution.
- Bundler’s output path determination (using the “exports” field from package.json) and the subsequent writing of bundled files to the correct output directories.
- For binary bundling, indicate a specialized flow: the bundler looks into “src/__bin__” based on the “bin” configuration in package.json, processes the file preserving the shebang, and outputs it as an ESM bundle.
- Optionally, include the Testing suite as a side component that executes tests on various fixture directories simulating different package configurations.

##### Architectural Patterns & Design Principles
- Pipeline/Flow Processing: Clearly represent the sequential steps – from reading package.json, gathering source code, processing through bundling/transpiling, to finally writing bundled files (with purging of output directories at the start).
- Convention over Configuration: Emphasize the opinionated nature – e.g., specific directory names (src, __bin__) and file naming conventions (index.js/ts mapping to “.” export).

##### Technology and Framework Labels
- Babel as the code transpiler.
- Terser for minification.
- Node.js runtime details (and note that “engines.node” can affect the Babel target).
- Optionally, note that tests use typical tools (as hinted at by test files, probably invoking CLI tests and other helpers).

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["Package.json"]
    
    subgraph "Package Metadata Parser"
        B["Parser_Main (packageParser.ts)"]
        B2["Parser_Modules (packageParser/)"]
    end

    C["Source Code Input (src/)"]
    
    subgraph "Bundler Process"
        D["Bundler Core Main (bundler.ts)"]
        E["Rollup Plugins (rollupPlugins/)"]
        F["Babel Transpiler"]
        G["Terser Minifier"]
    end

    H["Output & Dist Handling (OutputController.ts)"]
    I["CLI & Executable Handling (rlb.mts)"]
    J["Testing Framework (tests/)"]

    %% Data Flow
    A -->|"feeds"| B
    A -->|"feeds"| B2
    B -->|"provides_config"| D
    B2 -->|"provides_config"| D
    C -->|"sources"| D
    I -->|"triggers"| D
    D -->|"integrates"| E
    D -->|"transpiles"| F
    D -->|"minifies"| G
    D -->|"writes"| H
    J -.->|"validates"| D

    %% Click Events
    click C "https://github.com/comandeer/rollup-lib-bundler/tree/main/src/"
    click B "https://github.com/comandeer/rollup-lib-bundler/blob/main/src/packageParser.ts"
    click B2 "https://github.com/comandeer/rollup-lib-bundler/tree/main/src/packageParser/"
    click D "https://github.com/comandeer/rollup-lib-bundler/blob/main/src/bundler.ts"
    click E "https://github.com/comandeer/rollup-lib-bundler/tree/main/src/bundler/rollupPlugins/"
    click H "https://github.com/comandeer/rollup-lib-bundler/blob/main/src/OutputController.ts"
    click I "https://github.com/comandeer/rollup-lib-bundler/blob/main/src/__bin__/rlb.mts"
    click J "https://github.com/comandeer/rollup-lib-bundler/tree/main/tests/"

    %% Styles
    classDef input fill:#FFEEAD,stroke:#333,stroke-width:2px;
    classDef processing fill:#B2EBF2,stroke:#333,stroke-width:2px;
    classDef output fill:#C8E6C9,stroke:#333,stroke-width:2px;
    classDef testing fill:#FFCDD2,stroke:#333,stroke-width:2px;

    class A,C,I input;
    class B,B2,D,E,F,G processing;
    class H output;
    class J testing;
```
