Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/NewProject.res
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,35 @@ let updateRescriptJson = async (~projectName, ~versions: RescriptVersions.versio
let newProjectMessage = "Create a new ReScript project"

let getTemplateOptions = () =>
Templates.templates->Array.map(({name, displayName, shortDescription}) => {
Templates.templates->Array.map(({name, displayName, shortDescription, _}) => {
P.value: name,
label: displayName,
hint: shortDescription,
})

let getVariantOptions = (variants: array<Templates.variant>) =>
variants->Array.map(({name, displayName, shortDescription}) => {
P.value: name,
label: displayName,
hint: shortDescription,
})

let promptTemplateName = async () => {
let selectedName = await P.select({
message: "Select a template",
options: getTemplateOptions(),
})->P.resultOrRaise

switch Templates.templates->Array.find(template => template.name === selectedName) {
| Some({variants: Some(variants)}) =>
await P.select({
message: "Select a variant",
options: getVariantOptions(variants),
})->P.resultOrRaise
| _ => selectedName
}
}

Comment on lines +73 to +95
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new optional prompt for a stack variant, let me know if you'd prefer a more flat structure as we have now. Just aiming to keep the first selection cleaner.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could eventually do the same for the react ones

let createProject = async (~templateName, ~projectName, ~versions) => {
let templatePath = CraPaths.getTemplatePath(~templateName)
let projectPath = Path.join2(Process.cwd(), projectName)
Expand Down Expand Up @@ -139,11 +162,7 @@ let createNewProject = async () => {

let templateName = switch commandLineArguments.templateName {
| Some(templateName) => templateName
| None =>
await P.select({
message: "Select a template",
options: getTemplateOptions(),
})->P.resultOrRaise
| None => await promptTemplateName()
}

let versions = useDefaultVersions
Expand Down
31 changes: 30 additions & 1 deletion src/Templates.res
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
type variant = {
name: string,
displayName: string,
shortDescription: string,
}

type t = {
name: string,
displayName: string,
shortDescription: string,
variants: option<array<variant>>,
}

let basicTemplateName = "rescript-template-basic"
let viteTemplateName = "rescript-template-vite"
let nextjsTemplateName = "rescript-template-nextjs"
let xoteTemplateName = "rescript-template-xote"
let xoteSsrTemplateName = "rescript-template-xote-ssr"
let templateNamePrefix = "rescript-template-"

let supportedTemplateNames = ["vite", "nextjs", "basic"]
let supportedTemplateNames = ["vite", "nextjs", "xote", "xote-ssr", "basic"]

let getTemplateName = templateName => {
let templateName = templateName->String.toLowerCase
Expand All @@ -24,15 +33,35 @@ let templates = [
name: viteTemplateName,
displayName: "Vite",
shortDescription: "Vite 7, React and Tailwind 4",
variants: None,
},
{
name: nextjsTemplateName,
displayName: "Next.js",
shortDescription: "Next.js 15 with static export and Tailwind 3",
variants: None,
},
{
name: xoteTemplateName,
displayName: "Xote",
shortDescription: "Xote with Vite, signals and Tailwind 4",
variants: Some([
{
name: xoteTemplateName,
displayName: "Client-Side Rendering (CSR)",
shortDescription: "Vite, signals and Tailwind 4",
},
{
name: xoteSsrTemplateName,
displayName: "Server-Side Rendering (SSR)",
shortDescription: "Vite SSR via Node server, signals and Tailwind 4",
},
]),
},
{
name: basicTemplateName,
displayName: "Basic",
shortDescription: "Command line hello world app",
variants: None,
},
]
70 changes: 70 additions & 0 deletions templates/rescript-template-xote-ssr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# xote · SSR template

A server-rendered starter for [xote](https://github.com/brnrdog/xote) apps. Renders HTML on a Node server, then hydrates on the client so reactivity takes over without re-rendering.

## Stack

- ReScript v12 (JSX transform: `XoteJSX`)
- xote ^6.2 — uses `SSR.renderToString`, `SSRState`, and `Hydration`
- rescript-signals
- Vite 7 — runs in middleware mode for dev, and produces both client and SSR bundles for production
- Tailwind CSS v4 (`@tailwindcss/vite`)

## Layout

```
.
├── index.html # template with <!--app-html--> and <!--app-state--> placeholders
├── server.mjs # Node HTTP server (Vite middleware in dev, static + SSR in prod)
├── vite.config.mjs
├── rescript.json
├── package.json
└── src/
├── styles.css
├── Counter.res # uses SSRState.signal so state survives hydration
├── Page.res # landing page composition
├── App.res # shared `view` thunk used by server and client
├── Server.res # exports render() → { html, stateScript }
└── Client.res # imports styles, calls Hydration.hydrateById
```

## How it fits together

1. `server.mjs` reads `index.html` and (in dev) hands it to Vite for asset/CSS transformation.
2. It imports `Server.res` and calls `render()`, which uses `SSR.renderToString(App.view)` to produce HTML and `SSRState.generateScript()` to produce a `<script>` tag carrying initial signal values.
3. The placeholders `<!--app-html-->` and `<!--app-state-->` in `index.html` are replaced with those two strings.
4. The browser loads `Client.res.mjs`, which calls `Hydration.hydrateById(App.view, "root")`. xote walks the existing DOM, attaches reactive bindings, and reads back the serialized `SSRState` so signals start with the same values the server used.

## Scripts

| Script | What it does |
| --- | --- |
| `npm run res:dev` | Watches and compiles ReScript on save. Run during development. |
| `npm run res:build` | One-shot ReScript build. |
| `npm run res:clean` | Removes ReScript build artifacts. |
| `npm run dev` | Runs `node server.mjs` with Vite in middleware mode. |
| `npm run build` | Builds the client bundle and the SSR bundle. |
| `npm run build:client` | Just the client bundle (`dist/client/`). |
| `npm run build:ssr` | Just the SSR bundle (`dist/server/Server.res.js`). |
| `npm run start` | Runs the server in production mode against the built bundles. |

## Develop

```bash
npm install
npm run res:dev
# in another terminal:
npm run dev
```

Open http://localhost:3000. The page is server-rendered (view source — the counter HTML is already there) and becomes interactive after hydration.

## Build & run in production

```bash
npm run res:build
npm run build
npm run start
```

`server.mjs` defaults to port 3000; override with `PORT=8080 npm run start`.
6 changes: 6 additions & 0 deletions templates/rescript-template-xote-ssr/_gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
dist
lib
*.res.mjs
*.res.js
.DS_Store
14 changes: 14 additions & 0 deletions templates/rescript-template-xote-ssr/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>xote · SSR template</title>
<link rel="stylesheet" href="/src/styles.css" />
</head>
<body>
<div id="root"><!--app-html--></div>
<!--app-state-->
<script type="module" src="/src/Client.res.mjs"></script>
</body>
</html>
Loading
Loading