@tschk/moonshine-astro 0.4.4 → 0.4.80
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 +176 -7
- package/package.json +18 -5
- package/src/actions.ts +556 -0
- package/src/assets.ts +710 -0
- package/src/config.ts +269 -0
- package/src/container.ts +560 -0
- package/src/content.ts +1523 -0
- package/src/context.ts +756 -0
- package/src/env.ts +361 -0
- package/src/i18n.ts +420 -0
- package/src/index.ts +264 -0
- package/src/loaders.ts +888 -0
- package/src/middleware.ts +206 -0
- package/src/routing.ts +239 -0
- package/src/ssr.ts +439 -0
- package/src/transitions.ts +496 -0
package/src/ssr.ts
ADDED
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
/** Independent synchronous Astro source factories; no host compiler ABI. */
|
|
2
|
+
export * from "@tschk/moonshine-dom/ssr";
|
|
3
|
+
import {
|
|
4
|
+
h as nativeH,
|
|
5
|
+
hAttributes,
|
|
6
|
+
hAttributeEntries,
|
|
7
|
+
defer,
|
|
8
|
+
renderToString,
|
|
9
|
+
scopeSSRChild,
|
|
10
|
+
type HTML,
|
|
11
|
+
type SSRChild,
|
|
12
|
+
} from "@tschk/moonshine-dom/ssr";
|
|
13
|
+
import { peekAstroContext, type AstroServerIslandEntry } from "./context";
|
|
14
|
+
|
|
15
|
+
const booleanAttributes = /* @__PURE__ */ new Set(
|
|
16
|
+
/* @__PURE__ */ "allowfullscreen async autofocus autoplay checked controls default defer disabled disablepictureinpicture disableremoteplayback formnovalidate inert loop muted nomodule novalidate open playsinline readonly required reversed scoped seamless selected itemscope".split(
|
|
17
|
+
" ",
|
|
18
|
+
),
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
/** Compiler-facing class:list normalization, independent of clsx. */
|
|
22
|
+
export function classList(value: unknown): string {
|
|
23
|
+
if (!value) return "";
|
|
24
|
+
if (typeof value === "string" || typeof value === "number")
|
|
25
|
+
return String(value);
|
|
26
|
+
if (Array.isArray(value))
|
|
27
|
+
return value.map(classList).filter(Boolean).join(" ");
|
|
28
|
+
if (typeof value === "object") {
|
|
29
|
+
const classes: string[] = [];
|
|
30
|
+
for (const key in value) if (Reflect.get(value, key)) classes.push(key);
|
|
31
|
+
return classes.join(" ");
|
|
32
|
+
}
|
|
33
|
+
return "";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Compiler-facing interpolation normalization for synchronous Astro factories. */
|
|
37
|
+
export function sourceChild(value: unknown): SSRChild {
|
|
38
|
+
if (!value && value !== 0) return undefined;
|
|
39
|
+
if (value === true) return "true";
|
|
40
|
+
if (Array.isArray(value)) return value.map(sourceChild);
|
|
41
|
+
if (typeof value === "function") return () => sourceChild(value());
|
|
42
|
+
if (
|
|
43
|
+
typeof value === "object" &&
|
|
44
|
+
value !== null &&
|
|
45
|
+
"then" in value &&
|
|
46
|
+
typeof value.then === "function"
|
|
47
|
+
) {
|
|
48
|
+
void Promise.resolve(value).catch(() => {});
|
|
49
|
+
throw new TypeError(
|
|
50
|
+
"Async Astro children are unsupported; use synchronous server data",
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
return value as SSRChild;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function style(value: object): string {
|
|
57
|
+
return Object.entries(value)
|
|
58
|
+
.filter(
|
|
59
|
+
([, item]) =>
|
|
60
|
+
typeof item === "number" || (typeof item === "string" && item.trim()),
|
|
61
|
+
)
|
|
62
|
+
.map(
|
|
63
|
+
([key, item]) =>
|
|
64
|
+
`${key[0] === "-" || key[1] === "-" ? key : key.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)}:${item}`,
|
|
65
|
+
)
|
|
66
|
+
.join(";");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Compiler-facing `define:vars` CSS custom property serialization. */
|
|
70
|
+
export function defineStyleVars(
|
|
71
|
+
vars: Record<string, unknown> | null | undefined,
|
|
72
|
+
): string {
|
|
73
|
+
if (!vars || typeof vars !== "object") return "";
|
|
74
|
+
const parts: string[] = [];
|
|
75
|
+
for (const [key, item] of Object.entries(vars)) {
|
|
76
|
+
if (item === undefined || item === null || typeof item === "boolean") {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
const text = String(item).trim();
|
|
80
|
+
if (!text) continue;
|
|
81
|
+
const propName = key.startsWith("--") ? key : `--${key}`;
|
|
82
|
+
parts.push(`${propName}:${text}`);
|
|
83
|
+
}
|
|
84
|
+
return parts.join(";");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Merge an element's `style`, `define:vars`, and `transition:name` into a single `style` value. */
|
|
88
|
+
export function mergeDefineVars(
|
|
89
|
+
existingStyle: unknown,
|
|
90
|
+
vars?: Record<string, unknown> | null,
|
|
91
|
+
transitionName?: unknown,
|
|
92
|
+
): string | undefined {
|
|
93
|
+
const segments: string[] = [];
|
|
94
|
+
if (typeof existingStyle === "string") {
|
|
95
|
+
const trimmed = existingStyle.trim().replace(/;+$/, "");
|
|
96
|
+
if (trimmed) segments.push(trimmed);
|
|
97
|
+
} else if (existingStyle !== null && typeof existingStyle === "object") {
|
|
98
|
+
const serialized = Array.isArray(existingStyle)
|
|
99
|
+
? `${style(existingStyle[0] ?? {})};${existingStyle[1] ?? ""}`.replace(
|
|
100
|
+
/^;+|;+$/g,
|
|
101
|
+
"",
|
|
102
|
+
)
|
|
103
|
+
: style(existingStyle);
|
|
104
|
+
if (serialized) segments.push(serialized);
|
|
105
|
+
}
|
|
106
|
+
const varsStr = defineStyleVars(vars);
|
|
107
|
+
if (varsStr) segments.push(varsStr);
|
|
108
|
+
if (
|
|
109
|
+
transitionName !== undefined &&
|
|
110
|
+
transitionName !== null &&
|
|
111
|
+
transitionName !== false
|
|
112
|
+
) {
|
|
113
|
+
const nameStr = String(transitionName).trim();
|
|
114
|
+
if (nameStr) {
|
|
115
|
+
segments.push(`view-transition-name:${nameStr}`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return segments.length > 0 ? segments.join(";") : undefined;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Compiler-facing `transition:animate` attribute value normalization. */
|
|
122
|
+
export function transitionAnimateValue(value: unknown): string | undefined {
|
|
123
|
+
if (value === undefined || value === null || value === false) {
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
if (value === true || value === "") {
|
|
127
|
+
return "initial";
|
|
128
|
+
}
|
|
129
|
+
if (typeof value === "string") {
|
|
130
|
+
return value.trim() || "initial";
|
|
131
|
+
}
|
|
132
|
+
if (typeof value === "object") {
|
|
133
|
+
const candidate = value as Record<string, unknown>;
|
|
134
|
+
if (typeof candidate.name === "string" && candidate.name.trim()) {
|
|
135
|
+
return candidate.name.trim();
|
|
136
|
+
}
|
|
137
|
+
const forwards = candidate.forwards as Record<string, unknown> | undefined;
|
|
138
|
+
if (forwards && typeof forwards === "object") {
|
|
139
|
+
const next = forwards.new;
|
|
140
|
+
if (Array.isArray(next)) {
|
|
141
|
+
if (
|
|
142
|
+
next.some((item) =>
|
|
143
|
+
(item as { name?: string })?.name?.includes("Slide"),
|
|
144
|
+
)
|
|
145
|
+
) {
|
|
146
|
+
return "slide";
|
|
147
|
+
}
|
|
148
|
+
} else if (next && typeof next === "object") {
|
|
149
|
+
const animName = (next as { name?: string }).name;
|
|
150
|
+
if (animName === "astroFadeIn") return "fade";
|
|
151
|
+
if (animName === "none") return "none";
|
|
152
|
+
if (animName) return animName;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return "custom";
|
|
156
|
+
}
|
|
157
|
+
return String(value);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Compiler-facing `transition:persist` attribute value normalization. */
|
|
161
|
+
export function transitionPersistValue(value: unknown): string | undefined {
|
|
162
|
+
if (value === undefined || value === null || value === false) {
|
|
163
|
+
return undefined;
|
|
164
|
+
}
|
|
165
|
+
if (value === true || value === "") {
|
|
166
|
+
return "";
|
|
167
|
+
}
|
|
168
|
+
return String(value);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Compiler-facing `transition:persist-props` boolean attribute normalization. */
|
|
172
|
+
export function transitionPersistPropsValue(
|
|
173
|
+
value: unknown,
|
|
174
|
+
): string | undefined {
|
|
175
|
+
if (value === undefined || value === null || value === false) {
|
|
176
|
+
return undefined;
|
|
177
|
+
}
|
|
178
|
+
return "";
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function attribute(
|
|
182
|
+
tag: string,
|
|
183
|
+
name: string,
|
|
184
|
+
value: unknown,
|
|
185
|
+
dynamic: boolean,
|
|
186
|
+
): string | undefined {
|
|
187
|
+
if (value == null) return undefined;
|
|
188
|
+
if (!dynamic && typeof value === "string") return value;
|
|
189
|
+
const lower = name.toLowerCase();
|
|
190
|
+
if (
|
|
191
|
+
booleanAttributes.has(lower) ||
|
|
192
|
+
(["download", "hidden", "popover"].includes(lower) &&
|
|
193
|
+
typeof value === "boolean")
|
|
194
|
+
) {
|
|
195
|
+
if (tag.includes("-") && lower !== "popover") return String(value);
|
|
196
|
+
return value ? "" : undefined;
|
|
197
|
+
}
|
|
198
|
+
if (name === "style" && typeof value === "object") {
|
|
199
|
+
if (Array.isArray(value) && value.length === 2)
|
|
200
|
+
return `${style(value[0] ?? {})};${value[1]}`;
|
|
201
|
+
return style(value);
|
|
202
|
+
}
|
|
203
|
+
return String(value);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export type SourceSlots = Readonly<Record<string, SSRChild>>;
|
|
207
|
+
|
|
208
|
+
function captureProps<P extends object>(props: P): P {
|
|
209
|
+
return { ...props };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** Compiler-facing component invocation; slots remain outside ordinary props. */
|
|
213
|
+
export function sourceComponent<P extends object>(
|
|
214
|
+
component: (props: P, slots: SourceSlots) => SSRChild,
|
|
215
|
+
props: P,
|
|
216
|
+
slots: Readonly<Record<string, () => SSRChild>> = {},
|
|
217
|
+
): SSRChild {
|
|
218
|
+
const captured = captureProps(props);
|
|
219
|
+
const values = captured as Record<string, unknown>;
|
|
220
|
+
if (values["class:list"] !== undefined) {
|
|
221
|
+
const list = values["class:list"];
|
|
222
|
+
delete values["class:list"];
|
|
223
|
+
const normalized = classList([values.class, list]);
|
|
224
|
+
if (normalized) values.class = normalized;
|
|
225
|
+
else delete values.class;
|
|
226
|
+
}
|
|
227
|
+
const supplied: SourceSlots = Object.fromEntries(
|
|
228
|
+
Object.entries(slots).map(([name, render]) => [
|
|
229
|
+
name,
|
|
230
|
+
scopeSSRChild(() => sourceChild(render())),
|
|
231
|
+
]),
|
|
232
|
+
);
|
|
233
|
+
let result: { value: SSRChild } | { error: unknown } | undefined;
|
|
234
|
+
return scopeSSRChild(
|
|
235
|
+
defer(() => {
|
|
236
|
+
if (!result) {
|
|
237
|
+
try {
|
|
238
|
+
result = {
|
|
239
|
+
value: nativeH(
|
|
240
|
+
(actual: P) => sourceChild(component(actual, supplied)),
|
|
241
|
+
captured,
|
|
242
|
+
),
|
|
243
|
+
};
|
|
244
|
+
} catch (error) {
|
|
245
|
+
result = { error };
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if ("error" in result) throw result.error;
|
|
249
|
+
return result.value;
|
|
250
|
+
}),
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Compiler-facing Astro 5 `<Component server:defer>` Server Island invocation. */
|
|
255
|
+
export function sourceServerIsland<P extends object>(
|
|
256
|
+
component: (props: P, slots: SourceSlots) => SSRChild,
|
|
257
|
+
componentName: string,
|
|
258
|
+
props: P,
|
|
259
|
+
slots: Readonly<Record<string, () => SSRChild>> = {},
|
|
260
|
+
): SSRChild {
|
|
261
|
+
const captured = captureProps(props);
|
|
262
|
+
const componentSlots: Record<string, () => SSRChild> = {};
|
|
263
|
+
for (const [name, render] of Object.entries(slots)) {
|
|
264
|
+
if (name !== "fallback") {
|
|
265
|
+
componentSlots[name] = render;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
const hasFallback = Object.hasOwn(slots, "fallback");
|
|
269
|
+
const ctx = peekAstroContext();
|
|
270
|
+
const deferred = Boolean(ctx?.deferServerIslands);
|
|
271
|
+
const id = `island-${(ctx?.serverIslands.length ?? 0) + 1}`;
|
|
272
|
+
const renderIsland = () =>
|
|
273
|
+
renderToString(() => sourceComponent(component, captured, componentSlots));
|
|
274
|
+
|
|
275
|
+
if (ctx) {
|
|
276
|
+
const entry: AstroServerIslandEntry = {
|
|
277
|
+
id,
|
|
278
|
+
componentName,
|
|
279
|
+
props: { ...(captured as Record<string, unknown>) },
|
|
280
|
+
hasFallback,
|
|
281
|
+
deferred,
|
|
282
|
+
render: renderIsland,
|
|
283
|
+
};
|
|
284
|
+
ctx.serverIslands.push(entry);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (deferred) {
|
|
288
|
+
const fallbackContent = hasFallback
|
|
289
|
+
? scopeSSRChild(() => sourceChild(slots.fallback!()))
|
|
290
|
+
: undefined;
|
|
291
|
+
return h(
|
|
292
|
+
"astro-server-island",
|
|
293
|
+
{
|
|
294
|
+
"data-island-id": id,
|
|
295
|
+
"data-component": componentName,
|
|
296
|
+
},
|
|
297
|
+
fallbackContent,
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return sourceComponent(component, captured, componentSlots);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** Presence selects a slot, including an explicitly empty supplied slot. */
|
|
305
|
+
export function sourceSlot(
|
|
306
|
+
slots: SourceSlots,
|
|
307
|
+
name = "default",
|
|
308
|
+
fallback?: SSRChild,
|
|
309
|
+
): SSRChild {
|
|
310
|
+
return sourceChild(Object.hasOwn(slots, name) ? slots[name] : fallback);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** Check native slot presence without evaluating its supplied content. */
|
|
314
|
+
export function sourceHasSlot(slots: SourceSlots, name: string): boolean {
|
|
315
|
+
return Object.hasOwn(slots, name);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** Render a named slot synchronously to an HTML string (`Astro.slots.render(name, args?)`). */
|
|
319
|
+
export function sourceRenderSlot(
|
|
320
|
+
slots: SourceSlots,
|
|
321
|
+
name = "default",
|
|
322
|
+
args?: readonly unknown[],
|
|
323
|
+
): string {
|
|
324
|
+
if (Object.hasOwn(slots, name)) {
|
|
325
|
+
const slotEntry = slots[name];
|
|
326
|
+
if (
|
|
327
|
+
typeof slotEntry === "function" &&
|
|
328
|
+
Array.isArray(args) &&
|
|
329
|
+
args.length > 0
|
|
330
|
+
) {
|
|
331
|
+
return renderToString(() =>
|
|
332
|
+
sourceChild((slotEntry as (...a: unknown[]) => unknown)(...args)),
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
return renderToString(() => sourceChild(slotEntry));
|
|
336
|
+
}
|
|
337
|
+
const ctx = peekAstroContext();
|
|
338
|
+
if (ctx?.slots.has(name)) {
|
|
339
|
+
const rendered = ctx.slots.render(name, args ? [...args] : []);
|
|
340
|
+
if (typeof rendered === "string") {
|
|
341
|
+
return rendered;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return "";
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
export function h<P extends object>(
|
|
348
|
+
component: (props: P & { children?: SSRChild }) => SSRChild,
|
|
349
|
+
props: P | null,
|
|
350
|
+
...children: SSRChild[]
|
|
351
|
+
): SSRChild;
|
|
352
|
+
export function h(
|
|
353
|
+
tag: string,
|
|
354
|
+
props?: Record<string, unknown> | null,
|
|
355
|
+
...children: SSRChild[]
|
|
356
|
+
): HTML;
|
|
357
|
+
export function h(
|
|
358
|
+
tag: string | ((props: any) => SSRChild),
|
|
359
|
+
props: Record<string, unknown> | null = null,
|
|
360
|
+
...children: SSRChild[]
|
|
361
|
+
): SSRChild {
|
|
362
|
+
if (typeof tag === "function") {
|
|
363
|
+
const captured = props == null ? null : captureProps(props);
|
|
364
|
+
return nativeH(tag, captured, ...children.map(sourceChild));
|
|
365
|
+
}
|
|
366
|
+
const attributes = Object.fromEntries(
|
|
367
|
+
Object.keys(props ?? {}).map((name) => [
|
|
368
|
+
name === "className" ? "class" : name,
|
|
369
|
+
() => {
|
|
370
|
+
const supplied = props![name];
|
|
371
|
+
const dynamic = typeof supplied === "function";
|
|
372
|
+
return attribute(tag, name, dynamic ? supplied() : supplied, dynamic);
|
|
373
|
+
},
|
|
374
|
+
]),
|
|
375
|
+
);
|
|
376
|
+
return hAttributes(tag, attributes, ...children.map(sourceChild));
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** Encoded compiler-facing attribute; repeated names remain separate entries. */
|
|
380
|
+
export type SourceAttribute = readonly [
|
|
381
|
+
name: string,
|
|
382
|
+
value: string | undefined,
|
|
383
|
+
];
|
|
384
|
+
|
|
385
|
+
/** Coerce at the authored position, before subsequent expressions or getters. */
|
|
386
|
+
export function sourceAttribute(
|
|
387
|
+
tag: string,
|
|
388
|
+
name: string,
|
|
389
|
+
supplied: unknown,
|
|
390
|
+
dynamic = false,
|
|
391
|
+
): SourceAttribute {
|
|
392
|
+
if (!/^[a-zA-Z_:][a-zA-Z0-9:_.-]*$/.test(name)) {
|
|
393
|
+
throw new TypeError("Invalid server attribute name");
|
|
394
|
+
}
|
|
395
|
+
if (name.includes(":") && name !== "class:list") {
|
|
396
|
+
throw new TypeError(
|
|
397
|
+
`Unsupported Astro spread directive or namespace: ${name}`,
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
const outputName =
|
|
401
|
+
name === "class:list" || name === "className" ? "class" : name;
|
|
402
|
+
const value = /^on/i.test(name)
|
|
403
|
+
? undefined
|
|
404
|
+
: name === "class:list"
|
|
405
|
+
? classList(supplied) || undefined
|
|
406
|
+
: attribute(tag, name, supplied, dynamic);
|
|
407
|
+
return [outputName, value];
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** Read and coerce own string keys in order, without invoking function data. */
|
|
411
|
+
export function sourceSpread(tag: string, value: unknown): SourceAttribute[] {
|
|
412
|
+
if (value == null) return [];
|
|
413
|
+
const object = Object(value) as Record<string, unknown>;
|
|
414
|
+
return Object.keys(object).map((name) =>
|
|
415
|
+
sourceAttribute(tag, name, object[name], true),
|
|
416
|
+
);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** Compiler-facing intrinsic with ordered, already captured source attributes. */
|
|
420
|
+
export function sourceElement(
|
|
421
|
+
tag: string,
|
|
422
|
+
entries: ReadonlyArray<SourceAttribute>,
|
|
423
|
+
...children: SSRChild[]
|
|
424
|
+
): HTML {
|
|
425
|
+
return hAttributeEntries(tag, entries, ...children.map(sourceChild));
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
export {
|
|
429
|
+
useAstro,
|
|
430
|
+
peekAstroContext,
|
|
431
|
+
withAstroContext,
|
|
432
|
+
createAstroGlobal,
|
|
433
|
+
AstroCookies,
|
|
434
|
+
AstroCookie,
|
|
435
|
+
type AstroGlobal,
|
|
436
|
+
type AstroContextInit,
|
|
437
|
+
type AstroSlots,
|
|
438
|
+
type AstroServerIslandEntry,
|
|
439
|
+
} from "./context";
|