How khromov/mochi works
Purpose and Workspace Shape
Mochi is a Bun-only SSR framework for Svelte 5, centered on islands-based selective hydration and small client bundles. This monorepo contains the framework, app scaffolder, reference/template apps, documentation, and demos. Framework public entry points are packages/mochi/src/index.ts and CLI entry points packages/mochi/src/cli.ts; development orchestration starts from scripts/dev.ts.
Framework Startup, Configuration, and Serving
Mochi owns application startup and request-serving integration over Bun.serve(). It reads framework and Svelte configuration, establishes public assets and request setup, and exposes application-facing APIs. Key boundaries are framework configuration versus user route/component code, and Bun runtime APIs versus portable framework abstractions.
• packages/mochi/src/Mochi.ts
• packages/mochi/src/mochiConfig.ts
• packages/mochi/src/requestSetup.ts
Build and Compilation Pipeline
The build stage compiles Svelte application code, tracks bundle metadata, preprocesses source, and creates server/client artifacts. It protects server-only imports from client leakage and caches compilation/preprocessing work. Build output drives runtime component resolution and island manifests.
• packages/mochi/src/build.ts
• packages/mochi/src/compileCache.ts
• packages/mochi/src/serverOnlyScan.ts
SSR Rendering and Component Registry
A request resolves to SSR rendering through the component registry, which manages compiled components and fallback/reload behavior. HTML shell and head handling separate framework-provided document structure from app page rendering; error handling supplies failure boundaries/default pages.
• packages/mochi/src/ComponentRegistry.ts
• packages/mochi/src/templates/default-shell.html
• packages/mochi/src/errors.ts
Islands and Client Runtime
Selective hydration is the defining execution boundary: server output registers island props and emits browser web components; only marked/hydratable components receive client bundles and hydration behavior. Server islands can render through isolated endpoints, with crypto protecting payloads and CSS tracked across nested island responses.
• packages/mochi/src/islandPropsRegistry.ts
• packages/mochi/src/web-components/HydratableIsland.ts
• packages/mochi/src/web-components/ServerIsland.ts
HTTP Features and Stateful Services
Framework request features include cookies, CSRF, enhanced forms, middleware, caching, queues, events, and proxy support. These operate in the Bun server process; optional persistence includes Bun SQLite and filesystem-backed cache storage. Email supports rendering Svelte emails, development outbox/viewer, and configured transports.
• packages/mochi/src/cache.ts
• packages/mochi/src/forms.ts
• packages/mochi/src/email/mailer.ts
Consumer Applications and Scaffolding
The documentation site is itself a Mochi application and the broadest integration consumer; its route assembly begins in packages/site/src/index.ts and packages/site/src/routes.ts. Standalone demos exercise framework features, while the minimal app is the template basis. create-mochi copies/templates this starter for npm users.
• packages/site/src/index.ts
• packages/demos/src/index.ts
• packages/cli/src/create.ts
Runtime and Deployment
Bun (minimum version pinned in .bun-version) is the required runtime for development, build, serving, and optional SQLite. Production docs and demos are independently containerized and deployed as separate CapRover applications via GHCR images. Docker boundaries are Dockerfile for the site and packages/demos/Dockerfile.production for demos; CI deployment is defined in .github/workflows/build.yml.
Main entry points: Mochi.ts Mochi server · ComponentRegistry.ts Component registry · build.ts Build pipeline · index.ts Framework public API
Components 23
Mochi framework
Bun-only SSR framework and runtime features.
- Framework public API
packages/mochi/src/index.ts - Mochi server · Owns startup and Bun request serving.
packages/mochi/src/Mochi.ts - Framework configuration
packages/mochi/src/mochiConfig.ts - Request setup
packages/mochi/src/requestSetup.ts - Build pipeline · Produces server/client artifacts and island metadata.
packages/mochi/src/build.ts - Compilation cache
packages/mochi/src/compileCache.ts - Server-only import scan · Prevents server imports from leaking into client bundles.
packages/mochi/src/serverOnlyScan.ts - Component registry · Manages compiled components, fallback, and reload behavior.
packages/mochi/src/ComponentRegistry.ts - Default HTML shell
packages/mochi/src/templates/default-shell.html - Error boundaries
packages/mochi/src/errors.ts - Island props registry · Registers props for browser island execution.
packages/mochi/src/islandPropsRegistry.ts - Hydratable islands · Loads client bundles only for marked components.
packages/mochi/src/web-components/HydratableIsland.ts - Server islands · Protects payloads and tracks nested-island CSS.
packages/mochi/src/web-components/ServerIsland.ts - HTTP and state features · Forms, cookies, CSRF, middleware, cache, queues, events, and proxy support.
packages/mochi/src/forms.ts - Email service · Renders Svelte emails with outbox/viewer and transports.
packages/mochi/src/email/mailer.ts
Consumers and tooling
Applications, scaffolding, and development entry points.
- Development orchestration
scripts/dev.ts - create-mochi · Copies/templates the starter application.
packages/cli/src/create.ts - Documentation site · Broad integration consumer and route assembly.
packages/site/src/index.ts - Feature demos
packages/demos/src/index.ts
Runtime and deployment
Bun execution and independently deployed applications.
- Bun runtime · Runs development, builds, serving, and optional SQLite.
.bun-version - Site container
Dockerfile - Demos container
packages/demos/Dockerfile.production - CI deployment · Builds GHCR images and deploys site and demos separately.
.github/workflows/build.yml
Connections 24
- Development orchestration → Bun runtime: runs on
- Framework public API → Mochi server: exposes
- Mochi server → Framework configuration: loads
- Mochi server → Request setup: initializes
- Mochi server → Component registry: resolves requests through
- Mochi server → HTTP and state features: hosts
- Mochi server → Bun runtime: serves via Bun.serve
- Build pipeline → Compilation cache: caches compilation
- Build pipeline → Server-only import scan: validates imports
- Build pipeline → Component registry: supplies compiled components
- Build pipeline → Island props registry: creates island metadata
- Component registry → Default HTML shell: renders inside
- Component registry → Error boundaries: uses failure boundaries
- Component registry → Island props registry: registers island props
- Island props registry → Hydratable islands: drives browser hydration
- Component registry → Server islands: renders isolated islands
- HTTP and state features → Email service: supports email workflows
- create-mochi → Framework public API: targets framework
- Documentation site → Framework public API: consumes
- Feature demos → Framework public API: exercises
- Documentation site → Site container: packaged as
- Feature demos → Demos container: packaged as
- CI deployment → Site container: builds and deploys
- CI deployment → Demos container: builds and deploys