#sdf1 [hhhhg Farm.js Architecture
This document provides an overview of the Farm.js framework architecture and design decisions.
Farm.js is built as a monorepo containing multiple packages that work together to provide a complete full-stack framework experience.
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Developer │ │ Farm.js CLI │ │ Vite Plugin │
│ Experience │◄──►│ (@farm.js/cli) │◄──►│ (farmjs/vite) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │ │
▼ ▼ ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ File-based │ │ Route Manager │ │ Server │
│ Routing │◄──►│ (routing/) │◄──►│ Renderer │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │ │
▼ ▼ ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ React Server │ │ Client │ │ Build │
│ Components │◄──►│ Hydration │◄──►│ System │
└─────────────────┘ └─────────────────┘ └─────────────────┘
The main framework package containing:
- Core Types (
src/types.ts) - TypeScript definitions for the entire framework - Route Manager (
src/routing/) - File-based routing system with Next.js-like semantics - Server Renderer (
src/server/) - RSC rendering and SSR capabilities - Client Runtime (
src/client/) - Client-side hydration and navigation - Vite Plugin (
src/vite.ts) - Integration with Vite build system - Utilities (
src/utils.ts) - Shared utility functions
Command-line interface for development:
- Development server (
farm dev) - Production builds (
farm build) - Project scaffolding integration
Project creation tool:
- Interactive project setup
- Template system
- TypeScript/JavaScript options
Documentation website built with fumadocs:
- Getting started guides
- API reference
- Examples and tutorials
- Built with Next.js and fumadocs
Example applications showcasing different use cases:
- basic/ - Simple Farm.js application
- with-database/ - Database integration patterns
- e-commerce/ - Full-featured application
Development testing environment:
- Manual testing of framework features
- Browser-based verification
- Feature demonstration
- Request arrives at Vite dev server
- Vite middleware intercepts non-asset requests
- Route Manager matches URL to file-based routes
- Server Renderer loads and renders React Server Components
- HTML response is streamed to the client with RSC payload
- Client runtime hydrates the application
- Build process generates optimized bundles
- Static analysis discovers all routes and dependencies
- Server bundle contains RSC rendering logic
- Client bundle contains hydration and navigation code
- Runtime serves pre-built or dynamically rendered pages
- File-based routing with
page.tsxandlayout.tsx - App Router-style directory structure
- Compatible TypeScript types and patterns
- Familiar developer experience
- Leverages Vite's fast development server
- Uses Vite's plugin system for customization
- Benefits from Vite's optimized build process
- Maintains Vite's excellent DX
- Full RSC support with streaming
- Server and client component boundaries
- Optimized data fetching patterns
- Progressive enhancement approach
- Predictable file structure and naming
- Clear separation of concerns
- Consistent patterns throughout
- Well-documented APIs and types
Responsible for:
- Discovering routes from file system
- Parsing dynamic route segments
- Matching URLs to route handlers
- Loading route modules dynamically
// Example route matching
const { route, params, layouts } = routeManager.matchRoute("/users/123");
// Returns: { route: UserPage, params: { id: '123' }, layouts: [RootLayout] }Handles:
- React Server Component rendering
- HTML streaming with RSC payload
- Layout composition and nesting
- Error boundaries and 404 handling
Provides:
- Development middleware integration
- Client/server code splitting
- RSC module resolution
- Build-time optimizations
Manages:
- Application hydration
- Client-side navigation
- Route transitions
- State management
- Utility functions (route parsing, matching)
- Core logic components
- Type safety verification
- Route discovery and matching
- Server rendering pipeline
- Client hydration process
- Playground application
- Example applications
- Browser compatibility
- Vite's instant server start
- Fast HMR with file watching
- Optimized module resolution
- Minimal build overhead
- React Server Components reduce client bundle size
- Streaming SSR improves perceived performance
- Optimized hydration process
- Code splitting by route
- Vite's optimized build pipeline
- Tree shaking and dead code elimination
- Efficient bundling strategies
- Parallel processing where possible
- Server Actions implementation
- Enhanced metadata handling
- Middleware system
- Plugin ecosystem
- Edge runtime support
- Custom Vite plugins
- Route middleware
- Custom renderers
- Build hooks
- Development tools
For developers wanting to understand or contribute to Farm.js:
- Start with the playground - See the framework in action
- Explore examples - Understand common patterns
- Read the source - Core logic is well-documented
- Run tests - Understand expected behavior
- Check documentation - Comprehensive guides and API reference
This architecture provides a solid foundation for a modern full-stack framework while maintaining simplicity and extensibility.