feat(examples): give the custom renderer's .leaf files their own markup

Merged
KI
11d ago
+858-244

Problem

The custom renderer example's .leaf files were JSX with a script block on top, so leaf read like React with a different file extension.

Change

The example's .leaf files use leaf's own markup now, the same format as farming-labs/leaf#2:

<script module>
  export const hydrate = true;
</script>

<script>
  import { useState } from "leaf";
  const [count, setCount] = useState(0);
</script>

<button @click="setCount((n) => n + 1)">Clicked {{ count }} times</button>
<leaf:if test="count > 3">
  <p>That's a lot of clicks.</p>
</leaf:if>
  • {{ expression }} in text and attributes, @event="statement" for handlers, and HTML attribute names (class, for).
  • <leaf:if test>, <leaf:else-if test>, <leaf:else>, and <leaf:for each in index>.
  • <script module> holds module scope. <script> runs on every render with props in scope, and a script that awaits makes an async component.

farm-leaf/src/compile.js turns the markup into JSX, and the Vite module compiles that with oxc as before. JSX left in a .leaf file fails with the file, the line, and the .leaf spelling.

The layout, pages, and error and not-found pages move from .tsx to .leaf. /recovers and /islands stay .tsx, which shows JSX still works next to them.

The flat guides/ route files from #3154 use the markup too. They were JSX, which the compiler rejects (4 of the 6 failed with LEAF_COMPILE_ERROR after merging main). harvest.leaf keeps its // @hydrate interaction pragma in the module script, where hydration.resolve() reads it. The notes page now covers <leaf:if>/<leaf:else> and <leaf:for> with an index.

Safety and compatibility

Example, docs, and blog only. No published package or core behavior changes.

Verification

Run on head 4af31480e (main 02f4975bb merged in):

  • pnpm --filter farm-leaf test: 50 passed. The new compile.test.js renders compiled .leaf sources and compares them with the same trees written as createElement() calls. It also covers 10 error cases and line numbers.
  • pnpm --filter farm-leaf type-check and pnpm --filter farm-custom-renderer-example type-check: ok.
  • playwright.custom-renderer.config.ts against farm dev and against the production build: 18/18 each, including the 4 guides tests and the 2 islands tests under the .leaf layout. I ran it locally in installed Chrome (channel: "chrome") because this machine lacks Playwright's headless shell.
  • A page with <p>{count}</p> fails farm build with page.leaf:2: `{` starts a JSX expression…. farm dev logs the same message and answers the page with a 500, which is what the docs say.
  • Control: compileLeaf() on main's 6 guides files fails 4 ([crop], _layout, harvest, watering.client); the converted files compile.
  • oxfmt --check on the changed files: clean.

Not run: pnpm --dir docs exec farm generate --check (the worktree lacks a devtools build; no docs pages were added or moved) and pnpm docs:build.

Documentation and release

  • docs/renderers/custom: the Vite module snippet passes the file to the compiler. "Your own file format" shows the markup and says to throw compile errors with the file and line.
  • The blog post: steps 5 to 7 show .leaf pages, step 7 explains the markup and gives the compiler's real size (429 lines), and the intro no longer calls leaf React-like.
  • The example README has the markup and a note that TypeScript doesn't check .leaf files.
  • No release.

Summary by cubic

Makes the custom renderer’s .leaf files use leaf’s own markup instead of JSX, so the example shows a distinct component format. The compiler converts the markup to JSX and reports errors with the source file and line.

  • Adds {{ }} values, @event handlers, conditional tags, and list rendering, plus LeafCompileError for build and dev failures.
  • Moves fixtures, layouts, and error pages to .leaf; /recovers stays .tsx.
  • Adds tests covering compiled output and error reporting, and updates docs and the blog post.