sibujs 1.4.0 → 2.0.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.
Files changed (190) hide show
  1. package/README.md +105 -119
  2. package/dist/browser.cjs +288 -80
  3. package/dist/browser.d.cts +19 -9
  4. package/dist/browser.d.ts +19 -9
  5. package/dist/browser.js +6 -6
  6. package/dist/build.cjs +1019 -313
  7. package/dist/build.d.cts +1 -1
  8. package/dist/build.d.ts +1 -1
  9. package/dist/build.js +15 -13
  10. package/dist/cdn.global.js +17 -16
  11. package/dist/chunk-2RA7SHDA.js +65 -0
  12. package/dist/chunk-2UPRY23K.js +80 -0
  13. package/dist/chunk-3JHCYHWN.js +125 -0
  14. package/dist/{chunk-ZWKZCBO6.js → chunk-3LR7GLWQ.js} +154 -33
  15. package/dist/{chunk-3AIRKM3B.js → chunk-3NSGB5JN.js} +115 -34
  16. package/dist/{chunk-3ARAQO7B.js → chunk-52YJLLRO.js} +29 -6
  17. package/dist/chunk-54EDRCEF.js +93 -0
  18. package/dist/chunk-7JDB7I65.js +1327 -0
  19. package/dist/{chunk-WZSPOOER.js → chunk-CC65Y57T.js} +8 -5
  20. package/dist/{chunk-23VV7YD3.js → chunk-DFPFITST.js} +25 -30
  21. package/dist/{chunk-WR5D4EGH.js → chunk-GTBNNBJ6.js} +14 -2
  22. package/dist/chunk-HB24TBAF.js +121 -0
  23. package/dist/{chunk-CZUGLNJS.js → chunk-ITX6OO3F.js} +3 -3
  24. package/dist/{chunk-JAKHTMQU.js → chunk-JA6667UN.js} +206 -46
  25. package/dist/{chunk-77L6NL3X.js → chunk-JXMMDLBY.js} +306 -183
  26. package/dist/{chunk-3X2YG6YM.js → chunk-JYD2PWXH.js} +59 -28
  27. package/dist/{chunk-F3FA4F32.js → chunk-KLRMB5ZS.js} +135 -79
  28. package/dist/{chunk-5X6PP2UK.js → chunk-LMLD24FC.js} +2 -2
  29. package/dist/{chunk-M4NLBH4I.js → chunk-LYTCUZ7H.js} +3 -2
  30. package/dist/{chunk-TSOKIX5Z.js → chunk-MIUAXB7K.js} +126 -74
  31. package/dist/{chunk-QWZG56ET.js → chunk-ND2664SF.js} +558 -190
  32. package/dist/{chunk-JCI5M6U6.js → chunk-O2MNQFLP.js} +261 -79
  33. package/dist/{chunk-EWFVA3TJ.js → chunk-R73P76YZ.js} +1 -1
  34. package/dist/{chunk-2BYQDGN3.js → chunk-SAHNHTFC.js} +234 -63
  35. package/dist/chunk-UCS6AMJ7.js +79 -0
  36. package/dist/{chunk-ZD6OAMTH.js → chunk-VLPPXTYG.js} +90 -35
  37. package/dist/{chunk-OUZZEE4S.js → chunk-WOMYAHHI.js} +17 -11
  38. package/dist/{contracts-xo5ckdRP.d.cts → contracts-ey_Qh8ef.d.cts} +7 -8
  39. package/dist/{contracts-xo5ckdRP.d.ts → contracts-ey_Qh8ef.d.ts} +7 -8
  40. package/dist/{customElement-D2DJp_xn.d.cts → customElement-CPfIrbvg.d.cts} +18 -9
  41. package/dist/{customElement-D2DJp_xn.d.ts → customElement-CPfIrbvg.d.ts} +18 -9
  42. package/dist/data.cjs +452 -100
  43. package/dist/data.d.cts +20 -2
  44. package/dist/data.d.ts +20 -2
  45. package/dist/data.js +11 -9
  46. package/dist/devtools.cjs +535 -247
  47. package/dist/devtools.d.cts +1 -1
  48. package/dist/devtools.d.ts +1 -1
  49. package/dist/devtools.js +34 -30
  50. package/dist/ecosystem.cjs +499 -143
  51. package/dist/ecosystem.d.cts +13 -11
  52. package/dist/ecosystem.d.ts +13 -11
  53. package/dist/ecosystem.js +12 -11
  54. package/dist/extras.cjs +3639 -1629
  55. package/dist/extras.d.cts +11 -11
  56. package/dist/extras.d.ts +11 -11
  57. package/dist/extras.js +58 -45
  58. package/dist/index.cjs +1023 -313
  59. package/dist/index.d.cts +128 -55
  60. package/dist/index.d.ts +128 -55
  61. package/dist/index.js +28 -16
  62. package/dist/{introspect-BumjnBKr.d.cts → introspect-BWNjNw64.d.cts} +22 -2
  63. package/dist/{introspect-CZrlcaYy.d.ts → introspect-cY2pg9pW.d.ts} +22 -2
  64. package/dist/motion.cjs +90 -36
  65. package/dist/motion.d.cts +1 -1
  66. package/dist/motion.d.ts +1 -1
  67. package/dist/motion.js +4 -4
  68. package/dist/patterns.cjs +414 -81
  69. package/dist/patterns.d.cts +53 -20
  70. package/dist/patterns.d.ts +53 -20
  71. package/dist/patterns.js +7 -7
  72. package/dist/performance.cjs +364 -108
  73. package/dist/performance.d.cts +29 -17
  74. package/dist/performance.d.ts +29 -17
  75. package/dist/performance.js +13 -6
  76. package/dist/plugin-D30wlGW5.d.cts +71 -0
  77. package/dist/plugin-D30wlGW5.d.ts +71 -0
  78. package/dist/plugins.cjs +652 -271
  79. package/dist/plugins.d.cts +13 -6
  80. package/dist/plugins.d.ts +13 -6
  81. package/dist/plugins.js +116 -50
  82. package/dist/{ssr-Do_SiVoL.d.cts → ssr-CrVNy6Pa.d.cts} +9 -15
  83. package/dist/{ssr-Do_SiVoL.d.ts → ssr-CrVNy6Pa.d.ts} +9 -15
  84. package/dist/{ssr-4PBXAOO3.js → ssr-FXD2PPMC.js} +4 -3
  85. package/dist/ssr.cjs +648 -219
  86. package/dist/ssr.d.cts +27 -7
  87. package/dist/ssr.d.ts +27 -7
  88. package/dist/ssr.js +12 -11
  89. package/dist/{tagFactory-DaJ0YWX6.d.ts → tagFactory-S17H2qxu.d.cts} +9 -1
  90. package/dist/{tagFactory-DaJ0YWX6.d.cts → tagFactory-S17H2qxu.d.ts} +9 -1
  91. package/dist/testing.cjs +252 -63
  92. package/dist/testing.d.cts +17 -4
  93. package/dist/testing.d.ts +17 -4
  94. package/dist/testing.js +100 -44
  95. package/dist/ui.cjs +576 -168
  96. package/dist/ui.d.cts +13 -16
  97. package/dist/ui.d.ts +13 -16
  98. package/dist/ui.js +20 -17
  99. package/dist/widgets.cjs +1001 -93
  100. package/dist/widgets.d.cts +104 -2
  101. package/dist/widgets.d.ts +104 -2
  102. package/dist/widgets.js +9 -7
  103. package/package.json +8 -2
  104. package/dist/chunk-32DY64NT.js +0 -282
  105. package/dist/chunk-3CRQALYP.js +0 -877
  106. package/dist/chunk-4EI4AG32.js +0 -482
  107. package/dist/chunk-4MYMUBRS.js +0 -21
  108. package/dist/chunk-6HLLIF3K.js +0 -398
  109. package/dist/chunk-6LSNVCS2.js +0 -937
  110. package/dist/chunk-6SA3QQES.js +0 -61
  111. package/dist/chunk-7BF6TK55.js +0 -1097
  112. package/dist/chunk-7TQKR4PP.js +0 -294
  113. package/dist/chunk-7V26P53V.js +0 -712
  114. package/dist/chunk-AZ3ISID5.js +0 -298
  115. package/dist/chunk-B7SWRFUT.js +0 -332
  116. package/dist/chunk-BGN5ZMP4.js +0 -26
  117. package/dist/chunk-BTU3TJDS.js +0 -365
  118. package/dist/chunk-BW3WT46K.js +0 -937
  119. package/dist/chunk-C6KFWOFV.js +0 -616
  120. package/dist/chunk-CHF5OHIA.js +0 -61
  121. package/dist/chunk-CHJ27IGK.js +0 -26
  122. package/dist/chunk-CMBFNA7L.js +0 -27
  123. package/dist/chunk-DAHRH4ON.js +0 -331
  124. package/dist/chunk-DKOHBI74.js +0 -924
  125. package/dist/chunk-DTCOOBMX.js +0 -725
  126. package/dist/chunk-EBGIRKQY.js +0 -616
  127. package/dist/chunk-EUZND3CB.js +0 -27
  128. package/dist/chunk-EVCZO745.js +0 -365
  129. package/dist/chunk-FGOEVHY3.js +0 -60
  130. package/dist/chunk-G3BOQPVO.js +0 -365
  131. package/dist/chunk-GCOK2LC3.js +0 -282
  132. package/dist/chunk-HGMJFBC7.js +0 -654
  133. package/dist/chunk-K5ZUMYVS.js +0 -89
  134. package/dist/chunk-KQPDEVVS.js +0 -398
  135. package/dist/chunk-L6JRBDNS.js +0 -60
  136. package/dist/chunk-LA6KQEDU.js +0 -712
  137. package/dist/chunk-MDVXJWFN.js +0 -304
  138. package/dist/chunk-MEZVEBPN.js +0 -2008
  139. package/dist/chunk-MK4ERFYL.js +0 -2249
  140. package/dist/chunk-MLKGABMK.js +0 -9
  141. package/dist/chunk-MQ5GOYPH.js +0 -2249
  142. package/dist/chunk-N6IZB6KJ.js +0 -567
  143. package/dist/chunk-NEKUBFPT.js +0 -60
  144. package/dist/chunk-NHUC2QWH.js +0 -282
  145. package/dist/chunk-NMRUZALC.js +0 -1097
  146. package/dist/chunk-NYVAC6P5.js +0 -37
  147. package/dist/chunk-OF7UZIVB.js +0 -725
  148. package/dist/chunk-P6W3STU4.js +0 -2249
  149. package/dist/chunk-PBHF5WKN.js +0 -616
  150. package/dist/chunk-PTQJDMRT.js +0 -146
  151. package/dist/chunk-PZEGYCF5.js +0 -61
  152. package/dist/chunk-QBMDLBU2.js +0 -975
  153. package/dist/chunk-RQGQSLQK.js +0 -725
  154. package/dist/chunk-SDLZDHKP.js +0 -107
  155. package/dist/chunk-TNQWPPE6.js +0 -37
  156. package/dist/chunk-UHNL42EF.js +0 -2730
  157. package/dist/chunk-UNXCEF6S.js +0 -21
  158. package/dist/chunk-V2XTI523.js +0 -347
  159. package/dist/chunk-VAU366PN.js +0 -2241
  160. package/dist/chunk-VMVDTCXB.js +0 -712
  161. package/dist/chunk-VRW3FULF.js +0 -725
  162. package/dist/chunk-WADYRCO2.js +0 -304
  163. package/dist/chunk-WILQZRO4.js +0 -282
  164. package/dist/chunk-WUHJISPP.js +0 -298
  165. package/dist/chunk-XYU6TZOW.js +0 -182
  166. package/dist/chunk-Y6GP4QGG.js +0 -276
  167. package/dist/chunk-YECR7UIA.js +0 -347
  168. package/dist/chunk-YUTWTI4B.js +0 -654
  169. package/dist/chunk-Z65KYU7I.js +0 -26
  170. package/dist/chunk-Z6POF5YC.js +0 -975
  171. package/dist/chunk-ZBJP6WFL.js +0 -482
  172. package/dist/contracts-DDrwxvJ-.d.cts +0 -245
  173. package/dist/contracts-DDrwxvJ-.d.ts +0 -245
  174. package/dist/contracts-DOrhwbke.d.cts +0 -245
  175. package/dist/contracts-DOrhwbke.d.ts +0 -245
  176. package/dist/customElement-BKQfbSZQ.d.cts +0 -262
  177. package/dist/customElement-BKQfbSZQ.d.ts +0 -262
  178. package/dist/customElement-yz8uyk-0.d.cts +0 -308
  179. package/dist/customElement-yz8uyk-0.d.ts +0 -308
  180. package/dist/introspect-Cb0zgpi2.d.cts +0 -477
  181. package/dist/introspect-Y2xNXGSf.d.ts +0 -477
  182. package/dist/plugin-Bek4RhJY.d.cts +0 -43
  183. package/dist/plugin-Bek4RhJY.d.ts +0 -43
  184. package/dist/ssr-3RXHP5ES.js +0 -38
  185. package/dist/ssr-6GIMY5MX.js +0 -38
  186. package/dist/ssr-BA6sxxUd.d.cts +0 -135
  187. package/dist/ssr-BA6sxxUd.d.ts +0 -135
  188. package/dist/ssr-WKUPVSSK.js +0 -36
  189. package/dist/tagFactory-Dl8QCLga.d.cts +0 -23
  190. package/dist/tagFactory-Dl8QCLga.d.ts +0 -23
@@ -1,298 +0,0 @@
1
- import {
2
- isDev
3
- } from "./chunk-UNXCEF6S.js";
4
-
5
- // src/platform/ssr.ts
6
- var _isDev = isDev();
7
- function ssrErrorComment(err) {
8
- if (_isDev) {
9
- return `<!--SSR error: ${escapeHtml(err instanceof Error ? err.message : String(err))}-->`;
10
- }
11
- return "<!--SSR error-->";
12
- }
13
- var VOID_ELEMENTS = /* @__PURE__ */ new Set([
14
- "area",
15
- "base",
16
- "br",
17
- "col",
18
- "embed",
19
- "hr",
20
- "img",
21
- "input",
22
- "link",
23
- "meta",
24
- "param",
25
- "source",
26
- "track",
27
- "wbr"
28
- ]);
29
- function renderToString(element) {
30
- if (element instanceof DocumentFragment) {
31
- return Array.from(element.childNodes).map((child) => {
32
- try {
33
- return renderToString(child);
34
- } catch (err) {
35
- return ssrErrorComment(err);
36
- }
37
- }).join("");
38
- }
39
- if (element.nodeType === 3) {
40
- return escapeHtml(element.textContent || "");
41
- }
42
- if (element.nodeType === 8) {
43
- const content = (element.textContent || "").replace(/-->/g, "--&gt;");
44
- return `<!--${content}-->`;
45
- }
46
- if (!(element instanceof HTMLElement)) {
47
- return element.textContent || "";
48
- }
49
- const tag = element.tagName.toLowerCase();
50
- let html = `<${tag}`;
51
- for (const attr of Array.from(element.attributes)) {
52
- html += ` ${attr.name}="${escapeAttr(attr.value)}"`;
53
- }
54
- if (element.dataset && !element.dataset.sibuHydrate) {
55
- html += ` data-sibu-ssr="true"`;
56
- }
57
- if (VOID_ELEMENTS.has(tag)) {
58
- return `${html} />`;
59
- }
60
- html += ">";
61
- for (const child of Array.from(element.childNodes)) {
62
- try {
63
- html += renderToString(child);
64
- } catch (err) {
65
- html += ssrErrorComment(err);
66
- }
67
- }
68
- html += `</${tag}>`;
69
- return html;
70
- }
71
- function hydrate(component, container) {
72
- const clientTree = component();
73
- hydrateNode(container.firstElementChild, clientTree);
74
- container.setAttribute("data-sibu-hydrated", "true");
75
- }
76
- function hydrateNode(serverNode, clientNode) {
77
- if (!serverNode) return;
78
- const serverChildren = Array.from(serverNode.children);
79
- const clientChildren = Array.from(clientNode.children);
80
- for (let i = 0; i < Math.min(serverChildren.length, clientChildren.length); i++) {
81
- hydrateNode(serverChildren[i], clientChildren[i]);
82
- }
83
- }
84
- function trustHTML(html) {
85
- return html;
86
- }
87
- function renderToDocument(component, options = {}) {
88
- let content;
89
- try {
90
- content = renderToString(component());
91
- } catch (err) {
92
- content = ssrErrorComment(err);
93
- }
94
- const metaTags = (options.meta || []).map(
95
- (attrs) => `<meta ${Object.entries(attrs).map(([k, v]) => `${k}="${escapeAttr(v)}"`).join(" ")} />`
96
- ).join("\n ");
97
- const linkTags = (options.links || []).map(
98
- (attrs) => `<link ${Object.entries(attrs).map(([k, v]) => `${k}="${escapeAttr(v)}"`).join(" ")} />`
99
- ).join("\n ");
100
- const scriptTags = (options.scripts || []).map((src) => `<script src="${escapeAttr(src)}"></script>`).join("\n ");
101
- const bodyAttrs = options.bodyAttrs ? " " + Object.entries(options.bodyAttrs).map(([k, v]) => `${k}="${escapeAttr(v)}"`).join(" ") : "";
102
- return `<!DOCTYPE html>
103
- <html>
104
- <head>
105
- <meta charset="UTF-8" />
106
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
107
- ${options.title ? `<title>${escapeHtml(options.title)}</title>` : ""}
108
- ${metaTags}
109
- ${linkTags}
110
- ${options.headExtra || ""}
111
- </head>
112
- <body${bodyAttrs}>
113
- <div id="app">${content}</div>
114
- ${scriptTags}
115
- </body>
116
- </html>`;
117
- }
118
- async function* renderToStream(element) {
119
- if (element instanceof DocumentFragment) {
120
- for (const child of Array.from(element.childNodes)) {
121
- try {
122
- yield* renderToStream(child);
123
- } catch (err) {
124
- yield ssrErrorComment(err);
125
- }
126
- }
127
- return;
128
- }
129
- if (element.nodeType === 3) {
130
- yield escapeHtml(element.textContent || "");
131
- return;
132
- }
133
- if (element.nodeType === 8) {
134
- const content = (element.textContent || "").replace(/-->/g, "--&gt;");
135
- yield `<!--${content}-->`;
136
- return;
137
- }
138
- if (!(element instanceof HTMLElement)) {
139
- yield element.textContent || "";
140
- return;
141
- }
142
- const tag = element.tagName.toLowerCase();
143
- let openTag = `<${tag}`;
144
- for (const attr of Array.from(element.attributes)) {
145
- openTag += ` ${attr.name}="${escapeAttr(attr.value)}"`;
146
- }
147
- if (VOID_ELEMENTS.has(tag)) {
148
- yield `${openTag} />`;
149
- return;
150
- }
151
- yield `${openTag}>`;
152
- for (const child of Array.from(element.childNodes)) {
153
- try {
154
- yield* renderToStream(child);
155
- } catch (err) {
156
- yield ssrErrorComment(err);
157
- }
158
- }
159
- yield `</${tag}>`;
160
- }
161
- async function collectStream(stream) {
162
- let result = "";
163
- for await (const chunk of stream) {
164
- result += chunk;
165
- }
166
- return result;
167
- }
168
- function renderToReadableStream(element) {
169
- const generator = renderToStream(element);
170
- return new ReadableStream({
171
- async pull(controller) {
172
- const { value, done } = await generator.next();
173
- if (done) {
174
- controller.close();
175
- } else {
176
- controller.enqueue(value);
177
- }
178
- },
179
- cancel() {
180
- generator.return(void 0);
181
- }
182
- });
183
- }
184
- function island(id, component) {
185
- const el = component();
186
- el.setAttribute("data-sibu-island", id);
187
- return el;
188
- }
189
- function hydrateIslands(container, islands) {
190
- const markers = container.querySelectorAll("[data-sibu-island]");
191
- for (const marker of Array.from(markers)) {
192
- const id = marker.getAttribute("data-sibu-island") ?? "";
193
- const factory = islands[id];
194
- if (!factory) continue;
195
- const clientTree = factory();
196
- hydrateNode(marker, clientTree);
197
- marker.setAttribute("data-sibu-hydrated", "true");
198
- }
199
- container.setAttribute("data-sibu-hydrated", "partial");
200
- }
201
- function hydrateProgressively(container, islands, options) {
202
- const markers = container.querySelectorAll("[data-sibu-island]");
203
- const cleanups = [];
204
- for (const marker of Array.from(markers)) {
205
- const id = marker.getAttribute("data-sibu-island") ?? "";
206
- const factory = islands[id];
207
- if (!factory) continue;
208
- const observer = new IntersectionObserver(
209
- (entries) => {
210
- for (const entry of entries) {
211
- if (entry.isIntersecting) {
212
- const clientTree = factory();
213
- hydrateNode(marker, clientTree);
214
- marker.setAttribute("data-sibu-hydrated", "true");
215
- observer.disconnect();
216
- break;
217
- }
218
- }
219
- },
220
- { rootMargin: "200px", ...options }
221
- );
222
- observer.observe(marker);
223
- cleanups.push(() => observer.disconnect());
224
- }
225
- container.setAttribute("data-sibu-hydrated", "progressive");
226
- return () => {
227
- for (const cleanup of cleanups) cleanup();
228
- };
229
- }
230
- var suspenseIdCounter = 0;
231
- function resetSSRState() {
232
- suspenseIdCounter = 0;
233
- }
234
- function ssrSuspense(props) {
235
- const id = `sibu-sus-${suspenseIdCounter++}`;
236
- const fallbackEl = props.fallback();
237
- const wrapper = document.createElement("div");
238
- wrapper.setAttribute("data-sibu-suspense-id", id);
239
- wrapper.appendChild(fallbackEl);
240
- const promise = props.content().then((resolvedEl) => ({
241
- id,
242
- html: renderToString(resolvedEl)
243
- }));
244
- return { element: wrapper, promise };
245
- }
246
- function suspenseSwapScript(id, nonce) {
247
- const safeId = id.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/</g, "\\u003c").replace(/>/g, "\\u003e");
248
- const nonceAttr = nonce ? ` nonce="${escapeAttr(nonce)}"` : "";
249
- return `<script${nonceAttr}>(function(){var t=document.getElementById("sibu-resolved-${safeId}");var f=document.querySelector('[data-sibu-suspense-id="${safeId}"]');if(t&&f){while(t.firstChild)f.appendChild(t.firstChild);t.remove();f.removeAttribute("data-sibu-suspense-id");}})()</script>`;
250
- }
251
- async function* renderToSuspenseStream(element, pendingBoundaries = []) {
252
- yield* renderToStream(element);
253
- if (pendingBoundaries.length > 0) {
254
- const resolved = await Promise.all(pendingBoundaries);
255
- for (const { id, html } of resolved) {
256
- yield `<div hidden id="sibu-resolved-${escapeAttr(id)}">${html}</div>`;
257
- yield suspenseSwapScript(id);
258
- }
259
- }
260
- }
261
- var SSR_DATA_ATTR = "__SIBU_SSR_DATA__";
262
- function serializeState(state, nonce) {
263
- const json = JSON.stringify(state).replace(/</g, "\\u003c").replace(/>/g, "\\u003e").replace(/&/g, "\\u0026");
264
- const nonceAttr = nonce ? ` nonce="${escapeAttr(nonce)}"` : "";
265
- return `<script${nonceAttr}>window.${SSR_DATA_ATTR}=${json}</script>`;
266
- }
267
- function deserializeState(validate) {
268
- if (typeof window === "undefined") return void 0;
269
- const raw = window[SSR_DATA_ATTR];
270
- if (raw === void 0) return void 0;
271
- if (validate && !validate(raw)) return void 0;
272
- return raw;
273
- }
274
- function escapeHtml(str) {
275
- return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
276
- }
277
- function escapeAttr(str) {
278
- return str.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
279
- }
280
-
281
- export {
282
- renderToString,
283
- hydrate,
284
- trustHTML,
285
- renderToDocument,
286
- renderToStream,
287
- collectStream,
288
- renderToReadableStream,
289
- island,
290
- hydrateIslands,
291
- hydrateProgressively,
292
- resetSSRState,
293
- ssrSuspense,
294
- suspenseSwapScript,
295
- renderToSuspenseStream,
296
- serializeState,
297
- deserializeState
298
- };
@@ -1,182 +0,0 @@
1
- // src/patterns/hoc.ts
2
- function withWrapper(WrappedComponent, wrapper) {
3
- return (props) => wrapper(WrappedComponent, props);
4
- }
5
- function withDefaults(component, defaults) {
6
- return (props) => component({ ...defaults, ...props });
7
- }
8
- function compose(...wrappers) {
9
- return (component) => wrappers.reduceRight((comp, wrapper) => wrapper(comp), component);
10
- }
11
-
12
- // src/patterns/composable.ts
13
- function composable(setup) {
14
- return setup;
15
- }
16
- function RenderProp(props) {
17
- return props.render(props.data());
18
- }
19
- function withBoundary(name, component) {
20
- return (props) => {
21
- const wrapper = document.createElement("div");
22
- wrapper.setAttribute("data-sibu-boundary", name);
23
- try {
24
- const el = component(props);
25
- wrapper.appendChild(el);
26
- } catch (error) {
27
- const errorEl = document.createElement("div");
28
- errorEl.setAttribute("data-sibu-boundary-error", name);
29
- errorEl.style.cssText = "color: red; border: 1px solid red; padding: 8px; margin: 4px;";
30
- errorEl.textContent = `[${name}] ${error instanceof Error ? error.message : String(error)}`;
31
- wrapper.appendChild(errorEl);
32
- }
33
- return wrapper;
34
- };
35
- }
36
- function createSlots(slots) {
37
- return {
38
- renderSlot(name, fallback) {
39
- const slotFn = slots[name];
40
- if (slotFn) {
41
- const result = slotFn();
42
- if (Array.isArray(result)) {
43
- const fragment = document.createElement("div");
44
- fragment.style.display = "contents";
45
- for (const el of result) fragment.appendChild(el);
46
- return fragment;
47
- }
48
- return result;
49
- }
50
- return fallback ? fallback() : null;
51
- },
52
- hasSlot(name) {
53
- return name in slots;
54
- }
55
- };
56
- }
57
-
58
- // src/patterns/componentProps.ts
59
- function defineComponent(config) {
60
- const { defaults, setup } = config;
61
- return (props) => {
62
- const merged = defaults ? { ...defaults, ...props } : props;
63
- return setup(merged);
64
- };
65
- }
66
- function defineSlottedComponent(config) {
67
- const { defaults, setup } = config;
68
- return (props) => {
69
- const merged = defaults ? { ...defaults, ...props } : props;
70
- return setup(merged);
71
- };
72
- }
73
- function withProps(component, mapProps) {
74
- return (props) => component(mapProps(props));
75
- }
76
-
77
- // src/patterns/contracts.ts
78
- var validators = {
79
- string: ((value, name) => typeof value === "string" || `${name} must be a string, got ${typeof value}`),
80
- number: ((value, name) => typeof value === "number" || `${name} must be a number, got ${typeof value}`),
81
- boolean: ((value, name) => typeof value === "boolean" || `${name} must be a boolean, got ${typeof value}`),
82
- function: ((value, name) => typeof value === "function" || `${name} must be a function, got ${typeof value}`),
83
- object: ((value, name) => typeof value === "object" && value !== null || `${name} must be an object`),
84
- array: ((value, name) => Array.isArray(value) || `${name} must be an array`),
85
- required: ((value, name) => value != null || `${name} is required`),
86
- oneOf: (...values) => (value, name) => values.includes(value) || `${name} must be one of: ${values.join(", ")}`,
87
- instanceOf: (ctor) => (value, name) => value instanceof ctor || `${name} must be an instance of ${ctor.name}`,
88
- arrayOf: (itemValidator) => (value, name) => {
89
- if (!Array.isArray(value)) return `${name} must be an array`;
90
- for (let i = 0; i < value.length; i++) {
91
- const result = itemValidator(value[i], `${name}[${i}]`);
92
- if (result !== true) return result;
93
- }
94
- return true;
95
- },
96
- shape: (schema) => (value, name) => {
97
- if (typeof value !== "object" || value === null) return `${name} must be an object`;
98
- for (const [key, validator] of Object.entries(schema)) {
99
- const result = validator(value[key], `${name}.${key}`);
100
- if (result !== true) return result;
101
- }
102
- return true;
103
- },
104
- optional: (validator) => (value, name) => {
105
- if (value == null) return true;
106
- return validator(value, name);
107
- },
108
- range: (min, max) => (value, name) => {
109
- if (typeof value !== "number") return `${name} must be a number`;
110
- return value >= min && value <= max || `${name} must be between ${min} and ${max}`;
111
- },
112
- pattern: (regex) => (value, name) => {
113
- if (typeof value !== "string") return `${name} must be a string`;
114
- return regex.test(value) || `${name} must match pattern ${regex}`;
115
- }
116
- };
117
- function validateProps(props, schema) {
118
- const result = { ...props };
119
- const errors = [];
120
- const isDev = typeof process === "undefined" || process?.env?.NODE_ENV !== "production";
121
- for (const [key, def] of Object.entries(schema)) {
122
- const propDef = typeof def === "function" ? { type: def } : def;
123
- if (result[key] == null && propDef.default !== void 0) {
124
- result[key] = typeof propDef.default === "function" ? propDef.default() : propDef.default;
125
- }
126
- if (!isDev) continue;
127
- if (propDef.required && result[key] == null) {
128
- errors.push(`Prop '${key}' is required`);
129
- continue;
130
- }
131
- if (result[key] == null) continue;
132
- if (propDef.type) {
133
- const typeResult = propDef.type(result[key], key);
134
- if (typeResult !== true) errors.push(typeResult);
135
- }
136
- if (propDef.validator) {
137
- const validResult = propDef.validator(result[key], key);
138
- if (validResult !== true) errors.push(validResult);
139
- }
140
- }
141
- if (errors.length > 0 && isDev) {
142
- console.warn(`[SibuJS] Prop validation errors:
143
- ${errors.map((e) => ` - ${e}`).join("\n")}`);
144
- }
145
- return result;
146
- }
147
- function defineStrictComponent(config) {
148
- return (props) => {
149
- const validated = validateProps(props, config.props);
150
- return config.setup(validated);
151
- };
152
- }
153
- function assertType(value, validator, label) {
154
- if (typeof process !== "undefined" && process?.env?.NODE_ENV === "production") return;
155
- const result = validator(value, label || "value");
156
- if (result !== true) {
157
- throw new TypeError(`[SibuJS Contract] ${result}`);
158
- }
159
- }
160
- function createGuard(validator) {
161
- return (value) => {
162
- return validator(value, "value") === true;
163
- };
164
- }
165
-
166
- export {
167
- withWrapper,
168
- withDefaults,
169
- compose,
170
- composable,
171
- RenderProp,
172
- withBoundary,
173
- createSlots,
174
- defineComponent,
175
- defineSlottedComponent,
176
- withProps,
177
- validators,
178
- validateProps,
179
- defineStrictComponent,
180
- assertType,
181
- createGuard
182
- };