|
1 | | -= RSR template repo - see RSR_OUTLINE.adoc in root for general background and specification |
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +// SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
| 3 | + |
| 4 | += rescript-vite |
| 5 | +:toc: macro |
| 6 | +:toclevels: 3 |
| 7 | + |
| 8 | +A Vite plugin for first-class ReScript support with HMR, compiler integration, |
| 9 | +error overlay, and optional BoJ (Bundle of Joy) build orchestration via the |
| 10 | +ssg-mcp cartridge. |
| 11 | + |
| 12 | +toc::[] |
| 13 | + |
| 14 | +== What is rescript-vite? |
| 15 | + |
| 16 | +rescript-vite integrates the ReScript compiler into Vite's build pipeline. |
| 17 | +It automatically spawns the ReScript compiler in build or watch mode, tracks |
| 18 | +compiled `.res.js` output files for hot module replacement (HMR), and forwards |
| 19 | +compiler diagnostics to Vite's error overlay. When a BoJ server is available, |
| 20 | +it can delegate builds to the ssg-mcp cartridge for caching, telemetry, and |
| 21 | +orchestrated compilation. |
| 22 | + |
| 23 | +=== Features |
| 24 | + |
| 25 | +* *Automatic compiler management* -- spawns `rescript build` (one-shot) or |
| 26 | + `rescript build -w` (watch mode) depending on whether Vite is in `build` |
| 27 | + or `serve` mode. |
| 28 | +* *HMR for .res files* -- detects recompiled `.res.js` files and triggers |
| 29 | + hot module replacement in the browser. |
| 30 | +* *Error overlay integration* -- pushes ReScript compiler errors to Vite's |
| 31 | + built-in error overlay with file, line, and column info. |
| 32 | +* *Diagnostic parsing* -- parses ReScript compiler output including ANSI |
| 33 | + escape codes, extracting structured diagnostic objects. |
| 34 | +* *Deno compatible* -- auto-detects the Deno runtime and adjusts compiler |
| 35 | + invocation accordingly. |
| 36 | +* *BoJ build orchestration* -- optionally delegates builds to a running |
| 37 | + BoJ ssg-mcp cartridge for incremental builds, caching, and telemetry. |
| 38 | +* *panic-attacker compatible* -- diagnostic format maps cleanly to SARIF-style |
| 39 | + output used by panic-attacker weak-point scanning. |
| 40 | + |
| 41 | +== Installation |
| 42 | + |
| 43 | +=== Using Deno |
2 | 44 |
|
3 | | -[TIP] |
4 | | -==== |
5 | | -**AI-Assisted Install:** Just tell any AI assistant: + |
6 | | -`Set up {{PROJECT_NAME}} from https://{{FORGE}}/{{OWNER}}/{{REPO}}` + |
7 | | -The AI reads this repo, asks you a few questions, and handles everything. See <<ai-install,details below>>. |
8 | | -==== |
| 45 | +[source,bash] |
| 46 | +---- |
| 47 | +deno add npm:rescript-vite |
| 48 | +---- |
| 49 | + |
| 50 | +=== Using npm (where Deno is not available) |
| 51 | + |
| 52 | +[source,bash] |
| 53 | +---- |
| 54 | +npm install rescript-vite |
| 55 | +---- |
9 | 56 |
|
10 | | -== This is your repo - don't forget to rename me! |
| 57 | +=== Peer dependencies |
11 | 58 |
|
12 | | -== ABI/FFI Standards (Hyperpolymath Universal Standard) |
| 59 | +rescript-vite expects these as peer dependencies: |
13 | 60 |
|
14 | | -**All repos with foreign function interfaces MUST follow this standard:** |
| 61 | +* `rescript` ^12.0.0 |
| 62 | +* `@rescript/core` ^1.0.0 |
| 63 | +* `vite` ^6.0.0 |
15 | 64 |
|
16 | | -* **ABI (Application Binary Interface)** → **Idris2** (`src/abi/*.idr`) |
17 | | -** Type definitions with dependent type proofs |
18 | | -** Memory layout verification |
19 | | -** Platform-specific ABIs with compile-time selection |
20 | | -** Formal verification of interface correctness |
| 65 | +== Usage |
21 | 66 |
|
22 | | -* **FFI (Foreign Function Interface)** → **Zig** (`ffi/zig/src/*.zig`) |
23 | | -** C-compatible function implementations |
24 | | -** Zero-cost abstractions |
25 | | -** Memory-safe by default |
26 | | -** Cross-compilation support |
| 67 | +=== Basic setup |
| 68 | + |
| 69 | +[source,javascript] |
| 70 | +---- |
| 71 | +// vite.config.js |
| 72 | +import rescriptPlugin from "rescript-vite"; |
| 73 | +
|
| 74 | +export default { |
| 75 | + plugins: [rescriptPlugin()], |
| 76 | +}; |
| 77 | +---- |
| 78 | + |
| 79 | +=== With options |
| 80 | + |
| 81 | +[source,javascript] |
| 82 | +---- |
| 83 | +// vite.config.js |
| 84 | +import { make } from "rescript-vite"; |
| 85 | +
|
| 86 | +export default { |
| 87 | + plugins: [ |
| 88 | + make({ |
| 89 | + useDeno: true, |
| 90 | + logLevel: "verbose", |
| 91 | + compilerFlags: ["-warn-error", "+a"], |
| 92 | + }), |
| 93 | + ], |
| 94 | +}; |
| 95 | +---- |
27 | 96 |
|
28 | | -* **Generated C Headers** → Auto-generated from Idris2 ABI (`generated/abi/*.h`) |
29 | | -** Bridge between Idris2 and Zig |
30 | | -** Consumed by any language via C ABI |
| 97 | +=== With BoJ build orchestration |
31 | 98 |
|
32 | | -**Directory Structure:** |
| 99 | +[source,javascript] |
33 | 100 | ---- |
34 | | -project/ |
35 | | -├── src/abi/ # Idris2 ABI definitions (REQUIRED) |
36 | | -├── ffi/zig/ # Zig FFI implementation (REQUIRED) |
37 | | -├── generated/abi/ # Auto-generated C headers |
38 | | -└── bindings/ # Language-specific wrappers (optional) |
| 101 | +// vite.config.js |
| 102 | +import { make } from "rescript-vite"; |
| 103 | +
|
| 104 | +export default { |
| 105 | + plugins: [ |
| 106 | + make({ |
| 107 | + boj: true, |
| 108 | + bojEndpoint: "http://localhost:7077/mcp/ssg", |
| 109 | + logLevel: "info", |
| 110 | + }), |
| 111 | + ], |
| 112 | +}; |
39 | 113 | ---- |
40 | 114 |
|
41 | | -**See:** `ABI-FFI-README.md` for complete documentation |
| 115 | +When `boj: true` is set, the plugin probes the BoJ ssg-mcp endpoint at |
| 116 | +build start. If the server is reachable, production builds are delegated to |
| 117 | +it. If unreachable, the plugin falls back to direct compiler invocation. |
| 118 | + |
| 119 | +== API Reference |
| 120 | + |
| 121 | +=== `make(options?)` |
| 122 | + |
| 123 | +Creates a Vite plugin instance. Returns a standard Vite plugin object with |
| 124 | +`name: "rescript-vite"` and `enforce: "pre"`. |
42 | 125 |
|
43 | | -== AI Gatekeeper Protocol (MANDATORY) |
| 126 | +==== Options |
44 | 127 |
|
45 | | -**CRITICAL:** All repos MUST include an AI manifest. |
| 128 | +[cols="1,1,2,1"] |
| 129 | +|=== |
| 130 | +|Option |Type |Description |Default |
46 | 131 |
|
47 | | -* **File:** `0-AI-MANIFEST.a2ml` (included in this template) |
48 | | -* **Purpose:** Universal entry point for ALL AI agents (Claude, Gemini, OpenAI, etc.) |
49 | | -* **Location:** Repository root (alphabetically first) |
50 | | -* **Customize:** Update `[YOUR-REPO-NAME]` placeholders and repository structure section |
| 132 | +|`boj` |
| 133 | +|`boolean` |
| 134 | +|Enable BoJ ssg-mcp build orchestration. The plugin probes the endpoint |
| 135 | + at build start and falls back to direct compilation if unavailable. |
| 136 | +|`false` |
51 | 137 |
|
52 | | -**The manifest declares:** |
| 138 | +|`bojEndpoint` |
| 139 | +|`string` |
| 140 | +|URL of the BoJ ssg-mcp cartridge endpoint. |
| 141 | +|`http://localhost:7077/mcp/ssg` |
53 | 142 |
|
54 | | -* Canonical file locations (e.g., "machine-readable files ONLY in `.machine_readable/`") |
55 | | -* Critical invariants (rules that must NEVER be violated) |
56 | | -* Repository structure and organization |
57 | | -* Lifecycle hooks (on-enter, on-exit) for session logging |
| 143 | +|`useDeno` |
| 144 | +|`boolean` |
| 145 | +|Use Deno to run the ReScript compiler (`deno run -A npm:rescript`). |
| 146 | +|Auto-detected |
58 | 147 |
|
59 | | -**Benefits:** |
| 148 | +|`rescriptBin` |
| 149 | +|`string` |
| 150 | +|Path to the rescript binary. When set, the plugin invokes this binary |
| 151 | + directly instead of using `npx`. |
| 152 | +|`undefined` (uses `npx`) |
60 | 153 |
|
61 | | -* ✅ Prevents duplicate file errors (e.g., machine-readable files in wrong locations) |
62 | | -* ✅ Context preserved across sessions and AI platforms |
63 | | -* ✅ No repeated explanations - mechanical enforcement |
64 | | -* ✅ Works universally (not just Claude) |
| 154 | +|`compilerFlags` |
| 155 | +|`string[]` |
| 156 | +|Extra flags passed to the ReScript compiler. |
| 157 | +|`[]` |
65 | 158 |
|
66 | | -**See:** link:https://github.com/{{OWNER}}/0-ai-gatekeeper-protocol[AI Gatekeeper Protocol] for details |
| 159 | +|`logLevel` |
| 160 | +|`"silent" \| "info" \| "verbose"` |
| 161 | +|Controls console output verbosity. |
| 162 | +|`"info"` |
| 163 | +|=== |
67 | 164 |
|
68 | | -**Enforcement:** |
| 165 | +=== Default export |
69 | 166 |
|
70 | | -* MCP server (mcp-repo-guardian) - Hard enforcement for Claude |
71 | | -* FUSE wrapper (repo-guardian-fs) - Universal enforcement (coming soon) |
72 | | -* CI/CD validation - Continuous compliance checking |
| 167 | +The module's default export is a plugin instance created with default options: |
73 | 168 |
|
74 | | -== TOPOLOGY.md (Architecture Map) |
| 169 | +[source,javascript] |
| 170 | +---- |
| 171 | +import rescriptPlugin from "rescript-vite"; |
| 172 | +// Equivalent to: make() |
| 173 | +---- |
75 | 174 |
|
76 | | -Every repo should include a `TOPOLOGY.md` in its root — an ASCII architecture diagram combined with a completion dashboard. This gives any contributor (human or AI) an instant picture of the whole project. |
| 175 | +=== `rescript-vite/boj` (BojBridge) |
77 | 176 |
|
78 | | -* **Template:** `TOPOLOGY.md` (included in this template) |
79 | | -* **Guide:** `docs/TOPOLOGY-GUIDE.adoc` (includes AI prompt for generation, conventions, batch rollout) |
80 | | -* **Sections:** System Architecture (ASCII diagram), Completion Dashboard (progress bars), Key Dependencies (critical path) |
| 177 | +The BoJ bridge is available as a separate export for direct use: |
81 | 178 |
|
82 | | -See `docs/TOPOLOGY-GUIDE.adoc` for how to generate bespoke TOPOLOGY.md files for existing repos using an AI agent. |
| 179 | +[source,javascript] |
| 180 | +---- |
| 181 | +import { make, probe, requestBuild, recentEvents, disconnect } from "rescript-vite/boj"; |
83 | 182 |
|
84 | | -== AI CLI standards |
| 183 | +const bridge = make("http://localhost:7077/mcp/ssg"); |
| 184 | +const connected = await probe(bridge); |
| 185 | +---- |
85 | 186 |
|
86 | | -- **FIRST:** Customize `0-AI-MANIFEST.a2ml` for your repo before any other work |
87 | | -- All machine-readable content lives in `.machine_readable/` — state files (`*.a2ml`), `anchors/`, `bot_directives/`, `contractiles/`, and `ai/`. |
88 | | -- Include `.machine_readable/` structure from this template in new repos. |
| 187 | +==== BojBridge API |
89 | 188 |
|
90 | | -== Directory Structure Policy (Owner Override) |
| 189 | +* `make(endpoint?)` -- Create a bridge instance (disconnected by default). |
| 190 | +* `probe(bridge)` -- Attempt to connect; returns `true` on success. |
| 191 | +* `isConnected(bridge)` -- Check connection state. |
| 192 | +* `requestBuild(bridge, request)` -- Send a build request via JSON-RPC. |
| 193 | +* `recentEvents(bridge, limit?)` -- Get recent telemetry events (default limit: 20). |
| 194 | +* `disconnect(bridge)` -- Disconnect the bridge. |
91 | 195 |
|
92 | | -The owner policy for this template family is: |
| 196 | +== BoJ Integration |
93 | 197 |
|
94 | | -* Keep only essential entry-point files in root. |
95 | | -* Put licensing materials in `licensing/` (retain root `LICENSE` for forge/license detection). |
96 | | -* Put documentation under `docs/` with explicit tracks: |
97 | | -** `docs/theory/` |
98 | | -** `docs/practice/` |
99 | | -** `docs/whitepapers/academic/` |
100 | | -** `docs/whitepapers/industry/` |
101 | | -* Keep test fixtures/outputs under `tests/`. |
102 | | -* Keep AI-agent guidance in `.machine_readable/ai/`. |
103 | | -* Prefer `Containerfile` (not `Dockerfile`) for container builds. |
| 198 | +The plugin integrates with the *BoJ (Bundle of Joy)* server's `ssg-mcp` |
| 199 | +cartridge. BoJ is a multi-cartridge build orchestration system; the ssg-mcp |
| 200 | +cartridge handles static site generation and ReScript compilation with: |
104 | 201 |
|
105 | | -== Maintenance Gate (Release) |
| 202 | +* *Build caching* -- avoids redundant recompilation when source files |
| 203 | + have not changed. |
| 204 | +* *Telemetry* -- records build events (duration, file count, cache hit rate, |
| 205 | + diagnostic count) for monitoring and analysis. |
| 206 | +* *MCP protocol* -- communicates via JSON-RPC 2.0 over HTTP, calling the |
| 207 | + `tools/call` method with the `ssg_build` tool name. |
106 | 208 |
|
107 | | -This template includes a maintenance workflow that is intended to be a release gate. |
| 209 | +When `boj: true` is set: |
108 | 210 |
|
109 | | -- Scripted audit: `scripts/maintenance/run-maintenance.sh` |
110 | | -- Permission lock/restore helper: `scripts/maintenance/perms-state.sh` |
111 | | -- Checklist: `docs/maintenance/MAINTENANCE-CHECKLIST.md` |
| 211 | +1. On `buildStart`, the plugin probes `{bojEndpoint}/health`. |
| 212 | +2. If healthy, production builds are sent as `ssg_build` requests. |
| 213 | +3. If the BoJ request fails, the plugin falls back to direct `rescript build`. |
| 214 | +4. Telemetry events are recorded in the bridge for later inspection. |
112 | 215 |
|
113 | | -Recommended release flow: |
| 216 | +== Development |
| 217 | + |
| 218 | +=== Build |
114 | 219 |
|
115 | 220 | [source,bash] |
116 | 221 | ---- |
117 | | -just maint-audit |
118 | | -just maint-hard-pass |
| 222 | +deno task res:build |
119 | 223 | ---- |
120 | 224 |
|
121 | | -Permission policy is audit-first by default. Use `just perms-lock` only when intentionally hardening local modes, and `just perms-restore` to restore your previous local permission state. |
122 | | - |
123 | | -[[ai-install]] |
124 | | -== AI-Assisted Installation |
| 225 | +=== Test |
125 | 226 |
|
126 | | -**You don't need to read this README to get started.** Just say this to any AI: |
127 | | - |
128 | | -[source,text] |
| 227 | +[source,bash] |
129 | 228 | ---- |
130 | | -Set up {{PROJECT_NAME}} from https://{{FORGE}}/{{OWNER}}/{{REPO}} |
| 229 | +deno task test |
131 | 230 | ---- |
132 | 231 |
|
133 | | -The URL is the spec. The AI fetches this repo, reads `docs/AI_INSTALLATION_GUIDE.adoc`, and handles everything -- prerequisites, build, config, verification. You answer a few questions and confirm the privacy notice. No commands, no installers, no forms. |
| 232 | +=== Benchmarks |
134 | 233 |
|
135 | | -If the AI already knows this project, shorter prompts work too. If it doesn't, the URL gives it everything. |
| 234 | +[source,bash] |
| 235 | +---- |
| 236 | +deno bench --no-check --allow-all tests/bench_test.js |
| 237 | +---- |
136 | 238 |
|
137 | | -**For developers:** Fill in `docs/AI_INSTALLATION_GUIDE.adoc` with your project-specific install recipe. Paste the highlight box from `docs/AI-INSTALL-README-SECTION.adoc` into your README. Run `just validate-ai-install` to check completeness. finishbot verifies this pre-release. |
| 239 | +== License |
138 | 240 |
|
139 | | -The normal installation instructions remain below for anyone who prefers to do things manually. |
| 241 | +PMPL-1.0-or-later (Palimpsest License). |
140 | 242 |
|
141 | | -== Standard Dependencies (Optional) |
| 243 | +See link:LICENSE[LICENSE] for the full text. |
142 | 244 |
|
143 | | -Add your project's standard dependencies here. Remove or customize this section |
144 | | -for your ecosystem. |
| 245 | +== Author |
145 | 246 |
|
146 | | -// Example: |
147 | | -// [cols="1,2,1"] |
148 | | -// |=== |
149 | | -// |Library |Purpose |Status |
150 | | -// |
151 | | -// |your-library |
152 | | -// |Description |
153 | | -// |Required / Recommended / Optional |
154 | | -// |=== |
| 247 | +Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk> |
0 commit comments