Waku¶
Daishi Kato 製の Waku を採用した最小構成の React Server Components アプリです。テンプレートは RSC 境界の 両側を ReScript で提供します: Server Component (Greet.res、JS は配信されない) と Client Component (Counter.res、薄い TSX "use client" ラッパー越しにクライアント化)。
生成されるレイアウト¶
src/
├── pages/
│ └── index.tsx
├── components/
│ ├── Greet.res
│ ├── Counter.res
│ └── CounterClient.tsx
└── __tests__/
└── Greet.test.mjs
tsconfig.json
rescript.json
package.json
主なファイル¶
src/pages/index.tsx— Server Component (src/pages/配下の Waku 既定)。ReScript のGreetを直接、CounterClientClient ラッパーを併せて import します。src/components/Greet.res— Server Component の本体。サーバー側で HTML にレンダリングされ、このコンポーネントの JS は一切配信されません。src/components/Counter.res—useStateを使った ReScript 製 Client Component の本体。src/components/CounterClient.tsx—"use client"ディレクティブを持ち、ReScript のmake関数を再エクスポートする薄い TSX ラッパー。ReScript 自身は"use client"を出力できないため、この小さなファイルが正攻法のワークアラウンドになります。
npm スクリプト¶
コマンド |
説明 |
|---|---|
|
|
|
ReScript をコンパイルしてから |
|
本番サーバーを起動します。 |
|
Vitest を実行します。 |
Day-two ポインタ¶
Server Component の追加 —
src/components/配下に新しい.resを追加し、ページや別の Server Component から import するだけで OK です。Client ラッパーは不要です。Client Component の追加 — 本体を ReScript で書き、先頭が
"use client"で始まりmakeを再エクスポートする<Name>Client.tsxラッパーを追加します。ラッパーはできるだけ薄く保ってください。Server Functions — お使いの Waku バージョンで安定していれば、サーバーファイルから任意の関数をエクスポートして Client Component から呼び出します。Waku がビルド時に RPC へ変換します。