hadars 0.1.17 → 0.1.19

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.
@@ -0,0 +1,225 @@
1
+ /**
2
+ * slim-react – a lightweight, SSR-only React-compatible runtime.
3
+ *
4
+ * Provides just enough of the React API surface to server-render
5
+ * components that use hooks, Context and Suspense.
6
+ */
7
+
8
+ // ---- Symbols & types ----
9
+ export {
10
+ SLIM_ELEMENT,
11
+ FRAGMENT_TYPE,
12
+ SUSPENSE_TYPE,
13
+ type SlimElement,
14
+ type SlimNode,
15
+ type ComponentFunction,
16
+ } from "./types";
17
+
18
+ // ---- JSX runtime ----
19
+ import { jsx, jsxs, jsxDEV, createElement, Fragment } from "./jsx";
20
+ export { jsx, jsxs, jsxDEV, createElement, Fragment };
21
+
22
+ // ---- Hooks (SSR stubs) ----
23
+ import {
24
+ useState,
25
+ useReducer,
26
+ useEffect,
27
+ useLayoutEffect,
28
+ useInsertionEffect,
29
+ useRef,
30
+ useMemo,
31
+ useCallback,
32
+ useId,
33
+ useDebugValue,
34
+ useImperativeHandle,
35
+ useSyncExternalStore,
36
+ useTransition,
37
+ useDeferredValue,
38
+ useOptimistic,
39
+ useFormStatus,
40
+ useActionState,
41
+ use,
42
+ startTransition,
43
+ } from "./hooks";
44
+ export {
45
+ useState,
46
+ useReducer,
47
+ useEffect,
48
+ useLayoutEffect,
49
+ useInsertionEffect,
50
+ useRef,
51
+ useMemo,
52
+ useCallback,
53
+ useId,
54
+ useDebugValue,
55
+ useImperativeHandle,
56
+ useSyncExternalStore,
57
+ useTransition,
58
+ useDeferredValue,
59
+ useOptimistic,
60
+ useFormStatus,
61
+ useActionState,
62
+ use,
63
+ startTransition,
64
+ };
65
+
66
+ // ---- Context ----
67
+ import { createContext } from "./context";
68
+ export { createContext, type Context } from "./context";
69
+
70
+ // Re-export useContext from hooks.ts? No – we have it in context.ts
71
+ // Actually useContext reads from the context object, let's export a
72
+ // single implementation that lives close to createContext:
73
+ import type { Context } from "./context";
74
+ export function useContext<T>(context: Context<T>): T {
75
+ return context._currentValue;
76
+ }
77
+
78
+ // ---- Rendering ----
79
+ import { renderToStream, renderToString, renderToReadableStream } from "./render";
80
+ export { renderToStream, renderToString, renderToReadableStream };
81
+
82
+ // ---- Suspense (as a JSX tag) ----
83
+ import { SUSPENSE_TYPE } from "./types";
84
+ export const Suspense = SUSPENSE_TYPE;
85
+
86
+ // ---- Compat utilities ----
87
+ import { SLIM_ELEMENT, type SlimElement, type SlimNode } from "./types";
88
+
89
+ export function isValidElement(obj: unknown): obj is SlimElement {
90
+ return (
91
+ typeof obj === "object" &&
92
+ obj !== null &&
93
+ (obj as any).$$typeof === SLIM_ELEMENT
94
+ );
95
+ }
96
+
97
+ export function cloneElement(
98
+ element: SlimElement,
99
+ overrideProps?: Record<string, any>,
100
+ ...children: SlimNode[]
101
+ ): SlimElement {
102
+ return {
103
+ $$typeof: SLIM_ELEMENT,
104
+ type: element.type,
105
+ props: {
106
+ ...element.props,
107
+ ...overrideProps,
108
+ ...(children.length === 1
109
+ ? { children: children[0] }
110
+ : children.length > 1
111
+ ? { children }
112
+ : {}),
113
+ },
114
+ key: overrideProps?.key ?? element.key,
115
+ };
116
+ }
117
+
118
+ export function forwardRef<P = any>(
119
+ render: (props: P, ref: any) => SlimNode,
120
+ ): (props: P & { ref?: any }) => SlimNode {
121
+ const component = (props: P & { ref?: any }) =>
122
+ render(props, (props as any).ref ?? null);
123
+ (component as any).displayName =
124
+ (render as any).displayName || (render as any).name || "ForwardRef";
125
+ return component;
126
+ }
127
+
128
+ export function memo<P = any>(
129
+ component: (props: P) => SlimNode,
130
+ ): (props: P) => SlimNode {
131
+ return component; // no memoisation needed for SSR
132
+ }
133
+
134
+ export function lazy<P = any>(
135
+ factory: () => Promise<{ default: (props: P) => SlimNode }>,
136
+ ): (props: P) => SlimNode {
137
+ let resolved: ((props: P) => SlimNode) | null = null;
138
+ let promise: Promise<void> | null = null;
139
+
140
+ return function LazyComponent(props: P): SlimNode {
141
+ if (resolved) return resolved(props);
142
+ if (!promise) {
143
+ promise = factory().then((mod) => {
144
+ resolved = mod.default;
145
+ });
146
+ }
147
+ throw promise; // Suspense protocol
148
+ };
149
+ }
150
+
151
+ // ---- Children helpers ----
152
+ function toFlatArray(children: SlimNode): SlimNode[] {
153
+ if (children == null || typeof children === "boolean") return [];
154
+ if (Array.isArray(children)) return children.flatMap(toFlatArray);
155
+ return [children];
156
+ }
157
+
158
+ export const Children = {
159
+ map(
160
+ children: SlimNode,
161
+ fn: (child: SlimNode, index: number) => SlimNode,
162
+ ): SlimNode[] {
163
+ return toFlatArray(children).map((child, i) => fn(child, i));
164
+ },
165
+ forEach(
166
+ children: SlimNode,
167
+ fn: (child: SlimNode, index: number) => void,
168
+ ): void {
169
+ toFlatArray(children).forEach((child, i) => fn(child, i));
170
+ },
171
+ count(children: SlimNode): number {
172
+ return toFlatArray(children).length;
173
+ },
174
+ only(children: SlimNode): SlimElement {
175
+ const arr = toFlatArray(children);
176
+ if (arr.length !== 1) throw new Error("Children.only expected one child");
177
+ return arr[0] as SlimElement;
178
+ },
179
+ toArray: toFlatArray,
180
+ };
181
+
182
+ // ---- React.Component (basic class component support) ----
183
+ export class Component<P = {}, S = {}> {
184
+ props: P;
185
+ state: S;
186
+ context: any;
187
+
188
+ constructor(props: P) {
189
+ this.props = props;
190
+ this.state = {} as S;
191
+ }
192
+
193
+ setState(_partial: Partial<S> | ((prev: S) => Partial<S>)) {}
194
+ forceUpdate() {}
195
+ render(): SlimNode {
196
+ return null;
197
+ }
198
+ }
199
+
200
+ export class PureComponent<P = {}, S = {}> extends Component<P, S> {}
201
+
202
+ // ---- Default export ----
203
+ // Mirrors `import React from 'react'` so code that uses React.useState,
204
+ // React.createContext, React.Suspense, etc. works without changes.
205
+ // All names here are already imported/defined above — no re-imports needed.
206
+ const React = {
207
+ // Hooks
208
+ useState, useReducer, useEffect, useLayoutEffect, useInsertionEffect,
209
+ useRef, useMemo, useCallback, useId, useDebugValue, useImperativeHandle,
210
+ useSyncExternalStore, useTransition, useDeferredValue,
211
+ useOptimistic, useFormStatus, useActionState, use, startTransition,
212
+ // Context
213
+ createContext, useContext,
214
+ // Elements
215
+ createElement, cloneElement, isValidElement, forwardRef, memo, lazy,
216
+ Fragment, Suspense,
217
+ // Compat
218
+ Children, Component, PureComponent,
219
+ // Rendering
220
+ renderToStream, renderToString, renderToReadableStream,
221
+ // Version stub
222
+ version: "19.0.0-slim",
223
+ };
224
+
225
+ export default React;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * JSX automatic-transform runtime for slim-react.
3
+ *
4
+ * Aliased from `react/jsx-runtime` in SSR rspack builds so that the
5
+ * automatic JSX transform produces slim-react elements instead of React ones.
6
+ */
7
+ export { jsx, jsxs, jsxDEV, Fragment } from "./jsx";
@@ -0,0 +1,53 @@
1
+ import {
2
+ SLIM_ELEMENT,
3
+ FRAGMENT_TYPE,
4
+ type SlimElement,
5
+ type SlimNode,
6
+ type ComponentFunction,
7
+ } from "./types";
8
+
9
+ // ---- Fragment ----
10
+ export const Fragment = FRAGMENT_TYPE;
11
+
12
+ // ---- jsx / jsxs (automatic transform) ----
13
+ // The automatic JSX transform calls jsx(type, props, key?)
14
+ // where props already contains `children`.
15
+ export function jsx(
16
+ type: string | ComponentFunction | symbol,
17
+ props: Record<string, any>,
18
+ key?: string | number | null,
19
+ ): SlimElement {
20
+ return {
21
+ $$typeof: SLIM_ELEMENT,
22
+ type,
23
+ props: props || {},
24
+ key: key ?? (props?.key ?? null),
25
+ };
26
+ }
27
+
28
+ export { jsx as jsxs, jsx as jsxDEV };
29
+
30
+ // ---- createElement (classic transform) ----
31
+ export function createElement(
32
+ type: string | ComponentFunction | symbol,
33
+ props?: Record<string, any> | null,
34
+ ...children: SlimNode[]
35
+ ): SlimElement {
36
+ const normalizedProps: Record<string, any> = { ...(props || {}) };
37
+
38
+ if (children.length === 1) {
39
+ normalizedProps.children = children[0];
40
+ } else if (children.length > 1) {
41
+ normalizedProps.children = children;
42
+ }
43
+
44
+ const key = normalizedProps.key ?? null;
45
+ delete normalizedProps.key;
46
+
47
+ return {
48
+ $$typeof: SLIM_ELEMENT,
49
+ type,
50
+ props: normalizedProps,
51
+ key,
52
+ };
53
+ }