@tschk/moonshine-vue 0.4.49 → 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 +38 -34
- package/package.json +8 -5
- package/src/app.ts +426 -13
- package/src/context.ts +222 -16
- package/src/lifecycle.ts +80 -1
- package/src/map.ts +196 -0
- package/src/reactive.ts +1017 -28
- package/src/router.ts +1562 -0
- package/src/server-renderer.ts +257 -0
- package/src/slots.ts +26 -0
- package/src/store.ts +1042 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host-free Vue 3 server-renderer runtime on Moonshine SSR.
|
|
3
|
+
* Provides `renderToString`, `renderToWebStream`, `renderToSimpleStream`,
|
|
4
|
+
* `useSSRContext`, `SSRContext`, and low-level `ssr*` template helpers.
|
|
5
|
+
*/
|
|
6
|
+
import {
|
|
7
|
+
escapeHtml,
|
|
8
|
+
h as hSSR,
|
|
9
|
+
renderToString as renderMoonshineSSR,
|
|
10
|
+
type SSRChild,
|
|
11
|
+
} from "@tschk/moonshine-dom/ssr";
|
|
12
|
+
import { renderAppSSRSymbol, type NativeApplication } from "./app";
|
|
13
|
+
import {
|
|
14
|
+
peekSSRContext,
|
|
15
|
+
useSSRContext,
|
|
16
|
+
withSSRContext,
|
|
17
|
+
withSSRContextAsync,
|
|
18
|
+
type SSRContext,
|
|
19
|
+
} from "./context";
|
|
20
|
+
import {
|
|
21
|
+
withServerPrefetchSession,
|
|
22
|
+
type ServerPrefetchSession,
|
|
23
|
+
} from "./lifecycle";
|
|
24
|
+
import { normalizeClassSSR, normalizeStyleSSR } from "./normalize";
|
|
25
|
+
import { toDisplayStringSSR } from "./text";
|
|
26
|
+
|
|
27
|
+
export { useSSRContext, type SSRContext };
|
|
28
|
+
|
|
29
|
+
export interface SimpleReadable {
|
|
30
|
+
push(chunk: string | null): void;
|
|
31
|
+
destroy(err: unknown): void;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isNativeApp(
|
|
35
|
+
input: unknown,
|
|
36
|
+
): input is NativeApplication & { [renderAppSSRSymbol]: () => SSRChild } {
|
|
37
|
+
return (
|
|
38
|
+
typeof input === "object" &&
|
|
39
|
+
input !== null &&
|
|
40
|
+
renderAppSSRSymbol in input &&
|
|
41
|
+
typeof (input as any)[renderAppSSRSymbol] === "function"
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function executeRenderPass(input: unknown, ssrContext: SSRContext): string {
|
|
46
|
+
return withSSRContext(ssrContext, () =>
|
|
47
|
+
renderMoonshineSSR(() => {
|
|
48
|
+
if (isNativeApp(input)) {
|
|
49
|
+
return input[renderAppSSRSymbol]();
|
|
50
|
+
}
|
|
51
|
+
if (typeof input === "function") {
|
|
52
|
+
return hSSR(input as (props?: any) => SSRChild, {});
|
|
53
|
+
}
|
|
54
|
+
return input as SSRChild;
|
|
55
|
+
}),
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Asynchronously render a native Vue application or component factory to an HTML string,
|
|
61
|
+
* awaiting any `onServerPrefetch` hooks and populating `context.teleports`.
|
|
62
|
+
*/
|
|
63
|
+
export async function renderToString(
|
|
64
|
+
input: unknown,
|
|
65
|
+
context?: SSRContext,
|
|
66
|
+
): Promise<string> {
|
|
67
|
+
const ssrContext: SSRContext = context ?? {};
|
|
68
|
+
const initialTeleports = ssrContext.teleports
|
|
69
|
+
? { ...ssrContext.teleports }
|
|
70
|
+
: undefined;
|
|
71
|
+
const session: ServerPrefetchSession = {
|
|
72
|
+
phase: "collect",
|
|
73
|
+
hooks: [],
|
|
74
|
+
cells: [],
|
|
75
|
+
cursor: 0,
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const firstHtml = withServerPrefetchSession(session, () =>
|
|
79
|
+
executeRenderPass(input, ssrContext),
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
if (session.hooks.length === 0) {
|
|
83
|
+
return firstHtml;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
session.phase = "awaiting";
|
|
87
|
+
await withSSRContextAsync(ssrContext, () =>
|
|
88
|
+
Promise.all(session.hooks.map((hook) => hook())),
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
session.phase = "replay";
|
|
92
|
+
session.cursor = 0;
|
|
93
|
+
if (initialTeleports) {
|
|
94
|
+
ssrContext.teleports = { ...initialTeleports };
|
|
95
|
+
} else {
|
|
96
|
+
delete ssrContext.teleports;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return withServerPrefetchSession(session, () =>
|
|
100
|
+
executeRenderPass(input, ssrContext),
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Render a native Vue application or component factory to a Web `ReadableStream<Uint8Array>`.
|
|
106
|
+
*/
|
|
107
|
+
export function renderToWebStream(
|
|
108
|
+
input: unknown,
|
|
109
|
+
context?: SSRContext,
|
|
110
|
+
): ReadableStream<Uint8Array> {
|
|
111
|
+
const encoder = new TextEncoder();
|
|
112
|
+
return new ReadableStream<Uint8Array>({
|
|
113
|
+
async start(controller) {
|
|
114
|
+
try {
|
|
115
|
+
const html = await renderToString(input, context);
|
|
116
|
+
if (html.length > 0) {
|
|
117
|
+
controller.enqueue(encoder.encode(html));
|
|
118
|
+
}
|
|
119
|
+
controller.close();
|
|
120
|
+
} catch (error) {
|
|
121
|
+
controller.error(error);
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Render a native Vue application or component factory into a `SimpleReadable` stream sink.
|
|
129
|
+
*/
|
|
130
|
+
export function renderToSimpleStream<T extends SimpleReadable>(
|
|
131
|
+
input: unknown,
|
|
132
|
+
context: SSRContext | undefined,
|
|
133
|
+
stream: T,
|
|
134
|
+
): T {
|
|
135
|
+
renderToString(input, context)
|
|
136
|
+
.then((html) => {
|
|
137
|
+
if (html.length > 0) {
|
|
138
|
+
stream.push(html);
|
|
139
|
+
}
|
|
140
|
+
stream.push(null);
|
|
141
|
+
})
|
|
142
|
+
.catch((error) => {
|
|
143
|
+
stream.destroy(error);
|
|
144
|
+
});
|
|
145
|
+
return stream;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Interpolate and HTML-escape a display value for SSR output. */
|
|
149
|
+
export function ssrInterpolate(value: unknown): string {
|
|
150
|
+
return escapeHtml(toDisplayStringSSR(value));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Render a single HTML attribute key/value pair with leading space when active. */
|
|
154
|
+
export function ssrRenderAttr(key: string, value: unknown): string {
|
|
155
|
+
if (
|
|
156
|
+
value === undefined ||
|
|
157
|
+
value === null ||
|
|
158
|
+
value === false ||
|
|
159
|
+
typeof value === "function" ||
|
|
160
|
+
typeof value === "symbol"
|
|
161
|
+
) {
|
|
162
|
+
return "";
|
|
163
|
+
}
|
|
164
|
+
if (value === true) {
|
|
165
|
+
return ` ${key}`;
|
|
166
|
+
}
|
|
167
|
+
return ` ${key}="${escapeHtml(String(value))}"`;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Normalize and escape a dynamic `class` value for SSR attribute interpolation. */
|
|
171
|
+
export function ssrRenderClass(raw: unknown): string {
|
|
172
|
+
return escapeHtml(normalizeClassSSR(raw));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Normalize and escape a dynamic `style` value for SSR attribute interpolation. */
|
|
176
|
+
export function ssrRenderStyle(raw: unknown): string {
|
|
177
|
+
const normalized = normalizeStyleSSR(raw);
|
|
178
|
+
if (!normalized) return "";
|
|
179
|
+
if (typeof normalized === "string") return escapeHtml(normalized);
|
|
180
|
+
if (typeof normalized === "object") {
|
|
181
|
+
const parts: string[] = [];
|
|
182
|
+
for (const [key, val] of Object.entries(normalized)) {
|
|
183
|
+
if (
|
|
184
|
+
val === undefined ||
|
|
185
|
+
val === null ||
|
|
186
|
+
val === "" ||
|
|
187
|
+
typeof val === "boolean"
|
|
188
|
+
) {
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
const cssKey = key.startsWith("--")
|
|
192
|
+
? key
|
|
193
|
+
: key.replace(/[A-Z]/g, (ch) => `-${ch.toLowerCase()}`);
|
|
194
|
+
parts.push(`${cssKey}:${String(val)}`);
|
|
195
|
+
}
|
|
196
|
+
return escapeHtml(parts.join(";"));
|
|
197
|
+
}
|
|
198
|
+
return escapeHtml(String(normalized));
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Render an attributes map into a space-prefixed HTML attribute string. */
|
|
202
|
+
export function ssrRenderAttrs(
|
|
203
|
+
attrs: Record<string, unknown> | null | undefined,
|
|
204
|
+
): string {
|
|
205
|
+
if (!attrs || typeof attrs !== "object") return "";
|
|
206
|
+
let out = "";
|
|
207
|
+
for (const [key, value] of Object.entries(attrs)) {
|
|
208
|
+
if (
|
|
209
|
+
key === "key" ||
|
|
210
|
+
key === "ref" ||
|
|
211
|
+
key === "children" ||
|
|
212
|
+
key === "$slots" ||
|
|
213
|
+
key === "slots" ||
|
|
214
|
+
key.startsWith("on")
|
|
215
|
+
) {
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
if (key === "class") {
|
|
219
|
+
const cls = ssrRenderClass(value);
|
|
220
|
+
if (cls) out += ` class="${cls}"`;
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
if (key === "style") {
|
|
224
|
+
const style = ssrRenderStyle(value);
|
|
225
|
+
if (style) out += ` style="${style}"`;
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
out += ssrRenderAttr(key, value);
|
|
229
|
+
}
|
|
230
|
+
return out;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** Render an SSR teleport block into `SSRContext.teleports` or inline when disabled. */
|
|
234
|
+
export function ssrRenderTeleport(
|
|
235
|
+
parentPush: (chunk: string) => void,
|
|
236
|
+
contentRenderFn: (push: (chunk: string) => void) => void,
|
|
237
|
+
target: string | null | undefined,
|
|
238
|
+
disabled?: boolean,
|
|
239
|
+
context?: SSRContext,
|
|
240
|
+
): void {
|
|
241
|
+
parentPush("<!--teleport start-->");
|
|
242
|
+
let teleportContent = "";
|
|
243
|
+
contentRenderFn((chunk) => {
|
|
244
|
+
teleportContent += chunk;
|
|
245
|
+
});
|
|
246
|
+
if (disabled) {
|
|
247
|
+
parentPush(teleportContent);
|
|
248
|
+
} else if (typeof target === "string" && target) {
|
|
249
|
+
const ssrCtx = context ?? peekSSRContext();
|
|
250
|
+
if (ssrCtx) {
|
|
251
|
+
ssrCtx.teleports ??= {};
|
|
252
|
+
ssrCtx.teleports[target] =
|
|
253
|
+
(ssrCtx.teleports[target] ?? "") + teleportContent;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
parentPush("<!--teleport end-->");
|
|
257
|
+
}
|
package/src/slots.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/** Compiler-private static default-slot transport; independent of declared props. */
|
|
2
2
|
import {
|
|
3
3
|
getComponentContext,
|
|
4
|
+
peekComponentContext,
|
|
4
5
|
withComponentConsumer,
|
|
6
|
+
type ComponentContext,
|
|
5
7
|
} from "@tschk/moonshine-dom/component";
|
|
6
8
|
import {
|
|
7
9
|
createComponentScope,
|
|
@@ -12,6 +14,25 @@ import type { DOMChild } from "@tschk/moonshine-dom";
|
|
|
12
14
|
import type { SSRChild } from "@tschk/moonshine-dom/ssr";
|
|
13
15
|
const slots = /* @__PURE__ */ Symbol("moonshine.vue.slots");
|
|
14
16
|
type Content = () => unknown;
|
|
17
|
+
const frameDefaultSlotSymbol = Symbol.for("moonshine.vue.frame.defaultSlot");
|
|
18
|
+
const frameDefaultSlots = /* @__PURE__ */ new WeakMap<
|
|
19
|
+
ComponentContext,
|
|
20
|
+
Content
|
|
21
|
+
>();
|
|
22
|
+
export function getDefaultSlotFactory(
|
|
23
|
+
source: object | null | undefined,
|
|
24
|
+
frame?: ComponentContext,
|
|
25
|
+
): Content | undefined {
|
|
26
|
+
if (frame) {
|
|
27
|
+
const fromFrame =
|
|
28
|
+
frameDefaultSlots.get(frame) ??
|
|
29
|
+
(Reflect.get(frame, frameDefaultSlotSymbol) as Content | undefined);
|
|
30
|
+
if (typeof fromFrame === "function") return fromFrame;
|
|
31
|
+
}
|
|
32
|
+
if (!source || typeof source !== "object") return undefined;
|
|
33
|
+
const supplied = Reflect.get(source, slots, source) as Content | undefined;
|
|
34
|
+
return typeof supplied === "function" ? supplied : undefined;
|
|
35
|
+
}
|
|
15
36
|
/** Each authored outlet gets a distinct instance; its factory identity is static. */
|
|
16
37
|
function createSlotOutlet(
|
|
17
38
|
source: object,
|
|
@@ -63,6 +84,11 @@ export function withDefaultSlot<P extends object, R>(
|
|
|
63
84
|
if (typeof component !== "function" || typeof content !== "function")
|
|
64
85
|
throw new TypeError("Static component and slot factories required");
|
|
65
86
|
return (source) => {
|
|
87
|
+
const activeFrame = peekComponentContext();
|
|
88
|
+
if (activeFrame) {
|
|
89
|
+
frameDefaultSlots.set(activeFrame, content);
|
|
90
|
+
Reflect.set(activeFrame, frameDefaultSlotSymbol, content);
|
|
91
|
+
}
|
|
66
92
|
const target = Object.create(
|
|
67
93
|
Object.getPrototypeOf(source),
|
|
68
94
|
Object.getOwnPropertyDescriptors(source),
|