@kubb/renderer-jsx 5.0.0-beta.2 → 5.0.0-beta.20

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/index.js CHANGED
@@ -4,14 +4,17 @@ import { Fragment, jsx } from "./jsx-runtime.js";
4
4
  import { createArrowFunction, createBreak, createConst, createExport, createFunction, createImport, createJsx, createSource, createText, createType } from "@kubb/ast";
5
5
  //#region ../../internals/utils/src/context.ts
6
6
  /**
7
- * Context stack for tracking the current context values
7
+ * Context stack for tracking the current context values.
8
+ *
9
+ * WeakMap keyed by symbol so entries are GC-eligible once no external code
10
+ * holds a reference to the context key — important for long-running agent
11
+ * builds where plugins create and discard context keys across repeated runs.
8
12
  *
9
- * Note: This uses a global Map for simplicity in code generation scenarios.
10
13
  * For concurrent runtime execution, consider using AsyncLocalStorage or
11
14
  * instance-based context management.
12
15
  */
13
- const contextStack = /* @__PURE__ */ new Map();
14
- const contextDefaults = /* @__PURE__ */ new Map();
16
+ const contextStack = /* @__PURE__ */ new WeakMap();
17
+ const contextDefaults = /* @__PURE__ */ new WeakMap();
15
18
  /**
16
19
  * Provides a value to descendant components (Vue 3 style)
17
20
  *
@@ -343,7 +346,7 @@ function Jsx({ children }) {
343
346
  Jsx.displayName = "Jsx";
344
347
  //#endregion
345
348
  //#region src/components/Root.tsx
346
- var import_react = require_react();
349
+ var import_react = /* @__PURE__ */ __toESM(require_react());
347
350
  var ErrorBoundary = class extends import_react.Component {
348
351
  state = { hasError: false };
349
352
  static displayName = "ErrorBoundary";
@@ -481,7 +484,7 @@ const nodeNames = new Set([
481
484
  const createNode = (nodeName) => {
482
485
  return {
483
486
  nodeName,
484
- attributes: /* @__PURE__ */ new Map(),
487
+ attributes: Object.create(null),
485
488
  childNodes: [],
486
489
  parentNode: void 0
487
490
  };
@@ -529,26 +532,22 @@ const removeChildNode = (node, removeNode) => {
529
532
  * Set an attribute on `node`, storing it in the node's `attributes` map.
530
533
  */
531
534
  const setAttribute = (node, key, value) => {
532
- node.attributes.set(key, value);
535
+ node.attributes[key] = value;
533
536
  };
534
537
  /**
535
538
  * Create a new {@link TextNode} with the given text value.
536
539
  */
537
540
  const createTextNode = (text) => {
538
- const node = {
541
+ return {
539
542
  nodeName: TEXT_NODE_NAME,
540
543
  nodeValue: text,
541
544
  parentNode: void 0
542
545
  };
543
- setTextNodeValue(node, text);
544
- return node;
545
546
  };
546
547
  /**
547
548
  * Update the `nodeValue` of an existing {@link TextNode}.
548
- * Non-string values are coerced to strings via `String(text)`.
549
549
  */
550
550
  const setTextNodeValue = (node, text) => {
551
- if (typeof text !== "string") text = String(text);
552
551
  node.nodeValue = text;
553
552
  };
554
553
  //#endregion
@@ -17817,7 +17816,7 @@ const Renderer = (0, import_react_reconciler.default)({
17817
17816
  return false;
17818
17817
  },
17819
17818
  supportsMutation: true,
17820
- isPrimaryRenderer: true,
17819
+ isPrimaryRenderer: false,
17821
17820
  supportsPersistence: false,
17822
17821
  supportsHydration: false,
17823
17822
  scheduleTimeout: setTimeout,
@@ -17883,20 +17882,24 @@ const Renderer = (0, import_react_reconciler.default)({
17883
17882
  });
17884
17883
  //#endregion
17885
17884
  //#region src/utils.ts
17885
+ function toBool$1(val) {
17886
+ return val ?? false;
17887
+ }
17888
+ __name(toBool$1, "toBool");
17886
17889
  /**
17887
17890
  * Collect the text and nested AST-node children of a single kubb-* element.
17888
17891
  *
17889
- * `#text` children become raw {@link TextNode}s; nested `kubb-function`, `kubb-const`,
17892
+ * `#text` children become raw text nodes; nested `kubb-function`, `kubb-const`,
17890
17893
  * `kubb-type`, and similar elements are converted into their respective {@link CodeNode}s.
17891
- * Any unrecognized DOM elements are silently skipped.
17894
+ * Any unrecognized element names are silently skipped.
17892
17895
  */
17893
- function collectChildNodes(element) {
17896
+ function collectCodeNodes$1(element) {
17894
17897
  const result = [];
17895
17898
  for (const child of element.childNodes) {
17896
17899
  if (!child) continue;
17897
17900
  if (child.nodeName === "#text") {
17898
17901
  const text = child.nodeValue;
17899
- if (text && text.trim().length > 0) result.push(createText(text));
17902
+ if (text && text.trim()) result.push(createText(text));
17900
17903
  continue;
17901
17904
  }
17902
17905
  if (child.nodeName === "br") {
@@ -17906,164 +17909,162 @@ function collectChildNodes(element) {
17906
17909
  if (child.nodeName === "kubb-function") {
17907
17910
  const attrs = child.attributes;
17908
17911
  result.push(createFunction({
17909
- name: attrs.get("name"),
17910
- params: attrs.get("params"),
17911
- export: attrs.get("export"),
17912
- default: attrs.get("default"),
17913
- async: attrs.get("async"),
17914
- generics: attrs.get("generics"),
17915
- returnType: attrs.get("returnType"),
17916
- JSDoc: attrs.get("JSDoc"),
17917
- nodes: collectChildNodes(child)
17912
+ name: attrs["name"],
17913
+ params: attrs["params"],
17914
+ export: attrs["export"],
17915
+ default: attrs["default"],
17916
+ async: attrs["async"],
17917
+ generics: attrs["generics"],
17918
+ returnType: attrs["returnType"],
17919
+ JSDoc: attrs["JSDoc"],
17920
+ nodes: collectCodeNodes$1(child)
17918
17921
  }));
17919
- } else if (child.nodeName === "kubb-arrow-function") {
17922
+ continue;
17923
+ }
17924
+ if (child.nodeName === "kubb-arrow-function") {
17920
17925
  const attrs = child.attributes;
17921
17926
  result.push(createArrowFunction({
17922
- name: attrs.get("name"),
17923
- params: attrs.get("params"),
17924
- export: attrs.get("export"),
17925
- default: attrs.get("default"),
17926
- async: attrs.get("async"),
17927
- generics: attrs.get("generics"),
17928
- returnType: attrs.get("returnType"),
17929
- singleLine: attrs.get("singleLine"),
17930
- JSDoc: attrs.get("JSDoc"),
17931
- nodes: collectChildNodes(child)
17927
+ name: attrs["name"],
17928
+ params: attrs["params"],
17929
+ export: attrs["export"],
17930
+ default: attrs["default"],
17931
+ async: attrs["async"],
17932
+ generics: attrs["generics"],
17933
+ returnType: attrs["returnType"],
17934
+ singleLine: attrs["singleLine"],
17935
+ JSDoc: attrs["JSDoc"],
17936
+ nodes: collectCodeNodes$1(child)
17932
17937
  }));
17933
- } else if (child.nodeName === "kubb-const") {
17938
+ continue;
17939
+ }
17940
+ if (child.nodeName === "kubb-const") {
17934
17941
  const attrs = child.attributes;
17935
17942
  result.push(createConst({
17936
- name: attrs.get("name"),
17937
- type: attrs.get("type"),
17938
- export: attrs.get("export"),
17939
- asConst: attrs.get("asConst"),
17940
- JSDoc: attrs.get("JSDoc"),
17941
- nodes: collectChildNodes(child)
17943
+ name: attrs["name"],
17944
+ type: attrs["type"],
17945
+ export: attrs["export"],
17946
+ asConst: attrs["asConst"],
17947
+ JSDoc: attrs["JSDoc"],
17948
+ nodes: collectCodeNodes$1(child)
17942
17949
  }));
17943
- } else if (child.nodeName === "kubb-type") {
17950
+ continue;
17951
+ }
17952
+ if (child.nodeName === "kubb-type") {
17944
17953
  const attrs = child.attributes;
17945
17954
  result.push(createType({
17946
- name: attrs.get("name"),
17947
- export: attrs.get("export"),
17948
- JSDoc: attrs.get("JSDoc"),
17949
- nodes: collectChildNodes(child)
17955
+ name: attrs["name"],
17956
+ export: attrs["export"],
17957
+ JSDoc: attrs["JSDoc"],
17958
+ nodes: collectCodeNodes$1(child)
17950
17959
  }));
17951
- } else if (child.nodeName === "kubb-jsx") {
17960
+ continue;
17961
+ }
17962
+ if (child.nodeName === "kubb-jsx") {
17952
17963
  const textChild = child.childNodes[0];
17953
17964
  const value = textChild?.nodeName === "#text" ? textChild.nodeValue : "";
17954
17965
  if (value) result.push(createJsx(value));
17966
+ continue;
17955
17967
  }
17956
17968
  }
17957
17969
  return result;
17958
17970
  }
17971
+ __name(collectCodeNodes$1, "collectCodeNodes");
17959
17972
  /**
17960
- * Traverse `node` and collect all `<kubb-source>` elements into a `Set<SourceNode>`.
17973
+ * Yields every {@link SourceNode}, {@link ExportNode}, and {@link ImportNode}
17974
+ * within a `<kubb-file>` subtree in a single tree walk.
17961
17975
  *
17962
- * Elements whose `nodeName` is in `ignores` are skipped entirely (including their subtrees).
17963
- * This is used to collect source blocks from a file node while excluding import/export subtrees.
17964
- *
17965
- * @example Collect sources while ignoring export and import elements
17966
- * ```ts
17967
- * const sources = squashSourceNodes(fileElement, ['kubb-export', 'kubb-import'])
17968
- * ```
17976
+ * Import and export elements are leaf nodes. Once yielded, the walker does not
17977
+ * recurse into them, which also prevents source collection from descending into
17978
+ * their subtrees. Dispatch on `.kind` (`'Source'`, `'Export'`, `'Import'`) to
17979
+ * separate the results.
17969
17980
  */
17970
- function squashSourceNodes(node, ignores) {
17971
- const ignoreSet = new Set(ignores);
17972
- const sources = /* @__PURE__ */ new Set();
17973
- const walk = (current) => {
17974
- for (const child of current.childNodes) {
17975
- if (!child) continue;
17976
- if (child.nodeName !== "#text" && ignoreSet.has(child.nodeName)) continue;
17977
- if (child.nodeName === "kubb-source") {
17978
- const source = createSource({
17979
- name: child.attributes.get("name")?.toString(),
17980
- isTypeOnly: child.attributes.get("isTypeOnly") ?? false,
17981
- isExportable: child.attributes.get("isExportable") ?? false,
17982
- isIndexable: child.attributes.get("isIndexable") ?? false,
17983
- nodes: collectChildNodes(child)
17984
- });
17985
- sources.add(source);
17986
- continue;
17987
- }
17988
- if (child.nodeName !== "#text" && nodeNames.has(child.nodeName)) walk(child);
17981
+ function* collectFileEntries(node) {
17982
+ for (const child of node.childNodes) {
17983
+ if (!child || child.nodeName === "#text") continue;
17984
+ if (child.nodeName === "kubb-source") {
17985
+ yield createSource({
17986
+ name: child.attributes["name"]?.toString(),
17987
+ isTypeOnly: toBool$1(child.attributes["isTypeOnly"]),
17988
+ isExportable: toBool$1(child.attributes["isExportable"]),
17989
+ isIndexable: toBool$1(child.attributes["isIndexable"]),
17990
+ nodes: collectCodeNodes$1(child)
17991
+ });
17992
+ continue;
17989
17993
  }
17990
- };
17991
- walk(node);
17992
- return sources;
17994
+ if (child.nodeName === "kubb-export") {
17995
+ yield createExport({
17996
+ name: child.attributes["name"],
17997
+ path: child.attributes["path"],
17998
+ isTypeOnly: toBool$1(child.attributes["isTypeOnly"]),
17999
+ asAlias: toBool$1(child.attributes["asAlias"])
18000
+ });
18001
+ continue;
18002
+ }
18003
+ if (child.nodeName === "kubb-import") {
18004
+ yield createImport({
18005
+ name: child.attributes["name"],
18006
+ path: child.attributes["path"],
18007
+ root: child.attributes["root"],
18008
+ isTypeOnly: toBool$1(child.attributes["isTypeOnly"]),
18009
+ isNameSpace: toBool$1(child.attributes["isNameSpace"])
18010
+ });
18011
+ continue;
18012
+ }
18013
+ if (nodeNames.has(child.nodeName)) yield* collectFileEntries(child);
18014
+ }
17993
18015
  }
17994
18016
  /**
17995
- * Traverse `node` and collect all `<kubb-export>` elements into a `Set<ExportNode>`.
18017
+ * Runs a single {@link collectFileEntries} pass over a `<kubb-file>` DOM element
18018
+ * and assembles the result into a {@link FileNode}, bucketing each yielded
18019
+ * node by its `.kind`.
17996
18020
  */
17997
- function squashExportNodes(node) {
17998
- const exports = /* @__PURE__ */ new Set();
17999
- const walk = (current) => {
18000
- for (const child of current.childNodes) {
18001
- if (!child) continue;
18002
- if (child.nodeName !== "#text" && nodeNames.has(child.nodeName)) walk(child);
18003
- if (child.nodeName === "kubb-export") exports.add(createExport({
18004
- name: child.attributes.get("name"),
18005
- path: child.attributes.get("path"),
18006
- isTypeOnly: child.attributes.get("isTypeOnly") ?? false,
18007
- asAlias: child.attributes.get("asAlias") ?? false
18008
- }));
18021
+ function createFileNode(child) {
18022
+ const sources = [];
18023
+ const exports = [];
18024
+ const imports = [];
18025
+ for (const node of collectFileEntries(child)) {
18026
+ if (node.kind === "Source") {
18027
+ sources.push(node);
18028
+ continue;
18029
+ }
18030
+ if (node.kind === "Export") {
18031
+ exports.push(node);
18032
+ continue;
18009
18033
  }
18034
+ imports.push(node);
18035
+ }
18036
+ return {
18037
+ baseName: child.attributes["baseName"],
18038
+ path: child.attributes["path"],
18039
+ meta: child.attributes["meta"] || {},
18040
+ footer: child.attributes["footer"],
18041
+ banner: child.attributes["banner"],
18042
+ sources,
18043
+ exports,
18044
+ imports
18010
18045
  };
18011
- walk(node);
18012
- return exports;
18013
18046
  }
18014
18047
  /**
18015
- * Traverse `node` and collect all `<kubb-import>` elements into a `Set<ImportNode>`.
18048
+ * Yields each {@link FileNode} as it is encountered during the tree walk,
18049
+ * without collecting into an intermediate array. Callers can begin processing
18050
+ * each file before the rest of the tree is traversed.
18016
18051
  */
18017
- function squashImportNodes(node) {
18018
- const imports = /* @__PURE__ */ new Set();
18019
- const walk = (current) => {
18020
- for (const child of current.childNodes) {
18021
- if (!child) continue;
18022
- if (child.nodeName !== "#text" && nodeNames.has(child.nodeName)) walk(child);
18023
- if (child.nodeName === "kubb-import") imports.add(createImport({
18024
- name: child.attributes.get("name"),
18025
- path: child.attributes.get("path"),
18026
- root: child.attributes.get("root"),
18027
- isTypeOnly: child.attributes.get("isTypeOnly") ?? false,
18028
- isNameSpace: child.attributes.get("isNameSpace") ?? false
18029
- }));
18030
- }
18031
- };
18032
- walk(node);
18033
- return imports;
18052
+ function* streamFiles(node) {
18053
+ for (const child of node.childNodes) {
18054
+ if (!child) continue;
18055
+ if (child.nodeName !== "#text" && child.nodeName !== "kubb-file" && nodeNames.has(child.nodeName)) yield* streamFiles(child);
18056
+ if (child.nodeName === "kubb-file" && child.attributes["baseName"] !== void 0 && child.attributes["path"] !== void 0) yield createFileNode(child);
18057
+ }
18034
18058
  }
18035
18059
  /**
18036
18060
  * Walk the virtual DOM tree rooted at `node` and convert every `<kubb-file>` element
18037
18061
  * into a {@link FileNode}, collecting its source blocks, imports, and exports.
18038
18062
  *
18039
- * Returns the list of file nodes in document order. Nested files are supported
18063
+ * Returns the list of file nodes in document order. Nested files are supported;
18040
18064
  * the walker descends into non-file elements and recurses through them.
18041
18065
  */
18042
- function processFiles(node) {
18043
- const collected = [];
18044
- function walk(current) {
18045
- for (const child of current.childNodes) {
18046
- if (!child) continue;
18047
- if (child.nodeName !== "#text" && child.nodeName !== "kubb-file" && nodeNames.has(child.nodeName)) walk(child);
18048
- if (child.nodeName === "kubb-file") {
18049
- if (child.attributes.has("baseName") && child.attributes.has("path")) {
18050
- const sources = squashSourceNodes(child, ["kubb-export", "kubb-import"]);
18051
- collected.push({
18052
- baseName: child.attributes.get("baseName"),
18053
- path: child.attributes.get("path"),
18054
- meta: child.attributes.get("meta") || {},
18055
- footer: child.attributes.get("footer"),
18056
- banner: child.attributes.get("banner"),
18057
- sources: [...sources],
18058
- exports: [...squashExportNodes(child)],
18059
- imports: [...squashImportNodes(child)]
18060
- });
18061
- }
18062
- }
18063
- }
18064
- }
18065
- walk(node);
18066
- return collected;
18066
+ function collectFiles(node) {
18067
+ return [...streamFiles(node)];
18067
18068
  }
18068
18069
  //#endregion
18069
18070
  //#region src/Runtime.tsx
@@ -18075,7 +18076,7 @@ var Runtime = class {
18075
18076
  nodes = [];
18076
18077
  #container;
18077
18078
  #rootNode;
18078
- constructor(options) {
18079
+ constructor(options = {}) {
18079
18080
  this.#options = options;
18080
18081
  this.#rootNode = createNode("kubb-root");
18081
18082
  this.#rootNode.onRender = this.onRender;
@@ -18088,7 +18089,7 @@ var Runtime = class {
18088
18089
  console.log(data);
18089
18090
  };
18090
18091
  const logRecoverableError = typeof reportError === "function" ? reportError : console.error;
18091
- const rootTag = import_constants.ConcurrentRoot;
18092
+ const rootTag = import_constants.LegacyRoot;
18092
18093
  this.#container = Renderer.createContainer(this.#rootNode, rootTag, null, false, false, "id", logRecoverableError, logRecoverableError, logRecoverableError, null);
18093
18094
  this.unsubscribeExit = onProcessExit((code) => {
18094
18095
  this.unmount(code);
@@ -18101,7 +18102,7 @@ var Runtime = class {
18101
18102
  onRender = () => {
18102
18103
  const task = this.#renderPromise.catch(() => {}).then(() => {
18103
18104
  if (this.#isUnmounted) return;
18104
- const files = processFiles(this.#rootNode);
18105
+ const files = collectFiles(this.#rootNode);
18105
18106
  this.nodes.push(...files);
18106
18107
  if (!this.#options?.debug) return;
18107
18108
  });
@@ -18146,43 +18147,260 @@ var Runtime = class {
18146
18147
  }
18147
18148
  this.resolveExitPromise();
18148
18149
  }
18149
- async waitUntilExit() {
18150
- if (!this.exitPromise) this.exitPromise = new Promise((resolve, reject) => {
18151
- this.resolveExitPromise = resolve;
18152
- this.rejectExitPromise = reject;
18153
- });
18154
- return this.exitPromise;
18150
+ };
18151
+ //#endregion
18152
+ //#region src/SyncRuntime.tsx
18153
+ /**
18154
+ * Walks `element`, resolving arrays, Fragments, and function components
18155
+ * transparently, then calls `onText` for primitive values and `onHost` for
18156
+ * every host element encountered. Pure function components are called
18157
+ * synchronously; hooks and class components are not supported.
18158
+ */
18159
+ function walkElement(element, onText, onHost) {
18160
+ if (element == null || typeof element === "boolean") return;
18161
+ if (typeof element === "string" || typeof element === "number" || typeof element === "bigint") {
18162
+ onText(String(element));
18163
+ return;
18164
+ }
18165
+ if (Array.isArray(element)) {
18166
+ for (const child of element) walkElement(child, onText, onHost);
18167
+ return;
18168
+ }
18169
+ if (typeof element === "object" && "$$typeof" in element) {
18170
+ const el = element;
18171
+ const { type } = el;
18172
+ const props = el.props;
18173
+ if (type === import_react.Fragment) {
18174
+ walkElement(props["children"], onText, onHost);
18175
+ return;
18176
+ }
18177
+ if (typeof type === "function") {
18178
+ walkElement(type(props), onText, onHost);
18179
+ return;
18180
+ }
18181
+ if (typeof type === "string") onHost(type, props);
18182
+ }
18183
+ }
18184
+ function toBool(val) {
18185
+ return val ?? false;
18186
+ }
18187
+ function collectCodeNodes(props) {
18188
+ const nodes = [];
18189
+ collectCode(props["children"], nodes);
18190
+ return nodes;
18191
+ }
18192
+ function collectCode(element, nodes) {
18193
+ walkElement(element, (text) => {
18194
+ if (text.trim()) nodes.push(createText(text));
18195
+ }, (type, props) => resolveCodeNode(type, props, nodes));
18196
+ }
18197
+ function resolveCodeNode(type, props, nodes) {
18198
+ if (type === "br") {
18199
+ nodes.push(createBreak());
18200
+ return;
18201
+ }
18202
+ if (type === "kubb-jsx") {
18203
+ let value = "";
18204
+ walkElement(props["children"], (t) => {
18205
+ value += t;
18206
+ }, () => {});
18207
+ if (value) nodes.push(createJsx(value));
18208
+ return;
18209
+ }
18210
+ if (type === "kubb-function") {
18211
+ nodes.push(createFunction({
18212
+ name: props["name"],
18213
+ params: props["params"],
18214
+ export: props["export"],
18215
+ default: props["default"],
18216
+ async: props["async"],
18217
+ generics: props["generics"],
18218
+ returnType: props["returnType"],
18219
+ JSDoc: props["JSDoc"],
18220
+ nodes: collectCodeNodes(props)
18221
+ }));
18222
+ return;
18223
+ }
18224
+ if (type === "kubb-arrow-function") {
18225
+ nodes.push(createArrowFunction({
18226
+ name: props["name"],
18227
+ params: props["params"],
18228
+ export: props["export"],
18229
+ default: props["default"],
18230
+ async: props["async"],
18231
+ generics: props["generics"],
18232
+ returnType: props["returnType"],
18233
+ singleLine: props["singleLine"],
18234
+ JSDoc: props["JSDoc"],
18235
+ nodes: collectCodeNodes(props)
18236
+ }));
18237
+ return;
18238
+ }
18239
+ if (type === "kubb-const") {
18240
+ nodes.push(createConst({
18241
+ name: props["name"],
18242
+ type: props["type"],
18243
+ export: props["export"],
18244
+ asConst: props["asConst"],
18245
+ JSDoc: props["JSDoc"],
18246
+ nodes: collectCodeNodes(props)
18247
+ }));
18248
+ return;
18249
+ }
18250
+ if (type === "kubb-type") {
18251
+ nodes.push(createType({
18252
+ name: props["name"],
18253
+ export: props["export"],
18254
+ JSDoc: props["JSDoc"],
18255
+ nodes: collectCodeNodes(props)
18256
+ }));
18257
+ return;
18258
+ }
18259
+ }
18260
+ function collectFileChildren(element) {
18261
+ const sources = [];
18262
+ const exports = [];
18263
+ const imports = [];
18264
+ walkElement(element, (text) => {
18265
+ if (text.trim()) throw new Error(`[react] '${text}' should be part of <File.Source> component when using the <File/> component`);
18266
+ }, (type, props) => {
18267
+ if (type === "kubb-source") {
18268
+ sources.push(createSource({
18269
+ name: props["name"]?.toString(),
18270
+ isTypeOnly: toBool(props["isTypeOnly"]),
18271
+ isExportable: toBool(props["isExportable"]),
18272
+ isIndexable: toBool(props["isIndexable"]),
18273
+ nodes: collectCodeNodes(props)
18274
+ }));
18275
+ return;
18276
+ }
18277
+ if (type === "kubb-export") {
18278
+ exports.push(createExport({
18279
+ name: props["name"],
18280
+ path: props["path"],
18281
+ isTypeOnly: toBool(props["isTypeOnly"]),
18282
+ asAlias: toBool(props["asAlias"])
18283
+ }));
18284
+ return;
18285
+ }
18286
+ if (type === "kubb-import") {
18287
+ imports.push(createImport({
18288
+ name: props["name"],
18289
+ path: props["path"],
18290
+ root: props["root"],
18291
+ isTypeOnly: toBool(props["isTypeOnly"]),
18292
+ isNameSpace: toBool(props["isNameSpace"])
18293
+ }));
18294
+ return;
18295
+ }
18296
+ const nested = collectFileChildren(props["children"]);
18297
+ sources.push(...nested.sources);
18298
+ exports.push(...nested.exports);
18299
+ imports.push(...nested.imports);
18300
+ });
18301
+ return {
18302
+ sources,
18303
+ exports,
18304
+ imports
18305
+ };
18306
+ }
18307
+ function* walkFiles(element) {
18308
+ if (element == null || typeof element === "boolean") return;
18309
+ if (typeof element === "string" || typeof element === "number" || typeof element === "bigint") return;
18310
+ if (Array.isArray(element)) {
18311
+ for (const child of element) yield* walkFiles(child);
18312
+ return;
18313
+ }
18314
+ if (typeof element === "object" && "$$typeof" in element) {
18315
+ const el = element;
18316
+ const { type } = el;
18317
+ const props = el.props;
18318
+ if (type === import_react.Fragment) {
18319
+ yield* walkFiles(props["children"]);
18320
+ return;
18321
+ }
18322
+ if (typeof type === "function") {
18323
+ yield* walkFiles(type(props));
18324
+ return;
18325
+ }
18326
+ if (typeof type === "string") if (type === "kubb-file" && props["baseName"] !== void 0 && props["path"] !== void 0) {
18327
+ const { sources, exports, imports } = collectFileChildren(props["children"]);
18328
+ yield {
18329
+ baseName: props["baseName"],
18330
+ path: props["path"],
18331
+ meta: props["meta"] || {},
18332
+ footer: props["footer"],
18333
+ banner: props["banner"],
18334
+ sources,
18335
+ exports,
18336
+ imports
18337
+ };
18338
+ } else yield* walkFiles(props["children"]);
18339
+ }
18340
+ }
18341
+ /**
18342
+ * Synchronous JSX renderer that walks the element tree in a single pass,
18343
+ * producing {@link FileNode} objects directly without an intermediate virtual
18344
+ * DOM. No React fiber, scheduler, or work loop is involved.
18345
+ *
18346
+ * All components must be pure functions; hooks and class components are not
18347
+ * supported. Produces identical output to the React-backed {@link Runtime} at
18348
+ * approximately 2–4× the speed and a fraction of the allocations.
18349
+ */
18350
+ var SyncRuntime = class {
18351
+ /**
18352
+ * Accumulated {@link FileNode} results from every {@link render} call.
18353
+ */
18354
+ nodes = [];
18355
+ /**
18356
+ * Walks `element` synchronously, converts every `<kubb-file>` subtree into
18357
+ * a {@link FileNode} with no intermediate virtual DOM, and appends the results
18358
+ * to {@link nodes}.
18359
+ */
18360
+ render(element) {
18361
+ for (const file of walkFiles(element)) this.nodes.push(file);
18362
+ }
18363
+ /**
18364
+ * Walks `element` synchronously and yields each {@link FileNode} as it is
18365
+ * produced, without buffering into an intermediate array first. Callers can
18366
+ * begin processing each file before the rest of the element tree is traversed.
18367
+ *
18368
+ * @example
18369
+ * ```ts
18370
+ * for (const file of runtime.stream(element)) {
18371
+ * await writeFile(file)
18372
+ * }
18373
+ * ```
18374
+ */
18375
+ *stream(element) {
18376
+ yield* walkFiles(element);
18155
18377
  }
18156
18378
  };
18157
18379
  //#endregion
18158
18380
  //#region src/createRenderer.tsx
18159
18381
  /**
18160
- * Create a Kubb JSX renderer.
18382
+ * Renderer factory for generators that produce JSX output.
18161
18383
  *
18162
- * The renderer converts a React JSX element tree built from the components in this
18163
- * package into an array of {@link FileNode} entries representing the generated files.
18384
+ * Pass as the `renderer` property of `defineGenerator`. Core drives rendering
18385
+ * without a hard dependency on `@kubb/renderer-jsx`.
18164
18386
  *
18165
- * @example Basic usage
18387
+ * @example
18166
18388
  * ```ts
18167
- * import { createRenderer, File } from '@kubb/renderer-jsx'
18389
+ * import { jsxRenderer } from '@kubb/renderer-jsx'
18168
18390
  *
18169
- * const renderer = createRenderer()
18170
- * await renderer.render(
18171
- * <File baseName="pet.ts" path="src/models/pet.ts">
18172
- * <File.Source name="Pet" isExportable isIndexable>
18173
- * {`export type Pet = { id: number; name: string }`}
18174
- * </File.Source>
18175
- * </File>
18176
- * )
18177
- * console.log(renderer.files) // [FileNode]
18178
- * renderer.unmount()
18391
+ * export const myGenerator = defineGenerator<PluginTs>({
18392
+ * renderer: jsxRenderer,
18393
+ * schema(node, options) {
18394
+ * return <File baseName="output.ts" path="src/output.ts">...</File>
18395
+ * },
18396
+ * })
18179
18397
  * ```
18180
18398
  */
18181
- function createRenderer(options = {}) {
18182
- const runtime = new Runtime(options);
18399
+ const jsxRenderer = () => {
18400
+ const runtime = new Runtime();
18183
18401
  return {
18184
- async render(Element) {
18185
- await runtime.render(Element);
18402
+ async render(element) {
18403
+ await runtime.render(element);
18186
18404
  },
18187
18405
  get files() {
18188
18406
  return runtime.nodes;
@@ -18191,30 +18409,49 @@ function createRenderer(options = {}) {
18191
18409
  runtime.unmount(error);
18192
18410
  }
18193
18411
  };
18194
- }
18412
+ };
18195
18413
  /**
18196
- * A renderer factory for generators that produce JSX output.
18414
+ * Lightweight renderer factory with no React fiber, scheduler, or work loop.
18197
18415
  *
18198
- * Pass this as the `renderer` property of a `defineGenerator` call so that
18199
- * core can render the JSX element tree returned by your generator methods
18200
- * without a hard dependency on `@kubb/renderer-jsx`.
18416
+ * Walks the JSX element tree in a single recursive pass. All components must be
18417
+ * pure functions; hooks and class components are not supported. Drop-in
18418
+ * replacement for {@link jsxRenderer} at approximately 2–4× the speed.
18201
18419
  *
18202
- * @example
18420
+ * @example Drop-in replacement
18203
18421
  * ```ts
18204
- * import { jsxRenderer } from '@kubb/renderer-jsx'
18205
- * import { defineGenerator } from '@kubb/core'
18422
+ * import { jsxRendererSync } from '@kubb/renderer-jsx'
18206
18423
  *
18207
18424
  * export const myGenerator = defineGenerator<PluginTs>({
18208
- * name: 'my-generator',
18209
- * renderer: jsxRenderer,
18425
+ * renderer: jsxRendererSync,
18210
18426
  * schema(node, options) {
18211
18427
  * return <File baseName="output.ts" path="src/output.ts">...</File>
18212
18428
  * },
18213
18429
  * })
18214
18430
  * ```
18431
+ *
18432
+ * @example Stream files as they are produced
18433
+ * ```ts
18434
+ * for await (const file of jsxRendererSync().stream(element)) {
18435
+ * await writeFile(file)
18436
+ * }
18437
+ * ```
18215
18438
  */
18216
- const jsxRenderer = () => createRenderer();
18439
+ const jsxRendererSync = () => {
18440
+ const runtime = new SyncRuntime();
18441
+ return {
18442
+ async render(element) {
18443
+ runtime.render(element);
18444
+ },
18445
+ get files() {
18446
+ return runtime.nodes;
18447
+ },
18448
+ stream(element) {
18449
+ return runtime.stream(element);
18450
+ },
18451
+ unmount(_error) {}
18452
+ };
18453
+ };
18217
18454
  //#endregion
18218
- export { Const, File, Function$1 as Function, Jsx, Root, Type, createContext, createRenderer, inject, jsxRenderer, provide, unprovide };
18455
+ export { Const, File, Function$1 as Function, Jsx, Root, Type, createContext, inject, jsxRenderer, jsxRendererSync, provide, unprovide };
18219
18456
 
18220
18457
  //# sourceMappingURL=index.js.map