feat(farm-leaf): hydrate .leaf client components as islands
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 returnscompileLeaf()'s output. Farm 0.1.86 reads.leaffiles 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 withisland = "load", and/islands/leaf, a.leafserver page that renders two of them, one withclient: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:
.leafclient 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/coreand@farm.js/cli0.1.86, and farm-leaf's peer floor rises to>=0.1.86..tsxislands 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) andpnpm test:e2e:dev: 20 passed each, with installed Chrome.- The same
/islands/leaftest in Farm's custom-renderer example fails withoutcompile(): no<f-island>forplanter.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.leaffiles when planning islands. - Keeps a module script's directives ahead of hoisted imports in the compiled module.
- Adds a
planter.leafclient component and a/islands/leafserver page with e2e coverage. - Moves to
@farm.js/coreand@farm.js/cli0.1.86;>=0.1.86is now the peer floor.