hadars 0.1.23 → 0.1.25

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/dist/cli.js CHANGED
@@ -160,6 +160,35 @@ function createElement(type, props, ...children) {
160
160
  }
161
161
 
162
162
  // src/slim-react/renderContext.ts
163
+ var MAP_KEY = "__slimReactContextMap";
164
+ var _g = globalThis;
165
+ if (!("__slimReactContextMap" in _g))
166
+ _g[MAP_KEY] = null;
167
+ function swapContextMap(map) {
168
+ const prev = _g[MAP_KEY];
169
+ _g[MAP_KEY] = map;
170
+ return prev;
171
+ }
172
+ function captureMap() {
173
+ return _g[MAP_KEY];
174
+ }
175
+ function getContextValue(context) {
176
+ const map = _g[MAP_KEY];
177
+ if (map && map.has(context))
178
+ return map.get(context);
179
+ const c = context;
180
+ return "_defaultValue" in c ? c._defaultValue : c._currentValue;
181
+ }
182
+ function pushContextValue(context, value) {
183
+ const map = _g[MAP_KEY];
184
+ const c = context;
185
+ const prev = map && map.has(context) ? map.get(context) : "_defaultValue" in c ? c._defaultValue : c._currentValue;
186
+ map?.set(context, value);
187
+ return prev;
188
+ }
189
+ function popContextValue(context, prev) {
190
+ _g[MAP_KEY]?.set(context, prev);
191
+ }
163
192
  var GLOBAL_KEY = "__slimReactRenderState";
164
193
  var EMPTY = { id: 0, overflow: "", bits: 0 };
165
194
  function s() {
@@ -572,7 +601,7 @@ function renderComponent(type, props, writer, isSvg) {
572
601
  }
573
602
  if (typeOf === REACT_CONSUMER) {
574
603
  const ctx2 = type._context;
575
- const value = ctx2?._currentValue;
604
+ const value = ctx2 ? getContextValue(ctx2) : void 0;
576
605
  const result2 = typeof props.children === "function" ? props.children(value) : null;
577
606
  const savedScope2 = pushComponentScope();
578
607
  const finish2 = () => popComponentScope(savedScope2);
@@ -588,18 +617,28 @@ function renderComponent(type, props, writer, isSvg) {
588
617
  let ctx;
589
618
  if (isProvider) {
590
619
  ctx = type._context ?? type;
591
- prevCtxValue = ctx._currentValue;
592
- ctx._currentValue = props.value;
620
+ prevCtxValue = pushContextValue(ctx, props.value);
593
621
  }
594
622
  const savedScope = pushComponentScope();
595
623
  if (isProvider && typeof type !== "function") {
596
624
  const finish2 = () => {
597
625
  popComponentScope(savedScope);
598
- ctx._currentValue = prevCtxValue;
626
+ popContextValue(ctx, prevCtxValue);
599
627
  };
600
628
  const r2 = renderChildren(props.children, writer, isSvg);
601
629
  if (r2 && typeof r2.then === "function") {
602
- return r2.then(finish2);
630
+ const m = captureMap();
631
+ return r2.then(
632
+ () => {
633
+ swapContextMap(m);
634
+ finish2();
635
+ },
636
+ (e) => {
637
+ swapContextMap(m);
638
+ finish2();
639
+ throw e;
640
+ }
641
+ );
603
642
  }
604
643
  finish2();
605
644
  return;
@@ -620,26 +659,54 @@ function renderComponent(type, props, writer, isSvg) {
620
659
  } catch (e) {
621
660
  popComponentScope(savedScope);
622
661
  if (isProvider)
623
- ctx._currentValue = prevCtxValue;
662
+ popContextValue(ctx, prevCtxValue);
624
663
  throw e;
625
664
  }
626
665
  const finish = () => {
627
666
  popComponentScope(savedScope);
628
667
  if (isProvider)
629
- ctx._currentValue = prevCtxValue;
668
+ popContextValue(ctx, prevCtxValue);
630
669
  };
631
670
  if (result instanceof Promise) {
671
+ const m = captureMap();
632
672
  return result.then((resolved) => {
673
+ swapContextMap(m);
633
674
  const r2 = renderNode(resolved, writer, isSvg);
634
675
  if (r2 && typeof r2.then === "function") {
635
- return r2.then(finish);
676
+ const m2 = captureMap();
677
+ return r2.then(
678
+ () => {
679
+ swapContextMap(m2);
680
+ finish();
681
+ },
682
+ (e) => {
683
+ swapContextMap(m2);
684
+ finish();
685
+ throw e;
686
+ }
687
+ );
636
688
  }
637
689
  finish();
690
+ }, (e) => {
691
+ swapContextMap(m);
692
+ finish();
693
+ throw e;
638
694
  });
639
695
  }
640
696
  const r = renderNode(result, writer, isSvg);
641
697
  if (r && typeof r.then === "function") {
642
- return r.then(finish);
698
+ const m = captureMap();
699
+ return r.then(
700
+ () => {
701
+ swapContextMap(m);
702
+ finish();
703
+ },
704
+ (e) => {
705
+ swapContextMap(m);
706
+ finish();
707
+ throw e;
708
+ }
709
+ );
643
710
  }
644
711
  finish();
645
712
  }
@@ -655,7 +722,9 @@ function renderChildArray(children, writer, isSvg) {
655
722
  const savedTree = pushTreeContext(totalChildren, i);
656
723
  const r = renderNode(children[i], writer, isSvg);
657
724
  if (r && typeof r.then === "function") {
725
+ const m = captureMap();
658
726
  return r.then(() => {
727
+ swapContextMap(m);
659
728
  popTreeContext(savedTree);
660
729
  return renderChildArrayFrom(children, i + 1, writer, isSvg);
661
730
  });
@@ -672,7 +741,9 @@ function renderChildArrayFrom(children, startIndex, writer, isSvg) {
672
741
  const savedTree = pushTreeContext(totalChildren, i);
673
742
  const r = renderNode(children[i], writer, isSvg);
674
743
  if (r && typeof r.then === "function") {
744
+ const m = captureMap();
675
745
  return r.then(() => {
746
+ swapContextMap(m);
676
747
  popTreeContext(savedTree);
677
748
  return renderChildArrayFrom(children, i + 1, writer, isSvg);
678
749
  });
@@ -699,7 +770,9 @@ async function renderSuspense(props, writer, isSvg = false) {
699
770
  const buffer = new BufferWriter();
700
771
  const r = renderNode(children, buffer, isSvg);
701
772
  if (r && typeof r.then === "function") {
773
+ const m = captureMap();
702
774
  await r;
775
+ swapContextMap(m);
703
776
  }
704
777
  writer.write("<!--$-->");
705
778
  buffer.flush(writer);
@@ -707,7 +780,9 @@ async function renderSuspense(props, writer, isSvg = false) {
707
780
  return;
708
781
  } catch (error) {
709
782
  if (error && typeof error.then === "function") {
783
+ const m = captureMap();
710
784
  await error;
785
+ swapContextMap(m);
711
786
  attempts++;
712
787
  } else {
713
788
  throw error;
@@ -718,40 +793,55 @@ async function renderSuspense(props, writer, isSvg = false) {
718
793
  writer.write("<!--$?-->");
719
794
  if (fallback) {
720
795
  const r = renderNode(fallback, writer, isSvg);
721
- if (r && typeof r.then === "function")
796
+ if (r && typeof r.then === "function") {
797
+ const m = captureMap();
722
798
  await r;
799
+ swapContextMap(m);
800
+ }
723
801
  }
724
802
  writer.write("<!--/$-->");
725
803
  }
726
804
  async function renderToString(element) {
727
- for (let attempt = 0; attempt < MAX_SUSPENSE_RETRIES; attempt++) {
728
- resetRenderState();
729
- const chunks = [];
730
- const writer = {
731
- lastWasText: false,
732
- write(c) {
733
- chunks.push(c);
734
- this.lastWasText = false;
735
- },
736
- text(s2) {
737
- chunks.push(s2);
738
- this.lastWasText = true;
739
- }
740
- };
741
- try {
742
- const r = renderNode(element, writer);
743
- if (r && typeof r.then === "function")
744
- await r;
745
- return chunks.join("");
746
- } catch (error) {
747
- if (error && typeof error.then === "function") {
748
- await error;
749
- continue;
805
+ const contextMap = /* @__PURE__ */ new Map();
806
+ const prev = swapContextMap(contextMap);
807
+ try {
808
+ for (let attempt = 0; attempt < MAX_SUSPENSE_RETRIES; attempt++) {
809
+ resetRenderState();
810
+ swapContextMap(contextMap);
811
+ const chunks = [];
812
+ const writer = {
813
+ lastWasText: false,
814
+ write(c) {
815
+ chunks.push(c);
816
+ this.lastWasText = false;
817
+ },
818
+ text(s2) {
819
+ chunks.push(s2);
820
+ this.lastWasText = true;
821
+ }
822
+ };
823
+ try {
824
+ const r = renderNode(element, writer);
825
+ if (r && typeof r.then === "function") {
826
+ const m = captureMap();
827
+ await r;
828
+ swapContextMap(m);
829
+ }
830
+ return chunks.join("");
831
+ } catch (error) {
832
+ if (error && typeof error.then === "function") {
833
+ const m = captureMap();
834
+ await error;
835
+ swapContextMap(m);
836
+ continue;
837
+ }
838
+ throw error;
750
839
  }
751
- throw error;
752
840
  }
841
+ throw new Error("[slim-react] renderToString exceeded maximum retries");
842
+ } finally {
843
+ swapContextMap(prev);
753
844
  }
754
- throw new Error("[slim-react] renderToString exceeded maximum retries");
755
845
  }
756
846
 
757
847
  // src/utils/response.tsx
@@ -1039,6 +1129,12 @@ var buildCompilerConfig = (entry, opts, includeHotPlugin) => {
1039
1129
  "@emotion/styled": path.resolve(process.cwd(), "node_modules", "@emotion", "styled")
1040
1130
  } : void 0;
1041
1131
  const externals = isServerBuild ? [
1132
+ // Node.js built-ins — must not be bundled; resolved by the runtime.
1133
+ "node:fs",
1134
+ "node:path",
1135
+ "node:os",
1136
+ "node:stream",
1137
+ "node:util",
1042
1138
  // react / react-dom are replaced by slim-react via alias above — not external.
1043
1139
  // emotion should be external on server builds to avoid client/browser code
1044
1140
  "@emotion/react",
@@ -100,6 +100,35 @@ function createElement(type, props, ...children) {
100
100
  }
101
101
 
102
102
  // src/slim-react/renderContext.ts
103
+ var MAP_KEY = "__slimReactContextMap";
104
+ var _g = globalThis;
105
+ if (!("__slimReactContextMap" in _g))
106
+ _g[MAP_KEY] = null;
107
+ function swapContextMap(map) {
108
+ const prev = _g[MAP_KEY];
109
+ _g[MAP_KEY] = map;
110
+ return prev;
111
+ }
112
+ function captureMap() {
113
+ return _g[MAP_KEY];
114
+ }
115
+ function getContextValue(context) {
116
+ const map = _g[MAP_KEY];
117
+ if (map && map.has(context))
118
+ return map.get(context);
119
+ const c = context;
120
+ return "_defaultValue" in c ? c._defaultValue : c._currentValue;
121
+ }
122
+ function pushContextValue(context, value) {
123
+ const map = _g[MAP_KEY];
124
+ const c = context;
125
+ const prev = map && map.has(context) ? map.get(context) : "_defaultValue" in c ? c._defaultValue : c._currentValue;
126
+ map?.set(context, value);
127
+ return prev;
128
+ }
129
+ function popContextValue(context, prev) {
130
+ _g[MAP_KEY]?.set(context, prev);
131
+ }
103
132
  var GLOBAL_KEY = "__slimReactRenderState";
104
133
  var EMPTY = { id: 0, overflow: "", bits: 0 };
105
134
  function s() {
@@ -225,8 +254,8 @@ function useActionState(_action, initialState, _permalink) {
225
254
  }, false];
226
255
  }
227
256
  function use(usable) {
228
- if (typeof usable === "object" && usable !== null && "_currentValue" in usable) {
229
- return usable._currentValue;
257
+ if (typeof usable === "object" && usable !== null && ("_currentValue" in usable || "_defaultValue" in usable)) {
258
+ return getContextValue(usable);
230
259
  }
231
260
  const promise = usable;
232
261
  if (promise.status === "fulfilled")
@@ -242,6 +271,7 @@ function startTransition(callback) {
242
271
  // src/slim-react/context.ts
243
272
  function createContext(defaultValue) {
244
273
  const context = {
274
+ _defaultValue: defaultValue,
245
275
  _currentValue: defaultValue,
246
276
  Provider: null,
247
277
  Consumer: null
@@ -617,7 +647,7 @@ function renderComponent(type, props, writer, isSvg) {
617
647
  }
618
648
  if (typeOf === REACT_CONSUMER) {
619
649
  const ctx2 = type._context;
620
- const value = ctx2?._currentValue;
650
+ const value = ctx2 ? getContextValue(ctx2) : void 0;
621
651
  const result2 = typeof props.children === "function" ? props.children(value) : null;
622
652
  const savedScope2 = pushComponentScope();
623
653
  const finish2 = () => popComponentScope(savedScope2);
@@ -633,18 +663,28 @@ function renderComponent(type, props, writer, isSvg) {
633
663
  let ctx;
634
664
  if (isProvider) {
635
665
  ctx = type._context ?? type;
636
- prevCtxValue = ctx._currentValue;
637
- ctx._currentValue = props.value;
666
+ prevCtxValue = pushContextValue(ctx, props.value);
638
667
  }
639
668
  const savedScope = pushComponentScope();
640
669
  if (isProvider && typeof type !== "function") {
641
670
  const finish2 = () => {
642
671
  popComponentScope(savedScope);
643
- ctx._currentValue = prevCtxValue;
672
+ popContextValue(ctx, prevCtxValue);
644
673
  };
645
674
  const r2 = renderChildren(props.children, writer, isSvg);
646
675
  if (r2 && typeof r2.then === "function") {
647
- return r2.then(finish2);
676
+ const m = captureMap();
677
+ return r2.then(
678
+ () => {
679
+ swapContextMap(m);
680
+ finish2();
681
+ },
682
+ (e) => {
683
+ swapContextMap(m);
684
+ finish2();
685
+ throw e;
686
+ }
687
+ );
648
688
  }
649
689
  finish2();
650
690
  return;
@@ -665,26 +705,54 @@ function renderComponent(type, props, writer, isSvg) {
665
705
  } catch (e) {
666
706
  popComponentScope(savedScope);
667
707
  if (isProvider)
668
- ctx._currentValue = prevCtxValue;
708
+ popContextValue(ctx, prevCtxValue);
669
709
  throw e;
670
710
  }
671
711
  const finish = () => {
672
712
  popComponentScope(savedScope);
673
713
  if (isProvider)
674
- ctx._currentValue = prevCtxValue;
714
+ popContextValue(ctx, prevCtxValue);
675
715
  };
676
716
  if (result instanceof Promise) {
717
+ const m = captureMap();
677
718
  return result.then((resolved) => {
719
+ swapContextMap(m);
678
720
  const r2 = renderNode(resolved, writer, isSvg);
679
721
  if (r2 && typeof r2.then === "function") {
680
- return r2.then(finish);
722
+ const m2 = captureMap();
723
+ return r2.then(
724
+ () => {
725
+ swapContextMap(m2);
726
+ finish();
727
+ },
728
+ (e) => {
729
+ swapContextMap(m2);
730
+ finish();
731
+ throw e;
732
+ }
733
+ );
681
734
  }
682
735
  finish();
736
+ }, (e) => {
737
+ swapContextMap(m);
738
+ finish();
739
+ throw e;
683
740
  });
684
741
  }
685
742
  const r = renderNode(result, writer, isSvg);
686
743
  if (r && typeof r.then === "function") {
687
- return r.then(finish);
744
+ const m = captureMap();
745
+ return r.then(
746
+ () => {
747
+ swapContextMap(m);
748
+ finish();
749
+ },
750
+ (e) => {
751
+ swapContextMap(m);
752
+ finish();
753
+ throw e;
754
+ }
755
+ );
688
756
  }
689
757
  finish();
690
758
  }
@@ -700,7 +768,9 @@ function renderChildArray(children, writer, isSvg) {
700
768
  const savedTree = pushTreeContext(totalChildren, i);
701
769
  const r = renderNode(children[i], writer, isSvg);
702
770
  if (r && typeof r.then === "function") {
771
+ const m = captureMap();
703
772
  return r.then(() => {
773
+ swapContextMap(m);
704
774
  popTreeContext(savedTree);
705
775
  return renderChildArrayFrom(children, i + 1, writer, isSvg);
706
776
  });
@@ -717,7 +787,9 @@ function renderChildArrayFrom(children, startIndex, writer, isSvg) {
717
787
  const savedTree = pushTreeContext(totalChildren, i);
718
788
  const r = renderNode(children[i], writer, isSvg);
719
789
  if (r && typeof r.then === "function") {
790
+ const m = captureMap();
720
791
  return r.then(() => {
792
+ swapContextMap(m);
721
793
  popTreeContext(savedTree);
722
794
  return renderChildArrayFrom(children, i + 1, writer, isSvg);
723
795
  });
@@ -744,7 +816,9 @@ async function renderSuspense(props, writer, isSvg = false) {
744
816
  const buffer = new BufferWriter();
745
817
  const r = renderNode(children, buffer, isSvg);
746
818
  if (r && typeof r.then === "function") {
819
+ const m = captureMap();
747
820
  await r;
821
+ swapContextMap(m);
748
822
  }
749
823
  writer.write("<!--$-->");
750
824
  buffer.flush(writer);
@@ -752,7 +826,9 @@ async function renderSuspense(props, writer, isSvg = false) {
752
826
  return;
753
827
  } catch (error) {
754
828
  if (error && typeof error.then === "function") {
829
+ const m = captureMap();
755
830
  await error;
831
+ swapContextMap(m);
756
832
  attempts++;
757
833
  } else {
758
834
  throw error;
@@ -763,16 +839,21 @@ async function renderSuspense(props, writer, isSvg = false) {
763
839
  writer.write("<!--$?-->");
764
840
  if (fallback) {
765
841
  const r = renderNode(fallback, writer, isSvg);
766
- if (r && typeof r.then === "function")
842
+ if (r && typeof r.then === "function") {
843
+ const m = captureMap();
767
844
  await r;
845
+ swapContextMap(m);
846
+ }
768
847
  }
769
848
  writer.write("<!--/$-->");
770
849
  }
771
850
  function renderToStream(element) {
772
851
  const encoder = new TextEncoder();
852
+ const contextMap = /* @__PURE__ */ new Map();
773
853
  return new ReadableStream({
774
854
  async start(controller) {
775
855
  resetRenderState();
856
+ const prev = swapContextMap(contextMap);
776
857
  const writer = {
777
858
  lastWasText: false,
778
859
  write(chunk) {
@@ -786,49 +867,66 @@ function renderToStream(element) {
786
867
  };
787
868
  try {
788
869
  const r = renderNode(element, writer);
789
- if (r && typeof r.then === "function")
870
+ if (r && typeof r.then === "function") {
871
+ const m = captureMap();
790
872
  await r;
873
+ swapContextMap(m);
874
+ }
791
875
  controller.close();
792
876
  } catch (error) {
793
877
  controller.error(error);
878
+ } finally {
879
+ swapContextMap(prev);
794
880
  }
795
881
  }
796
882
  });
797
883
  }
798
884
  async function renderToString(element) {
799
- for (let attempt = 0; attempt < MAX_SUSPENSE_RETRIES; attempt++) {
800
- resetRenderState();
801
- const chunks = [];
802
- const writer = {
803
- lastWasText: false,
804
- write(c) {
805
- chunks.push(c);
806
- this.lastWasText = false;
807
- },
808
- text(s2) {
809
- chunks.push(s2);
810
- this.lastWasText = true;
811
- }
812
- };
813
- try {
814
- const r = renderNode(element, writer);
815
- if (r && typeof r.then === "function")
816
- await r;
817
- return chunks.join("");
818
- } catch (error) {
819
- if (error && typeof error.then === "function") {
820
- await error;
821
- continue;
885
+ const contextMap = /* @__PURE__ */ new Map();
886
+ const prev = swapContextMap(contextMap);
887
+ try {
888
+ for (let attempt = 0; attempt < MAX_SUSPENSE_RETRIES; attempt++) {
889
+ resetRenderState();
890
+ swapContextMap(contextMap);
891
+ const chunks = [];
892
+ const writer = {
893
+ lastWasText: false,
894
+ write(c) {
895
+ chunks.push(c);
896
+ this.lastWasText = false;
897
+ },
898
+ text(s2) {
899
+ chunks.push(s2);
900
+ this.lastWasText = true;
901
+ }
902
+ };
903
+ try {
904
+ const r = renderNode(element, writer);
905
+ if (r && typeof r.then === "function") {
906
+ const m = captureMap();
907
+ await r;
908
+ swapContextMap(m);
909
+ }
910
+ return chunks.join("");
911
+ } catch (error) {
912
+ if (error && typeof error.then === "function") {
913
+ const m = captureMap();
914
+ await error;
915
+ swapContextMap(m);
916
+ continue;
917
+ }
918
+ throw error;
822
919
  }
823
- throw error;
824
920
  }
921
+ throw new Error("[slim-react] renderToString exceeded maximum retries");
922
+ } finally {
923
+ swapContextMap(prev);
825
924
  }
826
- throw new Error("[slim-react] renderToString exceeded maximum retries");
827
925
  }
828
926
 
829
927
  // src/slim-react/index.ts
830
928
  function useContext(context) {
831
- return context._currentValue;
929
+ return getContextValue(context);
832
930
  }
833
931
  var Suspense = SUSPENSE_TYPE;
834
932
  function isValidElement(obj) {
@@ -49,6 +49,7 @@ declare function startTransition(callback: () => void): void;
49
49
  * save / restore around Provider renders (handled by the renderer).
50
50
  */
51
51
  interface Context<T> {
52
+ _defaultValue: T;
52
53
  _currentValue: T;
53
54
  Provider: ContextProvider<T>;
54
55
  Consumer: (props: {