clovie 0.1.46 ā 0.1.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clovie",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.49",
|
|
4
4
|
"description": "Vintage web dev tooling with modern quality of life",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"static-site-generator",
|
|
@@ -85,7 +85,7 @@
|
|
|
85
85
|
},
|
|
86
86
|
"dependencies": {
|
|
87
87
|
"@jucie.io/engine": "^1.1.68",
|
|
88
|
-
"@jucie.io/engine-esbuild": "^1.0.
|
|
88
|
+
"@jucie.io/engine-esbuild": "^1.0.8",
|
|
89
89
|
"@jucie.io/engine-server": "^1.0.26",
|
|
90
90
|
"@jucie.io/reactive": "^1.0.30",
|
|
91
91
|
"chalk": "^5.6.2",
|
package/scripts/publish.js
CHANGED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# AGENTS ā Clovie Server Project
|
|
2
|
+
|
|
3
|
+
This is a Clovie server application (`type: 'server'`). Clovie runs an HTTP server with API endpoints, server-rendered pages, and optional middleware, all defined in `clovie.config.js`.
|
|
4
|
+
|
|
5
|
+
## Project structure
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
project/
|
|
9
|
+
āāā clovie.config.js # All app behavior: api, routes, hooks, middleware
|
|
10
|
+
āāā package.json
|
|
11
|
+
āāā views/ # Server-rendered HTML templates (Nunjucks by default)
|
|
12
|
+
āāā partials/ # Reusable template components
|
|
13
|
+
āāā scripts/ # Client-side JavaScript (bundled with esbuild)
|
|
14
|
+
āāā styles/ # SCSS/CSS stylesheets
|
|
15
|
+
āāā assets/ # Static files (images, fonts, etc.)
|
|
16
|
+
āāā dist/ # Built assets (generated)
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Key concepts
|
|
20
|
+
|
|
21
|
+
- **`clovie.config.js`** drives everything: `api` for JSON endpoints, `routes` for SSR pages, `hooks` and `middleware` for request lifecycle.
|
|
22
|
+
- **API routes** use `{ method, path, handler }` where `handler` receives `ctx` and returns via `ctx.respond.json()`, `.html()`, `.text()`, or `.file()`.
|
|
23
|
+
- **SSR routes** pair a `path` with a `template` and an async `data` function that returns template variables.
|
|
24
|
+
- **Factories** (`defineApi`, `defineRoutes`, `defineMiddleware`, `defineHooks` from `'clovie'`) let handlers access engine services via `useContext`. You can mix raw objects and factories in the same config array.
|
|
25
|
+
- Registration order: **hooks -> middleware -> api -> view routes + routes**.
|
|
26
|
+
|
|
27
|
+
## Commands
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npm run dev # development server with live reload
|
|
31
|
+
npm run build # build assets
|
|
32
|
+
npm start # production server
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Full Clovie reference
|
|
36
|
+
|
|
37
|
+
For the complete API (ctx object, factories, hooks, middleware, CLI), see `node_modules/clovie/AGENTS.md` or run `clovie skills show` to get the bundled Cursor skill.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# AGENTS ā Clovie Static Site Project
|
|
2
|
+
|
|
3
|
+
This is a Clovie static site (`type: 'static'`). Clovie generates static HTML files from templates and data defined in `clovie.config.js`.
|
|
4
|
+
|
|
5
|
+
## Project structure
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
project/
|
|
9
|
+
āāā clovie.config.js # All site behavior: data, routes, render engine
|
|
10
|
+
āāā package.json
|
|
11
|
+
āāā views/ # HTML templates (Nunjucks by default)
|
|
12
|
+
āāā partials/ # Reusable template components
|
|
13
|
+
āāā scripts/ # Client-side JavaScript (bundled with esbuild)
|
|
14
|
+
āāā styles/ # SCSS/CSS stylesheets
|
|
15
|
+
āāā assets/ # Static files (images, fonts, etc.)
|
|
16
|
+
āāā dist/ # Built output (generated)
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Key concepts
|
|
20
|
+
|
|
21
|
+
- **`clovie.config.js`** drives everything: `data` for global template variables, `routes` for dynamic page generation, `renderEngine` for the template engine.
|
|
22
|
+
- **Views** in `views/` are compiled to HTML with the template data. Each `.html` file becomes a page.
|
|
23
|
+
- **Routes** with `repeat` generate multiple pages from data (e.g. one page per blog post).
|
|
24
|
+
- **Partials** are registered by filename (e.g. `partials/header.html` becomes `header`).
|
|
25
|
+
|
|
26
|
+
## Commands
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm run dev # development server with live reload
|
|
30
|
+
npm run build # production build to dist/
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Full Clovie reference
|
|
34
|
+
|
|
35
|
+
For the complete API (ctx object, factories, hooks, middleware, CLI), see `node_modules/clovie/AGENTS.md` or run `clovie skills show` to get the bundled Cursor skill.
|
|
@@ -1,300 +0,0 @@
|
|
|
1
|
-
# AI Development Guide for Clovie Server Projects
|
|
2
|
-
|
|
3
|
-
> **For AI Assistants**: This document explains how to work with Clovie server applications. Read this to understand the project structure, configuration, and development patterns. For Cursor, you can also install `clovie` and run **`clovie skills show`** to paste the bundled project skill, or copy `node_modules/clovie/.cursor/skills/clovie.mdc` into `.cursor/skills/`.
|
|
4
|
-
|
|
5
|
-
## What is Clovie Server Mode?
|
|
6
|
-
|
|
7
|
-
Clovie Server Mode creates full-stack web applications with Express-like functionality, API endpoints, and server-side rendering. It's built on a service-oriented architecture using `@jucie.io/engine`.
|
|
8
|
-
|
|
9
|
-
## Factories (`api`, `routes`, `middleware`, `hooks`)
|
|
10
|
-
|
|
11
|
-
Prefer **`defineRoutes`** / **`defineApi`** (same function), **`defineMiddleware`**, and **`defineHooks`** from `clovie` when:
|
|
12
|
-
|
|
13
|
-
- Handlers need **`useContext`** (e.g. `log`, file service) at registration time.
|
|
14
|
-
- You split APIs across files and export small factory modules.
|
|
15
|
-
|
|
16
|
-
`api` and `routes` accept **plain objects**, **arrays**, **one factory**, or **arrays mixing factories and raw routes**; the engine normalizes them before `server.use(...)`. Registration order: **hooks ā middleware ā api ā SSR routes (views + `routes`)**. See the upstream [CONFIGURATION.md](https://github.com/adrianjonmiller/clovie/blob/main/docs/CONFIGURATION.md) section *Factories for api, routes, middleware, and hooks*.
|
|
17
|
-
|
|
18
|
-
## Project Structure
|
|
19
|
-
|
|
20
|
-
```
|
|
21
|
-
project/
|
|
22
|
-
āāā clovie.config.js # Main configuration with API routes
|
|
23
|
-
āāā package.json # Dependencies and scripts
|
|
24
|
-
āāā views/ # Server-rendered HTML templates
|
|
25
|
-
āāā partials/ # Reusable template components
|
|
26
|
-
āāā scripts/ # Client-side JavaScript
|
|
27
|
-
āāā styles/ # SCSS/CSS stylesheets
|
|
28
|
-
āāā assets/ # Static files (images, fonts, etc.)
|
|
29
|
-
āāā dist/ # Built assets (generated)
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
## Configuration (`clovie.config.js`)
|
|
33
|
-
|
|
34
|
-
The configuration file defines API endpoints, server routes, and application behavior.
|
|
35
|
-
|
|
36
|
-
### Basic Server Configuration
|
|
37
|
-
|
|
38
|
-
```javascript
|
|
39
|
-
export default {
|
|
40
|
-
type: 'server', // Server application mode
|
|
41
|
-
port: 3000, // Server port
|
|
42
|
-
|
|
43
|
-
renderEngine: 'nunjucks', // Template engine
|
|
44
|
-
|
|
45
|
-
data: { // Global application data
|
|
46
|
-
app: {
|
|
47
|
-
name: 'My App',
|
|
48
|
-
version: '1.0.0'
|
|
49
|
-
},
|
|
50
|
-
nav: [
|
|
51
|
-
{ title: 'Home', url: '/' },
|
|
52
|
-
{ title: 'About', url: '/about' }
|
|
53
|
-
]
|
|
54
|
-
},
|
|
55
|
-
|
|
56
|
-
// API endpoints (RESTful JSON APIs)
|
|
57
|
-
api: [
|
|
58
|
-
{
|
|
59
|
-
path: '/api/status',
|
|
60
|
-
method: 'GET',
|
|
61
|
-
handler: async (ctx) => {
|
|
62
|
-
return ctx.respond.json({
|
|
63
|
-
status: 'ok',
|
|
64
|
-
timestamp: new Date().toISOString(),
|
|
65
|
-
uptime: process.uptime()
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
],
|
|
70
|
-
|
|
71
|
-
// Server-rendered routes (SSR pages)
|
|
72
|
-
routes: [
|
|
73
|
-
{
|
|
74
|
-
name: 'Homepage',
|
|
75
|
-
path: '/',
|
|
76
|
-
template: 'index.html',
|
|
77
|
-
data: async ({ context }) => {
|
|
78
|
-
return {
|
|
79
|
-
title: 'Welcome to My App'
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
]
|
|
84
|
-
};
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
## API Endpoints
|
|
88
|
-
|
|
89
|
-
API endpoints are defined in the `api` array and handle HTTP requests:
|
|
90
|
-
|
|
91
|
-
### Basic API Structure
|
|
92
|
-
|
|
93
|
-
```javascript
|
|
94
|
-
{
|
|
95
|
-
path: '/api/items',
|
|
96
|
-
method: 'GET',
|
|
97
|
-
handler: async (ctx) => {
|
|
98
|
-
return ctx.respond.json({ items: [] });
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
### Context Object (`ctx`)
|
|
104
|
-
|
|
105
|
-
The context object provides access to request data:
|
|
106
|
-
|
|
107
|
-
```javascript
|
|
108
|
-
// Request information
|
|
109
|
-
ctx.req.method // HTTP method (GET, POST, etc.)
|
|
110
|
-
ctx.req.url // Full URL
|
|
111
|
-
ctx.req.headers // Request headers
|
|
112
|
-
|
|
113
|
-
// Parsed data
|
|
114
|
-
ctx.body // Request body (JSON/form data)
|
|
115
|
-
ctx.query // URL query parameters (?name=value)
|
|
116
|
-
ctx.params // Route parameters (:id)
|
|
117
|
-
|
|
118
|
-
// Response methods
|
|
119
|
-
ctx.respond.json(data, status) // Send JSON response
|
|
120
|
-
ctx.respond.text(text, status) // Send text response
|
|
121
|
-
ctx.respond.html(html, status) // Send HTML response
|
|
122
|
-
ctx.respond.file(path, status) // Send file response
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
### API Examples
|
|
126
|
-
|
|
127
|
-
```javascript
|
|
128
|
-
// GET /api/items - List items
|
|
129
|
-
{
|
|
130
|
-
path: '/api/items',
|
|
131
|
-
method: 'GET',
|
|
132
|
-
handler: async (ctx) => {
|
|
133
|
-
return ctx.respond.json({ items: [] });
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// POST /api/items - Create item
|
|
138
|
-
{
|
|
139
|
-
path: '/api/items',
|
|
140
|
-
method: 'POST',
|
|
141
|
-
handler: async (ctx) => {
|
|
142
|
-
const { name } = ctx.body;
|
|
143
|
-
if (!name) {
|
|
144
|
-
return ctx.respond.json({ error: 'Name is required' }, 400);
|
|
145
|
-
}
|
|
146
|
-
return ctx.respond.json({ success: true, name }, 201);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// GET /api/items/:id - Get item by ID
|
|
151
|
-
{
|
|
152
|
-
path: '/api/items/:id',
|
|
153
|
-
method: 'GET',
|
|
154
|
-
handler: async (ctx) => {
|
|
155
|
-
const { id } = ctx.params;
|
|
156
|
-
return ctx.respond.json({ id });
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
## Server-Rendered Routes
|
|
162
|
-
|
|
163
|
-
Server routes generate HTML pages with server-side rendering:
|
|
164
|
-
|
|
165
|
-
### Route Structure
|
|
166
|
-
|
|
167
|
-
```javascript
|
|
168
|
-
{
|
|
169
|
-
name: 'Route Name',
|
|
170
|
-
path: '/dashboard',
|
|
171
|
-
template: 'dashboard.html',
|
|
172
|
-
data: async ({ context }) => {
|
|
173
|
-
return {
|
|
174
|
-
title: 'Dashboard',
|
|
175
|
-
user: context.req.user
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
The `setup` function receives `{ context, rerender }` and returns the template data. It can be async for data fetching:
|
|
182
|
-
|
|
183
|
-
```javascript
|
|
184
|
-
{
|
|
185
|
-
path: '/news',
|
|
186
|
-
template: 'news.html',
|
|
187
|
-
data: async ({ context }) => {
|
|
188
|
-
const response = await fetch('https://api.example.com/news');
|
|
189
|
-
const articles = await response.json();
|
|
190
|
-
return { articles, title: 'Latest News' };
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
```
|
|
194
|
-
|
|
195
|
-
## Template Integration
|
|
196
|
-
|
|
197
|
-
Templates receive data from `setup` and can use any supported template engine:
|
|
198
|
-
|
|
199
|
-
### Nunjucks Example
|
|
200
|
-
|
|
201
|
-
```html
|
|
202
|
-
<!-- views/dashboard.html -->
|
|
203
|
-
<h1>{{ title }}</h1>
|
|
204
|
-
|
|
205
|
-
{% if items.length > 0 %}
|
|
206
|
-
{% for item in items %}
|
|
207
|
-
<article>
|
|
208
|
-
<h3>{{ item.title }}</h3>
|
|
209
|
-
<p>{{ item.excerpt }}</p>
|
|
210
|
-
</article>
|
|
211
|
-
{% endfor %}
|
|
212
|
-
{% else %}
|
|
213
|
-
<p>No items found.</p>
|
|
214
|
-
{% endif %}
|
|
215
|
-
```
|
|
216
|
-
|
|
217
|
-
## Advanced Features
|
|
218
|
-
|
|
219
|
-
### Middleware Support
|
|
220
|
-
|
|
221
|
-
```javascript
|
|
222
|
-
import express from 'express';
|
|
223
|
-
import cors from 'cors';
|
|
224
|
-
|
|
225
|
-
export default {
|
|
226
|
-
type: 'server',
|
|
227
|
-
|
|
228
|
-
middleware: [
|
|
229
|
-
express.json(),
|
|
230
|
-
express.urlencoded({ extended: true }),
|
|
231
|
-
cors({ origin: 'http://localhost:3000' }),
|
|
232
|
-
|
|
233
|
-
(req, res, next) => {
|
|
234
|
-
console.log(`${new Date().toISOString()} ${req.method} ${req.url}`);
|
|
235
|
-
next();
|
|
236
|
-
}
|
|
237
|
-
],
|
|
238
|
-
|
|
239
|
-
api: [...]
|
|
240
|
-
};
|
|
241
|
-
```
|
|
242
|
-
|
|
243
|
-
### Server Hooks
|
|
244
|
-
|
|
245
|
-
```javascript
|
|
246
|
-
export default {
|
|
247
|
-
hooks: {
|
|
248
|
-
onRequest: async (ctx) => {
|
|
249
|
-
console.log(`${ctx.req.method} ${ctx.req.url}`);
|
|
250
|
-
},
|
|
251
|
-
preHandler: async (ctx) => {
|
|
252
|
-
if (ctx.req.url.startsWith('/api/') && !ctx.req.headers.authorization) {
|
|
253
|
-
return ctx.respond.json({ error: 'Unauthorized' }, 401);
|
|
254
|
-
}
|
|
255
|
-
},
|
|
256
|
-
onError: async (ctx, error) => {
|
|
257
|
-
console.error('Server error:', error);
|
|
258
|
-
return ctx.respond.json({ error: 'Internal server error' }, 500);
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
};
|
|
262
|
-
```
|
|
263
|
-
|
|
264
|
-
## Development Workflow
|
|
265
|
-
|
|
266
|
-
### Commands
|
|
267
|
-
|
|
268
|
-
```bash
|
|
269
|
-
# Development server with live reload
|
|
270
|
-
npm run dev
|
|
271
|
-
|
|
272
|
-
# Build assets
|
|
273
|
-
npm run build
|
|
274
|
-
|
|
275
|
-
# Production server
|
|
276
|
-
npm start
|
|
277
|
-
```
|
|
278
|
-
|
|
279
|
-
### File Watching
|
|
280
|
-
|
|
281
|
-
Clovie automatically watches and rebuilds:
|
|
282
|
-
- Template changes ā Re-render pages
|
|
283
|
-
- Script/style changes ā Rebuild assets
|
|
284
|
-
- Config changes ā Restart server
|
|
285
|
-
|
|
286
|
-
## Best Practices
|
|
287
|
-
|
|
288
|
-
### API Design
|
|
289
|
-
1. **Use proper HTTP status codes** (200, 201, 400, 404, 500)
|
|
290
|
-
2. **Validate input data** before processing
|
|
291
|
-
3. **Handle errors gracefully** with meaningful messages
|
|
292
|
-
4. **Use consistent response formats**
|
|
293
|
-
|
|
294
|
-
### Templates
|
|
295
|
-
1. **Use semantic HTML**
|
|
296
|
-
2. **Handle missing data** with fallbacks
|
|
297
|
-
3. **Keep templates simple** and focused
|
|
298
|
-
4. **Use partials** for reusable components
|
|
299
|
-
|
|
300
|
-
This guide should help you understand and work with any Clovie server project effectively.
|
|
@@ -1,301 +0,0 @@
|
|
|
1
|
-
# AI Development Guide for Clovie Projects
|
|
2
|
-
|
|
3
|
-
> **For AI Assistants**: This document explains how to work with Clovie projects. Read this to understand the project structure, configuration, and development patterns.
|
|
4
|
-
|
|
5
|
-
## What is Clovie?
|
|
6
|
-
|
|
7
|
-
Clovie is a Node.js-based static site generator and full-stack web framework that bridges static sites and web applications. It uses a service-oriented architecture built on `@jucie.io/engine`.
|
|
8
|
-
|
|
9
|
-
## Project Structure
|
|
10
|
-
|
|
11
|
-
```
|
|
12
|
-
project/
|
|
13
|
-
āāā clovie.config.js # Main configuration file
|
|
14
|
-
āāā package.json # Dependencies and scripts
|
|
15
|
-
āāā views/ # HTML templates (server-rendered)
|
|
16
|
-
āāā partials/ # Reusable template components
|
|
17
|
-
āāā scripts/ # Client-side JavaScript
|
|
18
|
-
āāā styles/ # SCSS/CSS stylesheets
|
|
19
|
-
āāā assets/ # Static files (images, fonts, etc.)
|
|
20
|
-
āāā data/ # Database files (server mode only)
|
|
21
|
-
āāā dist/ # Built output (generated)
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
## Configuration (`clovie.config.js`)
|
|
25
|
-
|
|
26
|
-
The configuration file is the heart of any Clovie project. It defines how the project behaves.
|
|
27
|
-
|
|
28
|
-
### Static Mode Configuration
|
|
29
|
-
|
|
30
|
-
```javascript
|
|
31
|
-
export default {
|
|
32
|
-
type: 'static', // Generate static HTML files
|
|
33
|
-
renderEngine: 'nunjucks', // Template engine
|
|
34
|
-
|
|
35
|
-
data: { // Global data available in templates
|
|
36
|
-
site: { title: 'My Site' },
|
|
37
|
-
nav: [{ title: 'Home', url: '/' }]
|
|
38
|
-
},
|
|
39
|
-
|
|
40
|
-
routes: [ // Dynamic page generation
|
|
41
|
-
{
|
|
42
|
-
name: 'Blog Posts',
|
|
43
|
-
path: '/posts/:slug',
|
|
44
|
-
template: 'post.html',
|
|
45
|
-
repeat: (data) => data.posts, // Generate page for each post
|
|
46
|
-
data: (globalData, post) => ({ // Data for each page
|
|
47
|
-
...globalData,
|
|
48
|
-
post,
|
|
49
|
-
title: `${post.title} - ${globalData.site.title}`
|
|
50
|
-
})
|
|
51
|
-
}
|
|
52
|
-
]
|
|
53
|
-
};
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
### Server Mode Configuration
|
|
57
|
-
|
|
58
|
-
```javascript
|
|
59
|
-
export default {
|
|
60
|
-
type: 'server', // Express-like web application
|
|
61
|
-
port: 3000,
|
|
62
|
-
|
|
63
|
-
data: { // Global application data
|
|
64
|
-
app: { name: 'My App' }
|
|
65
|
-
},
|
|
66
|
-
|
|
67
|
-
api: [ // REST API endpoints
|
|
68
|
-
{
|
|
69
|
-
path: '/api/users',
|
|
70
|
-
method: 'GET',
|
|
71
|
-
handler: async (ctx, database) => {
|
|
72
|
-
const users = database.collection('users');
|
|
73
|
-
return ctx.respond.json({ users: users.all() });
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
],
|
|
77
|
-
|
|
78
|
-
routes: [ // Server-rendered pages
|
|
79
|
-
{
|
|
80
|
-
name: 'User Profile',
|
|
81
|
-
path: '/user/:id',
|
|
82
|
-
template: 'profile.html',
|
|
83
|
-
data: async (ctx, database) => {
|
|
84
|
-
const user = database.collection('users').get([ctx.params.id]);
|
|
85
|
-
return { user, title: `${user.name}'s Profile` };
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
]
|
|
89
|
-
};
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
## Template Engines
|
|
93
|
-
|
|
94
|
-
Clovie supports multiple template engines:
|
|
95
|
-
|
|
96
|
-
### Nunjucks (Default - Jinja2-like)
|
|
97
|
-
```html
|
|
98
|
-
{% extends "layout.html" %}
|
|
99
|
-
{% block content %}
|
|
100
|
-
<h1>{{ site.title }}</h1>
|
|
101
|
-
{% for post in posts %}
|
|
102
|
-
<article>{{ post.title }}</article>
|
|
103
|
-
{% endfor %}
|
|
104
|
-
{% endblock %}
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
### Handlebars
|
|
108
|
-
```html
|
|
109
|
-
{{> header}}
|
|
110
|
-
<h1>{{site.title}}</h1>
|
|
111
|
-
{{#each posts}}
|
|
112
|
-
<article>{{title}}</article>
|
|
113
|
-
{{/each}}
|
|
114
|
-
{{> footer}}
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
### Pug
|
|
118
|
-
```pug
|
|
119
|
-
extends layout
|
|
120
|
-
block content
|
|
121
|
-
h1= site.title
|
|
122
|
-
each post in posts
|
|
123
|
-
article= post.title
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
## API Endpoints (Server Mode)
|
|
127
|
-
|
|
128
|
-
API endpoints receive a `ctx` (context) parameter:
|
|
129
|
-
|
|
130
|
-
```javascript
|
|
131
|
-
{
|
|
132
|
-
path: '/api/posts/:id',
|
|
133
|
-
method: 'GET',
|
|
134
|
-
handler: async (ctx) => {
|
|
135
|
-
const { id } = ctx.params;
|
|
136
|
-
return ctx.respond.json({ id });
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
### Context Object (`ctx`)
|
|
142
|
-
- `ctx.req` - HTTP request object
|
|
143
|
-
- `ctx.body` - Request body (parsed JSON/form data)
|
|
144
|
-
- `ctx.query` - URL query parameters
|
|
145
|
-
- `ctx.params` - Route parameters
|
|
146
|
-
- `ctx.respond.json(data, status)` - Send JSON response
|
|
147
|
-
- `ctx.respond.text(text, status)` - Send text response
|
|
148
|
-
- `ctx.respond.html(html, status)` - Send HTML response
|
|
149
|
-
|
|
150
|
-
## Development Commands
|
|
151
|
-
|
|
152
|
-
```bash
|
|
153
|
-
# Development server with live reload
|
|
154
|
-
npm run dev
|
|
155
|
-
# or
|
|
156
|
-
clovie dev
|
|
157
|
-
|
|
158
|
-
# Build for production
|
|
159
|
-
npm run build
|
|
160
|
-
# or
|
|
161
|
-
clovie build
|
|
162
|
-
|
|
163
|
-
# Start production server (server mode)
|
|
164
|
-
npm start
|
|
165
|
-
# or
|
|
166
|
-
clovie serve
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
## Common Development Patterns
|
|
170
|
-
|
|
171
|
-
### 1. Loading External Data
|
|
172
|
-
```javascript
|
|
173
|
-
export default {
|
|
174
|
-
data: async () => {
|
|
175
|
-
const posts = await fetch('https://api.example.com/posts')
|
|
176
|
-
.then(r => r.json());
|
|
177
|
-
|
|
178
|
-
return {
|
|
179
|
-
site: { title: 'My Blog' },
|
|
180
|
-
posts,
|
|
181
|
-
categories: [...new Set(posts.map(p => p.category))]
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
};
|
|
185
|
-
```
|
|
186
|
-
|
|
187
|
-
### 2. Dynamic Route Generation
|
|
188
|
-
```javascript
|
|
189
|
-
routes: [
|
|
190
|
-
{
|
|
191
|
-
name: 'Blog Posts',
|
|
192
|
-
path: '/posts/:slug',
|
|
193
|
-
template: 'post.html',
|
|
194
|
-
repeat: (data) => data.posts,
|
|
195
|
-
data: (globalData, post) => ({
|
|
196
|
-
...globalData,
|
|
197
|
-
post,
|
|
198
|
-
title: `${post.title} - ${globalData.site.title}`
|
|
199
|
-
})
|
|
200
|
-
}
|
|
201
|
-
]
|
|
202
|
-
```
|
|
203
|
-
|
|
204
|
-
### 3. API with Validation
|
|
205
|
-
```javascript
|
|
206
|
-
{
|
|
207
|
-
path: '/api/posts',
|
|
208
|
-
method: 'POST',
|
|
209
|
-
handler: async (ctx) => {
|
|
210
|
-
const { title, content } = ctx.body;
|
|
211
|
-
|
|
212
|
-
if (!title || title.trim().length < 3) {
|
|
213
|
-
return ctx.respond.json(
|
|
214
|
-
{ error: 'Title must be at least 3 characters' },
|
|
215
|
-
400
|
|
216
|
-
);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
return ctx.respond.json({ success: true, title: title.trim(), content }, 201);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
```
|
|
223
|
-
|
|
224
|
-
### 4. Server-Side Rendering with Data
|
|
225
|
-
```javascript
|
|
226
|
-
{
|
|
227
|
-
name: 'News Feed',
|
|
228
|
-
path: '/news',
|
|
229
|
-
template: 'news.html',
|
|
230
|
-
data: async ({ context }) => {
|
|
231
|
-
const response = await fetch('https://api.example.com/news');
|
|
232
|
-
const articles = await response.json();
|
|
233
|
-
return { articles, title: 'Latest News' };
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
```
|
|
237
|
-
|
|
238
|
-
## File Watching & Live Reload
|
|
239
|
-
|
|
240
|
-
Clovie automatically watches for changes in:
|
|
241
|
-
- `views/` - Template files
|
|
242
|
-
- `partials/` - Partial components
|
|
243
|
-
- `scripts/` - JavaScript files
|
|
244
|
-
- `styles/` - SCSS/CSS files
|
|
245
|
-
- `assets/` - Static assets
|
|
246
|
-
|
|
247
|
-
Changes trigger automatic rebuilds and browser refresh.
|
|
248
|
-
|
|
249
|
-
## Asset Pipeline
|
|
250
|
-
|
|
251
|
-
- **Scripts**: Compiled with esbuild (or custom compiler)
|
|
252
|
-
- **Styles**: SCSS compilation with Sass
|
|
253
|
-
- **Assets**: Copied as-is to output directory
|
|
254
|
-
- **Templates**: Rendered with chosen template engine
|
|
255
|
-
|
|
256
|
-
## Error Handling
|
|
257
|
-
|
|
258
|
-
### API Errors
|
|
259
|
-
```javascript
|
|
260
|
-
// Return proper HTTP status codes
|
|
261
|
-
return ctx.respond.json({ error: 'Not found' }, 404);
|
|
262
|
-
return ctx.respond.json({ error: 'Invalid input' }, 400);
|
|
263
|
-
```
|
|
264
|
-
|
|
265
|
-
### Template Errors
|
|
266
|
-
```javascript
|
|
267
|
-
// Handle missing data gracefully in templates
|
|
268
|
-
{% if user %}
|
|
269
|
-
<h1>{{ user.name }}</h1>
|
|
270
|
-
{% else %}
|
|
271
|
-
<h1>Unknown User</h1>
|
|
272
|
-
{% endif %}
|
|
273
|
-
```
|
|
274
|
-
|
|
275
|
-
## Best Practices
|
|
276
|
-
|
|
277
|
-
1. **Use async data loading** for external APIs
|
|
278
|
-
2. **Validate input** in API endpoints
|
|
279
|
-
3. **Handle errors gracefully** with proper HTTP status codes
|
|
280
|
-
4. **Use semantic HTML** in templates
|
|
281
|
-
5. **Organize data logically** in the config file
|
|
282
|
-
6. **Test builds regularly** with `npm run build`
|
|
283
|
-
|
|
284
|
-
## Troubleshooting
|
|
285
|
-
|
|
286
|
-
### Common Issues
|
|
287
|
-
- **Port already in use**: `clovie kill --port 3000`
|
|
288
|
-
- **Template not found**: Check file paths in config
|
|
289
|
-
- **Build failures**: Check console output for detailed errors
|
|
290
|
-
|
|
291
|
-
### Debug Mode
|
|
292
|
-
Add `console.log()` statements in handlers and data functions to debug issues.
|
|
293
|
-
|
|
294
|
-
## Key Differences from Other Frameworks
|
|
295
|
-
|
|
296
|
-
- **No client-side routing** - Uses traditional server-side rendering
|
|
297
|
-
- **No build-time JavaScript** - Server handles all dynamic content
|
|
298
|
-
- **Template agnostic** - Choose your preferred template engine
|
|
299
|
-
- **Dual mode** - Same codebase can be static or server-rendered
|
|
300
|
-
|
|
301
|
-
This guide should help you understand and work with any Clovie project effectively.
|