@kubb/renderer-jsx 5.0.0-beta.4 → 5.0.0-beta.40

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.
Files changed (45) hide show
  1. package/README.md +134 -0
  2. package/dist/index.cjs +621 -193
  3. package/dist/index.d.ts +326 -90
  4. package/dist/index.js +615 -193
  5. package/dist/jsx-dev-runtime.cjs +1 -1
  6. package/dist/jsx-dev-runtime.d.ts +7 -8
  7. package/dist/jsx-dev-runtime.js +2 -2
  8. package/dist/{jsx-namespace-CNp0arTN.d.ts → jsx-namespace-dmStM1a2.d.ts} +3 -3
  9. package/dist/{jsx-runtime-DdmO3p0U.cjs → jsx-runtime-4M1bV6ub.cjs} +9 -9
  10. package/dist/{jsx-runtime-Cvu_ZYgL.js → jsx-runtime-CQ6-_gue.js} +10 -10
  11. package/dist/jsx-runtime.cjs +1 -1
  12. package/dist/jsx-runtime.d.ts +9 -10
  13. package/dist/jsx-runtime.js +2 -2
  14. package/dist/{types-nAFMiWFw.d.ts → types-B5VGpHs0.d.ts} +11 -10
  15. package/dist/types.d.ts +1 -1
  16. package/package.json +8 -12
  17. package/src/Renderer.ts +1 -5
  18. package/src/Runtime.tsx +7 -18
  19. package/src/SyncRuntime.tsx +309 -0
  20. package/src/components/Callout.tsx +59 -0
  21. package/src/components/CodeBlock.tsx +37 -0
  22. package/src/components/Const.tsx +4 -4
  23. package/src/components/File.tsx +7 -5
  24. package/src/components/Frontmatter.tsx +38 -0
  25. package/src/components/Function.tsx +8 -8
  26. package/src/components/Heading.tsx +34 -0
  27. package/src/components/Jsx.tsx +1 -1
  28. package/src/components/List.tsx +40 -0
  29. package/src/components/Paragraph.tsx +28 -0
  30. package/src/components/Type.tsx +3 -3
  31. package/src/constants.ts +19 -9
  32. package/src/createRenderer.tsx +81 -62
  33. package/src/dom.ts +7 -19
  34. package/src/index.ts +7 -1
  35. package/src/types.ts +9 -8
  36. package/src/utils.ts +153 -174
  37. package/dist/index.cjs.map +0 -1
  38. package/dist/index.js.map +0 -1
  39. package/dist/jsx-dev-runtime.cjs.map +0 -1
  40. package/dist/jsx-dev-runtime.js.map +0 -1
  41. package/dist/jsx-runtime-Cvu_ZYgL.js.map +0 -1
  42. package/dist/jsx-runtime-DdmO3p0U.cjs.map +0 -1
  43. package/dist/jsx-runtime.cjs.map +0 -1
  44. package/dist/jsx-runtime.js.map +0 -1
  45. /package/dist/{chunk-Bb7HlUDG.js → chunk-DoukXa0m.js} +0 -0
package/dist/index.js CHANGED
@@ -1,17 +1,21 @@
1
- import { n as __name, r as __toESM, t as __commonJSMin } from "./chunk-Bb7HlUDG.js";
2
- import { n as require_react } from "./jsx-runtime-Cvu_ZYgL.js";
1
+ import { n as __name, r as __toESM, t as __commonJSMin } from "./chunk-DoukXa0m.js";
2
+ import { n as require_react } from "./jsx-runtime-CQ6-_gue.js";
3
3
  import { Fragment, jsx } from "./jsx-runtime.js";
4
+ import { stringify } from "yaml";
4
5
  import { createArrowFunction, createBreak, createConst, createExport, createFunction, createImport, createJsx, createSource, createText, createType } from "@kubb/ast";
5
6
  //#region ../../internals/utils/src/context.ts
6
7
  /**
7
- * Context stack for tracking the current context values
8
+ * Context stack for tracking the current context values.
9
+ *
10
+ * WeakMap keyed by symbol so entries are GC-eligible once no external code
11
+ * holds a reference to the context key — important for long-running agent
12
+ * builds where plugins create and discard context keys across repeated runs.
8
13
  *
9
- * Note: This uses a global Map for simplicity in code generation scenarios.
10
14
  * For concurrent runtime execution, consider using AsyncLocalStorage or
11
15
  * instance-based context management.
12
16
  */
13
- const contextStack = /* @__PURE__ */ new Map();
14
- const contextDefaults = /* @__PURE__ */ new Map();
17
+ const contextStack = /* @__PURE__ */ new WeakMap();
18
+ const contextDefaults = /* @__PURE__ */ new WeakMap();
15
19
  /**
16
20
  * Provides a value to descendant components (Vue 3 style)
17
21
  *
@@ -106,6 +110,64 @@ function onProcessExit(callback) {
106
110
  return unsubscribe;
107
111
  }
108
112
  //#endregion
113
+ //#region src/components/Callout.tsx
114
+ const CALLOUT_LABEL = {
115
+ tip: "TIP",
116
+ note: "NOTE",
117
+ important: "IMPORTANT",
118
+ warning: "WARNING",
119
+ caution: "CAUTION"
120
+ };
121
+ /**
122
+ * Renders a GitHub-style alert callout, portable across GitHub, GitLab,
123
+ * VitePress, Obsidian, and MDX.
124
+ *
125
+ * Emits a `<File.Source>` block containing `> [!TYPE] Title` followed by the
126
+ * body with every line prefixed by `> `.
127
+ *
128
+ * @example
129
+ * ```tsx
130
+ * <Callout type="tip">Run `kubb start --watch` to keep the generator hot.</Callout>
131
+ * // > [!TIP]
132
+ * // > Run `kubb start --watch` to keep the generator hot.
133
+ *
134
+ * <Callout type="warning" title="Heads up">Breaking change in v6.</Callout>
135
+ * // > [!WARNING] Heads up
136
+ * // > Breaking change in v6.
137
+ * ```
138
+ */
139
+ function Callout({ type, title, children }) {
140
+ const label = CALLOUT_LABEL[type];
141
+ return /* @__PURE__ */ jsx("kubb-source", {
142
+ name: "callout",
143
+ children: `${title ? `> [!${label}] ${title}` : `> [!${label}]`}\n${children.trimEnd().split("\n").map((line) => line.length > 0 ? `> ${line}` : ">").join("\n")}`
144
+ });
145
+ }
146
+ Callout.displayName = "Callout";
147
+ //#endregion
148
+ //#region src/components/CodeBlock.tsx
149
+ /**
150
+ * Renders a fenced markdown code block.
151
+ *
152
+ * Emits a `<File.Source>` block containing the children wrapped in
153
+ * triple-backtick fences with an optional language tag.
154
+ *
155
+ * @example
156
+ * ```tsx
157
+ * <CodeBlock lang="typescript">{'const pet = { id: 1 }'}</CodeBlock>
158
+ * // ```typescript
159
+ * // const pet = { id: 1 }
160
+ * // ```
161
+ * ```
162
+ */
163
+ function CodeBlock({ lang, children }) {
164
+ return /* @__PURE__ */ jsx("kubb-source", {
165
+ name: "codeBlock",
166
+ children: `\`\`\`${lang ?? ""}\n${children}\n\`\`\``
167
+ });
168
+ }
169
+ CodeBlock.displayName = "CodeBlock";
170
+ //#endregion
109
171
  //#region src/components/Const.tsx
110
172
  /**
111
173
  * Generates a TypeScript constant declaration.
@@ -260,6 +322,33 @@ File.Export = FileExport;
260
322
  File.Import = FileImport;
261
323
  File.Source = FileSource;
262
324
  //#endregion
325
+ //#region src/components/Frontmatter.tsx
326
+ /**
327
+ * Emits a YAML frontmatter envelope at the top of a generated markdown file.
328
+ *
329
+ * Renders a `<File.Source>` block containing `---\n<yaml>\n---`. Place it as
330
+ * the first child of `<File>` so it appears at the top of the output. Pair with
331
+ * `parserMd` to write `.md` files that downstream tooling (VitePress, MDX,
332
+ * static-site generators) treats as frontmatter.
333
+ *
334
+ * @example Page frontmatter at the top of a generated markdown file
335
+ * ```tsx
336
+ * <File baseName="pets.md" path="src/pets.md">
337
+ * <Frontmatter data={{ title: 'Pets', layout: 'doc' }} />
338
+ * <File.Source>
339
+ * {'# Pets\n\nList of pets.'}
340
+ * </File.Source>
341
+ * </File>
342
+ * ```
343
+ */
344
+ function Frontmatter({ data }) {
345
+ return /* @__PURE__ */ jsx("kubb-source", {
346
+ name: "frontmatter",
347
+ children: `---\n${stringify(data).trimEnd()}\n---`
348
+ });
349
+ }
350
+ Frontmatter.displayName = "Frontmatter";
351
+ //#endregion
263
352
  //#region src/components/Function.tsx
264
353
  /**
265
354
  * Generates a TypeScript function declaration.
@@ -321,13 +410,34 @@ function ArrowFunction({ children, ...props }) {
321
410
  ArrowFunction.displayName = "ArrowFunction";
322
411
  Function$1.Arrow = ArrowFunction;
323
412
  //#endregion
413
+ //#region src/components/Heading.tsx
414
+ /**
415
+ * Renders an ATX-style markdown heading.
416
+ *
417
+ * Emits a `<File.Source>` block containing `${'#'.repeat(level)} ${children}`.
418
+ * Use inside a `<File>` rendered by `parserMd`.
419
+ *
420
+ * @example
421
+ * ```tsx
422
+ * <Heading level={2}>Installation</Heading>
423
+ * // ## Installation
424
+ * ```
425
+ */
426
+ function Heading({ level, children }) {
427
+ return /* @__PURE__ */ jsx("kubb-source", {
428
+ name: "heading",
429
+ children: `${"#".repeat(level)} ${children}`
430
+ });
431
+ }
432
+ Heading.displayName = "Heading";
433
+ //#endregion
324
434
  //#region src/components/Jsx.tsx
325
435
  /**
326
436
  * Embeds a raw JSX string verbatim in the generated source code.
327
437
  *
328
438
  * Use this component when you need to include JSX markup (including fragments
329
439
  * `<>…</>`) in the body of a generated function or component. The `children`
330
- * prop must be a plain string expression attributes that reference runtime
440
+ * prop must be a plain string, expression attributes that reference runtime
331
441
  * values should be written as template literals.
332
442
  *
333
443
  * @example
@@ -342,8 +452,55 @@ function Jsx({ children }) {
342
452
  }
343
453
  Jsx.displayName = "Jsx";
344
454
  //#endregion
455
+ //#region src/components/List.tsx
456
+ /**
457
+ * Renders a markdown list.
458
+ *
459
+ * Emits a `<File.Source>` block containing one entry per line, prefixed with
460
+ * `1.` / `2.` … when `ordered`, or `-` otherwise.
461
+ *
462
+ * @example
463
+ * ```tsx
464
+ * <List items={['Add the parser', 'Render the page']} />
465
+ * // - Add the parser
466
+ * // - Render the page
467
+ *
468
+ * <List ordered items={['First', 'Second']} />
469
+ * // 1. First
470
+ * // 2. Second
471
+ * ```
472
+ */
473
+ function List({ ordered, items }) {
474
+ return /* @__PURE__ */ jsx("kubb-source", {
475
+ name: "list",
476
+ children: items.map((item, index) => `${ordered ? `${index + 1}.` : "-"} ${item}`).join("\n")
477
+ });
478
+ }
479
+ List.displayName = "List";
480
+ //#endregion
481
+ //#region src/components/Paragraph.tsx
482
+ /**
483
+ * Renders a markdown paragraph.
484
+ *
485
+ * Emits a `<File.Source>` block containing the text as-is. Paragraphs are
486
+ * separated from surrounding blocks by blank lines via the parser's source
487
+ * joining.
488
+ *
489
+ * @example
490
+ * ```tsx
491
+ * <Paragraph>{'A pet object with `id` and `name` fields.'}</Paragraph>
492
+ * ```
493
+ */
494
+ function Paragraph({ children }) {
495
+ return /* @__PURE__ */ jsx("kubb-source", {
496
+ name: "paragraph",
497
+ children
498
+ });
499
+ }
500
+ Paragraph.displayName = "Paragraph";
501
+ //#endregion
345
502
  //#region src/components/Root.tsx
346
- var import_react = require_react();
503
+ var import_react = /* @__PURE__ */ __toESM(require_react());
347
504
  var ErrorBoundary = class extends import_react.Component {
348
505
  state = { hasError: false };
349
506
  static displayName = "ErrorBoundary";
@@ -378,7 +535,7 @@ Root.displayName = "Root";
378
535
  /**
379
536
  * Generates a TypeScript type alias declaration.
380
537
  *
381
- * Throws if `name` does not start with an uppercase letter TypeScript type aliases
538
+ * Throws if `name` does not start with an uppercase letter. TypeScript type aliases
382
539
  * should follow PascalCase naming conventions.
383
540
  *
384
541
  * @example Simple exported type alias
@@ -408,7 +565,7 @@ function Type({ children, ...props }) {
408
565
  }
409
566
  Type.displayName = "Type";
410
567
  //#endregion
411
- //#region ../../node_modules/.pnpm/react-reconciler@0.33.0_react@19.2.5/node_modules/react-reconciler/cjs/react-reconciler-constants.production.js
568
+ //#region ../../node_modules/.pnpm/react-reconciler@0.33.0_react@19.2.6/node_modules/react-reconciler/cjs/react-reconciler-constants.production.js
412
569
  /**
413
570
  * @license React
414
571
  * react-reconciler-constants.production.js
@@ -428,7 +585,7 @@ var require_react_reconciler_constants_production = /* @__PURE__ */ __commonJSMi
428
585
  exports.NoEventPriority = 0;
429
586
  }));
430
587
  //#endregion
431
- //#region ../../node_modules/.pnpm/react-reconciler@0.33.0_react@19.2.5/node_modules/react-reconciler/cjs/react-reconciler-constants.development.js
588
+ //#region ../../node_modules/.pnpm/react-reconciler@0.33.0_react@19.2.6/node_modules/react-reconciler/cjs/react-reconciler-constants.development.js
432
589
  /**
433
590
  * @license React
434
591
  * react-reconciler-constants.development.js
@@ -481,9 +638,9 @@ const nodeNames = new Set([
481
638
  const createNode = (nodeName) => {
482
639
  return {
483
640
  nodeName,
484
- attributes: /* @__PURE__ */ new Map(),
641
+ attributes: Object.create(null),
485
642
  childNodes: [],
486
- parentNode: void 0
643
+ parentNode: null
487
644
  };
488
645
  };
489
646
  /**
@@ -521,7 +678,7 @@ const insertBeforeNode = (node, newChildNode, beforeChildNode) => {
521
678
  * Does nothing if `removeNode` is not a direct child of `node`.
522
679
  */
523
680
  const removeChildNode = (node, removeNode) => {
524
- removeNode.parentNode = void 0;
681
+ removeNode.parentNode = null;
525
682
  const index = node.childNodes.indexOf(removeNode);
526
683
  if (index >= 0) node.childNodes.splice(index, 1);
527
684
  };
@@ -529,26 +686,22 @@ const removeChildNode = (node, removeNode) => {
529
686
  * Set an attribute on `node`, storing it in the node's `attributes` map.
530
687
  */
531
688
  const setAttribute = (node, key, value) => {
532
- node.attributes.set(key, value);
689
+ node.attributes[key] = value;
533
690
  };
534
691
  /**
535
692
  * Create a new {@link TextNode} with the given text value.
536
693
  */
537
694
  const createTextNode = (text) => {
538
- const node = {
695
+ return {
539
696
  nodeName: TEXT_NODE_NAME,
540
697
  nodeValue: text,
541
- parentNode: void 0
698
+ parentNode: null
542
699
  };
543
- setTextNodeValue(node, text);
544
- return node;
545
700
  };
546
701
  /**
547
702
  * Update the `nodeValue` of an existing {@link TextNode}.
548
- * Non-string values are coerced to strings via `String(text)`.
549
703
  */
550
704
  const setTextNodeValue = (node, text) => {
551
- if (typeof text !== "string") text = String(text);
552
705
  node.nodeValue = text;
553
706
  };
554
707
  //#endregion
@@ -1085,7 +1238,7 @@ var require_scheduler = /* @__PURE__ */ __commonJSMin(((exports, module) => {
1085
1238
  else module.exports = require_scheduler_development();
1086
1239
  }));
1087
1240
  //#endregion
1088
- //#region ../../node_modules/.pnpm/react-reconciler@0.33.0_react@19.2.5/node_modules/react-reconciler/cjs/react-reconciler.production.js
1241
+ //#region ../../node_modules/.pnpm/react-reconciler@0.33.0_react@19.2.6/node_modules/react-reconciler/cjs/react-reconciler.production.js
1089
1242
  /**
1090
1243
  * @license React
1091
1244
  * react-reconciler.production.js
@@ -7654,7 +7807,7 @@ var require_react_reconciler_production = /* @__PURE__ */ __commonJSMin(((export
7654
7807
  Object.defineProperty(module.exports, "__esModule", { value: !0 });
7655
7808
  }));
7656
7809
  //#endregion
7657
- //#region ../../node_modules/.pnpm/react-reconciler@0.33.0_react@19.2.5/node_modules/react-reconciler/cjs/react-reconciler.development.js
7810
+ //#region ../../node_modules/.pnpm/react-reconciler@0.33.0_react@19.2.6/node_modules/react-reconciler/cjs/react-reconciler.development.js
7658
7811
  /**
7659
7812
  * @license React
7660
7813
  * react-reconciler.development.js
@@ -17817,7 +17970,7 @@ const Renderer = (0, import_react_reconciler.default)({
17817
17970
  return false;
17818
17971
  },
17819
17972
  supportsMutation: true,
17820
- isPrimaryRenderer: true,
17973
+ isPrimaryRenderer: false,
17821
17974
  supportsPersistence: false,
17822
17975
  supportsHydration: false,
17823
17976
  scheduleTimeout: setTimeout,
@@ -17883,20 +18036,24 @@ const Renderer = (0, import_react_reconciler.default)({
17883
18036
  });
17884
18037
  //#endregion
17885
18038
  //#region src/utils.ts
18039
+ function toBool$1(val) {
18040
+ return val ?? false;
18041
+ }
18042
+ __name(toBool$1, "toBool");
17886
18043
  /**
17887
18044
  * Collect the text and nested AST-node children of a single kubb-* element.
17888
18045
  *
17889
- * `#text` children become raw {@link TextNode}s; nested `kubb-function`, `kubb-const`,
18046
+ * `#text` children become raw text nodes. Nested `kubb-function`, `kubb-const`,
17890
18047
  * `kubb-type`, and similar elements are converted into their respective {@link CodeNode}s.
17891
- * Any unrecognized DOM elements are silently skipped.
18048
+ * Any unrecognized element names are silently skipped.
17892
18049
  */
17893
- function collectChildNodes(element) {
18050
+ function collectCodeNodes$1(element) {
17894
18051
  const result = [];
17895
18052
  for (const child of element.childNodes) {
17896
18053
  if (!child) continue;
17897
18054
  if (child.nodeName === "#text") {
17898
18055
  const text = child.nodeValue;
17899
- if (text && text.trim().length > 0) result.push(createText(text));
18056
+ if (text && text.trim()) result.push(createText(text));
17900
18057
  continue;
17901
18058
  }
17902
18059
  if (child.nodeName === "br") {
@@ -17906,164 +18063,162 @@ function collectChildNodes(element) {
17906
18063
  if (child.nodeName === "kubb-function") {
17907
18064
  const attrs = child.attributes;
17908
18065
  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)
18066
+ name: attrs["name"],
18067
+ params: attrs["params"],
18068
+ export: attrs["export"],
18069
+ default: attrs["default"],
18070
+ async: attrs["async"],
18071
+ generics: attrs["generics"],
18072
+ returnType: attrs["returnType"],
18073
+ JSDoc: attrs["JSDoc"],
18074
+ nodes: collectCodeNodes$1(child)
17918
18075
  }));
17919
- } else if (child.nodeName === "kubb-arrow-function") {
18076
+ continue;
18077
+ }
18078
+ if (child.nodeName === "kubb-arrow-function") {
17920
18079
  const attrs = child.attributes;
17921
18080
  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)
18081
+ name: attrs["name"],
18082
+ params: attrs["params"],
18083
+ export: attrs["export"],
18084
+ default: attrs["default"],
18085
+ async: attrs["async"],
18086
+ generics: attrs["generics"],
18087
+ returnType: attrs["returnType"],
18088
+ singleLine: attrs["singleLine"],
18089
+ JSDoc: attrs["JSDoc"],
18090
+ nodes: collectCodeNodes$1(child)
17932
18091
  }));
17933
- } else if (child.nodeName === "kubb-const") {
18092
+ continue;
18093
+ }
18094
+ if (child.nodeName === "kubb-const") {
17934
18095
  const attrs = child.attributes;
17935
18096
  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)
18097
+ name: attrs["name"],
18098
+ type: attrs["type"],
18099
+ export: attrs["export"],
18100
+ asConst: attrs["asConst"],
18101
+ JSDoc: attrs["JSDoc"],
18102
+ nodes: collectCodeNodes$1(child)
17942
18103
  }));
17943
- } else if (child.nodeName === "kubb-type") {
18104
+ continue;
18105
+ }
18106
+ if (child.nodeName === "kubb-type") {
17944
18107
  const attrs = child.attributes;
17945
18108
  result.push(createType({
17946
- name: attrs.get("name"),
17947
- export: attrs.get("export"),
17948
- JSDoc: attrs.get("JSDoc"),
17949
- nodes: collectChildNodes(child)
18109
+ name: attrs["name"],
18110
+ export: attrs["export"],
18111
+ JSDoc: attrs["JSDoc"],
18112
+ nodes: collectCodeNodes$1(child)
17950
18113
  }));
17951
- } else if (child.nodeName === "kubb-jsx") {
18114
+ continue;
18115
+ }
18116
+ if (child.nodeName === "kubb-jsx") {
17952
18117
  const textChild = child.childNodes[0];
17953
18118
  const value = textChild?.nodeName === "#text" ? textChild.nodeValue : "";
17954
18119
  if (value) result.push(createJsx(value));
18120
+ continue;
17955
18121
  }
17956
18122
  }
17957
18123
  return result;
17958
18124
  }
18125
+ __name(collectCodeNodes$1, "collectCodeNodes");
17959
18126
  /**
17960
- * Traverse `node` and collect all `<kubb-source>` elements into a `Set<SourceNode>`.
18127
+ * Yields every {@link SourceNode}, {@link ExportNode}, and {@link ImportNode}
18128
+ * within a `<kubb-file>` subtree in a single tree walk.
17961
18129
  *
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
- * ```
18130
+ * Import and export elements are leaf nodes. Once yielded, the walker does not
18131
+ * recurse into them, which also prevents source collection from descending into
18132
+ * their subtrees. Dispatch on `.kind` (`'Source'`, `'Export'`, `'Import'`) to
18133
+ * separate the results.
17969
18134
  */
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);
18135
+ function* collectFileEntries(node) {
18136
+ for (const child of node.childNodes) {
18137
+ if (!child || child.nodeName === "#text") continue;
18138
+ if (child.nodeName === "kubb-source") {
18139
+ yield createSource({
18140
+ name: child.attributes["name"]?.toString(),
18141
+ isTypeOnly: toBool$1(child.attributes["isTypeOnly"]),
18142
+ isExportable: toBool$1(child.attributes["isExportable"]),
18143
+ isIndexable: toBool$1(child.attributes["isIndexable"]),
18144
+ nodes: collectCodeNodes$1(child)
18145
+ });
18146
+ continue;
17989
18147
  }
17990
- };
17991
- walk(node);
17992
- return sources;
18148
+ if (child.nodeName === "kubb-export") {
18149
+ yield createExport({
18150
+ name: child.attributes["name"],
18151
+ path: child.attributes["path"],
18152
+ isTypeOnly: toBool$1(child.attributes["isTypeOnly"]),
18153
+ asAlias: toBool$1(child.attributes["asAlias"])
18154
+ });
18155
+ continue;
18156
+ }
18157
+ if (child.nodeName === "kubb-import") {
18158
+ yield createImport({
18159
+ name: child.attributes["name"],
18160
+ path: child.attributes["path"],
18161
+ root: child.attributes["root"],
18162
+ isTypeOnly: toBool$1(child.attributes["isTypeOnly"]),
18163
+ isNameSpace: toBool$1(child.attributes["isNameSpace"])
18164
+ });
18165
+ continue;
18166
+ }
18167
+ if (nodeNames.has(child.nodeName)) yield* collectFileEntries(child);
18168
+ }
17993
18169
  }
17994
18170
  /**
17995
- * Traverse `node` and collect all `<kubb-export>` elements into a `Set<ExportNode>`.
18171
+ * Runs a single {@link collectFileEntries} pass over a `<kubb-file>` DOM element
18172
+ * and assembles the result into a {@link FileNode}, bucketing each yielded
18173
+ * node by its `.kind`.
17996
18174
  */
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
- }));
18175
+ function createFileNode(child) {
18176
+ const sources = [];
18177
+ const exports = [];
18178
+ const imports = [];
18179
+ for (const node of collectFileEntries(child)) {
18180
+ if (node.kind === "Source") {
18181
+ sources.push(node);
18182
+ continue;
18183
+ }
18184
+ if (node.kind === "Export") {
18185
+ exports.push(node);
18186
+ continue;
18009
18187
  }
18188
+ imports.push(node);
18189
+ }
18190
+ return {
18191
+ baseName: child.attributes["baseName"],
18192
+ path: child.attributes["path"],
18193
+ meta: child.attributes["meta"] || {},
18194
+ footer: child.attributes["footer"],
18195
+ banner: child.attributes["banner"],
18196
+ sources,
18197
+ exports,
18198
+ imports
18010
18199
  };
18011
- walk(node);
18012
- return exports;
18013
18200
  }
18014
18201
  /**
18015
- * Traverse `node` and collect all `<kubb-import>` elements into a `Set<ImportNode>`.
18202
+ * Yields each {@link FileNode} as it is encountered during the tree walk,
18203
+ * without collecting into an intermediate array. Callers can begin processing
18204
+ * each file before the rest of the tree is traversed.
18016
18205
  */
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;
18206
+ function* streamFiles(node) {
18207
+ for (const child of node.childNodes) {
18208
+ if (!child) continue;
18209
+ if (child.nodeName !== "#text" && child.nodeName !== "kubb-file" && nodeNames.has(child.nodeName)) yield* streamFiles(child);
18210
+ if (child.nodeName === "kubb-file" && child.attributes["baseName"] !== void 0 && child.attributes["path"] !== void 0) yield createFileNode(child);
18211
+ }
18034
18212
  }
18035
18213
  /**
18036
18214
  * Walk the virtual DOM tree rooted at `node` and convert every `<kubb-file>` element
18037
18215
  * into a {@link FileNode}, collecting its source blocks, imports, and exports.
18038
18216
  *
18039
- * Returns the list of file nodes in document order. Nested files are supported
18217
+ * Returns the list of file nodes in document order. Nested files are supported;
18040
18218
  * the walker descends into non-file elements and recurses through them.
18041
18219
  */
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;
18220
+ function collectFiles(node) {
18221
+ return [...streamFiles(node)];
18067
18222
  }
18068
18223
  //#endregion
18069
18224
  //#region src/Runtime.tsx
@@ -18075,7 +18230,7 @@ var Runtime = class {
18075
18230
  nodes = [];
18076
18231
  #container;
18077
18232
  #rootNode;
18078
- constructor(options) {
18233
+ constructor(options = {}) {
18079
18234
  this.#options = options;
18080
18235
  this.#rootNode = createNode("kubb-root");
18081
18236
  this.#rootNode.onRender = this.onRender;
@@ -18088,7 +18243,7 @@ var Runtime = class {
18088
18243
  console.log(data);
18089
18244
  };
18090
18245
  const logRecoverableError = typeof reportError === "function" ? reportError : console.error;
18091
- const rootTag = import_constants.ConcurrentRoot;
18246
+ const rootTag = import_constants.LegacyRoot;
18092
18247
  this.#container = Renderer.createContainer(this.#rootNode, rootTag, null, false, false, "id", logRecoverableError, logRecoverableError, logRecoverableError, null);
18093
18248
  this.unsubscribeExit = onProcessExit((code) => {
18094
18249
  this.unmount(code);
@@ -18101,7 +18256,7 @@ var Runtime = class {
18101
18256
  onRender = () => {
18102
18257
  const task = this.#renderPromise.catch(() => {}).then(() => {
18103
18258
  if (this.#isUnmounted) return;
18104
- const files = processFiles(this.#rootNode);
18259
+ const files = collectFiles(this.#rootNode);
18105
18260
  this.nodes.push(...files);
18106
18261
  if (!this.#options?.debug) return;
18107
18262
  });
@@ -18146,75 +18301,342 @@ var Runtime = class {
18146
18301
  }
18147
18302
  this.resolveExitPromise();
18148
18303
  }
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;
18304
+ };
18305
+ //#endregion
18306
+ //#region src/SyncRuntime.tsx
18307
+ /**
18308
+ * Walks `element`, resolving arrays, Fragments, and function components
18309
+ * transparently, then calls `onText` for primitive values and `onHost` for
18310
+ * every host element encountered. Pure function components are called
18311
+ * synchronously. Hooks and class components are not supported.
18312
+ */
18313
+ function walkElement(element, onText, onHost) {
18314
+ if (element == null || typeof element === "boolean") return;
18315
+ if (typeof element === "string" || typeof element === "number" || typeof element === "bigint") {
18316
+ onText(String(element));
18317
+ return;
18318
+ }
18319
+ if (Array.isArray(element)) {
18320
+ for (const child of element) walkElement(child, onText, onHost);
18321
+ return;
18322
+ }
18323
+ if (typeof element === "object" && "$$typeof" in element) {
18324
+ const el = element;
18325
+ const { type } = el;
18326
+ const props = el.props;
18327
+ if (type === import_react.Fragment) {
18328
+ walkElement(props["children"], onText, onHost);
18329
+ return;
18330
+ }
18331
+ if (typeof type === "function") {
18332
+ walkElement(type(props), onText, onHost);
18333
+ return;
18334
+ }
18335
+ if (typeof type === "string") onHost(type, props);
18336
+ }
18337
+ }
18338
+ function toBool(val) {
18339
+ return val ?? false;
18340
+ }
18341
+ function collectCodeNodes(props) {
18342
+ const nodes = [];
18343
+ collectCode(props["children"], nodes);
18344
+ return nodes;
18345
+ }
18346
+ function collectCode(element, nodes) {
18347
+ walkElement(element, (text) => {
18348
+ if (text.trim()) nodes.push(createText(text));
18349
+ }, (type, props) => resolveCodeNode(type, props, nodes));
18350
+ }
18351
+ function resolveCodeNode(type, props, nodes) {
18352
+ if (type === "br") {
18353
+ nodes.push(createBreak());
18354
+ return;
18355
+ }
18356
+ if (type === "kubb-jsx") {
18357
+ let value = "";
18358
+ walkElement(props["children"], (t) => {
18359
+ value += t;
18360
+ }, () => {});
18361
+ if (value) nodes.push(createJsx(value));
18362
+ return;
18363
+ }
18364
+ if (type === "kubb-function") {
18365
+ nodes.push(createFunction({
18366
+ name: props["name"],
18367
+ params: props["params"],
18368
+ export: props["export"],
18369
+ default: props["default"],
18370
+ async: props["async"],
18371
+ generics: props["generics"],
18372
+ returnType: props["returnType"],
18373
+ JSDoc: props["JSDoc"],
18374
+ nodes: collectCodeNodes(props)
18375
+ }));
18376
+ return;
18377
+ }
18378
+ if (type === "kubb-arrow-function") {
18379
+ nodes.push(createArrowFunction({
18380
+ name: props["name"],
18381
+ params: props["params"],
18382
+ export: props["export"],
18383
+ default: props["default"],
18384
+ async: props["async"],
18385
+ generics: props["generics"],
18386
+ returnType: props["returnType"],
18387
+ singleLine: props["singleLine"],
18388
+ JSDoc: props["JSDoc"],
18389
+ nodes: collectCodeNodes(props)
18390
+ }));
18391
+ return;
18392
+ }
18393
+ if (type === "kubb-const") {
18394
+ nodes.push(createConst({
18395
+ name: props["name"],
18396
+ type: props["type"],
18397
+ export: props["export"],
18398
+ asConst: props["asConst"],
18399
+ JSDoc: props["JSDoc"],
18400
+ nodes: collectCodeNodes(props)
18401
+ }));
18402
+ return;
18403
+ }
18404
+ if (type === "kubb-type") {
18405
+ nodes.push(createType({
18406
+ name: props["name"],
18407
+ export: props["export"],
18408
+ JSDoc: props["JSDoc"],
18409
+ nodes: collectCodeNodes(props)
18410
+ }));
18411
+ return;
18412
+ }
18413
+ }
18414
+ function collectFileChildren(element) {
18415
+ const sources = [];
18416
+ const exports = [];
18417
+ const imports = [];
18418
+ walkElement(element, (text) => {
18419
+ if (text.trim()) throw new Error(`[react] '${text}' should be part of <File.Source> component when using the <File/> component`);
18420
+ }, (type, props) => {
18421
+ if (type === "kubb-source") {
18422
+ sources.push(createSource({
18423
+ name: props["name"]?.toString(),
18424
+ isTypeOnly: toBool(props["isTypeOnly"]),
18425
+ isExportable: toBool(props["isExportable"]),
18426
+ isIndexable: toBool(props["isIndexable"]),
18427
+ nodes: collectCodeNodes(props)
18428
+ }));
18429
+ return;
18430
+ }
18431
+ if (type === "kubb-export") {
18432
+ exports.push(createExport({
18433
+ name: props["name"],
18434
+ path: props["path"],
18435
+ isTypeOnly: toBool(props["isTypeOnly"]),
18436
+ asAlias: toBool(props["asAlias"])
18437
+ }));
18438
+ return;
18439
+ }
18440
+ if (type === "kubb-import") {
18441
+ imports.push(createImport({
18442
+ name: props["name"],
18443
+ path: props["path"],
18444
+ root: props["root"],
18445
+ isTypeOnly: toBool(props["isTypeOnly"]),
18446
+ isNameSpace: toBool(props["isNameSpace"])
18447
+ }));
18448
+ return;
18449
+ }
18450
+ const nested = collectFileChildren(props["children"]);
18451
+ sources.push(...nested.sources);
18452
+ exports.push(...nested.exports);
18453
+ imports.push(...nested.imports);
18454
+ });
18455
+ return {
18456
+ sources,
18457
+ exports,
18458
+ imports
18459
+ };
18460
+ }
18461
+ function* walkFiles(element) {
18462
+ if (element == null || typeof element === "boolean") return;
18463
+ if (typeof element === "string" || typeof element === "number" || typeof element === "bigint") return;
18464
+ if (Array.isArray(element)) {
18465
+ for (const child of element) yield* walkFiles(child);
18466
+ return;
18467
+ }
18468
+ if (typeof element === "object" && "$$typeof" in element) {
18469
+ const el = element;
18470
+ const { type } = el;
18471
+ const props = el.props;
18472
+ if (type === import_react.Fragment) {
18473
+ yield* walkFiles(props["children"]);
18474
+ return;
18475
+ }
18476
+ if (typeof type === "function") {
18477
+ yield* walkFiles(type(props));
18478
+ return;
18479
+ }
18480
+ if (typeof type === "string") if (type === "kubb-file" && props["baseName"] !== void 0 && props["path"] !== void 0) {
18481
+ const { sources, exports, imports } = collectFileChildren(props["children"]);
18482
+ yield {
18483
+ baseName: props["baseName"],
18484
+ path: props["path"],
18485
+ meta: props["meta"] || {},
18486
+ footer: props["footer"],
18487
+ banner: props["banner"],
18488
+ sources,
18489
+ exports,
18490
+ imports
18491
+ };
18492
+ } else yield* walkFiles(props["children"]);
18493
+ }
18494
+ }
18495
+ /**
18496
+ * Synchronous JSX renderer that walks the element tree in a single pass,
18497
+ * producing {@link FileNode} objects directly without an intermediate virtual
18498
+ * DOM. No React fiber, scheduler, or work loop is involved.
18499
+ *
18500
+ * All components must be pure functions. Hooks and class components are not
18501
+ * supported. Produces identical output to the React-backed {@link Runtime} at
18502
+ * approximately 2, 4× the speed and a fraction of the allocations.
18503
+ */
18504
+ var SyncRuntime = class {
18505
+ /**
18506
+ * Accumulated {@link FileNode} results from every {@link render} call.
18507
+ */
18508
+ nodes = [];
18509
+ /**
18510
+ * Walks `element` synchronously, converts every `<kubb-file>` subtree into
18511
+ * a {@link FileNode} with no intermediate virtual DOM, and appends the results
18512
+ * to {@link nodes}.
18513
+ */
18514
+ render(element) {
18515
+ for (const file of walkFiles(element)) this.nodes.push(file);
18516
+ }
18517
+ /**
18518
+ * Walks `element` synchronously and yields each {@link FileNode} as it is
18519
+ * produced, without buffering into an intermediate array first. Callers can
18520
+ * begin processing each file before the rest of the element tree is traversed.
18521
+ *
18522
+ * @example
18523
+ * ```ts
18524
+ * for (const file of runtime.stream(element)) {
18525
+ * await writeFile(file)
18526
+ * }
18527
+ * ```
18528
+ */
18529
+ *stream(element) {
18530
+ yield* walkFiles(element);
18155
18531
  }
18156
18532
  };
18157
18533
  //#endregion
18158
18534
  //#region src/createRenderer.tsx
18159
18535
  /**
18160
- * Create a Kubb JSX renderer.
18536
+ * Renderer factory that turns the JSX produced by a generator into
18537
+ * `FileNode`s using React's reconciler under the hood. Pass as the `renderer`
18538
+ * property on `defineGenerator`. Kubb core stays generic, with no hard
18539
+ * dependency on `@kubb/renderer-jsx`.
18161
18540
  *
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.
18541
+ * Use this when generators rely on React features (hooks, suspense, context).
18542
+ * For pure-function components, see {@link jsxRendererSync} for ~2-4× faster
18543
+ * rendering.
18164
18544
  *
18165
- * @example Basic usage
18166
- * ```ts
18167
- * import { createRenderer, File } from '@kubb/renderer-jsx'
18545
+ * @example Wire up a JSX generator
18546
+ * ```tsx
18547
+ * import { defineGenerator } from '@kubb/core'
18548
+ * import { jsxRenderer } from '@kubb/renderer-jsx'
18168
18549
  *
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()
18550
+ * export const myGenerator = defineGenerator<PluginTs>({
18551
+ * name: 'types',
18552
+ * renderer: jsxRenderer,
18553
+ * schema(node, ctx) {
18554
+ * return (
18555
+ * <File baseName="output.ts" path={`${ctx.root}/output.ts`}>
18556
+ * <Type node={node} resolver={ctx.resolver} />
18557
+ * </File>
18558
+ * )
18559
+ * },
18560
+ * })
18179
18561
  * ```
18180
18562
  */
18181
- function createRenderer(options = {}) {
18182
- const runtime = new Runtime(options);
18563
+ const jsxRenderer = () => {
18564
+ const runtime = new Runtime();
18183
18565
  return {
18184
- async render(Element) {
18185
- await runtime.render(Element);
18566
+ async render(element) {
18567
+ await runtime.render(element);
18186
18568
  },
18187
18569
  get files() {
18188
18570
  return runtime.nodes;
18189
18571
  },
18572
+ dispose() {
18573
+ runtime.unmount();
18574
+ },
18190
18575
  unmount(error) {
18191
18576
  runtime.unmount(error);
18577
+ },
18578
+ [Symbol.dispose]() {
18579
+ this.dispose();
18192
18580
  }
18193
18581
  };
18194
- }
18582
+ };
18195
18583
  /**
18196
- * A renderer factory for generators that produce JSX output.
18584
+ * Lightweight renderer that walks the JSX tree in a single recursive pass, * no React reconciler, no scheduler. Drop-in replacement for
18585
+ * {@link jsxRenderer} at roughly 2, 4× the throughput.
18197
18586
  *
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`.
18587
+ * Constraints: every component must be a pure function. Hooks, suspense, and
18588
+ * class components are not supported.
18201
18589
  *
18202
- * @example
18203
- * ```ts
18204
- * import { jsxRenderer } from '@kubb/renderer-jsx'
18590
+ * Use this for generators that produce large amounts of output and do not need
18591
+ * React's runtime features. It also exposes `stream()` for incremental file
18592
+ * emission.
18593
+ *
18594
+ * @example Drop-in faster renderer
18595
+ * ```tsx
18205
18596
  * import { defineGenerator } from '@kubb/core'
18597
+ * import { jsxRendererSync } from '@kubb/renderer-jsx'
18206
18598
  *
18207
18599
  * export const myGenerator = defineGenerator<PluginTs>({
18208
- * name: 'my-generator',
18209
- * renderer: jsxRenderer,
18210
- * schema(node, options) {
18211
- * return <File baseName="output.ts" path="src/output.ts">...</File>
18600
+ * name: 'types',
18601
+ * renderer: jsxRendererSync,
18602
+ * schema(node, ctx) {
18603
+ * return (
18604
+ * <File baseName="output.ts" path={`${ctx.root}/output.ts`}>
18605
+ * <Type node={node} resolver={ctx.resolver} />
18606
+ * </File>
18607
+ * )
18212
18608
  * },
18213
18609
  * })
18214
18610
  * ```
18611
+ *
18612
+ * @example Stream files as they are produced
18613
+ * ```tsx
18614
+ * const renderer = jsxRendererSync()
18615
+ * for (const file of renderer.stream(element)) {
18616
+ * await writeFile(file.path, file.sources[0])
18617
+ * }
18618
+ * ```
18215
18619
  */
18216
- const jsxRenderer = () => createRenderer();
18620
+ const jsxRendererSync = () => {
18621
+ const runtime = new SyncRuntime();
18622
+ return {
18623
+ async render(element) {
18624
+ runtime.render(element);
18625
+ },
18626
+ get files() {
18627
+ return runtime.nodes;
18628
+ },
18629
+ stream(element) {
18630
+ return runtime.stream(element);
18631
+ },
18632
+ dispose() {},
18633
+ unmount(_error) {},
18634
+ [Symbol.dispose]() {
18635
+ this.dispose();
18636
+ }
18637
+ };
18638
+ };
18217
18639
  //#endregion
18218
- export { Const, File, Function$1 as Function, Jsx, Root, Type, createContext, createRenderer, inject, jsxRenderer, provide, unprovide };
18640
+ export { Callout, CodeBlock, Const, File, Frontmatter, Function$1 as Function, Heading, Jsx, List, Paragraph, Root, Type, createContext, inject, jsxRenderer, jsxRendererSync, provide, unprovide };
18219
18641
 
18220
18642
  //# sourceMappingURL=index.js.map