hadars 0.1.22 → 0.1.24

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,11 @@
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 new Error('Dynamic require of "' + x + '" is not supported');
7
+ });
8
+
9
+ export {
10
+ __require
11
+ };
package/dist/cli.js CHANGED
@@ -1,4 +1,11 @@
1
1
  #!/usr/bin/env node
2
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
3
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
4
+ }) : x)(function(x) {
5
+ if (typeof require !== "undefined")
6
+ return require.apply(this, arguments);
7
+ throw Error('Dynamic require of "' + x + '" is not supported');
8
+ });
2
9
 
3
10
  // cli.ts
4
11
  import { spawn as spawn2 } from "node:child_process";
@@ -160,6 +167,40 @@ function createElement(type, props, ...children) {
160
167
  }
161
168
 
162
169
  // src/slim-react/renderContext.ts
170
+ var CONTEXT_STORE_KEY = "__slimReactContextStore";
171
+ var _g = globalThis;
172
+ if (!_g[CONTEXT_STORE_KEY]) {
173
+ try {
174
+ const { AsyncLocalStorage } = __require("node:async_hooks");
175
+ _g[CONTEXT_STORE_KEY] = new AsyncLocalStorage();
176
+ } catch {
177
+ _g[CONTEXT_STORE_KEY] = null;
178
+ }
179
+ }
180
+ var _contextStore = _g[CONTEXT_STORE_KEY];
181
+ function runWithContextStore(fn) {
182
+ return _contextStore ? _contextStore.run(/* @__PURE__ */ new Map(), fn) : fn();
183
+ }
184
+ function getContextValue(context) {
185
+ const store = _contextStore?.getStore();
186
+ if (store && store.has(context))
187
+ return store.get(context);
188
+ const c = context;
189
+ return "_defaultValue" in c ? c._defaultValue : c._currentValue;
190
+ }
191
+ function pushContextValue(context, value) {
192
+ const store = _contextStore?.getStore();
193
+ const c = context;
194
+ const prev = store && store.has(context) ? store.get(context) : "_defaultValue" in c ? c._defaultValue : c._currentValue;
195
+ if (store)
196
+ store.set(context, value);
197
+ return prev;
198
+ }
199
+ function popContextValue(context, prev) {
200
+ const store = _contextStore?.getStore();
201
+ if (store)
202
+ store.set(context, prev);
203
+ }
163
204
  var GLOBAL_KEY = "__slimReactRenderState";
164
205
  var EMPTY = { id: 0, overflow: "", bits: 0 };
165
206
  function s() {
@@ -572,7 +613,7 @@ function renderComponent(type, props, writer, isSvg) {
572
613
  }
573
614
  if (typeOf === REACT_CONSUMER) {
574
615
  const ctx2 = type._context;
575
- const value = ctx2?._currentValue;
616
+ const value = ctx2 ? getContextValue(ctx2) : void 0;
576
617
  const result2 = typeof props.children === "function" ? props.children(value) : null;
577
618
  const savedScope2 = pushComponentScope();
578
619
  const finish2 = () => popComponentScope(savedScope2);
@@ -588,18 +629,20 @@ function renderComponent(type, props, writer, isSvg) {
588
629
  let ctx;
589
630
  if (isProvider) {
590
631
  ctx = type._context ?? type;
591
- prevCtxValue = ctx._currentValue;
592
- ctx._currentValue = props.value;
632
+ prevCtxValue = pushContextValue(ctx, props.value);
593
633
  }
594
634
  const savedScope = pushComponentScope();
595
635
  if (isProvider && typeof type !== "function") {
596
636
  const finish2 = () => {
597
637
  popComponentScope(savedScope);
598
- ctx._currentValue = prevCtxValue;
638
+ popContextValue(ctx, prevCtxValue);
599
639
  };
600
640
  const r2 = renderChildren(props.children, writer, isSvg);
601
641
  if (r2 && typeof r2.then === "function") {
602
- return r2.then(finish2);
642
+ return r2.then(finish2, (e) => {
643
+ finish2();
644
+ throw e;
645
+ });
603
646
  }
604
647
  finish2();
605
648
  return;
@@ -620,26 +663,35 @@ function renderComponent(type, props, writer, isSvg) {
620
663
  } catch (e) {
621
664
  popComponentScope(savedScope);
622
665
  if (isProvider)
623
- ctx._currentValue = prevCtxValue;
666
+ popContextValue(ctx, prevCtxValue);
624
667
  throw e;
625
668
  }
626
669
  const finish = () => {
627
670
  popComponentScope(savedScope);
628
671
  if (isProvider)
629
- ctx._currentValue = prevCtxValue;
672
+ popContextValue(ctx, prevCtxValue);
630
673
  };
631
674
  if (result instanceof Promise) {
632
675
  return result.then((resolved) => {
633
676
  const r2 = renderNode(resolved, writer, isSvg);
634
677
  if (r2 && typeof r2.then === "function") {
635
- return r2.then(finish);
678
+ return r2.then(finish, (e) => {
679
+ finish();
680
+ throw e;
681
+ });
636
682
  }
637
683
  finish();
684
+ }, (e) => {
685
+ finish();
686
+ throw e;
638
687
  });
639
688
  }
640
689
  const r = renderNode(result, writer, isSvg);
641
690
  if (r && typeof r.then === "function") {
642
- return r.then(finish);
691
+ return r.then(finish, (e) => {
692
+ finish();
693
+ throw e;
694
+ });
643
695
  }
644
696
  finish();
645
697
  }
@@ -723,35 +775,37 @@ async function renderSuspense(props, writer, isSvg = false) {
723
775
  }
724
776
  writer.write("<!--/$-->");
725
777
  }
726
- 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;
778
+ function renderToString(element) {
779
+ return runWithContextStore(async () => {
780
+ for (let attempt = 0; attempt < MAX_SUSPENSE_RETRIES; attempt++) {
781
+ resetRenderState();
782
+ const chunks = [];
783
+ const writer = {
784
+ lastWasText: false,
785
+ write(c) {
786
+ chunks.push(c);
787
+ this.lastWasText = false;
788
+ },
789
+ text(s2) {
790
+ chunks.push(s2);
791
+ this.lastWasText = true;
792
+ }
793
+ };
794
+ try {
795
+ const r = renderNode(element, writer);
796
+ if (r && typeof r.then === "function")
797
+ await r;
798
+ return chunks.join("");
799
+ } catch (error) {
800
+ if (error && typeof error.then === "function") {
801
+ await error;
802
+ continue;
803
+ }
804
+ throw error;
750
805
  }
751
- throw error;
752
806
  }
753
- }
754
- throw new Error("[slim-react] renderToString exceeded maximum retries");
807
+ throw new Error("[slim-react] renderToString exceeded maximum retries");
808
+ });
755
809
  }
756
810
 
757
811
  // src/utils/response.tsx
@@ -872,7 +926,7 @@ var __dirname = process.cwd();
872
926
  var packageDir = pathMod.dirname(fileURLToPath(import.meta.url));
873
927
  var clientScriptPath = pathMod.resolve(packageDir, "template.html");
874
928
  var loaderPath = existsSync(pathMod.resolve(packageDir, "loader.cjs")) ? pathMod.resolve(packageDir, "loader.cjs") : pathMod.resolve(packageDir, "loader.ts");
875
- var getConfigBase = (mode) => {
929
+ var getConfigBase = (mode, isServerBuild = false) => {
876
930
  const isDev = mode === "development";
877
931
  return {
878
932
  experiments: {
@@ -971,9 +1025,12 @@ var getConfigBase = (mode) => {
971
1025
  };
972
1026
  };
973
1027
  var buildCompilerConfig = (entry, opts, includeHotPlugin) => {
974
- const Config = getConfigBase(opts.mode);
975
1028
  const { base } = opts;
976
1029
  const isDev = opts.mode === "development";
1030
+ const isServerBuild = Boolean(
1031
+ opts.output && typeof opts.output === "object" && (opts.output.library || String(opts.output.filename || "").includes("ssr"))
1032
+ );
1033
+ const Config = getConfigBase(opts.mode, isServerBuild);
977
1034
  const localConfig = {
978
1035
  ...Config,
979
1036
  module: {
@@ -1022,12 +1079,9 @@ var buildCompilerConfig = (entry, opts, includeHotPlugin) => {
1022
1079
  if (opts.moduleRules && opts.moduleRules.length > 0) {
1023
1080
  localConfig.module.rules.push(...opts.moduleRules);
1024
1081
  }
1025
- const isServerBuild2 = Boolean(
1026
- opts.output && typeof opts.output === "object" && (opts.output.library || String(opts.output.filename || "").includes("ssr"))
1027
- );
1028
1082
  const slimReactIndex = pathMod.resolve(packageDir, "slim-react", "index.js");
1029
1083
  const slimReactJsx = pathMod.resolve(packageDir, "slim-react", "jsx-runtime.js");
1030
- const resolveAliases = isServerBuild2 ? {
1084
+ const resolveAliases = isServerBuild ? {
1031
1085
  // Route all React imports to slim-react for SSR.
1032
1086
  react: slimReactIndex,
1033
1087
  "react/jsx-runtime": slimReactJsx,
@@ -1038,7 +1092,17 @@ var buildCompilerConfig = (entry, opts, includeHotPlugin) => {
1038
1092
  "@emotion/cache": path.resolve(process.cwd(), "node_modules", "@emotion", "cache"),
1039
1093
  "@emotion/styled": path.resolve(process.cwd(), "node_modules", "@emotion", "styled")
1040
1094
  } : void 0;
1041
- const externals = isServerBuild2 ? [
1095
+ const externals = isServerBuild ? [
1096
+ // Node.js built-ins — must not be bundled; resolved by the runtime.
1097
+ // Both the bare name and the node: prefix are listed because rspack
1098
+ // may encounter either form depending on how the import is written.
1099
+ "node:async_hooks",
1100
+ "async_hooks",
1101
+ "node:fs",
1102
+ "node:path",
1103
+ "node:os",
1104
+ "node:stream",
1105
+ "node:util",
1042
1106
  // react / react-dom are replaced by slim-react via alias above — not external.
1043
1107
  // emotion should be external on server builds to avoid client/browser code
1044
1108
  "@emotion/react",
@@ -1059,9 +1123,9 @@ var buildCompilerConfig = (entry, opts, includeHotPlugin) => {
1059
1123
  extensions: [".tsx", ".ts", ".js", ".jsx"],
1060
1124
  alias: resolveAliases,
1061
1125
  // for server builds prefer the package "main"/"module" fields and avoid "browser" so we don't pick browser-specific entrypoints
1062
- mainFields: isServerBuild2 ? ["main", "module"] : ["browser", "module", "main"]
1126
+ mainFields: isServerBuild ? ["main", "module"] : ["browser", "module", "main"]
1063
1127
  };
1064
- const optimization = !isServerBuild2 && !isDev ? {
1128
+ const optimization = !isServerBuild && !isDev ? {
1065
1129
  moduleIds: "deterministic",
1066
1130
  splitChunks: {
1067
1131
  chunks: "all",
@@ -1089,7 +1153,7 @@ var buildCompilerConfig = (entry, opts, includeHotPlugin) => {
1089
1153
  externals,
1090
1154
  ...optimization !== void 0 ? { optimization } : {},
1091
1155
  plugins: [
1092
- new rspack.HtmlRspackPlugin({
1156
+ !isServerBuild && new rspack.HtmlRspackPlugin({
1093
1157
  publicPath: base || "/",
1094
1158
  template: opts.htmlTemplate ? pathMod.resolve(process.cwd(), opts.htmlTemplate) : clientScriptPath,
1095
1159
  scriptLoading: "module",
@@ -1097,12 +1161,7 @@ var buildCompilerConfig = (entry, opts, includeHotPlugin) => {
1097
1161
  inject: "head",
1098
1162
  minify: opts.mode === "production"
1099
1163
  }),
1100
- // Add `async` to the emitted module script so DOMContentLoaded fires
1101
- // as soon as HTML is parsed — without waiting for the bundle to execute.
1102
- // `<script type="module" async>` is valid: it downloads in parallel and
1103
- // executes without blocking DOMContentLoaded, while retaining module
1104
- // semantics (strict mode, ES imports, etc.).
1105
- {
1164
+ !isServerBuild && {
1106
1165
  apply(compiler) {
1107
1166
  compiler.hooks.emit.tapAsync("HadarsAsyncModuleScript", (compilation, cb) => {
1108
1167
  const asset = compilation.assets["out.html"];
@@ -1121,8 +1180,8 @@ var buildCompilerConfig = (entry, opts, includeHotPlugin) => {
1121
1180
  });
1122
1181
  }
1123
1182
  },
1124
- isDev && !isServerBuild2 && new ReactRefreshPlugin(),
1125
- includeHotPlugin && isDev && new rspack.HotModuleReplacementPlugin(),
1183
+ isDev && !isServerBuild && new ReactRefreshPlugin(),
1184
+ includeHotPlugin && isDev && !isServerBuild && new rspack.HotModuleReplacementPlugin(),
1126
1185
  ...extraPlugins
1127
1186
  ],
1128
1187
  ...localConfig,
@@ -1137,7 +1196,7 @@ var buildCompilerConfig = (entry, opts, includeHotPlugin) => {
1137
1196
  // for client builds. SSR builds still need it for dynamic import() of exports.
1138
1197
  experiments: {
1139
1198
  ...localConfig.experiments || {},
1140
- outputModule: isServerBuild2
1199
+ outputModule: isServerBuild
1141
1200
  },
1142
1201
  // Prevent rspack from watching its own build output — without this the
1143
1202
  // SSR watcher writing .hadars/index.ssr.js triggers the client compiler
package/dist/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ import "./chunk-EZUCZHGV.js";
2
+
1
3
  // src/utils/Head.tsx
2
4
  import React from "react";
3
5
  import { jsx } from "react/jsx-runtime";
@@ -100,6 +100,40 @@ function createElement(type, props, ...children) {
100
100
  }
101
101
 
102
102
  // src/slim-react/renderContext.ts
103
+ var CONTEXT_STORE_KEY = "__slimReactContextStore";
104
+ var _g = globalThis;
105
+ if (!_g[CONTEXT_STORE_KEY]) {
106
+ try {
107
+ const { AsyncLocalStorage } = require("async_hooks");
108
+ _g[CONTEXT_STORE_KEY] = new AsyncLocalStorage();
109
+ } catch {
110
+ _g[CONTEXT_STORE_KEY] = null;
111
+ }
112
+ }
113
+ var _contextStore = _g[CONTEXT_STORE_KEY];
114
+ function runWithContextStore(fn) {
115
+ return _contextStore ? _contextStore.run(/* @__PURE__ */ new Map(), fn) : fn();
116
+ }
117
+ function getContextValue(context) {
118
+ const store = _contextStore?.getStore();
119
+ if (store && store.has(context))
120
+ return store.get(context);
121
+ const c = context;
122
+ return "_defaultValue" in c ? c._defaultValue : c._currentValue;
123
+ }
124
+ function pushContextValue(context, value) {
125
+ const store = _contextStore?.getStore();
126
+ const c = context;
127
+ const prev = store && store.has(context) ? store.get(context) : "_defaultValue" in c ? c._defaultValue : c._currentValue;
128
+ if (store)
129
+ store.set(context, value);
130
+ return prev;
131
+ }
132
+ function popContextValue(context, prev) {
133
+ const store = _contextStore?.getStore();
134
+ if (store)
135
+ store.set(context, prev);
136
+ }
103
137
  var GLOBAL_KEY = "__slimReactRenderState";
104
138
  var EMPTY = { id: 0, overflow: "", bits: 0 };
105
139
  function s() {
@@ -225,8 +259,8 @@ function useActionState(_action, initialState, _permalink) {
225
259
  }, false];
226
260
  }
227
261
  function use(usable) {
228
- if (typeof usable === "object" && usable !== null && "_currentValue" in usable) {
229
- return usable._currentValue;
262
+ if (typeof usable === "object" && usable !== null && ("_currentValue" in usable || "_defaultValue" in usable)) {
263
+ return getContextValue(usable);
230
264
  }
231
265
  const promise = usable;
232
266
  if (promise.status === "fulfilled")
@@ -242,6 +276,7 @@ function startTransition(callback) {
242
276
  // src/slim-react/context.ts
243
277
  function createContext(defaultValue) {
244
278
  const context = {
279
+ _defaultValue: defaultValue,
245
280
  _currentValue: defaultValue,
246
281
  Provider: null,
247
282
  Consumer: null
@@ -617,7 +652,7 @@ function renderComponent(type, props, writer, isSvg) {
617
652
  }
618
653
  if (typeOf === REACT_CONSUMER) {
619
654
  const ctx2 = type._context;
620
- const value = ctx2?._currentValue;
655
+ const value = ctx2 ? getContextValue(ctx2) : void 0;
621
656
  const result2 = typeof props.children === "function" ? props.children(value) : null;
622
657
  const savedScope2 = pushComponentScope();
623
658
  const finish2 = () => popComponentScope(savedScope2);
@@ -633,18 +668,20 @@ function renderComponent(type, props, writer, isSvg) {
633
668
  let ctx;
634
669
  if (isProvider) {
635
670
  ctx = type._context ?? type;
636
- prevCtxValue = ctx._currentValue;
637
- ctx._currentValue = props.value;
671
+ prevCtxValue = pushContextValue(ctx, props.value);
638
672
  }
639
673
  const savedScope = pushComponentScope();
640
674
  if (isProvider && typeof type !== "function") {
641
675
  const finish2 = () => {
642
676
  popComponentScope(savedScope);
643
- ctx._currentValue = prevCtxValue;
677
+ popContextValue(ctx, prevCtxValue);
644
678
  };
645
679
  const r2 = renderChildren(props.children, writer, isSvg);
646
680
  if (r2 && typeof r2.then === "function") {
647
- return r2.then(finish2);
681
+ return r2.then(finish2, (e) => {
682
+ finish2();
683
+ throw e;
684
+ });
648
685
  }
649
686
  finish2();
650
687
  return;
@@ -665,26 +702,35 @@ function renderComponent(type, props, writer, isSvg) {
665
702
  } catch (e) {
666
703
  popComponentScope(savedScope);
667
704
  if (isProvider)
668
- ctx._currentValue = prevCtxValue;
705
+ popContextValue(ctx, prevCtxValue);
669
706
  throw e;
670
707
  }
671
708
  const finish = () => {
672
709
  popComponentScope(savedScope);
673
710
  if (isProvider)
674
- ctx._currentValue = prevCtxValue;
711
+ popContextValue(ctx, prevCtxValue);
675
712
  };
676
713
  if (result instanceof Promise) {
677
714
  return result.then((resolved) => {
678
715
  const r2 = renderNode(resolved, writer, isSvg);
679
716
  if (r2 && typeof r2.then === "function") {
680
- return r2.then(finish);
717
+ return r2.then(finish, (e) => {
718
+ finish();
719
+ throw e;
720
+ });
681
721
  }
682
722
  finish();
723
+ }, (e) => {
724
+ finish();
725
+ throw e;
683
726
  });
684
727
  }
685
728
  const r = renderNode(result, writer, isSvg);
686
729
  if (r && typeof r.then === "function") {
687
- return r.then(finish);
730
+ return r.then(finish, (e) => {
731
+ finish();
732
+ throw e;
733
+ });
688
734
  }
689
735
  finish();
690
736
  }
@@ -770,7 +816,7 @@ async function renderSuspense(props, writer, isSvg = false) {
770
816
  }
771
817
  function renderToStream(element) {
772
818
  const encoder = new TextEncoder();
773
- return new ReadableStream({
819
+ return runWithContextStore(() => new ReadableStream({
774
820
  async start(controller) {
775
821
  resetRenderState();
776
822
  const writer = {
@@ -793,42 +839,44 @@ function renderToStream(element) {
793
839
  controller.error(error);
794
840
  }
795
841
  }
796
- });
842
+ }));
797
843
  }
798
- 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;
844
+ function renderToString(element) {
845
+ return runWithContextStore(async () => {
846
+ for (let attempt = 0; attempt < MAX_SUSPENSE_RETRIES; attempt++) {
847
+ resetRenderState();
848
+ const chunks = [];
849
+ const writer = {
850
+ lastWasText: false,
851
+ write(c) {
852
+ chunks.push(c);
853
+ this.lastWasText = false;
854
+ },
855
+ text(s2) {
856
+ chunks.push(s2);
857
+ this.lastWasText = true;
858
+ }
859
+ };
860
+ try {
861
+ const r = renderNode(element, writer);
862
+ if (r && typeof r.then === "function")
863
+ await r;
864
+ return chunks.join("");
865
+ } catch (error) {
866
+ if (error && typeof error.then === "function") {
867
+ await error;
868
+ continue;
869
+ }
870
+ throw error;
822
871
  }
823
- throw error;
824
872
  }
825
- }
826
- throw new Error("[slim-react] renderToString exceeded maximum retries");
873
+ throw new Error("[slim-react] renderToString exceeded maximum retries");
874
+ });
827
875
  }
828
876
 
829
877
  // src/slim-react/index.ts
830
878
  function useContext(context) {
831
- return context._currentValue;
879
+ return getContextValue(context);
832
880
  }
833
881
  var Suspense = SUSPENSE_TYPE;
834
882
  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: {