@seip/bluebird-js 0.1.1 → 0.1.4
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/.vscode/extensions.json +7 -0
- package/.vscode/settings.json +10 -0
- package/AGENTS.md +124 -64
- package/README.md +236 -103
- package/app/routes/web.js +47 -55
- package/app/views/about.jsx +98 -0
- package/app/views/components/Header.jsx +42 -0
- package/app/views/components/LiveCounter.jsx +67 -0
- package/app/views/dashboard.jsx +300 -0
- package/app/views/index.jsx +123 -0
- package/app/views/layouts/main/view.jsx +201 -0
- package/app/views/legal/terms.html +29 -19
- package/app/views/login.jsx +141 -0
- package/core/actions.d.ts +4 -0
- package/core/actions.js +329 -0
- package/core/app.js +129 -22
- package/core/auth.js +1 -1
- package/core/cli/doctor.js +3 -4
- package/core/cli/init.js +153 -6
- package/core/cli/migrate.js +418 -145
- package/core/cli/view.js +76 -11
- package/core/config.js +35 -5
- package/core/csrf.js +1 -1
- package/core/database.js +52 -17
- package/core/debug.js +1 -1
- package/core/html.d.ts +54 -42
- package/core/html.js +473 -217
- package/core/index.d.ts +187 -85
- package/core/jsx.d.ts +38 -0
- package/core/jsx.js +1180 -0
- package/core/model.js +282 -61
- package/core/queue.js +7 -4
- package/core/render-cache.js +212 -212
- package/core/render.d.ts +20 -2
- package/core/render.js +320 -43
- package/core/upload.js +7 -1
- package/core/validate.js +1 -1
- package/core/view-loader.js +341 -19
- package/core/vite.js +1 -1
- package/package.json +9 -3
- package/public/js/bluebird-client.js +2313 -1049
- package/public/js/tailwind.js +8 -0
- package/app/views/about.js +0 -63
- package/app/views/dashboard.js +0 -147
- package/app/views/index.js +0 -80
- package/app/views/layouts/main/view.js +0 -81
- package/app/views/login.js +0 -80
- package/public/css/bluebird.min.css +0 -2
- package/public/js/bluebird.min.js +0 -1073
package/core/jsx.js
ADDED
|
@@ -0,0 +1,1180 @@
|
|
|
1
|
+
import nodeModule from "node:module";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { SafeHtml, escapeHtml, safeJson, isSafeHtml } from "./html.js";
|
|
5
|
+
|
|
6
|
+
export const JSX_ELEMENT = Symbol.for("Bluebird.JsxElement");
|
|
7
|
+
export const Fragment = Symbol.for("Bluebird.Fragment");
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Boolean HTML attributes that should be rendered as bare attribute names when true
|
|
11
|
+
* and omitted when false.
|
|
12
|
+
*/
|
|
13
|
+
const BOOLEAN_ATTRS = new Set([
|
|
14
|
+
"allowfullscreen",
|
|
15
|
+
"async",
|
|
16
|
+
"autofocus",
|
|
17
|
+
"autoplay",
|
|
18
|
+
"checked",
|
|
19
|
+
"controls",
|
|
20
|
+
"default",
|
|
21
|
+
"defer",
|
|
22
|
+
"disabled",
|
|
23
|
+
"formnovalidate",
|
|
24
|
+
"hidden",
|
|
25
|
+
"inert",
|
|
26
|
+
"ismap",
|
|
27
|
+
"itemscope",
|
|
28
|
+
"loop",
|
|
29
|
+
"multiple",
|
|
30
|
+
"muted",
|
|
31
|
+
"nomodule",
|
|
32
|
+
"novalidate",
|
|
33
|
+
"open",
|
|
34
|
+
"playsinline",
|
|
35
|
+
"readonly",
|
|
36
|
+
"required",
|
|
37
|
+
"reversed",
|
|
38
|
+
"selected",
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Self-closing void HTML elements that must not have a closing tag.
|
|
43
|
+
*/
|
|
44
|
+
const VOID_ELEMENTS = new Set([
|
|
45
|
+
"area",
|
|
46
|
+
"base",
|
|
47
|
+
"br",
|
|
48
|
+
"col",
|
|
49
|
+
"embed",
|
|
50
|
+
"hr",
|
|
51
|
+
"img",
|
|
52
|
+
"input",
|
|
53
|
+
"link",
|
|
54
|
+
"meta",
|
|
55
|
+
"param",
|
|
56
|
+
"source",
|
|
57
|
+
"track",
|
|
58
|
+
"wbr",
|
|
59
|
+
]);
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* CSS properties that accept unitless numeric values.
|
|
63
|
+
*/
|
|
64
|
+
const UNITLESS_CSS = new Set([
|
|
65
|
+
"animationIterationCount",
|
|
66
|
+
"aspectRatio",
|
|
67
|
+
"borderImageOutset",
|
|
68
|
+
"borderImageSlice",
|
|
69
|
+
"borderImageWidth",
|
|
70
|
+
"boxFlex",
|
|
71
|
+
"boxFlexGroup",
|
|
72
|
+
"boxOrdinalGroup",
|
|
73
|
+
"columnCount",
|
|
74
|
+
"columns",
|
|
75
|
+
"flex",
|
|
76
|
+
"flexGrow",
|
|
77
|
+
"flexPositive",
|
|
78
|
+
"flexShrink",
|
|
79
|
+
"flexNegative",
|
|
80
|
+
"flexOrder",
|
|
81
|
+
"gridArea",
|
|
82
|
+
"gridRow",
|
|
83
|
+
"gridRowEnd",
|
|
84
|
+
"gridRowSpan",
|
|
85
|
+
"gridRowStart",
|
|
86
|
+
"gridColumn",
|
|
87
|
+
"gridColumnEnd",
|
|
88
|
+
"gridColumnSpan",
|
|
89
|
+
"gridColumnStart",
|
|
90
|
+
"fontWeight",
|
|
91
|
+
"lineClamp",
|
|
92
|
+
"lineHeight",
|
|
93
|
+
"opacity",
|
|
94
|
+
"order",
|
|
95
|
+
"orphans",
|
|
96
|
+
"tabSize",
|
|
97
|
+
"widows",
|
|
98
|
+
"zIndex",
|
|
99
|
+
"zoom",
|
|
100
|
+
"fillOpacity",
|
|
101
|
+
"floodOpacity",
|
|
102
|
+
"stopOpacity",
|
|
103
|
+
"strokeDasharray",
|
|
104
|
+
"strokeDashoffset",
|
|
105
|
+
"strokeMiterlimit",
|
|
106
|
+
"strokeOpacity",
|
|
107
|
+
"strokeWidth",
|
|
108
|
+
]);
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Attribute name normalization map from React camelCase to standard HTML.
|
|
112
|
+
*/
|
|
113
|
+
const ATTR_MAP = {
|
|
114
|
+
className: "class",
|
|
115
|
+
htmlFor: "for",
|
|
116
|
+
tabIndex: "tabindex",
|
|
117
|
+
readOnly: "readonly",
|
|
118
|
+
maxLength: "maxlength",
|
|
119
|
+
minLength: "minlength",
|
|
120
|
+
cellPadding: "cellpadding",
|
|
121
|
+
cellSpacing: "cellspacing",
|
|
122
|
+
rowSpan: "rowspan",
|
|
123
|
+
colSpan: "colspan",
|
|
124
|
+
useMap: "usemap",
|
|
125
|
+
frameBorder: "frameborder",
|
|
126
|
+
contentEditable: "contenteditable",
|
|
127
|
+
crossOrigin: "crossorigin",
|
|
128
|
+
autoComplete: "autocomplete",
|
|
129
|
+
autoFocus: "autofocus",
|
|
130
|
+
autoPlay: "autoplay",
|
|
131
|
+
encType: "enctype",
|
|
132
|
+
formAction: "formaction",
|
|
133
|
+
formEncType: "formenctype",
|
|
134
|
+
formMethod: "formmethod",
|
|
135
|
+
formNoValidate: "formnovalidate",
|
|
136
|
+
formTarget: "formtarget",
|
|
137
|
+
hrefLang: "hreflang",
|
|
138
|
+
inputMode: "inputmode",
|
|
139
|
+
noValidate: "novalidate",
|
|
140
|
+
playsInline: "playsinline",
|
|
141
|
+
referrerPolicy: "referrerpolicy",
|
|
142
|
+
srcDoc: "srcdoc",
|
|
143
|
+
srcLang: "srclang",
|
|
144
|
+
srcSet: "srcset",
|
|
145
|
+
fetchPriority: "fetchpriority",
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Cached React module references if installed in the project.
|
|
150
|
+
* @type {{ React: any, ReactDOMServer: any } | null}
|
|
151
|
+
*/
|
|
152
|
+
let reactModules = null;
|
|
153
|
+
let reactLoadAttempted = false;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Attempts to dynamically load react and react-dom/server once if installed.
|
|
157
|
+
* @returns {Promise<{ React: any, ReactDOMServer: any } | null>}
|
|
158
|
+
*/
|
|
159
|
+
export async function getReactModules() {
|
|
160
|
+
if (reactLoadAttempted) return reactModules;
|
|
161
|
+
reactLoadAttempted = true;
|
|
162
|
+
try {
|
|
163
|
+
const [reactMod, reactDomServerMod] = await Promise.all([
|
|
164
|
+
import("react"),
|
|
165
|
+
import("react-dom/server"),
|
|
166
|
+
]);
|
|
167
|
+
reactModules = {
|
|
168
|
+
React: reactMod.default || reactMod,
|
|
169
|
+
ReactDOMServer: reactDomServerMod.default || reactDomServerMod,
|
|
170
|
+
};
|
|
171
|
+
} catch {
|
|
172
|
+
reactModules = null;
|
|
173
|
+
}
|
|
174
|
+
return reactModules;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Serializes a React style object into an inline CSS string.
|
|
179
|
+
* @param {Record<string, any>|string} style
|
|
180
|
+
* @returns {string}
|
|
181
|
+
*/
|
|
182
|
+
export function serializeStyle(style) {
|
|
183
|
+
if (!style) return "";
|
|
184
|
+
if (typeof style === "string") return style;
|
|
185
|
+
if (typeof style !== "object") return "";
|
|
186
|
+
|
|
187
|
+
let css = "";
|
|
188
|
+
for (const [key, val] of Object.entries(style)) {
|
|
189
|
+
if (val === null || val === undefined || val === false || val === "") continue;
|
|
190
|
+
const kebabKey = key.startsWith("--")
|
|
191
|
+
? key
|
|
192
|
+
: key.replace(/[A-Z]/g, (m) => `-${m.toLowerCase()}`);
|
|
193
|
+
let strVal = String(val);
|
|
194
|
+
if (typeof val === "number" && val !== 0 && !UNITLESS_CSS.has(key) && !key.startsWith("--")) {
|
|
195
|
+
strVal = `${val}px`;
|
|
196
|
+
}
|
|
197
|
+
css += `${css ? ";" : ""}${kebabKey}:${strVal}`;
|
|
198
|
+
}
|
|
199
|
+
return css;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Creates a lightweight JSX virtual element descriptor.
|
|
204
|
+
* Compatible with React.createElement(type, props, ...children) and automatic runtime jsx(type, props, key).
|
|
205
|
+
*
|
|
206
|
+
* @param {string|Function|Symbol} type
|
|
207
|
+
* @param {Record<string, any>|null} [props]
|
|
208
|
+
* @param {...any} children
|
|
209
|
+
* @returns {Object}
|
|
210
|
+
*/
|
|
211
|
+
export function h(type, props, ...children) {
|
|
212
|
+
const normalizedProps = { ...(props || {}) };
|
|
213
|
+
if (children.length === 1) {
|
|
214
|
+
normalizedProps.children = children[0];
|
|
215
|
+
} else if (children.length > 1) {
|
|
216
|
+
normalizedProps.children = children;
|
|
217
|
+
}
|
|
218
|
+
return {
|
|
219
|
+
$$typeof: Symbol.for("react.transitional.element"),
|
|
220
|
+
[JSX_ELEMENT]: true,
|
|
221
|
+
type,
|
|
222
|
+
props: normalizedProps,
|
|
223
|
+
key: normalizedProps.key ?? null,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Automatic JSX runtime factory (`jsx` / `jsxs`).
|
|
229
|
+
* @param {string|Function|Symbol} type
|
|
230
|
+
* @param {Record<string, any>} props
|
|
231
|
+
* @param {string|number} [key]
|
|
232
|
+
* @returns {Object}
|
|
233
|
+
*/
|
|
234
|
+
export function jsx(type, props, key) {
|
|
235
|
+
const normalizedProps = { ...(props || {}) };
|
|
236
|
+
if (key !== undefined) {
|
|
237
|
+
normalizedProps.key = key;
|
|
238
|
+
}
|
|
239
|
+
return {
|
|
240
|
+
$$typeof: Symbol.for("react.transitional.element"),
|
|
241
|
+
[JSX_ELEMENT]: true,
|
|
242
|
+
type,
|
|
243
|
+
props: normalizedProps,
|
|
244
|
+
key: key ?? normalizedProps.key ?? null,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export const jsxs = jsx;
|
|
249
|
+
export const jsxDEV = jsx;
|
|
250
|
+
export const createElement = h;
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Checks whether a value is a Bluebird JSX element or a standard React element.
|
|
254
|
+
* @param {any} val
|
|
255
|
+
* @returns {boolean}
|
|
256
|
+
*/
|
|
257
|
+
export function isJsxElement(val) {
|
|
258
|
+
if (!val || typeof val !== "object") return false;
|
|
259
|
+
if (val[JSX_ELEMENT] === true) return true;
|
|
260
|
+
const typeofSym = val.$$typeof;
|
|
261
|
+
if (typeof typeofSym === "symbol") {
|
|
262
|
+
const symStr = typeofSym.toString();
|
|
263
|
+
return symStr.includes("react.element") || symStr.includes("react.transitional.element") || symStr.includes("react.fragment");
|
|
264
|
+
}
|
|
265
|
+
return false;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Renders HTML attributes from a JSX props object.
|
|
270
|
+
* @param {Record<string, any>} props
|
|
271
|
+
* @returns {string}
|
|
272
|
+
*/
|
|
273
|
+
function renderAttributes(props) {
|
|
274
|
+
if (!props) return "";
|
|
275
|
+
let attrs = "";
|
|
276
|
+
|
|
277
|
+
for (const [rawKey, val] of Object.entries(props)) {
|
|
278
|
+
if (
|
|
279
|
+
rawKey === "children" ||
|
|
280
|
+
rawKey === "key" ||
|
|
281
|
+
rawKey === "ref" ||
|
|
282
|
+
rawKey === "dangerouslySetInnerHTML" ||
|
|
283
|
+
val === undefined ||
|
|
284
|
+
val === null ||
|
|
285
|
+
typeof val === "function"
|
|
286
|
+
) {
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const attrName = ATTR_MAP[rawKey] || rawKey;
|
|
291
|
+
const lowerName = attrName.toLowerCase();
|
|
292
|
+
|
|
293
|
+
if (BOOLEAN_ATTRS.has(lowerName)) {
|
|
294
|
+
if (val) {
|
|
295
|
+
attrs += ` ${lowerName}`;
|
|
296
|
+
}
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (attrName === "style" && typeof val === "object") {
|
|
301
|
+
const styleStr = serializeStyle(val);
|
|
302
|
+
if (styleStr) {
|
|
303
|
+
attrs += ` style="${escapeHtml(styleStr)}"`;
|
|
304
|
+
}
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (typeof val === "boolean") {
|
|
309
|
+
if (attrName.startsWith("data-") || attrName.startsWith("aria-")) {
|
|
310
|
+
attrs += ` ${attrName}="${val}"`;
|
|
311
|
+
} else if (val) {
|
|
312
|
+
attrs += ` ${attrName}`;
|
|
313
|
+
}
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
attrs += ` ${attrName}="${escapeHtml(String(val))}"`;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return attrs;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Renders any JSX node, async Server Component, SafeHtml, or primitive tree into an HTML string.
|
|
325
|
+
* Supports async function components (`export default async function Page()`),
|
|
326
|
+
* Fragments, `dangerouslySetInnerHTML`, and mixed `html\`...\`` SafeHtml objects.
|
|
327
|
+
*
|
|
328
|
+
* @param {any} node
|
|
329
|
+
* @param {Object} [ctx={}]
|
|
330
|
+
* @returns {Promise<string>}
|
|
331
|
+
*/
|
|
332
|
+
export async function renderJsxToString(node, ctx = {}) {
|
|
333
|
+
if (node === null || node === undefined || typeof node === "boolean") {
|
|
334
|
+
return "";
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
if (typeof node === "string") {
|
|
338
|
+
return escapeHtml(node);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (typeof node === "number" || typeof node === "bigint") {
|
|
342
|
+
return String(node);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
if (isSafeHtml(node)) {
|
|
346
|
+
return node.toString();
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
if (node instanceof Promise) {
|
|
350
|
+
const resolved = await node;
|
|
351
|
+
return renderJsxToString(resolved, ctx);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (Array.isArray(node)) {
|
|
355
|
+
let out = "";
|
|
356
|
+
for (let i = 0; i < node.length; i++) {
|
|
357
|
+
out += await renderJsxToString(node[i], ctx);
|
|
358
|
+
}
|
|
359
|
+
return out;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
if (isJsxElement(node)) {
|
|
363
|
+
const { type, props = {} } = node;
|
|
364
|
+
|
|
365
|
+
if (
|
|
366
|
+
type === Fragment ||
|
|
367
|
+
type === Symbol.for("react.fragment") ||
|
|
368
|
+
type === "" ||
|
|
369
|
+
type === null
|
|
370
|
+
) {
|
|
371
|
+
return renderJsxToString(props.children, ctx);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (typeof type === "function") {
|
|
375
|
+
const result = await type(props, ctx);
|
|
376
|
+
return renderJsxToString(result, ctx);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
if (typeof type === "string") {
|
|
380
|
+
const tag = type;
|
|
381
|
+
const attrs = renderAttributes(props);
|
|
382
|
+
|
|
383
|
+
if (VOID_ELEMENTS.has(tag.toLowerCase())) {
|
|
384
|
+
return `<${tag}${attrs} />`;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
let inner = "";
|
|
388
|
+
if (props.dangerouslySetInnerHTML && props.dangerouslySetInnerHTML.__html != null) {
|
|
389
|
+
inner = String(props.dangerouslySetInnerHTML.__html);
|
|
390
|
+
} else if (props.children !== undefined) {
|
|
391
|
+
if ((tag === "script" || tag === "style") && typeof props.children === "string") {
|
|
392
|
+
inner = props.children;
|
|
393
|
+
} else {
|
|
394
|
+
inner = await renderJsxToString(props.children, ctx);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return `<${tag}${attrs}>${inner}</${tag}>`;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
return escapeHtml(String(node));
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Resolves the island component name from a string or function reference.
|
|
407
|
+
* @param {string|Function} component
|
|
408
|
+
* @returns {string}
|
|
409
|
+
*/
|
|
410
|
+
function resolveComponentName(component) {
|
|
411
|
+
if (typeof component === "string") return component;
|
|
412
|
+
if (typeof component === "function") {
|
|
413
|
+
return component.displayName || component.name || "AnonymousIsland";
|
|
414
|
+
}
|
|
415
|
+
return "Island";
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Built-in `<Island>` / `<Client>` component for Bluebird React SSR.
|
|
420
|
+
* Renders a client-hydrated component container with anti-CLS layout locking (`minHeight` / `minWidth`),
|
|
421
|
+
* custom skeleton/fallback support, optional SSR pre-rendering of the component, and View Transition persistence.
|
|
422
|
+
*
|
|
423
|
+
* Usage in JSX:
|
|
424
|
+
* ```jsx
|
|
425
|
+
* <Island
|
|
426
|
+
* component={LiveCounter}
|
|
427
|
+
* props={{ initial: 10 }}
|
|
428
|
+
* skeleton={<div className="h-12 animate-pulse bg-slate-800 rounded-xl" />}
|
|
429
|
+
* minHeight="48px"
|
|
430
|
+
* persist={false}
|
|
431
|
+
* />
|
|
432
|
+
* ```
|
|
433
|
+
*
|
|
434
|
+
* @param {Object} options
|
|
435
|
+
* @param {string|Function} [options.component] - Component function or registered island name.
|
|
436
|
+
* @param {string} [options.name] - Explicit island name override.
|
|
437
|
+
* @param {Record<string, any>} [options.props={}] - Props serialized for client hydration.
|
|
438
|
+
* @param {any} [options.skeleton] - Skeleton JSX or SafeHtml rendered inside the container before client mount (prevents layout shift).
|
|
439
|
+
* @param {any} [options.fallback] - Alias for skeleton.
|
|
440
|
+
* @param {string|number} [options.minHeight] - CSS min-height to lock vertical space and prevent accordion/CLS shift.
|
|
441
|
+
* @param {string|number} [options.minWidth] - CSS min-width to lock horizontal space.
|
|
442
|
+
* @param {string} [options.className] - Optional CSS class on the island container.
|
|
443
|
+
* @param {Record<string, any>|string} [options.style] - Optional inline style on the island container.
|
|
444
|
+
* @param {boolean} [options.persist=false] - Whether to preserve the mounted island across SPA View Transitions.
|
|
445
|
+
* @param {boolean} [options.ssr=false] - If true and `component` is a function, pre-renders the component on the server inside the container.
|
|
446
|
+
* @param {string} [options.tag="div"] - HTML wrapper tag name.
|
|
447
|
+
* @param {any} [options.children] - Optional children used as skeleton fallback if `skeleton` is not specified.
|
|
448
|
+
* @returns {Promise<SafeHtml>}
|
|
449
|
+
*/
|
|
450
|
+
export async function Island(options = {}) {
|
|
451
|
+
const {
|
|
452
|
+
component,
|
|
453
|
+
client,
|
|
454
|
+
onMount,
|
|
455
|
+
name: explicitName,
|
|
456
|
+
props: explicitProps,
|
|
457
|
+
skeleton,
|
|
458
|
+
fallback,
|
|
459
|
+
minHeight,
|
|
460
|
+
minWidth,
|
|
461
|
+
className,
|
|
462
|
+
style,
|
|
463
|
+
persist = false,
|
|
464
|
+
persistent = false,
|
|
465
|
+
ssr = false,
|
|
466
|
+
tag = "div",
|
|
467
|
+
children,
|
|
468
|
+
...restProps
|
|
469
|
+
} = options;
|
|
470
|
+
|
|
471
|
+
const isPersistent = Boolean(persist || persistent);
|
|
472
|
+
const islandName = explicitName || resolveComponentName(component || client || onMount);
|
|
473
|
+
const componentProps = explicitProps !== undefined ? explicitProps : restProps;
|
|
474
|
+
|
|
475
|
+
let clientFn =
|
|
476
|
+
typeof client === "function"
|
|
477
|
+
? client
|
|
478
|
+
: typeof onMount === "function"
|
|
479
|
+
? onMount
|
|
480
|
+
: null;
|
|
481
|
+
|
|
482
|
+
if (!clientFn && typeof component === "function" && !ssr) {
|
|
483
|
+
clientFn = component;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
const mergedStyle = typeof style === "object" && style !== null ? { ...style } : {};
|
|
487
|
+
if (minHeight !== undefined) {
|
|
488
|
+
mergedStyle.minHeight = typeof minHeight === "number" ? `${minHeight}px` : minHeight;
|
|
489
|
+
}
|
|
490
|
+
if (minWidth !== undefined) {
|
|
491
|
+
mergedStyle.minWidth = typeof minWidth === "number" ? `${minWidth}px` : minWidth;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
const styleStr = typeof style === "string" ? style : serializeStyle(mergedStyle);
|
|
495
|
+
const styleAttr = styleStr ? ` style="${escapeHtml(styleStr)}"` : "";
|
|
496
|
+
const classAttr = className ? ` class="${escapeHtml(className)}"` : "";
|
|
497
|
+
const persistAttr = isPersistent ? ' data-persist="true"' : "";
|
|
498
|
+
const hydrateModeAttr = ssr ? ' data-hydrate="true"' : "";
|
|
499
|
+
const serializedProps = escapeHtml(safeJson(componentProps));
|
|
500
|
+
|
|
501
|
+
let innerHtml = "";
|
|
502
|
+
const fallbackNode = skeleton !== undefined ? skeleton : fallback !== undefined ? fallback : children;
|
|
503
|
+
|
|
504
|
+
if (fallbackNode !== undefined && fallbackNode !== null) {
|
|
505
|
+
innerHtml = await renderJsxToString(fallbackNode);
|
|
506
|
+
} else if (ssr && typeof component === "function") {
|
|
507
|
+
try {
|
|
508
|
+
const ssrNode = await component(componentProps);
|
|
509
|
+
innerHtml = await renderJsxToString(ssrNode);
|
|
510
|
+
} catch {
|
|
511
|
+
innerHtml = "";
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
let inlineMountScript = "";
|
|
516
|
+
if (clientFn) {
|
|
517
|
+
const fnStr = clientFn.toString().replace(/<\/script/gi, "<\\/script");
|
|
518
|
+
inlineMountScript = `<script data-bb-client-mount="true">(function(){var s=document.currentScript;var el=s&&s.parentElement;if(!el||el.__bb_fn_mounted__)return;el.__bb_fn_mounted__=true;if(window.__BLUEBIRD_ISLANDS__&&window.__BLUEBIRD_ISLANDS__[${safeJson(islandName)}]&&typeof window.__BLUEBIRD_MOUNT__==="function")return;var p=${safeJson(componentProps)};var fn=${fnStr};var mount=function(){if(window.Bluebird&&typeof window.Bluebird.mount==="function"){window.Bluebird.mount(el,fn,p);}else{fn(Object.assign({},p,{el:el,root:el,Bluebird:window.Bluebird}));}};if(!window.Bluebird||!window.Bluebird.mount){(window.__BB_MOUNT_QUEUE__=window.__BB_MOUNT_QUEUE__||[]).push(mount);}else{mount();}})();</script>`;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
return new SafeHtml(
|
|
522
|
+
`<${tag} data-island="${escapeHtml(islandName)}" data-props="${serializedProps}"${persistAttr}${hydrateModeAttr}${classAttr}${styleAttr}>${innerHtml}${inlineMountScript}</${tag}>`
|
|
523
|
+
);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* Alias for `<Island>` — marks a component boundary for client-side execution (`useEffect`, `useState`, etc.)
|
|
528
|
+
* with built-in skeleton and anti-accordion layout protection.
|
|
529
|
+
*/
|
|
530
|
+
export const Client = Island;
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* Server-safe fallback for useState in SSR.
|
|
534
|
+
* @param {any} [initial]
|
|
535
|
+
* @returns {[any, Function]}
|
|
536
|
+
*/
|
|
537
|
+
export function useState(initial) {
|
|
538
|
+
const val = typeof initial === "function" ? initial() : initial;
|
|
539
|
+
return [val, () => {}];
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* Server-safe no-op for useEffect in SSR.
|
|
544
|
+
* @param {Function} effectFn
|
|
545
|
+
* @param {Array<any>} [deps]
|
|
546
|
+
*/
|
|
547
|
+
export function useEffect() {}
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* Server-safe fallback for useRef in SSR.
|
|
551
|
+
* @param {any} [initial]
|
|
552
|
+
* @returns {{ current: any }}
|
|
553
|
+
*/
|
|
554
|
+
export function useRef(initial) {
|
|
555
|
+
return { current: initial };
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
* Server-safe fallback for useMemo in SSR.
|
|
560
|
+
* @param {Function} fn
|
|
561
|
+
* @param {Array<any>} [deps]
|
|
562
|
+
* @returns {any}
|
|
563
|
+
*/
|
|
564
|
+
export function useMemo(fn) {
|
|
565
|
+
return typeof fn === "function" ? fn() : fn;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* Server-safe fallback for useCallback in SSR.
|
|
570
|
+
* @param {Function} fn
|
|
571
|
+
* @param {Array<any>} [deps]
|
|
572
|
+
* @returns {Function}
|
|
573
|
+
*/
|
|
574
|
+
export function useCallback(fn) {
|
|
575
|
+
return fn;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
* Ultra-fast, zero-dependency JSX-to-JS compiler for Node.js ESM.
|
|
580
|
+
* Compiles standard React JSX syntax (elements, fragments `<>...</>`, spread attributes `{...props}`,
|
|
581
|
+
* nested expressions, boolean props, and component tags) into `h()` calls in memory.
|
|
582
|
+
*
|
|
583
|
+
* @param {string} source - Raw JSX module source code.
|
|
584
|
+
* @returns {string} Valid JavaScript ESM source code.
|
|
585
|
+
*/
|
|
586
|
+
export function compileJsx(source) {
|
|
587
|
+
let out = "";
|
|
588
|
+
let i = 0;
|
|
589
|
+
const len = source.length;
|
|
590
|
+
|
|
591
|
+
/**
|
|
592
|
+
* Skips string literals, template literals, and comments in normal JS code.
|
|
593
|
+
*/
|
|
594
|
+
while (i < len) {
|
|
595
|
+
const ch = source[i];
|
|
596
|
+
const next = source[i + 1];
|
|
597
|
+
|
|
598
|
+
if (ch === "/" && next === "/") {
|
|
599
|
+
const end = source.indexOf("\n", i + 2);
|
|
600
|
+
if (end === -1) {
|
|
601
|
+
out += source.slice(i);
|
|
602
|
+
break;
|
|
603
|
+
}
|
|
604
|
+
out += source.slice(i, end + 1);
|
|
605
|
+
i = end + 1;
|
|
606
|
+
continue;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
if (ch === "/" && next === "*") {
|
|
610
|
+
const end = source.indexOf("*/", i + 2);
|
|
611
|
+
if (end === -1) {
|
|
612
|
+
out += source.slice(i);
|
|
613
|
+
break;
|
|
614
|
+
}
|
|
615
|
+
out += source.slice(i, end + 2);
|
|
616
|
+
i = end + 2;
|
|
617
|
+
continue;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
if (ch === '"' || ch === "'") {
|
|
621
|
+
const quote = ch;
|
|
622
|
+
out += ch;
|
|
623
|
+
i++;
|
|
624
|
+
while (i < len) {
|
|
625
|
+
const c = source[i];
|
|
626
|
+
out += c;
|
|
627
|
+
i++;
|
|
628
|
+
if (c === "\\") {
|
|
629
|
+
if (i < len) {
|
|
630
|
+
out += source[i];
|
|
631
|
+
i++;
|
|
632
|
+
}
|
|
633
|
+
} else if (c === quote) {
|
|
634
|
+
break;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
continue;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
if (ch === "`") {
|
|
641
|
+
const tpl = readTemplateLiteral(source, i);
|
|
642
|
+
out += tpl.code;
|
|
643
|
+
i = tpl.nextIndex;
|
|
644
|
+
continue;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
if (ch === "<" && isJsxStart(source, i, out)) {
|
|
648
|
+
const parsed = parseJsxElement(source, i);
|
|
649
|
+
if (parsed) {
|
|
650
|
+
out += parsed.code;
|
|
651
|
+
i = parsed.nextIndex;
|
|
652
|
+
continue;
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
out += ch;
|
|
657
|
+
i++;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
const preamble = `import { h as __bb_h, Fragment as __bb_Fragment } from "@seip/bluebird-js/core/jsx.js";\n`;
|
|
661
|
+
return preamble + out;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
/**
|
|
665
|
+
* Determines whether a `<` character at `index` starts a JSX tag rather than a comparison operator.
|
|
666
|
+
* @param {string} source
|
|
667
|
+
* @param {number} index
|
|
668
|
+
* @param {string} precedingOutput
|
|
669
|
+
* @returns {boolean}
|
|
670
|
+
*/
|
|
671
|
+
function isJsxStart(source, index, precedingOutput) {
|
|
672
|
+
const next = source[index + 1];
|
|
673
|
+
if (!next) return false;
|
|
674
|
+
|
|
675
|
+
if (next !== ">" && !/[a-zA-Z_$]/.test(next)) {
|
|
676
|
+
return false;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
const trimmed = precedingOutput.trimEnd();
|
|
680
|
+
if (!trimmed) return true;
|
|
681
|
+
|
|
682
|
+
const lastChar = trimmed[trimmed.length - 1];
|
|
683
|
+
if ("=(,[{:;?!&|+-*%~^><".includes(lastChar)) {
|
|
684
|
+
return true;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
const wordMatch = trimmed.match(/([a-zA-Z0-9_$]+)$/);
|
|
688
|
+
if (wordMatch) {
|
|
689
|
+
const kw = wordMatch[1];
|
|
690
|
+
if (
|
|
691
|
+
kw === "return" ||
|
|
692
|
+
kw === "yield" ||
|
|
693
|
+
kw === "await" ||
|
|
694
|
+
kw === "case" ||
|
|
695
|
+
kw === "throw" ||
|
|
696
|
+
kw === "typeof" ||
|
|
697
|
+
kw === "void" ||
|
|
698
|
+
kw === "in" ||
|
|
699
|
+
kw === "of" ||
|
|
700
|
+
kw === "else"
|
|
701
|
+
) {
|
|
702
|
+
return true;
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
return false;
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
/**
|
|
710
|
+
* Reads a template literal including nested `${...}` expressions that might contain JSX.
|
|
711
|
+
* @param {string} source
|
|
712
|
+
* @param {number} start
|
|
713
|
+
* @returns {{ code: string, nextIndex: number }}
|
|
714
|
+
*/
|
|
715
|
+
function readTemplateLiteral(source, start) {
|
|
716
|
+
let code = "`";
|
|
717
|
+
let i = start + 1;
|
|
718
|
+
const len = source.length;
|
|
719
|
+
|
|
720
|
+
while (i < len) {
|
|
721
|
+
const ch = source[i];
|
|
722
|
+
if (ch === "\\") {
|
|
723
|
+
code += source.slice(i, i + 2);
|
|
724
|
+
i += 2;
|
|
725
|
+
continue;
|
|
726
|
+
}
|
|
727
|
+
if (ch === "`") {
|
|
728
|
+
code += "`";
|
|
729
|
+
i++;
|
|
730
|
+
break;
|
|
731
|
+
}
|
|
732
|
+
if (ch === "$" && source[i + 1] === "{") {
|
|
733
|
+
code += "${";
|
|
734
|
+
const expr = readBalancedExpression(source, i + 2);
|
|
735
|
+
code += transformJsFragment(expr.code);
|
|
736
|
+
code += "}";
|
|
737
|
+
i = expr.nextIndex;
|
|
738
|
+
continue;
|
|
739
|
+
}
|
|
740
|
+
code += ch;
|
|
741
|
+
i++;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
return { code, nextIndex: i };
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
/**
|
|
748
|
+
* Transforms a JS expression string that may contain nested JSX elements.
|
|
749
|
+
* @param {string} exprSource
|
|
750
|
+
* @returns {string}
|
|
751
|
+
*/
|
|
752
|
+
function transformJsFragment(exprSource) {
|
|
753
|
+
let out = "";
|
|
754
|
+
let i = 0;
|
|
755
|
+
const len = exprSource.length;
|
|
756
|
+
|
|
757
|
+
while (i < len) {
|
|
758
|
+
const ch = exprSource[i];
|
|
759
|
+
const next = exprSource[i + 1];
|
|
760
|
+
|
|
761
|
+
if (ch === "/" && next === "/") {
|
|
762
|
+
const end = exprSource.indexOf("\n", i + 2);
|
|
763
|
+
if (end === -1) {
|
|
764
|
+
out += exprSource.slice(i);
|
|
765
|
+
break;
|
|
766
|
+
}
|
|
767
|
+
out += exprSource.slice(i, end + 1);
|
|
768
|
+
i = end + 1;
|
|
769
|
+
continue;
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
if (ch === "/" && next === "*") {
|
|
773
|
+
const end = exprSource.indexOf("*/", i + 2);
|
|
774
|
+
if (end === -1) {
|
|
775
|
+
out += exprSource.slice(i);
|
|
776
|
+
break;
|
|
777
|
+
}
|
|
778
|
+
out += exprSource.slice(i, end + 2);
|
|
779
|
+
i = end + 2;
|
|
780
|
+
continue;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
if (ch === '"' || ch === "'") {
|
|
784
|
+
const quote = ch;
|
|
785
|
+
out += ch;
|
|
786
|
+
i++;
|
|
787
|
+
while (i < len) {
|
|
788
|
+
const c = exprSource[i];
|
|
789
|
+
out += c;
|
|
790
|
+
i++;
|
|
791
|
+
if (c === "\\") {
|
|
792
|
+
if (i < len) {
|
|
793
|
+
out += exprSource[i];
|
|
794
|
+
i++;
|
|
795
|
+
}
|
|
796
|
+
} else if (c === quote) {
|
|
797
|
+
break;
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
continue;
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
if (ch === "`") {
|
|
804
|
+
const tpl = readTemplateLiteral(exprSource, i);
|
|
805
|
+
out += tpl.code;
|
|
806
|
+
i = tpl.nextIndex;
|
|
807
|
+
continue;
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
if (ch === "<" && isJsxStart(exprSource, i, out)) {
|
|
811
|
+
const parsed = parseJsxElement(exprSource, i);
|
|
812
|
+
if (parsed) {
|
|
813
|
+
out += parsed.code;
|
|
814
|
+
i = parsed.nextIndex;
|
|
815
|
+
continue;
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
out += ch;
|
|
820
|
+
i++;
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
return out;
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
/**
|
|
827
|
+
* Reads a balanced `{ ... }` expression starting after the opening `{`.
|
|
828
|
+
* @param {string} source
|
|
829
|
+
* @param {number} start - Index immediately after `{`.
|
|
830
|
+
* @returns {{ code: string, nextIndex: number }}
|
|
831
|
+
*/
|
|
832
|
+
function readBalancedExpression(source, start) {
|
|
833
|
+
let depth = 1;
|
|
834
|
+
let i = start;
|
|
835
|
+
const len = source.length;
|
|
836
|
+
|
|
837
|
+
while (i < len && depth > 0) {
|
|
838
|
+
const ch = source[i];
|
|
839
|
+
const next = source[i + 1];
|
|
840
|
+
|
|
841
|
+
if (ch === "/" && next === "/") {
|
|
842
|
+
const end = source.indexOf("\n", i + 2);
|
|
843
|
+
i = end === -1 ? len : end + 1;
|
|
844
|
+
continue;
|
|
845
|
+
}
|
|
846
|
+
if (ch === "/" && next === "*") {
|
|
847
|
+
const end = source.indexOf("*/", i + 2);
|
|
848
|
+
i = end === -1 ? len : end + 2;
|
|
849
|
+
continue;
|
|
850
|
+
}
|
|
851
|
+
if (ch === '"' || ch === "'" || ch === "`") {
|
|
852
|
+
const quote = ch;
|
|
853
|
+
i++;
|
|
854
|
+
while (i < len) {
|
|
855
|
+
const c = source[i];
|
|
856
|
+
i++;
|
|
857
|
+
if (c === "\\") {
|
|
858
|
+
i++;
|
|
859
|
+
} else if (c === quote) {
|
|
860
|
+
break;
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
continue;
|
|
864
|
+
}
|
|
865
|
+
if (ch === "{") depth++;
|
|
866
|
+
else if (ch === "}") depth--;
|
|
867
|
+
|
|
868
|
+
if (depth > 0) i++;
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
const code = source.slice(start, i);
|
|
872
|
+
return { code, nextIndex: i + 1 };
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
/**
|
|
876
|
+
* Parses a JSX element or fragment starting at `start` (`<`).
|
|
877
|
+
* @param {string} source
|
|
878
|
+
* @param {number} start
|
|
879
|
+
* @returns {{ code: string, nextIndex: number } | null}
|
|
880
|
+
*/
|
|
881
|
+
function parseJsxElement(source, start) {
|
|
882
|
+
const len = source.length;
|
|
883
|
+
let i = start + 1;
|
|
884
|
+
|
|
885
|
+
if (source[i] === ">") {
|
|
886
|
+
i++;
|
|
887
|
+
const childrenResult = parseJsxChildren(source, i, "");
|
|
888
|
+
return {
|
|
889
|
+
code: `__bb_h(__bb_Fragment, null${childrenResult.args.length ? ", " + childrenResult.args.join(", ") : ""})`,
|
|
890
|
+
nextIndex: childrenResult.nextIndex,
|
|
891
|
+
};
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
const tagMatch = source.slice(i).match(/^([a-zA-Z_$][a-zA-Z0-9_$.-]*)/);
|
|
895
|
+
if (!tagMatch) return null;
|
|
896
|
+
|
|
897
|
+
const tagName = tagMatch[1];
|
|
898
|
+
i += tagName.length;
|
|
899
|
+
|
|
900
|
+
const isComponent = /^[A-Z_$]/.test(tagName) || tagName.includes(".");
|
|
901
|
+
const tagExpr = isComponent ? tagName : JSON.stringify(tagName);
|
|
902
|
+
|
|
903
|
+
const attrsResult = parseJsxAttributes(source, i);
|
|
904
|
+
if (!attrsResult) return null;
|
|
905
|
+
|
|
906
|
+
i = attrsResult.nextIndex;
|
|
907
|
+
const propsExpr = attrsResult.propsCode;
|
|
908
|
+
|
|
909
|
+
if (attrsResult.selfClosing) {
|
|
910
|
+
return {
|
|
911
|
+
code: `__bb_h(${tagExpr}, ${propsExpr})`,
|
|
912
|
+
nextIndex: i,
|
|
913
|
+
};
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
const childrenResult = parseJsxChildren(source, i, tagName);
|
|
917
|
+
return {
|
|
918
|
+
code: `__bb_h(${tagExpr}, ${propsExpr}${childrenResult.args.length ? ", " + childrenResult.args.join(", ") : ""})`,
|
|
919
|
+
nextIndex: childrenResult.nextIndex,
|
|
920
|
+
};
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
/**
|
|
924
|
+
* Parses attributes inside an opening JSX tag up to `>` or `/>`.
|
|
925
|
+
* @param {string} source
|
|
926
|
+
* @param {number} start
|
|
927
|
+
* @returns {{ propsCode: string, selfClosing: boolean, nextIndex: number } | null}
|
|
928
|
+
*/
|
|
929
|
+
function parseJsxAttributes(source, start) {
|
|
930
|
+
let i = start;
|
|
931
|
+
const len = source.length;
|
|
932
|
+
const entries = [];
|
|
933
|
+
|
|
934
|
+
while (i < len) {
|
|
935
|
+
while (i < len && /\s/.test(source[i])) i++;
|
|
936
|
+
if (i >= len) return null;
|
|
937
|
+
|
|
938
|
+
if (source[i] === "/" && source[i + 1] === ">") {
|
|
939
|
+
return {
|
|
940
|
+
propsCode: entries.length ? `{ ${entries.join(", ")} }` : "null",
|
|
941
|
+
selfClosing: true,
|
|
942
|
+
nextIndex: i + 2,
|
|
943
|
+
};
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
if (source[i] === ">") {
|
|
947
|
+
return {
|
|
948
|
+
propsCode: entries.length ? `{ ${entries.join(", ")} }` : "null",
|
|
949
|
+
selfClosing: false,
|
|
950
|
+
nextIndex: i + 1,
|
|
951
|
+
};
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
if (source[i] === "{") {
|
|
955
|
+
const expr = readBalancedExpression(source, i + 1);
|
|
956
|
+
const trimmed = expr.code.trim();
|
|
957
|
+
if (trimmed.startsWith("...")) {
|
|
958
|
+
entries.push(`...(${transformJsFragment(trimmed.slice(3))})`);
|
|
959
|
+
} else {
|
|
960
|
+
entries.push(transformJsFragment(trimmed));
|
|
961
|
+
}
|
|
962
|
+
i = expr.nextIndex;
|
|
963
|
+
continue;
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
const nameMatch = source.slice(i).match(/^([a-zA-Z_$][a-zA-Z0-9_$.:-]*)/);
|
|
967
|
+
if (!nameMatch) return null;
|
|
968
|
+
|
|
969
|
+
const attrName = nameMatch[1];
|
|
970
|
+
const safeKey = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(attrName)
|
|
971
|
+
? attrName
|
|
972
|
+
: JSON.stringify(attrName);
|
|
973
|
+
i += attrName.length;
|
|
974
|
+
|
|
975
|
+
while (i < len && /\s/.test(source[i])) i++;
|
|
976
|
+
|
|
977
|
+
if (source[i] !== "=") {
|
|
978
|
+
entries.push(`${safeKey}: true`);
|
|
979
|
+
continue;
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
i++;
|
|
983
|
+
while (i < len && /\s/.test(source[i])) i++;
|
|
984
|
+
|
|
985
|
+
const valChar = source[i];
|
|
986
|
+
if (valChar === '"' || valChar === "'") {
|
|
987
|
+
const quote = valChar;
|
|
988
|
+
let strVal = "";
|
|
989
|
+
i++;
|
|
990
|
+
while (i < len && source[i] !== quote) {
|
|
991
|
+
if (source[i] === "\\") {
|
|
992
|
+
strVal += source[i] + (source[i + 1] || "");
|
|
993
|
+
i += 2;
|
|
994
|
+
} else {
|
|
995
|
+
strVal += source[i];
|
|
996
|
+
i++;
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
i++;
|
|
1000
|
+
entries.push(`${safeKey}: ${JSON.stringify(decodeHtmlEntities(strVal))}`);
|
|
1001
|
+
} else if (valChar === "{") {
|
|
1002
|
+
const expr = readBalancedExpression(source, i + 1);
|
|
1003
|
+
entries.push(`${safeKey}: (${transformJsFragment(expr.code)})`);
|
|
1004
|
+
i = expr.nextIndex;
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
return null;
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
/**
|
|
1012
|
+
* Decodes common HTML entities in static JSX text and attributes.
|
|
1013
|
+
* @param {string} text
|
|
1014
|
+
* @returns {string}
|
|
1015
|
+
*/
|
|
1016
|
+
function decodeHtmlEntities(text) {
|
|
1017
|
+
if (!text.includes("&")) return text;
|
|
1018
|
+
return text
|
|
1019
|
+
.replace(/&/g, "&")
|
|
1020
|
+
.replace(/</g, "<")
|
|
1021
|
+
.replace(/>/g, ">")
|
|
1022
|
+
.replace(/"/g, '"')
|
|
1023
|
+
.replace(/'/g, "'")
|
|
1024
|
+
.replace(/'/g, "'")
|
|
1025
|
+
.replace(/→/g, "→")
|
|
1026
|
+
.replace(/←/g, "←")
|
|
1027
|
+
.replace(/•/g, "•")
|
|
1028
|
+
.replace(/−/g, "−")
|
|
1029
|
+
.replace(/+/g, "+")
|
|
1030
|
+
.replace(/×/g, "×")
|
|
1031
|
+
.replace(/ /g, "\u00A0");
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
/**
|
|
1035
|
+
* Normalizes JSX whitespace rules (collapsing multi-line indentation like Babel/React).
|
|
1036
|
+
* @param {string} rawText
|
|
1037
|
+
* @returns {string}
|
|
1038
|
+
*/
|
|
1039
|
+
function cleanJsxText(rawText) {
|
|
1040
|
+
if (!rawText.includes("\n") && !rawText.includes("\r")) {
|
|
1041
|
+
return decodeHtmlEntities(rawText);
|
|
1042
|
+
}
|
|
1043
|
+
const lines = rawText.split(/\r\n|\n|\r/);
|
|
1044
|
+
const cleaned = [];
|
|
1045
|
+
|
|
1046
|
+
for (let idx = 0; idx < lines.length; idx++) {
|
|
1047
|
+
let line = lines[idx].replace(/\t/g, " ");
|
|
1048
|
+
if (idx !== 0) {
|
|
1049
|
+
line = line.trimStart();
|
|
1050
|
+
}
|
|
1051
|
+
if (idx !== lines.length - 1) {
|
|
1052
|
+
line = line.trimEnd();
|
|
1053
|
+
}
|
|
1054
|
+
if (line.length > 0) {
|
|
1055
|
+
cleaned.push(line);
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
return decodeHtmlEntities(cleaned.join(" "));
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
/**
|
|
1063
|
+
* Parses JSX children until the matching closing tag `</tagName>` or `</>`.
|
|
1064
|
+
* @param {string} source
|
|
1065
|
+
* @param {number} start
|
|
1066
|
+
* @param {string} tagName - Empty string for fragments.
|
|
1067
|
+
* @returns {{ args: string[], nextIndex: number }}
|
|
1068
|
+
*/
|
|
1069
|
+
function parseJsxChildren(source, start, tagName) {
|
|
1070
|
+
let i = start;
|
|
1071
|
+
const len = source.length;
|
|
1072
|
+
const args = [];
|
|
1073
|
+
let textStart = i;
|
|
1074
|
+
|
|
1075
|
+
const flushText = (endIdx) => {
|
|
1076
|
+
if (endIdx > textStart) {
|
|
1077
|
+
const raw = source.slice(textStart, endIdx);
|
|
1078
|
+
const cleaned = cleanJsxText(raw);
|
|
1079
|
+
if (cleaned) {
|
|
1080
|
+
args.push(JSON.stringify(cleaned));
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
};
|
|
1084
|
+
|
|
1085
|
+
while (i < len) {
|
|
1086
|
+
if (source[i] === "<" && source[i + 1] === "/") {
|
|
1087
|
+
flushText(i);
|
|
1088
|
+
const closeStart = i + 2;
|
|
1089
|
+
const closeEnd = source.indexOf(">", closeStart);
|
|
1090
|
+
if (closeEnd === -1) {
|
|
1091
|
+
return { args, nextIndex: len };
|
|
1092
|
+
}
|
|
1093
|
+
return { args, nextIndex: closeEnd + 1 };
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
if (source[i] === "<") {
|
|
1097
|
+
flushText(i);
|
|
1098
|
+
const childEl = parseJsxElement(source, i);
|
|
1099
|
+
if (childEl) {
|
|
1100
|
+
args.push(childEl.code);
|
|
1101
|
+
i = childEl.nextIndex;
|
|
1102
|
+
textStart = i;
|
|
1103
|
+
continue;
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
if (source[i] === "{") {
|
|
1108
|
+
flushText(i);
|
|
1109
|
+
const expr = readBalancedExpression(source, i + 1);
|
|
1110
|
+
const trimmed = expr.code.trim();
|
|
1111
|
+
if (trimmed && !/^\/\*[\s\S]*\*\/$/.test(trimmed)) {
|
|
1112
|
+
args.push(`(${transformJsFragment(trimmed)})`);
|
|
1113
|
+
}
|
|
1114
|
+
i = expr.nextIndex;
|
|
1115
|
+
textStart = i;
|
|
1116
|
+
continue;
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
i++;
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
flushText(len);
|
|
1123
|
+
return { args, nextIndex: len };
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
let hooksRegistered = false;
|
|
1127
|
+
|
|
1128
|
+
/**
|
|
1129
|
+
* Registers Node.js native module customization hooks (`module.registerHooks`) so `.jsx` files
|
|
1130
|
+
* can import other `.jsx` components directly without any external build step or disk temp files.
|
|
1131
|
+
*/
|
|
1132
|
+
export function registerJsxLoader() {
|
|
1133
|
+
if (hooksRegistered) return;
|
|
1134
|
+
hooksRegistered = true;
|
|
1135
|
+
|
|
1136
|
+
try {
|
|
1137
|
+
if (typeof nodeModule?.registerHooks === "function") {
|
|
1138
|
+
nodeModule.registerHooks({
|
|
1139
|
+
load(url, context, nextLoad) {
|
|
1140
|
+
const cleanUrl = url.split("?")[0];
|
|
1141
|
+
if (cleanUrl.endsWith(".jsx")) {
|
|
1142
|
+
let rawSource;
|
|
1143
|
+
if (cleanUrl.startsWith("file:")) {
|
|
1144
|
+
const filePath = fileURLToPath(cleanUrl);
|
|
1145
|
+
rawSource = fs.readFileSync(filePath, "utf-8");
|
|
1146
|
+
} else {
|
|
1147
|
+
const loaded = nextLoad(url, { ...context, format: "module" });
|
|
1148
|
+
rawSource =
|
|
1149
|
+
typeof loaded.source === "string"
|
|
1150
|
+
? loaded.source
|
|
1151
|
+
: Buffer.from(loaded.source).toString("utf-8");
|
|
1152
|
+
}
|
|
1153
|
+
return {
|
|
1154
|
+
format: "module",
|
|
1155
|
+
shortCircuit: true,
|
|
1156
|
+
source: compileJsx(rawSource),
|
|
1157
|
+
};
|
|
1158
|
+
}
|
|
1159
|
+
return nextLoad(url, context);
|
|
1160
|
+
},
|
|
1161
|
+
});
|
|
1162
|
+
}
|
|
1163
|
+
} catch {}
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
registerJsxLoader();
|
|
1167
|
+
|
|
1168
|
+
export default {
|
|
1169
|
+
h,
|
|
1170
|
+
createElement,
|
|
1171
|
+
jsx,
|
|
1172
|
+
jsxs,
|
|
1173
|
+
Fragment,
|
|
1174
|
+
Island,
|
|
1175
|
+
Client,
|
|
1176
|
+
isJsxElement,
|
|
1177
|
+
renderJsxToString,
|
|
1178
|
+
compileJsx,
|
|
1179
|
+
registerJsxLoader,
|
|
1180
|
+
};
|