@opsydyn/astro-effect-ui 1.0.0-next.0 → 1.0.0-next.2
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 +537 -0
- package/client.d.mts +9 -2
- package/client.mjs +146 -35
- package/component.d.mts +21 -3
- package/component.mjs +11 -3
- package/directive-interaction.d.mts +17 -2
- package/directive-interaction.mjs +118 -12
- package/index.d.mts +10 -1
- package/index.mjs +77 -12
- package/package.json +19 -5
- package/runtime.d.mts +49 -0
- package/runtime.mjs +19 -0
- package/server.d.mts +12 -1
- package/server.mjs +37 -14
- package/slots-ELUCsLv9.mjs +105 -0
package/runtime.mjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { toAstro } from "./component.mjs";
|
|
2
|
+
//#region src/runtime.ts
|
|
3
|
+
/**
|
|
4
|
+
* Names the services an app's islands may use, once. Both runtimes must then provide
|
|
5
|
+
* them, and `astroComponent` accepts a component only if they cover what it needs.
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* export const islands = defineIslands<DeleteTodo | Flags>();
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
function defineIslands() {
|
|
12
|
+
return {
|
|
13
|
+
astroComponent: (component) => toAstro(component),
|
|
14
|
+
serverRuntime: (runtime) => runtime,
|
|
15
|
+
clientRuntime: (runtime) => runtime
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
export { defineIslands };
|
package/server.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { IslandProps } from "./component.mjs";
|
|
2
|
+
import { AnyServerRuntime } from "./runtime.mjs";
|
|
2
3
|
import { Component } from "@effect/ui";
|
|
3
4
|
import { AstroComponentMetadata } from "astro";
|
|
4
5
|
|
|
@@ -10,12 +11,22 @@ type RenderContext = Readonly<{
|
|
|
10
11
|
}>;
|
|
11
12
|
}>;
|
|
12
13
|
}>;
|
|
14
|
+
/** The server renderer, with every render run in the app's runtime when it has one. */
|
|
15
|
+
declare function makeServerRenderer(runtime?: AnyServerRuntime): {
|
|
16
|
+
name: string;
|
|
17
|
+
supportsAstroStaticSlot: true;
|
|
18
|
+
check: (component: any) => Promise<boolean>;
|
|
19
|
+
renderToStaticMarkup(this: RenderContext | void, component: Component.Any, props: IslandProps, slots: Record<string, string>, metadata?: AstroComponentMetadata): Promise<{
|
|
20
|
+
html: string;
|
|
21
|
+
}>;
|
|
22
|
+
};
|
|
13
23
|
declare const _default: {
|
|
14
24
|
name: string;
|
|
25
|
+
supportsAstroStaticSlot: true;
|
|
15
26
|
check: (component: any) => Promise<boolean>;
|
|
16
27
|
renderToStaticMarkup(this: RenderContext | void, component: Component.Any, props: IslandProps, slots: Record<string, string>, metadata?: AstroComponentMetadata): Promise<{
|
|
17
28
|
html: string;
|
|
18
29
|
}>;
|
|
19
30
|
};
|
|
20
31
|
//#endregion
|
|
21
|
-
export { _default as default };
|
|
32
|
+
export { _default as default, makeServerRenderer };
|
package/server.mjs
CHANGED
|
@@ -1,19 +1,42 @@
|
|
|
1
1
|
import { IslandRendererError, asIslandComponent } from "./component.mjs";
|
|
2
|
+
import { n as fillMarkup, o as slotsOf, s as withSlots, t as deferredUnderSlots } from "./slots-ELUCsLv9.mjs";
|
|
2
3
|
import { Component } from "@effect/ui";
|
|
3
|
-
import { Effect } from "effect";
|
|
4
|
+
import { Effect, ManagedRuntime } from "effect";
|
|
4
5
|
import { HtmlServer } from "@effect/ui/html";
|
|
5
6
|
//#region src/server.ts
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
7
|
+
/** The server renderer, with every render run in the app's runtime when it has one. */
|
|
8
|
+
function makeServerRenderer(runtime = {}) {
|
|
9
|
+
const run = runtime.layer === void 0 ? Effect.runPromise : ManagedRuntime.make(runtime.layer).runPromise;
|
|
10
|
+
return {
|
|
11
|
+
name: "astro-effect-ui",
|
|
12
|
+
supportsAstroStaticSlot: true,
|
|
13
|
+
check: async (component) => Component.isComponent(component),
|
|
14
|
+
async renderToStaticMarkup(component, props, slots, metadata) {
|
|
15
|
+
const definition = asIslandComponent(component);
|
|
16
|
+
const name = component.definition.name;
|
|
17
|
+
const isPrerendered = this?.result?.createAstro?.({}, null)?.isPrerendered ?? false;
|
|
18
|
+
const hydrate = Boolean(metadata?.hydrate);
|
|
19
|
+
const filled = slotsOf(component, slots);
|
|
20
|
+
const rendered = await run((filled === void 0 ? HtmlServer.renderToHtml(definition, props, {
|
|
21
|
+
static: isPrerendered,
|
|
22
|
+
budget: runtime.budget
|
|
23
|
+
}) : HtmlServer.renderToHtml(withSlots(component, props, filled), {}, {
|
|
24
|
+
static: isPrerendered,
|
|
25
|
+
budget: runtime.budget
|
|
26
|
+
})).pipe(Effect.withSpan("astro-effect-ui.render", { attributes: { component: name } })));
|
|
27
|
+
const html = filled === void 0 ? rendered.html : fillMarkup(rendered.html, filled, hydrate);
|
|
28
|
+
if (!hydrate) {
|
|
29
|
+
if (filled === void 0 ? rendered.model === void 0 : deferredUnderSlots(rendered.state["~deferred"])) {
|
|
30
|
+
const message = `${metadata?.displayName ?? name} rendered nothing on the server and has no client directive.`;
|
|
31
|
+
if (isPrerendered) throw new IslandRendererError({ message });
|
|
32
|
+
await run(Effect.logError(`[astro-effect-ui] ${message}`));
|
|
33
|
+
}
|
|
34
|
+
return { html };
|
|
35
|
+
}
|
|
36
|
+
return { html: `<div data-effect-ui-root>${html}</div><script type="application/json" data-effect-ui-state>${HtmlServer.serializeState(rendered.state)}<\/script>` };
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
var server_default = makeServerRenderer();
|
|
18
41
|
//#endregion
|
|
19
|
-
export { server_default as default };
|
|
42
|
+
export { server_default as default, makeServerRenderer };
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { IslandRendererError } from "./component.mjs";
|
|
2
|
+
import { Component, Mount } from "@effect/ui";
|
|
3
|
+
import { Effect, Schema } from "effect";
|
|
4
|
+
import { Html } from "@effect/ui/html";
|
|
5
|
+
//#region src/slots.ts
|
|
6
|
+
/** The element Astro reads an island's slots back from. The default slot has no name. */
|
|
7
|
+
const SLOT = "astro-slot";
|
|
8
|
+
const DEFAULT = "default";
|
|
9
|
+
const placeholder = /<astro-slot(?: name="([^"]*)")?><\/astro-slot>/g;
|
|
10
|
+
/**
|
|
11
|
+
* The slots to fill: those with content that the component declares. Content for a slot
|
|
12
|
+
* it does not declare is an error, except Astro's `fallback`, which is not a slot of the
|
|
13
|
+
* component.
|
|
14
|
+
*/
|
|
15
|
+
function slotsOf(component, slots) {
|
|
16
|
+
const declared = component.definition.slots;
|
|
17
|
+
const given = Object.entries(slots).filter(([name, html]) => html.trim() !== "" && (declared.includes(name) || name !== "fallback"));
|
|
18
|
+
const unknown = given.find(([name]) => !declared.includes(name));
|
|
19
|
+
if (unknown !== void 0) {
|
|
20
|
+
const names = declared.length === 0 ? "none" : declared.join(", ");
|
|
21
|
+
throw new IslandRendererError({ message: `${component.definition.name} has no slot named "${unknown[0]}". It declares: ${names}.` });
|
|
22
|
+
}
|
|
23
|
+
return given.length === 0 ? void 0 : Object.fromEntries(given);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A root that renders the component with Astro's slots in place. A root has no parent to
|
|
27
|
+
* fill its slots, so this is that parent. Each slot is an empty `astro-slot` element:
|
|
28
|
+
* Effect UI has no node for markup it did not build, so the content is put in around it,
|
|
29
|
+
* by `fillMarkup` on the server and by `fill` in the browser.
|
|
30
|
+
*/
|
|
31
|
+
function withSlots(component, props, slots, { fill, fallback } = {}) {
|
|
32
|
+
const Fill = Mount.make("FillAstroSlot", { args: { name: Schema.String } }, (element, { name }) => Effect.sync(() => {
|
|
33
|
+
fill?.(element, name);
|
|
34
|
+
return Component.noop;
|
|
35
|
+
}));
|
|
36
|
+
const declare = component;
|
|
37
|
+
return Html.component({
|
|
38
|
+
name: "AstroIsland",
|
|
39
|
+
model: Schema.Null,
|
|
40
|
+
message: Schema.Never
|
|
41
|
+
}).make({
|
|
42
|
+
init: () => null,
|
|
43
|
+
update: ($) => $.model,
|
|
44
|
+
view: (h) => declare(h, {
|
|
45
|
+
props,
|
|
46
|
+
slots: Object.fromEntries(Object.keys(slots).map((name) => {
|
|
47
|
+
return [name, (name === DEFAULT ? h.el(SLOT) : h.el(SLOT).attr("name", name)).onMount(Fill({ name }))];
|
|
48
|
+
})),
|
|
49
|
+
fallback: (result) => result._tag === "Failure" && fallback !== void 0 ? fallback(result, h) : h.span.attr("hidden", "")
|
|
50
|
+
})
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Whether the component, rendered under `withSlots`, was left to the browser: the paths
|
|
55
|
+
* the server deferred include a child of the wrapper itself.
|
|
56
|
+
*/
|
|
57
|
+
function deferredUnderSlots(deferred) {
|
|
58
|
+
return (deferred ?? []).some((path) => path.split("/").length === 3);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Puts each slot's HTML where the server left its empty element. An island that will
|
|
62
|
+
* hydrate keeps the element, which is how Astro finds the slot again; a static one gets
|
|
63
|
+
* the content alone. The render holds no markup but Effect UI's own, so one pass cannot
|
|
64
|
+
* be misled by what a slot contains.
|
|
65
|
+
*/
|
|
66
|
+
function fillMarkup(html, slots, hydrate) {
|
|
67
|
+
return html.replace(placeholder, (empty, name) => {
|
|
68
|
+
const content = slots[name ?? DEFAULT];
|
|
69
|
+
if (content === void 0) return empty;
|
|
70
|
+
if (!hydrate) return content;
|
|
71
|
+
return name === void 0 ? `<${SLOT}>${content}</${SLOT}>` : `<${SLOT} name="${name}">${content}</${SLOT}>`;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
const slotElements = (island) => [...island.querySelectorAll(SLOT)].filter((slot) => slot.closest("astro-island") === island);
|
|
75
|
+
const nameOf = (slot) => slot.getAttribute("name") ?? DEFAULT;
|
|
76
|
+
/**
|
|
77
|
+
* Keeps the nodes the server rendered into each slot. Hydration removes whatever a view
|
|
78
|
+
* does not describe, and a slot's content is exactly that, so the same nodes are handed
|
|
79
|
+
* back afterwards rather than parsed again.
|
|
80
|
+
*/
|
|
81
|
+
function keepSlotNodes(island) {
|
|
82
|
+
return new Map(slotElements(island).map((slot) => [nameOf(slot), [...slot.childNodes]]));
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* A `fill` for the browser. A slot's nodes are reused whenever they are free, so content
|
|
86
|
+
* survives hydration, and survives its slot being taken out of the view and placed again:
|
|
87
|
+
* an island nested in it keeps running. Only a slot never filled before is parsed.
|
|
88
|
+
*/
|
|
89
|
+
function slotFiller(slots, kept) {
|
|
90
|
+
return (element, name) => {
|
|
91
|
+
if (element.childNodes.length > 0) return;
|
|
92
|
+
const nodes = kept.get(name);
|
|
93
|
+
if (nodes !== void 0 && nodes.every((node) => node.parentNode?.isConnected !== true)) element.append(...nodes);
|
|
94
|
+
else {
|
|
95
|
+
element.innerHTML = slots[name] ?? "";
|
|
96
|
+
kept.set(name, [...element.childNodes]);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/** Fills every slot element of the island that is still empty. */
|
|
101
|
+
function fillSlotElements(island, fill) {
|
|
102
|
+
for (const slot of slotElements(island)) fill(slot, nameOf(slot));
|
|
103
|
+
}
|
|
104
|
+
//#endregion
|
|
105
|
+
export { slotFiller as a, keepSlotNodes as i, fillMarkup as n, slotsOf as o, fillSlotElements as r, withSlots as s, deferredUnderSlots as t };
|