|
| 1 | +# AGENTS.md |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +**server-reason-react** is a native OCaml/Reason implementation of React's server-side rendering (SSR) and React Server Components (RSC). It enables writing React components in Reason that compile to both native (server) and JavaScript (client via Melange). |
| 6 | + |
| 7 | +Used in production at ahrefs.com, app.ahrefs.com, and wordcount.com. |
| 8 | + |
| 9 | +## Language & Build System |
| 10 | + |
| 11 | +- **Primary languages**: Reason (`.re`/`.rei`) and OCaml (`.ml`/`.mli`) |
| 12 | +- **Build system**: [Dune](https://dune.build/) (version 3.9+) |
| 13 | +- **Package manager**: opam (OCaml) + npm (JS tooling) |
| 14 | +- **Compiler**: OCaml 5.4.0 (also supports 4.14.1) |
| 15 | +- **JS compilation**: [Melange](https://melange.re/) (Reason/OCaml to JavaScript) |
| 16 | +- **Formatter**: ocamlformat 0.28.1 (config in `.ocamlformat`, profile=default, margin=120) |
| 17 | + |
| 18 | +## Repository Structure |
| 19 | + |
| 20 | +``` |
| 21 | +packages/ # Monorepo — all library packages |
| 22 | + react/ # Core React module (server-side impl) |
| 23 | + reactDom/ # ReactDOM: renderToString, renderToStream, RSC |
| 24 | + server-reason-react-ppx/ # JSX PPX transformer for native |
| 25 | + browser-ppx/ # PPX to strip browser-only code on server |
| 26 | + melange.ppx/ # Melange compatibility PPX for native |
| 27 | + Belt/ # Belt standard library (native impl) |
| 28 | + Js/ # Js module (native impl, Melange compat) |
| 29 | + Dom/ # DOM type stubs |
| 30 | + webapi/ # Web API stubs for native compilation |
| 31 | + url/ # URL module (dual native/js implementations) |
| 32 | + promise/ # Promise module (dual native/js) |
| 33 | + html/ # HTML generation utilities |
| 34 | + fetch/ # Fetch API stub |
| 35 | + runtime/ # Runtime support module |
| 36 | + expand-styles-attribute/ # Style attribute expansion PPX |
| 37 | + esbuild-plugin/ # Esbuild plugin for client component extraction |
| 38 | + react-server-dom-esbuild/ # React Server DOM esbuild integration |
| 39 | +demo/ # Demo app (Dream server + client hydration) |
| 40 | +benchmark/ # Performance benchmarks |
| 41 | +documentation/ # odoc documentation source (.mld files) |
| 42 | +arch/ # Architecture reference (browser/server JS) |
| 43 | +``` |
| 44 | + |
| 45 | +## Essential Commands |
| 46 | + |
| 47 | +```bash |
| 48 | +make build # Build the project (dev profile) |
| 49 | +make build-prod # Build for production |
| 50 | +make test # Run all unit tests (dune build @runtest) |
| 51 | +make test-promote # Update test snapshots |
| 52 | +make format # Format code with ocamlformat |
| 53 | +make format-check # Check formatting (used in CI) |
| 54 | +make ppx-test # Run PPX-specific tests |
| 55 | +make ppx-test-promote # Promote PPX test snapshots |
| 56 | +make demo-serve # Build and serve the demo |
| 57 | +make bench # Run benchmarks |
| 58 | +make init # Full local setup (switch + deps + npm) |
| 59 | +``` |
| 60 | + |
| 61 | +## Testing |
| 62 | + |
| 63 | +Three testing strategies are used: |
| 64 | + |
| 65 | +1. **Alcotest unit tests** — Located in `test/` directories within each package. Run with `make test`. |
| 66 | +2. **Cram tests** (snapshot/golden tests) — Located as `.t` files in `cram/` or `tests/` directories. These test PPX transformations by comparing actual output against expected snapshots. Promote changes with `make test-promote`. |
| 67 | +3. **Inline expect tests** — Some packages use inline tests within source files. |
| 68 | + |
| 69 | +Tests should stay explicit and free of test-only logic. Prefer concrete inputs and direct assertions over conditionals, loops, or helpers that hide the behavior being verified. |
| 70 | + |
| 71 | +When modifying PPX code, always run `make ppx-test` and review snapshot diffs carefully before promoting. |
| 72 | + |
| 73 | +## Key Concepts |
| 74 | + |
| 75 | +### Universal Code |
| 76 | +Code that compiles for both native (server) and JavaScript (client). The project uses `copy_files` in dune to share `.re` files between Melange and native library targets. See `documentation/universal-code.mld`. |
| 77 | + |
| 78 | +### PPX Transformations |
| 79 | +Three PPXes are central to the project: |
| 80 | +- **`server-reason-react-ppx`** — Transforms JSX syntax to native React calls |
| 81 | +- **`browser-ppx`** — Strips browser-only code on server (`let%browser_only`, `switch%platform`, `@platform`) |
| 82 | +- **`melange.ppx`** — Makes Melange attributes (`mel.*`, `##`, `[%re]`, etc.) work in native |
| 83 | + |
| 84 | +### Dual Implementations |
| 85 | +Some modules have separate native and JS implementations sharing the same interface: |
| 86 | +- `packages/url/native/` vs `packages/url/js/` |
| 87 | +- `packages/promise/native/` vs `packages/promise/js/` |
| 88 | + |
| 89 | +### React Server Components (Experimental) |
| 90 | +RSC support includes `ReactServerDOM`, an esbuild plugin for client component extraction, and Dream middleware. This is still work in progress. |
| 91 | + |
| 92 | +## Coding Conventions |
| 93 | + |
| 94 | +- Reason (`.re`) is preferred for React components and new application code |
| 95 | +- OCaml (`.ml`) is used for library internals, PPX implementations, and Belt/Js modules |
| 96 | +- Interface files (`.mli`/`.rei`) are used to define public APIs |
| 97 | +- Each package has its own `dune` file defining libraries and test targets |
| 98 | +- Follow existing formatting — run `make format` before committing |
| 99 | +- PPX transformations use `ppxlib` and follow its visitor pattern |
| 100 | + |
| 101 | +## OCaml |
| 102 | + |
| 103 | +- Do not use `Obj.magic` or `%identity` unless it is absolutely necessary. |
| 104 | +- Avoid both at all costs; prefer type-safe alternatives even if they require a slightly larger refactor. |
| 105 | +- If either appears to be the only viable option, stop and ask the user before introducing it. |
| 106 | + |
| 107 | +## Do NOT |
| 108 | + |
| 109 | +- Modify `_build/`, `_opam/`, or `node_modules/` directories |
| 110 | +- Change `.ocamlformat` settings without discussion |
| 111 | +- Skip running tests after modifying PPX code — PPX changes can silently break downstream code |
| 112 | +- Introduce new opam dependencies without updating `dune-project` |
| 113 | +- Assume browser APIs are available on the server — use `let%browser_only` or `switch%platform` |
| 114 | + |
| 115 | +## CI |
| 116 | + |
| 117 | +GitHub Actions (`.github/workflows/ci.yml`) runs on push to `main` and PRs: |
| 118 | +- Matrix: `{macos, ubuntu}` x `{OCaml 4.14.1, OCaml 5.4.0}` |
| 119 | +- Steps: build, format check (OCaml 5+ only), tests, docs generation, benchmarks |
| 120 | + |
| 121 | +## Useful Links |
| 122 | + |
| 123 | +- [Documentation](https://ml-in-barcelona.github.io/server-reason-react/server-reason-react/index.html) |
| 124 | +- [Reason syntax docs](https://reasonml.github.io/docs/en/what-and-why) |
| 125 | +- [Melange docs](https://melange.re/) |
| 126 | +- [Dune docs](https://dune.readthedocs.io/) |
| 127 | +- [ppxlib docs](https://ocaml.org/p/ppxlib/latest/doc/index.html) |
0 commit comments