clarusjs 0.12.0 → 0.14.0
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/README.md +2 -0
- package/dist/check/generate.js +54 -11
- package/dist/cli/check.js +15 -4
- package/dist/compiler/class-style.d.ts +2 -0
- package/dist/compiler/class-style.js +53 -0
- package/dist/compiler/emit-client.js +24 -4
- package/dist/compiler/emit-server.js +15 -5
- package/dist/compiler/html-expr.d.ts +10 -2
- package/dist/compiler/html-expr.js +31 -17
- package/dist/compiler/icon.d.ts +24 -0
- package/dist/compiler/icon.js +83 -0
- package/dist/compiler/index.d.ts +4 -0
- package/dist/compiler/index.js +53 -9
- package/dist/compiler/ir.d.ts +23 -0
- package/dist/compiler/ir.js +69 -2
- package/dist/compiler/lint.js +12 -1
- package/dist/compiler/parse.d.ts +24 -0
- package/dist/compiler/parse.js +66 -0
- package/dist/compiler/script.d.ts +10 -1
- package/dist/compiler/script.js +17 -3
- package/dist/fetch/index.d.ts +157 -3
- package/dist/fetch/index.js +135 -14
- package/dist/icons.d.ts +59 -0
- package/dist/icons.js +112 -0
- package/dist/runtime/attrs.d.ts +27 -0
- package/dist/runtime/attrs.js +66 -0
- package/dist/runtime/dom.d.ts +15 -0
- package/dist/runtime/dom.js +50 -1
- package/dist/runtime/html.d.ts +6 -0
- package/dist/runtime/html.js +19 -1
- package/dist/runtime/index.d.ts +3 -2
- package/dist/runtime/index.js +3 -2
- package/dist/runtime/server.d.ts +14 -1
- package/dist/runtime/server.js +36 -2
- package/dist/server/handler.d.ts +1 -1
- package/dist/server/handler.js +5 -2
- package/dist/server/views.d.ts +1 -1
- package/dist/server/views.js +1 -1
- package/dist/vite/node.d.ts +2 -0
- package/dist/vite/node.js +13 -0
- package/dist/vite/plugin.d.ts +6 -1
- package/dist/vite/plugin.js +114 -17
- package/dist/vite/sitemap.d.ts +20 -1
- package/dist/vite/sitemap.js +27 -3
- package/package.json +5 -1
package/dist/runtime/attrs.js
CHANGED
|
@@ -29,6 +29,33 @@ function safeUrl(url, image) {
|
|
|
29
29
|
// ARIA states whose absence means "doesn't apply" (no aria-expanded: nothing expands). Other aria-* (aria-current:
|
|
30
30
|
// `active ? 'page' : false`) keep false = removed.
|
|
31
31
|
const ENUMERATED = new Set(['contenteditable', 'draggable', 'spellcheck', 'translate', 'aria-expanded', 'aria-pressed', 'aria-checked', 'aria-selected']);
|
|
32
|
+
/**
|
|
33
|
+
* A class from a string (as written), an object (its truthy keys) or an array (flattened, falsy entries dropped), like
|
|
34
|
+
* clsx. Templates use it for `[class]` values and `[class.name]` toggles.
|
|
35
|
+
* @example
|
|
36
|
+
* classValue(['a', on && 'b', { c: true, d: false }]) // 'a b c' when on
|
|
37
|
+
* @see https://clarusjs.com/guide/attributes
|
|
38
|
+
*/
|
|
39
|
+
export function classValue(value) {
|
|
40
|
+
const names = (v) => (!v ? [] : typeof v === 'string' ? v.split(/\s+/) : Array.isArray(v) ? v.flatMap(names) : typeof v === 'object' ? Object.keys(v).filter((k) => v[k]) : [String(v)]);
|
|
41
|
+
// Each class once, single spaces: a written class="star" and a bound 'star on' make "star on".
|
|
42
|
+
return [...new Set(names(value).filter(Boolean))].join(' ');
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* A style from a string (as written), an object (camelCase keys to kebab-case, custom properties as they are; null,
|
|
46
|
+
* undefined, false and '' left out; no units added) or an array of those. Templates use it for `[style]` values and
|
|
47
|
+
* `[style.prop]` bindings.
|
|
48
|
+
* @example
|
|
49
|
+
* styleValue({ fontSize: '2em', '--accent': c, color: null }) // 'font-size: 2em; --accent: …;'
|
|
50
|
+
* @see https://clarusjs.com/guide/attributes
|
|
51
|
+
*/
|
|
52
|
+
export function styleValue(value) {
|
|
53
|
+
// A string stays whole (a url(data:…;base64,…) holds a ';'); only a trailing ';' is dropped before joining.
|
|
54
|
+
const decls = (v) => !v ? [] : typeof v === 'string' ? [v.trim().replace(/;\s*$/, '')].filter(Boolean) : Array.isArray(v) ? v.flatMap(decls) : typeof v === 'object'
|
|
55
|
+
? Object.entries(v).filter(([, x]) => x != null && x !== false && x !== '').map(([k, x]) => `${k.startsWith('--') ? k : k.replace(/[A-Z]/g, (m) => '-' + m.toLowerCase())}: ${x}`)
|
|
56
|
+
: [];
|
|
57
|
+
return decls(value).map((d) => d + ';').join(' ');
|
|
58
|
+
}
|
|
32
59
|
/**
|
|
33
60
|
* What an attribute should become: `null` removes it, `true` makes it a bare boolean attribute, a string is its value.
|
|
34
61
|
*/
|
|
@@ -36,6 +63,14 @@ export function attrValue(name, value, tag) {
|
|
|
36
63
|
if (value == null)
|
|
37
64
|
return null;
|
|
38
65
|
const n = name.toLowerCase();
|
|
66
|
+
// A class from an object or an array (a spread's class, or one bound directly): its classes, never [object Object].
|
|
67
|
+
if (n === 'class' && typeof value === 'object')
|
|
68
|
+
return classValue(value) || null;
|
|
69
|
+
if (n === 'style' && typeof value === 'object')
|
|
70
|
+
return styleValue(value) || null;
|
|
71
|
+
// An empty class or style adds nothing (a spread without one, a class whose toggles are all off).
|
|
72
|
+
if ((n === 'class' || n === 'style') && value === '')
|
|
73
|
+
return null;
|
|
39
74
|
if (typeof value === 'boolean' && ENUMERATED.has(n))
|
|
40
75
|
return n === 'translate' ? (value ? 'yes' : 'no') : String(value); // translate: yes/no
|
|
41
76
|
if (value === false)
|
|
@@ -62,3 +97,34 @@ export function attrValue(name, value, tag) {
|
|
|
62
97
|
return UNSAFE_URL;
|
|
63
98
|
return s;
|
|
64
99
|
}
|
|
100
|
+
const ATTR_NAME = /^[a-zA-Z_:][-a-zA-Z0-9_:.]*$/;
|
|
101
|
+
/**
|
|
102
|
+
* A spread's attributes (#405): each key with its value as attrValue gives it, except the names written on the element
|
|
103
|
+
* (`skip`, lowercase), class and style (merged with the element's own), and what a spread may never set, listed in
|
|
104
|
+
* `dropped` with why: event handlers, srcdoc, invalid names, JSX names and objects or functions.
|
|
105
|
+
* @internal the runtimes call this; apps write [...]="attrs".
|
|
106
|
+
*/
|
|
107
|
+
export function spreadAttrs(obj, skip, tag) {
|
|
108
|
+
const set = [];
|
|
109
|
+
const dropped = [];
|
|
110
|
+
if (!obj || typeof obj !== 'object')
|
|
111
|
+
return { set, dropped };
|
|
112
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
113
|
+
const n = key.toLowerCase();
|
|
114
|
+
if (skip.includes(n) || n === 'class' || n === 'style')
|
|
115
|
+
continue;
|
|
116
|
+
const why = !ATTR_NAME.test(key) ? 'not an attribute name'
|
|
117
|
+
// clarusjs's own markers (data-cl-e, data-cl-a, data-cl-path…): data must not spoof the runtime's wiring.
|
|
118
|
+
: n.startsWith('data-cl-') || n.startsWith('cl-') ? 'reserved for clarusjs'
|
|
119
|
+
: n.startsWith('on') ? 'an event handler: use (event)="…" in an island'
|
|
120
|
+
: n === 'srcdoc' ? 'srcdoc is static only'
|
|
121
|
+
: key === 'className' ? 'a JSX name: use class' : key === 'htmlFor' ? 'a JSX name: use for'
|
|
122
|
+
: value !== null && (typeof value === 'object' || typeof value === 'function') ? 'not a value for an attribute (an object or a function)'
|
|
123
|
+
: '';
|
|
124
|
+
if (why)
|
|
125
|
+
dropped.push([key, why]);
|
|
126
|
+
else
|
|
127
|
+
set.push([key, attrValue(key, value, tag)]);
|
|
128
|
+
}
|
|
129
|
+
return { set, dropped };
|
|
130
|
+
}
|
package/dist/runtime/dom.d.ts
CHANGED
|
@@ -41,6 +41,15 @@ export declare function block(ctx: Ctx, index: number, which: () => number, bran
|
|
|
41
41
|
html: () => string;
|
|
42
42
|
wire: () => void;
|
|
43
43
|
}[]): void;
|
|
44
|
+
/**
|
|
45
|
+
* An @await's promise in an island (#430): `k()` is 0 while it's pending, 1 once it resolved, 2 once it rejected
|
|
46
|
+
* (tracked, so a block follows it); `v()` is the value or error. When the expression's signals change, a new
|
|
47
|
+
* promise starts (back to pending) and only the newest one's answer counts. @internal
|
|
48
|
+
*/
|
|
49
|
+
export declare function awaited(fn: () => unknown): {
|
|
50
|
+
k(): number;
|
|
51
|
+
v(): unknown;
|
|
52
|
+
};
|
|
44
53
|
/** @internal Compiled components call this; apps don't. */
|
|
45
54
|
export declare function region(ctx: Ctx, index: number, html: () => string, wire: () => void): void;
|
|
46
55
|
/**
|
|
@@ -69,6 +78,12 @@ export declare function text(hole: Hole, fn: () => unknown): void;
|
|
|
69
78
|
export declare function on(ctx: Ctx, index: number, type: string, fn: (e: Event) => void): void;
|
|
70
79
|
/** The element matching `selector` in `ctx`: this island's (not a nested island's), and in a row, that row's. */
|
|
71
80
|
export declare function marked(ctx: Ctx, selector: string): Element | undefined;
|
|
81
|
+
/**
|
|
82
|
+
* A reactive spread (#405): sets the attributes the object has, removes the ones it no longer has (only those it set:
|
|
83
|
+
* never one written on the element, which `skip` lists). Dropped keys are dropped silently here; the server warns.
|
|
84
|
+
* @internal
|
|
85
|
+
*/
|
|
86
|
+
export declare function spread(ctx: Ctx, index: number, fn: () => unknown, skip: readonly string[], tag?: string): void;
|
|
72
87
|
/** Keep attribute `name` of the element marked `data-cl-a="index"` in `ctx` in sync with `fn()`. @internal */
|
|
73
88
|
export declare function attr(ctx: Ctx, index: number, name: string, fn: () => unknown, tag?: string): void;
|
|
74
89
|
/**
|
package/dist/runtime/dom.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Attach helpers: find markers in server HTML and keep them updated. Browser only. Never re-renders existing HTML;
|
|
2
2
|
// only @if branches and @for rows that did not exist on the server are rendered here.
|
|
3
|
-
import { attrValue } from './attrs.js';
|
|
3
|
+
import { attrValue, spreadAttrs } from './attrs.js';
|
|
4
4
|
import { batch, effect, onDispose, scope, state, untrack } from './signals.js';
|
|
5
5
|
const rootOf = (ctx) => ('root' in ctx ? ctx.root : ctx);
|
|
6
6
|
/** Is `node` inside `ctx` (always true for a whole island)? */
|
|
@@ -139,6 +139,30 @@ const onlyIsland = (nodes) => {
|
|
|
139
139
|
const kept = nodes.filter((n) => n.nodeType !== 3 || n.textContent.trim());
|
|
140
140
|
return kept.length === 1 && kept[0].localName === 'cl-island' ? kept[0] : null;
|
|
141
141
|
};
|
|
142
|
+
/**
|
|
143
|
+
* An @await's promise in an island (#430): `k()` is 0 while it's pending, 1 once it resolved, 2 once it rejected
|
|
144
|
+
* (tracked, so a block follows it); `v()` is the value or error. When the expression's signals change, a new
|
|
145
|
+
* promise starts (back to pending) and only the newest one's answer counts. @internal
|
|
146
|
+
*/
|
|
147
|
+
export function awaited(fn) {
|
|
148
|
+
const k = state(0);
|
|
149
|
+
let v;
|
|
150
|
+
let run = 0;
|
|
151
|
+
onDispose(() => { run++; }); // an answer after the island is gone changes nothing
|
|
152
|
+
effect(() => {
|
|
153
|
+
const p = fn();
|
|
154
|
+
const id = ++run;
|
|
155
|
+
untrack(() => k.set(0));
|
|
156
|
+
Promise.resolve(p).then((x) => { if (id === run) {
|
|
157
|
+
v = x;
|
|
158
|
+
k.set(1);
|
|
159
|
+
} }, (e) => { if (id === run) {
|
|
160
|
+
v = e;
|
|
161
|
+
k.set(2);
|
|
162
|
+
} });
|
|
163
|
+
});
|
|
164
|
+
return { k: () => k.get(), v: () => v };
|
|
165
|
+
}
|
|
142
166
|
/** @internal Compiled components call this; apps don't. */
|
|
143
167
|
export function region(ctx, index, html, wire) {
|
|
144
168
|
const r = scan(ctx).regions[index];
|
|
@@ -328,6 +352,31 @@ export function marked(ctx, selector) {
|
|
|
328
352
|
const island = root.closest('cl-island');
|
|
329
353
|
return [...root.querySelectorAll(selector)].find((e) => e.closest('cl-island') === island && inside(ctx, e));
|
|
330
354
|
}
|
|
355
|
+
/**
|
|
356
|
+
* A reactive spread (#405): sets the attributes the object has, removes the ones it no longer has (only those it set:
|
|
357
|
+
* never one written on the element, which `skip` lists). Dropped keys are dropped silently here; the server warns.
|
|
358
|
+
* @internal
|
|
359
|
+
*/
|
|
360
|
+
export function spread(ctx, index, fn, skip, tag) {
|
|
361
|
+
const el = marked(ctx, `[data-cl-a="${index}"]`);
|
|
362
|
+
if (!el)
|
|
363
|
+
return;
|
|
364
|
+
let had = new Set();
|
|
365
|
+
effect(() => {
|
|
366
|
+
const now = new Set();
|
|
367
|
+
for (const [name, v] of spreadAttrs(fn(), skip, tag).set) {
|
|
368
|
+
now.add(name);
|
|
369
|
+
if (v === null)
|
|
370
|
+
el.removeAttribute(name);
|
|
371
|
+
else
|
|
372
|
+
el.setAttribute(name, v === true ? '' : v);
|
|
373
|
+
}
|
|
374
|
+
for (const name of had)
|
|
375
|
+
if (!now.has(name))
|
|
376
|
+
el.removeAttribute(name);
|
|
377
|
+
had = now;
|
|
378
|
+
});
|
|
379
|
+
}
|
|
331
380
|
/** Keep attribute `name` of the element marked `data-cl-a="index"` in `ctx` in sync with `fn()`. @internal */
|
|
332
381
|
export function attr(ctx, index, name, fn, tag) {
|
|
333
382
|
const el = marked(ctx, `[data-cl-a="${index}"]`);
|
package/dist/runtime/html.d.ts
CHANGED
|
@@ -4,6 +4,12 @@ export declare const HTML: unique symbol;
|
|
|
4
4
|
export declare function escape(value: unknown): string;
|
|
5
5
|
/** A value as JSON for a <script> (JSON-LD): <, >, & and line separators escaped, so it can't end the script. @internal */
|
|
6
6
|
export declare function json(value: unknown): string;
|
|
7
|
+
/**
|
|
8
|
+
* A spread's attributes as HTML (#405); `where` (file:line:col, server code only) names the place in a warning for
|
|
9
|
+
* each key it drops, once.
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
export declare function spreadHtml(obj: unknown, skip: readonly string[], tag?: string, where?: string): string;
|
|
7
13
|
/** ` name="value"`, ` name` (true) or '' (false/null/undefined), escaped and URL-checked. @internal */
|
|
8
14
|
export declare function attrHtml(name: string, value: unknown, tag?: string): string;
|
|
9
15
|
/** An @if block inside an island: markers record which branch (k, or -1 for none) was rendered. @internal */
|
package/dist/runtime/html.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// HTML-string helpers used by compiled code on the server (render) and in the browser (branch HTML for @if).
|
|
2
|
-
import { attrValue } from './attrs.js';
|
|
2
|
+
import { attrValue, spreadAttrs } from './attrs.js';
|
|
3
3
|
/** The mark of h()'s output (clarusjs/views). Symbol.for: two copies of the module agree. @internal */
|
|
4
4
|
export const HTML = Symbol.for('clarusjs.html');
|
|
5
5
|
const ENTITIES = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
|
|
@@ -16,6 +16,24 @@ export function escape(value) {
|
|
|
16
16
|
export function json(value) {
|
|
17
17
|
return (JSON.stringify(value) ?? 'null').replace(/[<>&\u2028\u2029]/g, (ch) => `\\u${ch.charCodeAt(0).toString(16).padStart(4, '0')}`);
|
|
18
18
|
}
|
|
19
|
+
const warned = new Set();
|
|
20
|
+
/**
|
|
21
|
+
* A spread's attributes as HTML (#405); `where` (file:line:col, server code only) names the place in a warning for
|
|
22
|
+
* each key it drops, once.
|
|
23
|
+
* @internal
|
|
24
|
+
*/
|
|
25
|
+
export function spreadHtml(obj, skip, tag, where) {
|
|
26
|
+
const { set, dropped } = spreadAttrs(obj, skip, tag);
|
|
27
|
+
// Once per place and reason, not per key: keys come from data, so a set of them could grow without end on a server.
|
|
28
|
+
if (where)
|
|
29
|
+
for (const [key, why] of dropped) {
|
|
30
|
+
if (warned.has(`${where} ${why}`))
|
|
31
|
+
continue;
|
|
32
|
+
warned.add(`${where} ${why}`);
|
|
33
|
+
console.warn(`[clarusjs] ${where}: [...] dropped ${JSON.stringify(key)} (${why})`);
|
|
34
|
+
}
|
|
35
|
+
return set.map(([name, v]) => (v === null ? '' : v === true ? ` ${name}` : ` ${name}="${escape(v)}"`)).join('');
|
|
36
|
+
}
|
|
19
37
|
/** ` name="value"`, ` name` (true) or '' (false/null/undefined), escaped and URL-checked. @internal */
|
|
20
38
|
export function attrHtml(name, value, tag) {
|
|
21
39
|
const v = attrValue(name, value, tag);
|
package/dist/runtime/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { state, derived, shared, effect, scope, onDispose, untrack, batch, type Signal, type ReadSignal } from './signals.ts';
|
|
2
|
-
export { holes, blocks, text, on, attr, block, forBlock, region, model, modelInput, type Hole, type Block, type Ctx } from './dom.ts';
|
|
3
|
-
export { escape, attrHtml, blockHtml, forHtml, regionHtml } from './html.ts';
|
|
2
|
+
export { holes, blocks, text, on, attr, spread, block, awaited, forBlock, region, model, modelInput, type Hole, type Block, type Ctx } from './dom.ts';
|
|
3
|
+
export { escape, attrHtml, spreadHtml, blockHtml, forHtml, regionHtml } from './html.ts';
|
|
4
4
|
export { parse as parseIslandData } from './serialize.ts';
|
|
5
|
+
export { classValue, styleValue } from './attrs.ts';
|
|
5
6
|
export { browser } from './env.ts';
|
package/dist/runtime/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Browser runtime used by compiled attach() functions.
|
|
2
2
|
export { state, derived, shared, effect, scope, onDispose, untrack, batch } from './signals.js';
|
|
3
|
-
export { holes, blocks, text, on, attr, block, forBlock, region, model, modelInput } from './dom.js';
|
|
4
|
-
export { escape, attrHtml, blockHtml, forHtml, regionHtml } from './html.js';
|
|
3
|
+
export { holes, blocks, text, on, attr, spread, block, awaited, forBlock, region, model, modelInput } from './dom.js';
|
|
4
|
+
export { escape, attrHtml, spreadHtml, blockHtml, forHtml, regionHtml } from './html.js';
|
|
5
5
|
export { parse as parseIslandData } from './serialize.js';
|
|
6
|
+
export { classValue, styleValue } from './attrs.js';
|
|
6
7
|
export { browser } from './env.js';
|
package/dist/runtime/server.d.ts
CHANGED
|
@@ -1,11 +1,24 @@
|
|
|
1
1
|
import { attrHtml } from './html.ts';
|
|
2
|
-
export { escape, json, blockHtml, forHtml, regionHtml } from './html.ts';
|
|
2
|
+
export { escape, json, blockHtml, forHtml, regionHtml, spreadHtml } from './html.ts';
|
|
3
|
+
export { classValue, styleValue } from './attrs.ts';
|
|
3
4
|
/**
|
|
4
5
|
* Wrap a stateful component's HTML in its island boundary: name, client chunk URL, props (`p`) and, when the
|
|
5
6
|
* component has state, the state values it rendered with (`s`), so the browser starts from the same values.
|
|
6
7
|
* @internal Compiled components call this; apps don't.
|
|
7
8
|
*/
|
|
8
9
|
export declare function island(name: string, src: string, props: Record<string, unknown>, html: string, state?: Record<string, unknown>, attach?: string, paths?: Record<string, unknown>): string;
|
|
10
|
+
/**
|
|
11
|
+
* The parts of a template level that calls components or places slots (#430): joined into a string, or into a
|
|
12
|
+
* promise of one when a part is a promise (a component that awaits in its <script server>). @internal
|
|
13
|
+
*/
|
|
14
|
+
export declare function join(parts: unknown[]): string | Promise<string>;
|
|
15
|
+
/**
|
|
16
|
+
* An @await outside islands (#430): the server waits for the promise and renders @then with the value or @catch
|
|
17
|
+
* with the error; without @catch, a rejection fails the render. @internal
|
|
18
|
+
*/
|
|
19
|
+
export declare function awaitHtml(promise: unknown, then: ((v: unknown) => string | Promise<string>) | null, caught: ((e: unknown) => string | Promise<string>) | null): Promise<string>;
|
|
20
|
+
/** A child component's HTML inside an island: a promise (it awaits) is an error naming both. @internal */
|
|
21
|
+
export declare function sync(html: string | Promise<string>, child: string, island: string): string;
|
|
9
22
|
/** ` name="value"`, ` name` (true) or '' (false/null/undefined), escaped and URL-checked. @internal */
|
|
10
23
|
export declare const attr: typeof attrHtml;
|
|
11
24
|
/**
|
package/dist/runtime/server.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// Server-side helpers used by compiled render() functions. Runs at build time or per request, never in the browser.
|
|
2
2
|
import { attrHtml, escape, HTML } from './html.js';
|
|
3
3
|
import { stringify } from './serialize.js';
|
|
4
|
-
export { escape, json, blockHtml, forHtml, regionHtml } from './html.js';
|
|
4
|
+
export { escape, json, blockHtml, forHtml, regionHtml, spreadHtml } from './html.js';
|
|
5
|
+
export { classValue, styleValue } from './attrs.js';
|
|
5
6
|
/**
|
|
6
7
|
* Wrap a stateful component's HTML in its island boundary: name, client chunk URL, props (`p`) and, when the
|
|
7
8
|
* component has state, the state values it rendered with (`s`), so the browser starts from the same values.
|
|
@@ -14,6 +15,36 @@ export function island(name, src, props, html, state, attach, paths) {
|
|
|
14
15
|
const d = Object.entries(paths ?? {}).map(([model, path]) => ` data-cl-path-${escape(model)}="${escape(String(path))}"`).join('');
|
|
15
16
|
return `<cl-island c="${escape(name)}" src="${escape(src)}" p="${escape(toBrowser(props))}"${s}${a}${d}>${html}</cl-island>`;
|
|
16
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* The parts of a template level that calls components or places slots (#430): joined into a string, or into a
|
|
20
|
+
* promise of one when a part is a promise (a component that awaits in its <script server>). @internal
|
|
21
|
+
*/
|
|
22
|
+
export function join(parts) {
|
|
23
|
+
return parts.some((p) => p instanceof Promise) ? Promise.all(parts).then((all) => all.join('')) : parts.join('');
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* An @await outside islands (#430): the server waits for the promise and renders @then with the value or @catch
|
|
27
|
+
* with the error; without @catch, a rejection fails the render. @internal
|
|
28
|
+
*/
|
|
29
|
+
export async function awaitHtml(promise, then, caught) {
|
|
30
|
+
let value;
|
|
31
|
+
try {
|
|
32
|
+
value = await promise;
|
|
33
|
+
}
|
|
34
|
+
catch (err) {
|
|
35
|
+
if (!caught)
|
|
36
|
+
throw err;
|
|
37
|
+
return caught(err);
|
|
38
|
+
}
|
|
39
|
+
return then ? then(value) : '';
|
|
40
|
+
}
|
|
41
|
+
/** A child component's HTML inside an island: a promise (it awaits) is an error naming both. @internal */
|
|
42
|
+
export function sync(html, child, island) {
|
|
43
|
+
if (typeof html === 'string')
|
|
44
|
+
return html;
|
|
45
|
+
html.catch(() => { }); // its own rejection isn't the error to report
|
|
46
|
+
throw new Error(`[clarusjs] <${child}> awaits in its <script server>, so it can't be inside the island <${island}>: an island re-renders its children in the browser, where it can't wait. Render <${child}> outside the island, or load its data in a parent and pass it in as props`);
|
|
47
|
+
}
|
|
17
48
|
/** ` name="value"`, ` name` (true) or '' (false/null/undefined), escaped and URL-checked. @internal */
|
|
18
49
|
export const attr = attrHtml;
|
|
19
50
|
/**
|
|
@@ -36,7 +67,10 @@ export function toBrowser(value, path = '', seen = new Set()) {
|
|
|
36
67
|
* compiled components that have a loader. @internal
|
|
37
68
|
*/
|
|
38
69
|
export function loaded(key) {
|
|
39
|
-
|
|
70
|
+
// The server handler keeps each render's data in an AsyncLocalStorage (#430: a render may pause at an await);
|
|
71
|
+
// this module stays free of Node APIs (it also runs in the docs playground), so it only reads it.
|
|
72
|
+
const scope = globalThis;
|
|
73
|
+
return (scope.__clLoadedStore?.getStore() ?? scope.__clLoaded)?.get(key) ?? {};
|
|
40
74
|
}
|
|
41
75
|
/**
|
|
42
76
|
* Render with component loaders' results available to `loaded()`. Rendering is synchronous, so one render's data never
|
package/dist/server/handler.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type ModeTable } from './modes.ts';
|
|
2
2
|
import { type HeadOptions, type SiteOptions } from './head.ts';
|
|
3
3
|
type PageModule = {
|
|
4
|
-
render(props?: object): string
|
|
4
|
+
render(props?: object): string | Promise<string>;
|
|
5
5
|
};
|
|
6
6
|
type Params = Record<string, string>;
|
|
7
7
|
/**
|
package/dist/server/handler.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { modeFor } from './modes.js';
|
|
2
|
-
import {
|
|
2
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
|
+
import { toBrowser } from '../runtime/server.js';
|
|
3
4
|
import { completeHead } from './head.js';
|
|
4
5
|
import { escape } from '../runtime/html.js';
|
|
5
6
|
const DATA = '__data.json';
|
|
6
7
|
const FRAG = 'index.frag.html';
|
|
7
8
|
const safeJson = (json) => json.replace(/</g, '\\u003c');
|
|
9
|
+
const loadedStore = new AsyncLocalStorage();
|
|
10
|
+
globalThis.__clLoadedStore = loadedStore;
|
|
8
11
|
const PARAM = /^\[([A-Za-z_$][\w$]*)\]$/;
|
|
9
12
|
function compile(path) {
|
|
10
13
|
const keys = [];
|
|
@@ -296,7 +299,7 @@ export function createHandler(pages, options = {}) {
|
|
|
296
299
|
let html;
|
|
297
300
|
try {
|
|
298
301
|
const module = await found.route.load();
|
|
299
|
-
html =
|
|
302
|
+
html = await loadedStore.run(componentData, () => module.render(props));
|
|
300
303
|
}
|
|
301
304
|
catch (err) {
|
|
302
305
|
return fail(`${found.route.file}: render failed: ${err?.message ?? err}`, err);
|
package/dist/server/views.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { HTML } from '../runtime/html.ts';
|
|
2
2
|
type View = {
|
|
3
|
-
render(props?: object, slots?: Record<string, string>): string
|
|
3
|
+
render(props?: object, slots?: Record<string, string>): string | Promise<string>;
|
|
4
4
|
};
|
|
5
5
|
type Registry = Record<string, () => Promise<View>>;
|
|
6
6
|
/**
|
package/dist/server/views.js
CHANGED
|
@@ -38,7 +38,7 @@ export async function h(name, props = {}, children = []) {
|
|
|
38
38
|
const filled = await Promise.all(entries.map(([, content]) => childrenHtml(content)));
|
|
39
39
|
entries.forEach(([slot], k) => { if (filled[k])
|
|
40
40
|
slots[slot] = filled[k]; });
|
|
41
|
-
return html((await load()).render(props, Object.keys(slots).length ? slots : undefined));
|
|
41
|
+
return html(await (await load()).render(props, Object.keys(slots).length ? slots : undefined));
|
|
42
42
|
}
|
|
43
43
|
const tag = name.toLowerCase();
|
|
44
44
|
if (!/^[a-z][a-z0-9-]*$/.test(tag))
|
package/dist/vite/node.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
2
|
export declare function toRequest(req: IncomingMessage): Request;
|
|
3
3
|
export declare function sendResponse(res: ServerResponse, response: Response): Promise<void>;
|
|
4
|
+
/** Whether an Accept-Encoding header allows `encoding`: named, or through `*`, and not with q=0. */
|
|
5
|
+
export declare function acceptsEncoding(header: string, encoding: string): boolean;
|
package/dist/vite/node.js
CHANGED
|
@@ -16,3 +16,16 @@ export async function sendResponse(res, response) {
|
|
|
16
16
|
response.headers.forEach((value, key) => res.setHeader(key, value));
|
|
17
17
|
res.end(Buffer.from(await response.arrayBuffer()));
|
|
18
18
|
}
|
|
19
|
+
export function acceptsEncoding(header, encoding) {
|
|
20
|
+
let star = false;
|
|
21
|
+
for (const part of header.toLowerCase().split(',')) {
|
|
22
|
+
const [name, ...params] = part.trim().split(';');
|
|
23
|
+
const q = params.map((p) => p.trim()).find((p) => p.startsWith('q='));
|
|
24
|
+
const allowed = !q || Number(q.slice(2)) > 0;
|
|
25
|
+
if (name === encoding)
|
|
26
|
+
return allowed;
|
|
27
|
+
if (name === '*')
|
|
28
|
+
star = allowed;
|
|
29
|
+
}
|
|
30
|
+
return star;
|
|
31
|
+
}
|
package/dist/vite/plugin.d.ts
CHANGED
|
@@ -66,10 +66,15 @@ export type ClarusOptions = {
|
|
|
66
66
|
/**
|
|
67
67
|
* With site.url, vite build writes sitemap.xml (static pages, dated from git). false: no sitemap. `exclude`: paths
|
|
68
68
|
* left out ('/legal', '/tags/*' one segment, '/drafts/**' any depth). A page opts out itself with
|
|
69
|
-
* <cl-head><meta name="sitemap" content="false"></cl-head> (removed from the built page) or noindex
|
|
69
|
+
* <cl-head><meta name="sitemap" content="false"></cl-head> (removed from the built page) or noindex, and sets its
|
|
70
|
+
* own date with <meta name="sitemap:lastmod" content="2026-11-01">. Each page's images (not alt="", not data:) are
|
|
71
|
+
* listed with Google's image extension; `images: false` leaves them out. `include`: paths the build can't list (server
|
|
72
|
+
* and client routes), or a function returning them (async, to read them from a database at build time).
|
|
70
73
|
*/
|
|
71
74
|
sitemap?: boolean | {
|
|
72
75
|
exclude?: string[];
|
|
76
|
+
images?: boolean;
|
|
77
|
+
include?: string[] | (() => string[] | Promise<string[]>);
|
|
73
78
|
};
|
|
74
79
|
/** With site.url, vite build writes robots.txt (allow all, and the sitemap), unless public/robots.txt exists. */
|
|
75
80
|
robots?: boolean;
|