feat(farm-leaf): hydrate .leaf client components as islands

Merged
KI
10d ago
+135-33

Problem

A client component written as a .leaf file never becomes an island. With "use client" in its module script and a server page that renders it, it comes out as plain HTML with no client code. The same component as .tsx hydrates (#4).

Root cause

Farm plans which client components become islands by reading source files before any compiler runs. A .leaf file isn't JavaScript, so Farm found no "use client" in it. leaf's compiler also hoisted a module script's imports above its directive, so the compiled module didn't start with "use client" either.

Change

  • packages/farm-leaf/src/index.ts: compile(source, file) on the descriptor, which returns compileLeaf()'s output. Farm 0.1.86 reads .leaf files through it when it plans islands (farming-labs/farm.js#3163).
  • packages/farm-leaf/src/compile.ts: keeps a module script's directives ("use client") ahead of the imports it hoists.
  • app/: src/components/planter.leaf, a "use client" component with island = "load", and /islands/leaf, a .leaf server page that renders two of them, one with client:idle.
  • Tests:
    • packages/farm-leaf/test/compile.test.ts: the directive stays first.
    • e2e/custom-renderer.spec.ts: both planters render as <f-island> with their own strategy, hydrate, and keep separate state.
  • README: .leaf client components in the file table and in "Make it yours".

Safety and compatibility

  • compile() is new in Farm 0.1.86, so the app and farm-leaf move to @farm.js/core and @farm.js/cli 0.1.86, and farm-leaf's peer floor rises to >=0.1.86.
  • .tsx islands and pages without client components render as before.

Verification

Against @farm.js/core and @farm.js/cli 0.1.86 installed from npm:

  • pnpm type-check: passes in all three projects.
  • pnpm test: 57 passed.
  • pnpm test:e2e (production build) and pnpm test:e2e:dev: 20 passed each, with installed Chrome.
  • The same /islands/leaf test in Farm's custom-renderer example fails without compile(): no <f-island> for planter.leaf (farming-labs/farm.js#3163). Not rerun here.

Documentation and release

README updated. Farm documents compile() in Custom renderers. No package release: farm-leaf is private.


Summary by cubic

"use client" components written as .leaf files now hydrate as islands instead of rendering as plain HTML.

  • Adds compile() to farm-leaf's descriptor so Farm 0.1.86 can read .leaf files when planning islands.
  • Keeps a module script's directives ahead of hoisted imports in the compiled module.
  • Adds a planter.leaf client component and a /islands/leaf server page with e2e coverage.
  • Moves to @farm.js/core and @farm.js/cli 0.1.86; >=0.1.86 is now the peer floor.