hadars 0.1.24 → 0.1.26

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.
@@ -7,44 +7,36 @@ import {
7
7
  createElement,
8
8
  jsx
9
9
  } from "../chunk-OS3V4CPN.js";
10
- import {
11
- __require
12
- } from "../chunk-EZUCZHGV.js";
13
10
 
14
11
  // src/slim-react/renderContext.ts
15
- var CONTEXT_STORE_KEY = "__slimReactContextStore";
12
+ var MAP_KEY = "__slimReactContextMap";
16
13
  var _g = globalThis;
17
- if (!_g[CONTEXT_STORE_KEY]) {
18
- try {
19
- const { AsyncLocalStorage } = __require("node:async_hooks");
20
- _g[CONTEXT_STORE_KEY] = new AsyncLocalStorage();
21
- } catch {
22
- _g[CONTEXT_STORE_KEY] = null;
23
- }
14
+ if (!("__slimReactContextMap" in _g))
15
+ _g[MAP_KEY] = null;
16
+ function swapContextMap(map) {
17
+ const prev = _g[MAP_KEY];
18
+ _g[MAP_KEY] = map;
19
+ return prev;
24
20
  }
25
- var _contextStore = _g[CONTEXT_STORE_KEY];
26
- function runWithContextStore(fn) {
27
- return _contextStore ? _contextStore.run(/* @__PURE__ */ new Map(), fn) : fn();
21
+ function captureMap() {
22
+ return _g[MAP_KEY];
28
23
  }
29
24
  function getContextValue(context) {
30
- const store = _contextStore?.getStore();
31
- if (store && store.has(context))
32
- return store.get(context);
25
+ const map = _g[MAP_KEY];
26
+ if (map && map.has(context))
27
+ return map.get(context);
33
28
  const c = context;
34
29
  return "_defaultValue" in c ? c._defaultValue : c._currentValue;
35
30
  }
36
31
  function pushContextValue(context, value) {
37
- const store = _contextStore?.getStore();
32
+ const map = _g[MAP_KEY];
38
33
  const c = context;
39
- const prev = store && store.has(context) ? store.get(context) : "_defaultValue" in c ? c._defaultValue : c._currentValue;
40
- if (store)
41
- store.set(context, value);
34
+ const prev = map && map.has(context) ? map.get(context) : "_defaultValue" in c ? c._defaultValue : c._currentValue;
35
+ map?.set(context, value);
42
36
  return prev;
43
37
  }
44
38
  function popContextValue(context, prev) {
45
- const store = _contextStore?.getStore();
46
- if (store)
47
- store.set(context, prev);
39
+ _g[MAP_KEY]?.set(context, prev);
48
40
  }
49
41
  var GLOBAL_KEY = "__slimReactRenderState";
50
42
  var EMPTY = { id: 0, overflow: "", bits: 0 };
@@ -590,10 +582,18 @@ function renderComponent(type, props, writer, isSvg) {
590
582
  };
591
583
  const r2 = renderChildren(props.children, writer, isSvg);
592
584
  if (r2 && typeof r2.then === "function") {
593
- return r2.then(finish2, (e) => {
594
- finish2();
595
- throw e;
596
- });
585
+ const m = captureMap();
586
+ return r2.then(
587
+ () => {
588
+ swapContextMap(m);
589
+ finish2();
590
+ },
591
+ (e) => {
592
+ swapContextMap(m);
593
+ finish2();
594
+ throw e;
595
+ }
596
+ );
597
597
  }
598
598
  finish2();
599
599
  return;
@@ -623,26 +623,45 @@ function renderComponent(type, props, writer, isSvg) {
623
623
  popContextValue(ctx, prevCtxValue);
624
624
  };
625
625
  if (result instanceof Promise) {
626
+ const m = captureMap();
626
627
  return result.then((resolved) => {
628
+ swapContextMap(m);
627
629
  const r2 = renderNode(resolved, writer, isSvg);
628
630
  if (r2 && typeof r2.then === "function") {
629
- return r2.then(finish, (e) => {
630
- finish();
631
- throw e;
632
- });
631
+ const m2 = captureMap();
632
+ return r2.then(
633
+ () => {
634
+ swapContextMap(m2);
635
+ finish();
636
+ },
637
+ (e) => {
638
+ swapContextMap(m2);
639
+ finish();
640
+ throw e;
641
+ }
642
+ );
633
643
  }
634
644
  finish();
635
645
  }, (e) => {
646
+ swapContextMap(m);
636
647
  finish();
637
648
  throw e;
638
649
  });
639
650
  }
640
651
  const r = renderNode(result, writer, isSvg);
641
652
  if (r && typeof r.then === "function") {
642
- return r.then(finish, (e) => {
643
- finish();
644
- throw e;
645
- });
653
+ const m = captureMap();
654
+ return r.then(
655
+ () => {
656
+ swapContextMap(m);
657
+ finish();
658
+ },
659
+ (e) => {
660
+ swapContextMap(m);
661
+ finish();
662
+ throw e;
663
+ }
664
+ );
646
665
  }
647
666
  finish();
648
667
  }
@@ -658,7 +677,9 @@ function renderChildArray(children, writer, isSvg) {
658
677
  const savedTree = pushTreeContext(totalChildren, i);
659
678
  const r = renderNode(children[i], writer, isSvg);
660
679
  if (r && typeof r.then === "function") {
680
+ const m = captureMap();
661
681
  return r.then(() => {
682
+ swapContextMap(m);
662
683
  popTreeContext(savedTree);
663
684
  return renderChildArrayFrom(children, i + 1, writer, isSvg);
664
685
  });
@@ -675,7 +696,9 @@ function renderChildArrayFrom(children, startIndex, writer, isSvg) {
675
696
  const savedTree = pushTreeContext(totalChildren, i);
676
697
  const r = renderNode(children[i], writer, isSvg);
677
698
  if (r && typeof r.then === "function") {
699
+ const m = captureMap();
678
700
  return r.then(() => {
701
+ swapContextMap(m);
679
702
  popTreeContext(savedTree);
680
703
  return renderChildArrayFrom(children, i + 1, writer, isSvg);
681
704
  });
@@ -702,7 +725,9 @@ async function renderSuspense(props, writer, isSvg = false) {
702
725
  const buffer = new BufferWriter();
703
726
  const r = renderNode(children, buffer, isSvg);
704
727
  if (r && typeof r.then === "function") {
728
+ const m = captureMap();
705
729
  await r;
730
+ swapContextMap(m);
706
731
  }
707
732
  writer.write("<!--$-->");
708
733
  buffer.flush(writer);
@@ -710,7 +735,9 @@ async function renderSuspense(props, writer, isSvg = false) {
710
735
  return;
711
736
  } catch (error) {
712
737
  if (error && typeof error.then === "function") {
738
+ const m = captureMap();
713
739
  await error;
740
+ swapContextMap(m);
714
741
  attempts++;
715
742
  } else {
716
743
  throw error;
@@ -721,16 +748,21 @@ async function renderSuspense(props, writer, isSvg = false) {
721
748
  writer.write("<!--$?-->");
722
749
  if (fallback) {
723
750
  const r = renderNode(fallback, writer, isSvg);
724
- if (r && typeof r.then === "function")
751
+ if (r && typeof r.then === "function") {
752
+ const m = captureMap();
725
753
  await r;
754
+ swapContextMap(m);
755
+ }
726
756
  }
727
757
  writer.write("<!--/$-->");
728
758
  }
729
759
  function renderToStream(element) {
730
760
  const encoder = new TextEncoder();
731
- return runWithContextStore(() => new ReadableStream({
761
+ const contextMap = /* @__PURE__ */ new Map();
762
+ return new ReadableStream({
732
763
  async start(controller) {
733
764
  resetRenderState();
765
+ const prev = swapContextMap(contextMap);
734
766
  const writer = {
735
767
  lastWasText: false,
736
768
  write(chunk) {
@@ -744,19 +776,27 @@ function renderToStream(element) {
744
776
  };
745
777
  try {
746
778
  const r = renderNode(element, writer);
747
- if (r && typeof r.then === "function")
779
+ if (r && typeof r.then === "function") {
780
+ const m = captureMap();
748
781
  await r;
782
+ swapContextMap(m);
783
+ }
749
784
  controller.close();
750
785
  } catch (error) {
751
786
  controller.error(error);
787
+ } finally {
788
+ swapContextMap(prev);
752
789
  }
753
790
  }
754
- }));
791
+ });
755
792
  }
756
- function renderToString(element) {
757
- return runWithContextStore(async () => {
793
+ async function renderToString(element) {
794
+ const contextMap = /* @__PURE__ */ new Map();
795
+ const prev = swapContextMap(contextMap);
796
+ try {
758
797
  for (let attempt = 0; attempt < MAX_SUSPENSE_RETRIES; attempt++) {
759
798
  resetRenderState();
799
+ swapContextMap(contextMap);
760
800
  const chunks = [];
761
801
  const writer = {
762
802
  lastWasText: false,
@@ -771,19 +811,26 @@ function renderToString(element) {
771
811
  };
772
812
  try {
773
813
  const r = renderNode(element, writer);
774
- if (r && typeof r.then === "function")
814
+ if (r && typeof r.then === "function") {
815
+ const m = captureMap();
775
816
  await r;
817
+ swapContextMap(m);
818
+ }
776
819
  return chunks.join("");
777
820
  } catch (error) {
778
821
  if (error && typeof error.then === "function") {
822
+ const m = captureMap();
779
823
  await error;
824
+ swapContextMap(m);
780
825
  continue;
781
826
  }
782
827
  throw error;
783
828
  }
784
829
  }
785
830
  throw new Error("[slim-react] renderToString exceeded maximum retries");
786
- });
831
+ } finally {
832
+ swapContextMap(prev);
833
+ }
787
834
  }
788
835
 
789
836
  // src/slim-react/index.ts
@@ -2,7 +2,6 @@ import {
2
2
  Fragment,
3
3
  jsx
4
4
  } from "../chunk-OS3V4CPN.js";
5
- import "../chunk-EZUCZHGV.js";
6
5
  export {
7
6
  Fragment,
8
7
  jsx,
@@ -1,11 +1,3 @@
1
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
- }) : x)(function(x) {
4
- if (typeof require !== "undefined")
5
- return require.apply(this, arguments);
6
- throw Error('Dynamic require of "' + x + '" is not supported');
7
- });
8
-
9
1
  // src/ssr-render-worker.ts
10
2
  import { workerData, parentPort } from "node:worker_threads";
11
3
  import { pathToFileURL } from "node:url";
@@ -75,39 +67,34 @@ function createElement(type, props, ...children) {
75
67
  }
76
68
 
77
69
  // src/slim-react/renderContext.ts
78
- var CONTEXT_STORE_KEY = "__slimReactContextStore";
70
+ var MAP_KEY = "__slimReactContextMap";
79
71
  var _g = globalThis;
80
- if (!_g[CONTEXT_STORE_KEY]) {
81
- try {
82
- const { AsyncLocalStorage } = __require("node:async_hooks");
83
- _g[CONTEXT_STORE_KEY] = new AsyncLocalStorage();
84
- } catch {
85
- _g[CONTEXT_STORE_KEY] = null;
86
- }
72
+ if (!("__slimReactContextMap" in _g))
73
+ _g[MAP_KEY] = null;
74
+ function swapContextMap(map) {
75
+ const prev = _g[MAP_KEY];
76
+ _g[MAP_KEY] = map;
77
+ return prev;
87
78
  }
88
- var _contextStore = _g[CONTEXT_STORE_KEY];
89
- function runWithContextStore(fn) {
90
- return _contextStore ? _contextStore.run(/* @__PURE__ */ new Map(), fn) : fn();
79
+ function captureMap() {
80
+ return _g[MAP_KEY];
91
81
  }
92
82
  function getContextValue(context) {
93
- const store = _contextStore?.getStore();
94
- if (store && store.has(context))
95
- return store.get(context);
83
+ const map = _g[MAP_KEY];
84
+ if (map && map.has(context))
85
+ return map.get(context);
96
86
  const c = context;
97
87
  return "_defaultValue" in c ? c._defaultValue : c._currentValue;
98
88
  }
99
89
  function pushContextValue(context, value) {
100
- const store = _contextStore?.getStore();
90
+ const map = _g[MAP_KEY];
101
91
  const c = context;
102
- const prev = store && store.has(context) ? store.get(context) : "_defaultValue" in c ? c._defaultValue : c._currentValue;
103
- if (store)
104
- store.set(context, value);
92
+ const prev = map && map.has(context) ? map.get(context) : "_defaultValue" in c ? c._defaultValue : c._currentValue;
93
+ map?.set(context, value);
105
94
  return prev;
106
95
  }
107
96
  function popContextValue(context, prev) {
108
- const store = _contextStore?.getStore();
109
- if (store)
110
- store.set(context, prev);
97
+ _g[MAP_KEY]?.set(context, prev);
111
98
  }
112
99
  var GLOBAL_KEY = "__slimReactRenderState";
113
100
  var EMPTY = { id: 0, overflow: "", bits: 0 };
@@ -547,10 +534,18 @@ function renderComponent(type, props, writer, isSvg) {
547
534
  };
548
535
  const r2 = renderChildren(props.children, writer, isSvg);
549
536
  if (r2 && typeof r2.then === "function") {
550
- return r2.then(finish2, (e) => {
551
- finish2();
552
- throw e;
553
- });
537
+ const m = captureMap();
538
+ return r2.then(
539
+ () => {
540
+ swapContextMap(m);
541
+ finish2();
542
+ },
543
+ (e) => {
544
+ swapContextMap(m);
545
+ finish2();
546
+ throw e;
547
+ }
548
+ );
554
549
  }
555
550
  finish2();
556
551
  return;
@@ -580,26 +575,45 @@ function renderComponent(type, props, writer, isSvg) {
580
575
  popContextValue(ctx, prevCtxValue);
581
576
  };
582
577
  if (result instanceof Promise) {
578
+ const m = captureMap();
583
579
  return result.then((resolved) => {
580
+ swapContextMap(m);
584
581
  const r2 = renderNode(resolved, writer, isSvg);
585
582
  if (r2 && typeof r2.then === "function") {
586
- return r2.then(finish, (e) => {
587
- finish();
588
- throw e;
589
- });
583
+ const m2 = captureMap();
584
+ return r2.then(
585
+ () => {
586
+ swapContextMap(m2);
587
+ finish();
588
+ },
589
+ (e) => {
590
+ swapContextMap(m2);
591
+ finish();
592
+ throw e;
593
+ }
594
+ );
590
595
  }
591
596
  finish();
592
597
  }, (e) => {
598
+ swapContextMap(m);
593
599
  finish();
594
600
  throw e;
595
601
  });
596
602
  }
597
603
  const r = renderNode(result, writer, isSvg);
598
604
  if (r && typeof r.then === "function") {
599
- return r.then(finish, (e) => {
600
- finish();
601
- throw e;
602
- });
605
+ const m = captureMap();
606
+ return r.then(
607
+ () => {
608
+ swapContextMap(m);
609
+ finish();
610
+ },
611
+ (e) => {
612
+ swapContextMap(m);
613
+ finish();
614
+ throw e;
615
+ }
616
+ );
603
617
  }
604
618
  finish();
605
619
  }
@@ -615,7 +629,9 @@ function renderChildArray(children, writer, isSvg) {
615
629
  const savedTree = pushTreeContext(totalChildren, i);
616
630
  const r = renderNode(children[i], writer, isSvg);
617
631
  if (r && typeof r.then === "function") {
632
+ const m = captureMap();
618
633
  return r.then(() => {
634
+ swapContextMap(m);
619
635
  popTreeContext(savedTree);
620
636
  return renderChildArrayFrom(children, i + 1, writer, isSvg);
621
637
  });
@@ -632,7 +648,9 @@ function renderChildArrayFrom(children, startIndex, writer, isSvg) {
632
648
  const savedTree = pushTreeContext(totalChildren, i);
633
649
  const r = renderNode(children[i], writer, isSvg);
634
650
  if (r && typeof r.then === "function") {
651
+ const m = captureMap();
635
652
  return r.then(() => {
653
+ swapContextMap(m);
636
654
  popTreeContext(savedTree);
637
655
  return renderChildArrayFrom(children, i + 1, writer, isSvg);
638
656
  });
@@ -659,7 +677,9 @@ async function renderSuspense(props, writer, isSvg = false) {
659
677
  const buffer = new BufferWriter();
660
678
  const r = renderNode(children, buffer, isSvg);
661
679
  if (r && typeof r.then === "function") {
680
+ const m = captureMap();
662
681
  await r;
682
+ swapContextMap(m);
663
683
  }
664
684
  writer.write("<!--$-->");
665
685
  buffer.flush(writer);
@@ -667,7 +687,9 @@ async function renderSuspense(props, writer, isSvg = false) {
667
687
  return;
668
688
  } catch (error) {
669
689
  if (error && typeof error.then === "function") {
690
+ const m = captureMap();
670
691
  await error;
692
+ swapContextMap(m);
671
693
  attempts++;
672
694
  } else {
673
695
  throw error;
@@ -678,15 +700,21 @@ async function renderSuspense(props, writer, isSvg = false) {
678
700
  writer.write("<!--$?-->");
679
701
  if (fallback) {
680
702
  const r = renderNode(fallback, writer, isSvg);
681
- if (r && typeof r.then === "function")
703
+ if (r && typeof r.then === "function") {
704
+ const m = captureMap();
682
705
  await r;
706
+ swapContextMap(m);
707
+ }
683
708
  }
684
709
  writer.write("<!--/$-->");
685
710
  }
686
- function renderToString(element) {
687
- return runWithContextStore(async () => {
711
+ async function renderToString(element) {
712
+ const contextMap = /* @__PURE__ */ new Map();
713
+ const prev = swapContextMap(contextMap);
714
+ try {
688
715
  for (let attempt = 0; attempt < MAX_SUSPENSE_RETRIES; attempt++) {
689
716
  resetRenderState();
717
+ swapContextMap(contextMap);
690
718
  const chunks = [];
691
719
  const writer = {
692
720
  lastWasText: false,
@@ -701,19 +729,26 @@ function renderToString(element) {
701
729
  };
702
730
  try {
703
731
  const r = renderNode(element, writer);
704
- if (r && typeof r.then === "function")
732
+ if (r && typeof r.then === "function") {
733
+ const m = captureMap();
705
734
  await r;
735
+ swapContextMap(m);
736
+ }
706
737
  return chunks.join("");
707
738
  } catch (error) {
708
739
  if (error && typeof error.then === "function") {
740
+ const m = captureMap();
709
741
  await error;
742
+ swapContextMap(m);
710
743
  continue;
711
744
  }
712
745
  throw error;
713
746
  }
714
747
  }
715
748
  throw new Error("[slim-react] renderToString exceeded maximum retries");
716
- });
749
+ } finally {
750
+ swapContextMap(prev);
751
+ }
717
752
  }
718
753
 
719
754
  // src/ssr-render-worker.ts
package/dist/ssr-watch.js CHANGED
@@ -171,30 +171,22 @@ var buildCompilerConfig = (entry2, opts, includeHotPlugin) => {
171
171
  // Route all React imports to slim-react for SSR.
172
172
  react: slimReactIndex,
173
173
  "react/jsx-runtime": slimReactJsx,
174
- "react/jsx-dev-runtime": slimReactJsx,
175
- // Keep emotion on the project's node_modules (server-safe entry).
176
- "@emotion/react": path.resolve(process.cwd(), "node_modules", "@emotion", "react"),
177
- "@emotion/server": path.resolve(process.cwd(), "node_modules", "@emotion", "server"),
178
- "@emotion/cache": path.resolve(process.cwd(), "node_modules", "@emotion", "cache"),
179
- "@emotion/styled": path.resolve(process.cwd(), "node_modules", "@emotion", "styled")
174
+ "react/jsx-dev-runtime": slimReactJsx
175
+ // @emotion/* is bundled (not external) so that its `react` imports are
176
+ // resolved through the alias above to slim-react. If left external,
177
+ // emotion loads real React from node_modules and calls
178
+ // ReactSharedInternals.H.useContext which requires React's dispatcher.
180
179
  } : void 0;
181
180
  const externals = isServerBuild ? [
182
181
  // Node.js built-ins — must not be bundled; resolved by the runtime.
183
- // Both the bare name and the node: prefix are listed because rspack
184
- // may encounter either form depending on how the import is written.
185
- "node:async_hooks",
186
- "async_hooks",
187
182
  "node:fs",
188
183
  "node:path",
189
184
  "node:os",
190
185
  "node:stream",
191
186
  "node:util",
192
- // react / react-dom are replaced by slim-react via alias above — not external.
193
- // emotion should be external on server builds to avoid client/browser code
194
- "@emotion/react",
195
- "@emotion/server",
196
- "@emotion/cache",
197
- "@emotion/styled"
187
+ // @emotion/server is only used outside component rendering (CSS extraction)
188
+ // and does not call React hooks, so it is safe to leave as external.
189
+ "@emotion/server"
198
190
  ] : void 0;
199
191
  const extraPlugins = [];
200
192
  if (opts.define && typeof opts.define === "object") {
@@ -209,7 +201,10 @@ var buildCompilerConfig = (entry2, opts, includeHotPlugin) => {
209
201
  extensions: [".tsx", ".ts", ".js", ".jsx"],
210
202
  alias: resolveAliases,
211
203
  // for server builds prefer the package "main"/"module" fields and avoid "browser" so we don't pick browser-specific entrypoints
212
- mainFields: isServerBuild ? ["main", "module"] : ["browser", "module", "main"]
204
+ mainFields: isServerBuild ? ["main", "module"] : ["browser", "module", "main"],
205
+ // for server builds exclude the "browser" condition so packages with package.json
206
+ // "exports" conditions (e.g. @emotion/*) resolve their Node/CJS entry, not the browser build
207
+ ...isServerBuild ? { conditionNames: ["node", "require", "default"] } : {}
213
208
  };
214
209
  const optimization = !isServerBuild && !isDev ? {
215
210
  moduleIds: "deterministic",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hadars",
3
- "version": "0.1.24",
3
+ "version": "0.1.26",
4
4
  "description": "Minimal SSR framework for React — rspack, HMR, TypeScript, Bun/Node/Deno",
5
5
  "module": "./dist/index.js",
6
6
  "type": "module",