feat(farm-leaf): give .leaf files their own markup

Merged
KI
11d ago
+804-209

Problem

A .leaf file was a .tsx file with the JSX moved to the bottom, so leaf read like React with a different file extension.

Change

.leaf files have their own markup now:

<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.</p></leaf:if>
  • {{ expression }} in text and attributes. A whole-value attribute passes the value itself, and mixed text becomes a string: href="/crops/{{ crop }}".
  • @click="statement" runs with event in scope. @click="save" passes save itself.
  • HTML attribute names: class, for, and boolean attributes such as checked.
  • <leaf:if test>, <leaf:else-if test>, <leaf:else>, and <leaf:for each in index>.
  • <script module> for imports and exports (hydrate, metadata, generateMetadata). <script> runs on every render with props in scope, and a script that awaits makes an async component.

compile.ts turns the markup into JSX, and oxc compiles that against leaf as before, so Farm still reads hydrate and "use client" from the source and server functions still work from .leaf files.

JSX in a .leaf file fails the build with the file, the line, and the .leaf spelling, for example:

notes/page.leaf:18: `onClick={...}` is JSX. In a .leaf file, write `@click="..."` for an event and `onClick="{{ value }}"` for a value.

The app's layout, pages, error page, and not-found page move to .leaf. The ui/ pages stay .tsx to show that JSX still works.

The flat guides/ route files from #3 use the markup too. They were JSX, which the compiler rejects (4 of the 6 failed after merging main). harvest.leaf keeps its // @hydrate interaction pragma in the module script, where hydration.resolve() reads it.

Safety and compatibility

  • .tsx files are unchanged.
  • A .leaf file written in the old JSX style now fails to build with a message naming the fix. The only ones were this repo's.
  • TypeScript doesn't check .leaf files, as the README says.

Verification

Run on head 9ed3bd7 (main 0ddca34 merged in, Farm 0.1.83 from npm):

  • pnpm install --frozen-lockfile (runs build:packages): ok
  • pnpm type-check: ok
  • pnpm test: 49 passed, including the new compile.test.ts. It renders compiled .leaf sources and compares them with the same trees written as createElement() calls, and it covers 10 error cases and line numbers.
  • pnpm test:e2e (production build) and pnpm test:e2e:dev: 17/17 each, locally in Chrome, including the 4 guides tests from #3. The notes test now checks <leaf:for> with its index and the <leaf:if>/<leaf:else> switch.

Documentation and release

The README has a .leaf section with the syntax table. Farm's examples/custom-renderer, the custom renderer docs, and the blog post follow in a farm.js PR.


Summary by cubic

Gives .leaf files their own HTML-like markup, so leaf templates no longer read like JSX with the markup at the bottom.

  • {{ expression }} inserts values in text and attributes; a whole-value attribute passes the value itself, and mixed text becomes a string.
  • @click="statement" runs with event in scope; @click="save" passes save itself.
  • <leaf:if test>, <leaf:else-if test>, <leaf:else>, and <leaf:for each in index> handle conditions and lists.
  • Attributes keep HTML names such as class and for, and boolean attributes like checked.
  • <script module> holds module scope; <script> runs on every render with props in scope, and a script that awaits makes an async component.
  • compile.ts turns the markup into JSX, so Farm still reads hydrate, "use client", and server functions from .leaf sources.

Migration

  • JSX in a .leaf file fails the build with the file, line, and a message naming the fix; the only affected files were this repo's.
  • The guides pages from #3 were written in JSX and failed after the merge; they now use the markup, and harvest.leaf keeps its // @hydrate interaction pragma in the module script.
  • .tsx files are unchanged, and TypeScript doesn't check .leaf files.
  • The app's layout, pages, error page, and not-found page move to .leaf; ui/ pages stay .tsx to show JSX still works.