# saltyshiomix/nextron architecture

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

- Interactive diagram: https://gitdiagram.com/saltyshiomix/nextron
- Repository: https://github.com/saltyshiomix/nextron

## Overview

##### Project Type and Purpose
- This is an open-source developer tool (a CLI/library) called “Nextron” that glues Next.js (renderer) together with Electron (desktop shell), providing `dev` and `build` commands, default webpack/Babel configs, and electron-builder integration.

##### File Structure Analysis
- Top-level scripts:
- babel.js – default Babel preset wrapper for main process
- dev.js – entry point for `nextron` / `nextron dev`
- rollup.config.js – bundling the CLI tool itself
- Core library (`lib`):
- lib/nextron.ts – CLI bootstrap & command dispatcher
- lib/nextron-dev.ts – implements the development server flow
- lib/nextron-build.ts – implements the production build & packaging flow
- lib/configs/* – functions that produce base, dev, and prod Webpack configs, and Nextron CLI config loader
- lib/logger.ts – logging utility
- Examples & Docs: directories showing user-project templates; not part of the runtime but illustrate usage.
- Config files: tsconfig.json, eslint.config.mjs, .babelrc (customizable), electron-builder.yml (packaging settings)

##### Key Insights from README
- CLI commands:
- `nextron` / `nextron dev` – launches Next.js dev server + Electron app
- `nextron build` – builds renderer (Next.js export) + main process + packages via electron-builder
- Extensibility: `nextron.config.js` hook to override source dirs & Webpack config
- Options: `--renderer-port`, `--run-only`, `--startup-delay`, `--electron-options`, `--config`, etc.

a. Main Components
1. CLI Entry Point
- dev.js / build.js → invokes `lib/nextron.ts`
2. Command Dispatcher
- Parses CLI args, chooses DevFlow or BuildFlow
3. Configuration Loader
- `lib/configs/getNextronConfig.ts` reads `nextron.config.js` or defaults
4. Webpack Config Generators
- `webpack.config.base.ts`, `.development.ts`, `.production.ts`
5. DevFlow (`lib/nextron-dev.ts`)
- Spawns Next.js dev server with renderer port
- Monitors Next.js readiness, spawns Electron with preload & background scripts
6. BuildFlow (`lib/nextron-build.ts`)
- Runs Webpack build for main process
- Runs Next.js export (static HTML under `app` directory)
- Invokes electron-builder with `electron-builder.yml` or CLI flags
7. Logger
- Shared logging interface across flows
8. External Processes & Services
- Node.js process for Next.js (renderer)
- Electron process for main & preload
- electron-builder for packaging

b. Relationships & Interactions
- CLI → Dispatcher → Config Loader →
- if dev: DevFlow → spawn Next.js → once ready spawn Electron
- if build: BuildFlow → Webpack(main) & Next.js(export) → electron-builder
- Config Loader injects user overrides into Webpack config generators
- Webpack Configs feed into both DevFlow and BuildFlow

c. Architectural Patterns & Principles
- Modular command pattern: separate modules for Dev and Build flows
- Plugin/Hook points: users can override or extend Webpack configs via `nextron.config.js` and `.babelrc`
- Process orchestration: orchestrates multiple child processes (Next.js server, Electron, electron-builder)
- Convention over configuration: sensible defaults in `lib/configs`, customizable via config files

d. Key Technologies & Frameworks
- Node.js & npm scripts (CLI)
- TypeScript (lib/*.ts) & Babel (babel.js)
- Webpack for bundling main process
- Next.js for renderer (both dev server and static export)
- Electron (main, preload, renderer hosting static Next.js pages)
- electron-builder for packaging

##### Essential Architectural Elements to Capture
- Separation of concerns: CLI vs. config vs. orchestration vs. packaging
- Flow of execution for both dev and build commands
- Extension/hook points where developers can customize behavior
- Interactions with external processes (Next.js dev server, Electron runtime, packaging tool)

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "CLI Scripts"
        CLI_Dev["CLI Entry Point (dev.js)"]:::cli
        CLI_Babel["Babel Preset Wrapper (babel.js)"]:::cli
        CLI_Rollup["CLI Bundler Configuration (rollup.config.js)"]:::cli
    end

    subgraph "Core Library"
        Dispatcher["Command Dispatcher & CLI Bootstrap\n(lib/nextron.ts)"]:::core
        DevFlow["DevFlow Implementation\n(lib/nextron-dev.ts)"]:::core
        BuildFlow["BuildFlow Implementation\n(lib/nextron-build.ts)"]:::core
        Logger["Logging Utility\n(lib/logger.ts)"]:::core
    end

    subgraph "Config Modules"
        ConfigLoader["Configuration Loader\n(lib/configs/getNextronConfig.ts)"]:::config
        WebpackBase["Base Webpack Config Generator\n(lib/configs/webpack.config.base.ts)"]:::config
        WebpackDev["Development Webpack Config Generator\n(lib/configs/webpack.config.development.ts)"]:::config
        WebpackProd["Production Webpack Config Generator\n(lib/configs/webpack.config.production.ts)"]:::config
        BabelConfigGen["Babel Config Generator\n(lib/configs/getBabelConfig.ts)"]:::config
        ExportHook["Nextron Export Command Hook\n(lib/configs/useExportCommand.ts)"]:::config
    end

    subgraph "User Config Files"
        UserNextronConfig["nextron.config.js"]:::ucfg
        UserBabelRC[".babelrc"]:::ucfg
    end

    subgraph "External Processes & Services"
        NextServer["Next.js Dev Server"]:::ext
        NextExport["Next.js Static Export"]:::ext
        ElectronApp["Electron Runtime"]:::ext
        Builder["electron-builder"]:::ext
    end

    CLI_Dev --> Dispatcher
    CLI_Babel --> Dispatcher
    CLI_Rollup --> Dispatcher

    Dispatcher --> ConfigLoader
    ConfigLoader --> WebpackBase
    ConfigLoader --> WebpackDev
    ConfigLoader --> WebpackProd
    ConfigLoader --> BabelConfigGen
    ConfigLoader --> ExportHook
    ConfigLoader --> UserNextronConfig
    ConfigLoader --> UserBabelRC

    Dispatcher -->|dev| DevFlow
    Dispatcher -->|build| BuildFlow

    WebpackBase --> DevFlow
    WebpackDev --> DevFlow
    BabelConfigGen --> DevFlow
    Logger --> DevFlow

    DevFlow --> NextServer
    NextServer --> ElectronApp

    WebpackBase --> BuildFlow
    WebpackProd --> BuildFlow
    BabelConfigGen --> BuildFlow
    ExportHook --> BuildFlow
    Logger --> BuildFlow

    BuildFlow --> NextExport
    NextExport --> Builder

    click CLI_Dev "https://github.com/saltyshiomix/nextron/blob/main/dev.js"
    click CLI_Babel "https://github.com/saltyshiomix/nextron/blob/main/babel.js"
    click CLI_Rollup "https://github.com/saltyshiomix/nextron/blob/main/rollup.config.js"
    click Dispatcher "https://github.com/saltyshiomix/nextron/blob/main/lib/nextron.ts"
    click DevFlow "https://github.com/saltyshiomix/nextron/blob/main/lib/nextron-dev.ts"
    click BuildFlow "https://github.com/saltyshiomix/nextron/blob/main/lib/nextron-build.ts"
    click ConfigLoader "https://github.com/saltyshiomix/nextron/blob/main/lib/configs/getNextronConfig.ts"
    click WebpackBase "https://github.com/saltyshiomix/nextron/blob/main/lib/configs/webpack.config.base.ts"
    click WebpackDev "https://github.com/saltyshiomix/nextron/blob/main/lib/configs/webpack.config.development.ts"
    click WebpackProd "https://github.com/saltyshiomix/nextron/blob/main/lib/configs/webpack.config.production.ts"
    click BabelConfigGen "https://github.com/saltyshiomix/nextron/blob/main/lib/configs/getBabelConfig.ts"
    click ExportHook "https://github.com/saltyshiomix/nextron/blob/main/lib/configs/useExportCommand.ts"
    click Logger "https://github.com/saltyshiomix/nextron/blob/main/lib/logger.ts"
    click Builder "https://github.com/saltyshiomix/nextron/blob/main/electron-builder.yml"
    click UserBabelRC "https://github.com/saltyshiomix/nextron/blob/main/.babelrc"

    classDef cli fill:#cde,stroke:#09f,stroke-width:2px
    classDef core fill:#def,stroke:#06f,stroke-width:2px
    classDef config fill:#fde,stroke:#f80,stroke-width:2px
    classDef ucfg fill:#fff,stroke:#aaa,stroke-dasharray:5 5
    classDef ext fill:#ffc,stroke:#fa0,stroke-width:2px
```
