@kubb/renderer-jsx 5.0.0-beta.17 → 5.0.0-beta.18
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.cjs +423 -176
- package/dist/index.d.ts +38 -10
- package/dist/index.js +423 -177
- package/dist/jsx-dev-runtime.d.ts +2 -2
- package/dist/{jsx-namespace-CNp0arTN.d.ts → jsx-namespace-BLGeMDcR.d.ts} +2 -2
- package/dist/jsx-runtime.d.ts +2 -2
- package/dist/{types-nAFMiWFw.d.ts → types-Dk7A1Y5U.d.ts} +2 -2
- package/dist/types.d.ts +1 -1
- package/package.json +2 -2
- package/src/Renderer.ts +1 -1
- package/src/Runtime.tsx +4 -4
- package/src/SyncRuntime.tsx +285 -0
- package/src/constants.ts +19 -9
- package/src/createRenderer.tsx +46 -5
- package/src/dom.ts +3 -13
- package/src/index.ts +1 -1
- package/src/types.ts +1 -1
- package/src/utils.ts +174 -206
package/dist/index.js
CHANGED
|
@@ -346,7 +346,7 @@ function Jsx({ children }) {
|
|
|
346
346
|
Jsx.displayName = "Jsx";
|
|
347
347
|
//#endregion
|
|
348
348
|
//#region src/components/Root.tsx
|
|
349
|
-
var import_react = require_react();
|
|
349
|
+
var import_react = /* @__PURE__ */ __toESM(require_react());
|
|
350
350
|
var ErrorBoundary = class extends import_react.Component {
|
|
351
351
|
state = { hasError: false };
|
|
352
352
|
static displayName = "ErrorBoundary";
|
|
@@ -454,20 +454,29 @@ var import_constants = (/* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
|
454
454
|
* Name used for text-node entries in the virtual DOM.
|
|
455
455
|
*/
|
|
456
456
|
const TEXT_NODE_NAME = "#text";
|
|
457
|
+
const KUBB_FILE = "kubb-file";
|
|
458
|
+
const KUBB_SOURCE = "kubb-source";
|
|
459
|
+
const KUBB_EXPORT = "kubb-export";
|
|
460
|
+
const KUBB_IMPORT = "kubb-import";
|
|
461
|
+
const KUBB_FUNCTION = "kubb-function";
|
|
462
|
+
const KUBB_ARROW_FUNCTION = "kubb-arrow-function";
|
|
463
|
+
const KUBB_CONST = "kubb-const";
|
|
464
|
+
const KUBB_TYPE = "kubb-type";
|
|
465
|
+
const KUBB_JSX = "kubb-jsx";
|
|
457
466
|
/**
|
|
458
467
|
* Set of all element names recognized by the Kubb renderer.
|
|
459
468
|
* Used to distinguish Kubb-owned elements from unrecognized or text nodes during tree traversal.
|
|
460
469
|
*/
|
|
461
470
|
const nodeNames = new Set([
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
+
KUBB_EXPORT,
|
|
472
|
+
KUBB_FILE,
|
|
473
|
+
KUBB_SOURCE,
|
|
474
|
+
KUBB_IMPORT,
|
|
475
|
+
KUBB_FUNCTION,
|
|
476
|
+
KUBB_ARROW_FUNCTION,
|
|
477
|
+
KUBB_CONST,
|
|
478
|
+
KUBB_TYPE,
|
|
479
|
+
KUBB_JSX,
|
|
471
480
|
"kubb-text",
|
|
472
481
|
"kubb-root",
|
|
473
482
|
"kubb-app",
|
|
@@ -484,7 +493,7 @@ const nodeNames = new Set([
|
|
|
484
493
|
const createNode = (nodeName) => {
|
|
485
494
|
return {
|
|
486
495
|
nodeName,
|
|
487
|
-
attributes:
|
|
496
|
+
attributes: Object.create(null),
|
|
488
497
|
childNodes: [],
|
|
489
498
|
parentNode: void 0
|
|
490
499
|
};
|
|
@@ -532,26 +541,22 @@ const removeChildNode = (node, removeNode) => {
|
|
|
532
541
|
* Set an attribute on `node`, storing it in the node's `attributes` map.
|
|
533
542
|
*/
|
|
534
543
|
const setAttribute = (node, key, value) => {
|
|
535
|
-
node.attributes
|
|
544
|
+
node.attributes[key] = value;
|
|
536
545
|
};
|
|
537
546
|
/**
|
|
538
547
|
* Create a new {@link TextNode} with the given text value.
|
|
539
548
|
*/
|
|
540
549
|
const createTextNode = (text) => {
|
|
541
|
-
|
|
550
|
+
return {
|
|
542
551
|
nodeName: TEXT_NODE_NAME,
|
|
543
552
|
nodeValue: text,
|
|
544
553
|
parentNode: void 0
|
|
545
554
|
};
|
|
546
|
-
setTextNodeValue(node, text);
|
|
547
|
-
return node;
|
|
548
555
|
};
|
|
549
556
|
/**
|
|
550
557
|
* Update the `nodeValue` of an existing {@link TextNode}.
|
|
551
|
-
* Non-string values are coerced to strings via `String(text)`.
|
|
552
558
|
*/
|
|
553
559
|
const setTextNodeValue = (node, text) => {
|
|
554
|
-
if (typeof text !== "string") text = String(text);
|
|
555
560
|
node.nodeValue = text;
|
|
556
561
|
};
|
|
557
562
|
//#endregion
|
|
@@ -17820,7 +17825,7 @@ const Renderer = (0, import_react_reconciler.default)({
|
|
|
17820
17825
|
return false;
|
|
17821
17826
|
},
|
|
17822
17827
|
supportsMutation: true,
|
|
17823
|
-
isPrimaryRenderer:
|
|
17828
|
+
isPrimaryRenderer: false,
|
|
17824
17829
|
supportsPersistence: false,
|
|
17825
17830
|
supportsHydration: false,
|
|
17826
17831
|
scheduleTimeout: setTimeout,
|
|
@@ -17886,187 +17891,182 @@ const Renderer = (0, import_react_reconciler.default)({
|
|
|
17886
17891
|
});
|
|
17887
17892
|
//#endregion
|
|
17888
17893
|
//#region src/utils.ts
|
|
17894
|
+
function toBool$1(val) {
|
|
17895
|
+
return val ?? false;
|
|
17896
|
+
}
|
|
17897
|
+
__name(toBool$1, "toBool");
|
|
17889
17898
|
/**
|
|
17890
17899
|
* Collect the text and nested AST-node children of a single kubb-* element.
|
|
17891
17900
|
*
|
|
17892
|
-
* `#text` children become raw
|
|
17901
|
+
* `#text` children become raw text nodes; nested `kubb-function`, `kubb-const`,
|
|
17893
17902
|
* `kubb-type`, and similar elements are converted into their respective {@link CodeNode}s.
|
|
17894
|
-
* Any unrecognized
|
|
17903
|
+
* Any unrecognized element names are silently skipped.
|
|
17895
17904
|
*/
|
|
17896
|
-
function
|
|
17905
|
+
function collectCodeNodes$1(element) {
|
|
17897
17906
|
const result = [];
|
|
17898
17907
|
for (const child of element.childNodes) {
|
|
17899
17908
|
if (!child) continue;
|
|
17900
|
-
|
|
17901
|
-
|
|
17902
|
-
|
|
17903
|
-
|
|
17904
|
-
|
|
17905
|
-
|
|
17906
|
-
|
|
17907
|
-
|
|
17908
|
-
|
|
17909
|
-
|
|
17910
|
-
|
|
17911
|
-
|
|
17912
|
-
|
|
17913
|
-
|
|
17914
|
-
|
|
17915
|
-
|
|
17916
|
-
|
|
17917
|
-
|
|
17918
|
-
|
|
17919
|
-
|
|
17920
|
-
|
|
17921
|
-
|
|
17922
|
-
|
|
17923
|
-
|
|
17924
|
-
|
|
17925
|
-
|
|
17926
|
-
|
|
17927
|
-
|
|
17928
|
-
|
|
17929
|
-
|
|
17930
|
-
|
|
17931
|
-
|
|
17932
|
-
|
|
17933
|
-
|
|
17934
|
-
|
|
17935
|
-
|
|
17936
|
-
|
|
17937
|
-
|
|
17938
|
-
|
|
17939
|
-
|
|
17940
|
-
|
|
17941
|
-
|
|
17942
|
-
|
|
17943
|
-
|
|
17944
|
-
|
|
17945
|
-
|
|
17946
|
-
|
|
17947
|
-
|
|
17948
|
-
|
|
17949
|
-
|
|
17950
|
-
|
|
17951
|
-
|
|
17952
|
-
|
|
17953
|
-
|
|
17954
|
-
|
|
17955
|
-
|
|
17956
|
-
|
|
17957
|
-
|
|
17909
|
+
switch (child.nodeName) {
|
|
17910
|
+
case TEXT_NODE_NAME: {
|
|
17911
|
+
const text = child.nodeValue;
|
|
17912
|
+
if (text && text.trim()) result.push(createText(text));
|
|
17913
|
+
break;
|
|
17914
|
+
}
|
|
17915
|
+
case "br":
|
|
17916
|
+
result.push(createBreak());
|
|
17917
|
+
break;
|
|
17918
|
+
case KUBB_FUNCTION: {
|
|
17919
|
+
const attrs = child.attributes;
|
|
17920
|
+
result.push(createFunction({
|
|
17921
|
+
name: attrs["name"],
|
|
17922
|
+
params: attrs["params"],
|
|
17923
|
+
export: attrs["export"],
|
|
17924
|
+
default: attrs["default"],
|
|
17925
|
+
async: attrs["async"],
|
|
17926
|
+
generics: attrs["generics"],
|
|
17927
|
+
returnType: attrs["returnType"],
|
|
17928
|
+
JSDoc: attrs["JSDoc"],
|
|
17929
|
+
nodes: collectCodeNodes$1(child)
|
|
17930
|
+
}));
|
|
17931
|
+
break;
|
|
17932
|
+
}
|
|
17933
|
+
case KUBB_ARROW_FUNCTION: {
|
|
17934
|
+
const attrs = child.attributes;
|
|
17935
|
+
result.push(createArrowFunction({
|
|
17936
|
+
name: attrs["name"],
|
|
17937
|
+
params: attrs["params"],
|
|
17938
|
+
export: attrs["export"],
|
|
17939
|
+
default: attrs["default"],
|
|
17940
|
+
async: attrs["async"],
|
|
17941
|
+
generics: attrs["generics"],
|
|
17942
|
+
returnType: attrs["returnType"],
|
|
17943
|
+
singleLine: attrs["singleLine"],
|
|
17944
|
+
JSDoc: attrs["JSDoc"],
|
|
17945
|
+
nodes: collectCodeNodes$1(child)
|
|
17946
|
+
}));
|
|
17947
|
+
break;
|
|
17948
|
+
}
|
|
17949
|
+
case KUBB_CONST: {
|
|
17950
|
+
const attrs = child.attributes;
|
|
17951
|
+
result.push(createConst({
|
|
17952
|
+
name: attrs["name"],
|
|
17953
|
+
type: attrs["type"],
|
|
17954
|
+
export: attrs["export"],
|
|
17955
|
+
asConst: attrs["asConst"],
|
|
17956
|
+
JSDoc: attrs["JSDoc"],
|
|
17957
|
+
nodes: collectCodeNodes$1(child)
|
|
17958
|
+
}));
|
|
17959
|
+
break;
|
|
17960
|
+
}
|
|
17961
|
+
case KUBB_TYPE: {
|
|
17962
|
+
const attrs = child.attributes;
|
|
17963
|
+
result.push(createType({
|
|
17964
|
+
name: attrs["name"],
|
|
17965
|
+
export: attrs["export"],
|
|
17966
|
+
JSDoc: attrs["JSDoc"],
|
|
17967
|
+
nodes: collectCodeNodes$1(child)
|
|
17968
|
+
}));
|
|
17969
|
+
break;
|
|
17970
|
+
}
|
|
17971
|
+
case KUBB_JSX: {
|
|
17972
|
+
const textChild = child.childNodes[0];
|
|
17973
|
+
const value = textChild?.nodeName === "#text" ? textChild.nodeValue : "";
|
|
17974
|
+
if (value) result.push(createJsx(value));
|
|
17975
|
+
break;
|
|
17976
|
+
}
|
|
17958
17977
|
}
|
|
17959
17978
|
}
|
|
17960
17979
|
return result;
|
|
17961
17980
|
}
|
|
17981
|
+
__name(collectCodeNodes$1, "collectCodeNodes");
|
|
17962
17982
|
/**
|
|
17963
|
-
*
|
|
17964
|
-
*
|
|
17965
|
-
* Elements whose `nodeName` is in `ignores` are skipped entirely (including their subtrees).
|
|
17966
|
-
* This is used to collect source blocks from a file node while excluding import/export subtrees.
|
|
17983
|
+
* Yields every {@link SourceNode}, {@link ExportNode}, and {@link ImportNode}
|
|
17984
|
+
* within a `<kubb-file>` subtree in a single tree walk.
|
|
17967
17985
|
*
|
|
17968
|
-
*
|
|
17969
|
-
*
|
|
17970
|
-
*
|
|
17971
|
-
*
|
|
17986
|
+
* Import and export elements are leaf nodes. Once yielded, the walker does not
|
|
17987
|
+
* recurse into them, which also prevents source collection from descending into
|
|
17988
|
+
* their subtrees. Dispatch on `.kind` (`'Source'`, `'Export'`, `'Import'`) to
|
|
17989
|
+
* separate the results.
|
|
17972
17990
|
*/
|
|
17973
|
-
function
|
|
17974
|
-
const
|
|
17975
|
-
|
|
17976
|
-
|
|
17977
|
-
|
|
17978
|
-
|
|
17979
|
-
|
|
17980
|
-
|
|
17981
|
-
|
|
17982
|
-
|
|
17983
|
-
|
|
17984
|
-
|
|
17985
|
-
isIndexable: child.attributes.get("isIndexable") ?? false,
|
|
17986
|
-
nodes: collectChildNodes(child)
|
|
17987
|
-
});
|
|
17988
|
-
sources.add(source);
|
|
17989
|
-
continue;
|
|
17990
|
-
}
|
|
17991
|
-
if (child.nodeName !== "#text" && nodeNames.has(child.nodeName)) walk(child);
|
|
17991
|
+
function* collectFileEntries(node) {
|
|
17992
|
+
for (const child of node.childNodes) {
|
|
17993
|
+
if (!child || child.nodeName === "#text") continue;
|
|
17994
|
+
if (child.nodeName === "kubb-source") {
|
|
17995
|
+
yield createSource({
|
|
17996
|
+
name: child.attributes["name"]?.toString(),
|
|
17997
|
+
isTypeOnly: toBool$1(child.attributes["isTypeOnly"]),
|
|
17998
|
+
isExportable: toBool$1(child.attributes["isExportable"]),
|
|
17999
|
+
isIndexable: toBool$1(child.attributes["isIndexable"]),
|
|
18000
|
+
nodes: collectCodeNodes$1(child)
|
|
18001
|
+
});
|
|
18002
|
+
continue;
|
|
17992
18003
|
}
|
|
17993
|
-
|
|
17994
|
-
|
|
17995
|
-
|
|
18004
|
+
if (child.nodeName === "kubb-export") {
|
|
18005
|
+
yield createExport({
|
|
18006
|
+
name: child.attributes["name"],
|
|
18007
|
+
path: child.attributes["path"],
|
|
18008
|
+
isTypeOnly: toBool$1(child.attributes["isTypeOnly"]),
|
|
18009
|
+
asAlias: toBool$1(child.attributes["asAlias"])
|
|
18010
|
+
});
|
|
18011
|
+
continue;
|
|
18012
|
+
}
|
|
18013
|
+
if (child.nodeName === "kubb-import") {
|
|
18014
|
+
yield createImport({
|
|
18015
|
+
name: child.attributes["name"],
|
|
18016
|
+
path: child.attributes["path"],
|
|
18017
|
+
root: child.attributes["root"],
|
|
18018
|
+
isTypeOnly: toBool$1(child.attributes["isTypeOnly"]),
|
|
18019
|
+
isNameSpace: toBool$1(child.attributes["isNameSpace"])
|
|
18020
|
+
});
|
|
18021
|
+
continue;
|
|
18022
|
+
}
|
|
18023
|
+
if (nodeNames.has(child.nodeName)) yield* collectFileEntries(child);
|
|
18024
|
+
}
|
|
17996
18025
|
}
|
|
17997
18026
|
/**
|
|
17998
|
-
*
|
|
18027
|
+
* Runs a single {@link collectFileEntries} pass over a `<kubb-file>` DOM element
|
|
18028
|
+
* and assembles the result into a {@link FileNode}, bucketing each yielded
|
|
18029
|
+
* node by its `.kind`.
|
|
17999
18030
|
*/
|
|
18000
|
-
function
|
|
18001
|
-
const
|
|
18002
|
-
const
|
|
18003
|
-
|
|
18004
|
-
|
|
18005
|
-
|
|
18006
|
-
|
|
18007
|
-
|
|
18008
|
-
|
|
18009
|
-
|
|
18010
|
-
|
|
18011
|
-
|
|
18012
|
-
|
|
18031
|
+
function createFileNode(child) {
|
|
18032
|
+
const sources = [];
|
|
18033
|
+
const exports = [];
|
|
18034
|
+
const imports = [];
|
|
18035
|
+
for (const node of collectFileEntries(child)) if (node.kind === "Source") sources.push(node);
|
|
18036
|
+
else if (node.kind === "Export") exports.push(node);
|
|
18037
|
+
else imports.push(node);
|
|
18038
|
+
return {
|
|
18039
|
+
baseName: child.attributes["baseName"],
|
|
18040
|
+
path: child.attributes["path"],
|
|
18041
|
+
meta: child.attributes["meta"] || {},
|
|
18042
|
+
footer: child.attributes["footer"],
|
|
18043
|
+
banner: child.attributes["banner"],
|
|
18044
|
+
sources,
|
|
18045
|
+
exports,
|
|
18046
|
+
imports
|
|
18013
18047
|
};
|
|
18014
|
-
walk(node);
|
|
18015
|
-
return exports;
|
|
18016
18048
|
}
|
|
18017
18049
|
/**
|
|
18018
|
-
*
|
|
18050
|
+
* Yields each {@link FileNode} as it is encountered during the tree walk,
|
|
18051
|
+
* without collecting into an intermediate array. Callers can begin processing
|
|
18052
|
+
* each file before the rest of the tree is traversed.
|
|
18019
18053
|
*/
|
|
18020
|
-
function
|
|
18021
|
-
const
|
|
18022
|
-
|
|
18023
|
-
|
|
18024
|
-
|
|
18025
|
-
|
|
18026
|
-
if (child.nodeName === "kubb-import") imports.add(createImport({
|
|
18027
|
-
name: child.attributes.get("name"),
|
|
18028
|
-
path: child.attributes.get("path"),
|
|
18029
|
-
root: child.attributes.get("root"),
|
|
18030
|
-
isTypeOnly: child.attributes.get("isTypeOnly") ?? false,
|
|
18031
|
-
isNameSpace: child.attributes.get("isNameSpace") ?? false
|
|
18032
|
-
}));
|
|
18033
|
-
}
|
|
18034
|
-
};
|
|
18035
|
-
walk(node);
|
|
18036
|
-
return imports;
|
|
18054
|
+
function* streamFiles(node) {
|
|
18055
|
+
for (const child of node.childNodes) {
|
|
18056
|
+
if (!child) continue;
|
|
18057
|
+
if (child.nodeName !== "#text" && child.nodeName !== "kubb-file" && nodeNames.has(child.nodeName)) yield* streamFiles(child);
|
|
18058
|
+
if (child.nodeName === "kubb-file" && child.attributes["baseName"] !== void 0 && child.attributes["path"] !== void 0) yield createFileNode(child);
|
|
18059
|
+
}
|
|
18037
18060
|
}
|
|
18038
18061
|
/**
|
|
18039
18062
|
* Walk the virtual DOM tree rooted at `node` and convert every `<kubb-file>` element
|
|
18040
18063
|
* into a {@link FileNode}, collecting its source blocks, imports, and exports.
|
|
18041
18064
|
*
|
|
18042
|
-
* Returns the list of file nodes in document order. Nested files are supported
|
|
18065
|
+
* Returns the list of file nodes in document order. Nested files are supported;
|
|
18043
18066
|
* the walker descends into non-file elements and recurses through them.
|
|
18044
18067
|
*/
|
|
18045
|
-
function
|
|
18046
|
-
|
|
18047
|
-
function walk(current) {
|
|
18048
|
-
for (const child of current.childNodes) {
|
|
18049
|
-
if (!child) continue;
|
|
18050
|
-
if (child.nodeName !== "#text" && child.nodeName !== "kubb-file" && nodeNames.has(child.nodeName)) walk(child);
|
|
18051
|
-
if (child.nodeName === "kubb-file") {
|
|
18052
|
-
if (child.attributes.has("baseName") && child.attributes.has("path")) {
|
|
18053
|
-
const sources = squashSourceNodes(child, ["kubb-export", "kubb-import"]);
|
|
18054
|
-
collected.push({
|
|
18055
|
-
baseName: child.attributes.get("baseName"),
|
|
18056
|
-
path: child.attributes.get("path"),
|
|
18057
|
-
meta: child.attributes.get("meta") || {},
|
|
18058
|
-
footer: child.attributes.get("footer"),
|
|
18059
|
-
banner: child.attributes.get("banner"),
|
|
18060
|
-
sources: [...sources],
|
|
18061
|
-
exports: [...squashExportNodes(child)],
|
|
18062
|
-
imports: [...squashImportNodes(child)]
|
|
18063
|
-
});
|
|
18064
|
-
}
|
|
18065
|
-
}
|
|
18066
|
-
}
|
|
18067
|
-
}
|
|
18068
|
-
walk(node);
|
|
18069
|
-
return collected;
|
|
18068
|
+
function collectFiles(node) {
|
|
18069
|
+
return [...streamFiles(node)];
|
|
18070
18070
|
}
|
|
18071
18071
|
//#endregion
|
|
18072
18072
|
//#region src/Runtime.tsx
|
|
@@ -18091,7 +18091,7 @@ var Runtime = class {
|
|
|
18091
18091
|
console.log(data);
|
|
18092
18092
|
};
|
|
18093
18093
|
const logRecoverableError = typeof reportError === "function" ? reportError : console.error;
|
|
18094
|
-
const rootTag = import_constants.
|
|
18094
|
+
const rootTag = import_constants.LegacyRoot;
|
|
18095
18095
|
this.#container = Renderer.createContainer(this.#rootNode, rootTag, null, false, false, "id", logRecoverableError, logRecoverableError, logRecoverableError, null);
|
|
18096
18096
|
this.unsubscribeExit = onProcessExit((code) => {
|
|
18097
18097
|
this.unmount(code);
|
|
@@ -18104,7 +18104,7 @@ var Runtime = class {
|
|
|
18104
18104
|
onRender = () => {
|
|
18105
18105
|
const task = this.#renderPromise.catch(() => {}).then(() => {
|
|
18106
18106
|
if (this.#isUnmounted) return;
|
|
18107
|
-
const files =
|
|
18107
|
+
const files = collectFiles(this.#rootNode);
|
|
18108
18108
|
this.nodes.push(...files);
|
|
18109
18109
|
if (!this.#options?.debug) return;
|
|
18110
18110
|
});
|
|
@@ -18151,21 +18151,226 @@ var Runtime = class {
|
|
|
18151
18151
|
}
|
|
18152
18152
|
};
|
|
18153
18153
|
//#endregion
|
|
18154
|
+
//#region src/SyncRuntime.tsx
|
|
18155
|
+
/**
|
|
18156
|
+
* Walks `element`, resolving arrays, Fragments, and function components
|
|
18157
|
+
* transparently, then calls `onText` for primitive values and `onHost` for
|
|
18158
|
+
* every host element encountered. Pure function components are called
|
|
18159
|
+
* synchronously; hooks and class components are not supported.
|
|
18160
|
+
*/
|
|
18161
|
+
function walkElement(element, onText, onHost) {
|
|
18162
|
+
if (element == null || typeof element === "boolean") return;
|
|
18163
|
+
if (typeof element === "string" || typeof element === "number" || typeof element === "bigint") {
|
|
18164
|
+
onText(String(element));
|
|
18165
|
+
return;
|
|
18166
|
+
}
|
|
18167
|
+
if (Array.isArray(element)) {
|
|
18168
|
+
for (const child of element) walkElement(child, onText, onHost);
|
|
18169
|
+
return;
|
|
18170
|
+
}
|
|
18171
|
+
if (typeof element === "object" && "$$typeof" in element) {
|
|
18172
|
+
const el = element;
|
|
18173
|
+
const { type } = el;
|
|
18174
|
+
const props = el.props;
|
|
18175
|
+
if (type === import_react.Fragment) walkElement(props["children"], onText, onHost);
|
|
18176
|
+
else if (typeof type === "function") walkElement(type(props), onText, onHost);
|
|
18177
|
+
else if (typeof type === "string") onHost(type, props);
|
|
18178
|
+
}
|
|
18179
|
+
}
|
|
18180
|
+
function toBool(val) {
|
|
18181
|
+
return val ?? false;
|
|
18182
|
+
}
|
|
18183
|
+
function collectCodeNodes(props) {
|
|
18184
|
+
const nodes = [];
|
|
18185
|
+
collectCode(props["children"], nodes);
|
|
18186
|
+
return nodes;
|
|
18187
|
+
}
|
|
18188
|
+
function collectCode(element, nodes) {
|
|
18189
|
+
walkElement(element, (text) => {
|
|
18190
|
+
if (text.trim()) nodes.push(createText(text));
|
|
18191
|
+
}, (type, props) => resolveCodeNode(type, props, nodes));
|
|
18192
|
+
}
|
|
18193
|
+
function resolveCodeNode(type, props, nodes) {
|
|
18194
|
+
if (type === "br") {
|
|
18195
|
+
nodes.push(createBreak());
|
|
18196
|
+
return;
|
|
18197
|
+
}
|
|
18198
|
+
if (type === "kubb-jsx") {
|
|
18199
|
+
let value = "";
|
|
18200
|
+
walkElement(props["children"], (t) => {
|
|
18201
|
+
value += t;
|
|
18202
|
+
}, () => {});
|
|
18203
|
+
if (value) nodes.push(createJsx(value));
|
|
18204
|
+
return;
|
|
18205
|
+
}
|
|
18206
|
+
if (type === "kubb-function") {
|
|
18207
|
+
nodes.push(createFunction({
|
|
18208
|
+
name: props["name"],
|
|
18209
|
+
params: props["params"],
|
|
18210
|
+
export: props["export"],
|
|
18211
|
+
default: props["default"],
|
|
18212
|
+
async: props["async"],
|
|
18213
|
+
generics: props["generics"],
|
|
18214
|
+
returnType: props["returnType"],
|
|
18215
|
+
JSDoc: props["JSDoc"],
|
|
18216
|
+
nodes: collectCodeNodes(props)
|
|
18217
|
+
}));
|
|
18218
|
+
return;
|
|
18219
|
+
}
|
|
18220
|
+
if (type === "kubb-arrow-function") {
|
|
18221
|
+
nodes.push(createArrowFunction({
|
|
18222
|
+
name: props["name"],
|
|
18223
|
+
params: props["params"],
|
|
18224
|
+
export: props["export"],
|
|
18225
|
+
default: props["default"],
|
|
18226
|
+
async: props["async"],
|
|
18227
|
+
generics: props["generics"],
|
|
18228
|
+
returnType: props["returnType"],
|
|
18229
|
+
singleLine: props["singleLine"],
|
|
18230
|
+
JSDoc: props["JSDoc"],
|
|
18231
|
+
nodes: collectCodeNodes(props)
|
|
18232
|
+
}));
|
|
18233
|
+
return;
|
|
18234
|
+
}
|
|
18235
|
+
if (type === "kubb-const") {
|
|
18236
|
+
nodes.push(createConst({
|
|
18237
|
+
name: props["name"],
|
|
18238
|
+
type: props["type"],
|
|
18239
|
+
export: props["export"],
|
|
18240
|
+
asConst: props["asConst"],
|
|
18241
|
+
JSDoc: props["JSDoc"],
|
|
18242
|
+
nodes: collectCodeNodes(props)
|
|
18243
|
+
}));
|
|
18244
|
+
return;
|
|
18245
|
+
}
|
|
18246
|
+
if (type === "kubb-type") {
|
|
18247
|
+
nodes.push(createType({
|
|
18248
|
+
name: props["name"],
|
|
18249
|
+
export: props["export"],
|
|
18250
|
+
JSDoc: props["JSDoc"],
|
|
18251
|
+
nodes: collectCodeNodes(props)
|
|
18252
|
+
}));
|
|
18253
|
+
return;
|
|
18254
|
+
}
|
|
18255
|
+
}
|
|
18256
|
+
function collectFileChildren(element) {
|
|
18257
|
+
const sources = [];
|
|
18258
|
+
const exports = [];
|
|
18259
|
+
const imports = [];
|
|
18260
|
+
walkElement(element, (text) => {
|
|
18261
|
+
if (text.trim()) throw new Error(`[react] '${text}' should be part of <File.Source> component when using the <File/> component`);
|
|
18262
|
+
}, (type, props) => {
|
|
18263
|
+
if (type === "kubb-source") {
|
|
18264
|
+
sources.push(createSource({
|
|
18265
|
+
name: props["name"]?.toString(),
|
|
18266
|
+
isTypeOnly: toBool(props["isTypeOnly"]),
|
|
18267
|
+
isExportable: toBool(props["isExportable"]),
|
|
18268
|
+
isIndexable: toBool(props["isIndexable"]),
|
|
18269
|
+
nodes: collectCodeNodes(props)
|
|
18270
|
+
}));
|
|
18271
|
+
return;
|
|
18272
|
+
}
|
|
18273
|
+
if (type === "kubb-export") {
|
|
18274
|
+
exports.push(createExport({
|
|
18275
|
+
name: props["name"],
|
|
18276
|
+
path: props["path"],
|
|
18277
|
+
isTypeOnly: toBool(props["isTypeOnly"]),
|
|
18278
|
+
asAlias: toBool(props["asAlias"])
|
|
18279
|
+
}));
|
|
18280
|
+
return;
|
|
18281
|
+
}
|
|
18282
|
+
if (type === "kubb-import") {
|
|
18283
|
+
imports.push(createImport({
|
|
18284
|
+
name: props["name"],
|
|
18285
|
+
path: props["path"],
|
|
18286
|
+
root: props["root"],
|
|
18287
|
+
isTypeOnly: toBool(props["isTypeOnly"]),
|
|
18288
|
+
isNameSpace: toBool(props["isNameSpace"])
|
|
18289
|
+
}));
|
|
18290
|
+
return;
|
|
18291
|
+
}
|
|
18292
|
+
const nested = collectFileChildren(props["children"]);
|
|
18293
|
+
sources.push(...nested.sources);
|
|
18294
|
+
exports.push(...nested.exports);
|
|
18295
|
+
imports.push(...nested.imports);
|
|
18296
|
+
});
|
|
18297
|
+
return {
|
|
18298
|
+
sources,
|
|
18299
|
+
exports,
|
|
18300
|
+
imports
|
|
18301
|
+
};
|
|
18302
|
+
}
|
|
18303
|
+
function* walkFiles(element) {
|
|
18304
|
+
const files = [];
|
|
18305
|
+
function onHost(type, props) {
|
|
18306
|
+
if (type === "kubb-file" && props["baseName"] !== void 0 && props["path"] !== void 0) {
|
|
18307
|
+
const { sources, exports, imports } = collectFileChildren(props["children"]);
|
|
18308
|
+
files.push({
|
|
18309
|
+
baseName: props["baseName"],
|
|
18310
|
+
path: props["path"],
|
|
18311
|
+
meta: props["meta"] || {},
|
|
18312
|
+
footer: props["footer"],
|
|
18313
|
+
banner: props["banner"],
|
|
18314
|
+
sources,
|
|
18315
|
+
exports,
|
|
18316
|
+
imports
|
|
18317
|
+
});
|
|
18318
|
+
} else walkElement(props["children"], () => {}, onHost);
|
|
18319
|
+
}
|
|
18320
|
+
walkElement(element, () => {}, onHost);
|
|
18321
|
+
yield* files;
|
|
18322
|
+
}
|
|
18323
|
+
/**
|
|
18324
|
+
* Synchronous JSX renderer that walks the element tree in a single pass,
|
|
18325
|
+
* producing {@link FileNode} objects directly without an intermediate virtual
|
|
18326
|
+
* DOM. No React fiber, scheduler, or work loop is involved.
|
|
18327
|
+
*
|
|
18328
|
+
* All components must be pure functions; hooks and class components are not
|
|
18329
|
+
* supported. Produces identical output to the React-backed {@link Runtime} at
|
|
18330
|
+
* approximately 2–4× the speed and a fraction of the allocations.
|
|
18331
|
+
*/
|
|
18332
|
+
var SyncRuntime = class {
|
|
18333
|
+
/**
|
|
18334
|
+
* Accumulated {@link FileNode} results from every {@link render} call.
|
|
18335
|
+
*/
|
|
18336
|
+
nodes = [];
|
|
18337
|
+
/**
|
|
18338
|
+
* Walks `element` synchronously, converts every `<kubb-file>` subtree into
|
|
18339
|
+
* a {@link FileNode} with no intermediate virtual DOM, and appends the results
|
|
18340
|
+
* to {@link nodes}.
|
|
18341
|
+
*/
|
|
18342
|
+
render(element) {
|
|
18343
|
+
for (const file of walkFiles(element)) this.nodes.push(file);
|
|
18344
|
+
}
|
|
18345
|
+
/**
|
|
18346
|
+
* Walks `element` synchronously and yields each {@link FileNode} as it is
|
|
18347
|
+
* produced, without buffering into an intermediate array first. Callers can
|
|
18348
|
+
* begin processing each file before the rest of the element tree is traversed.
|
|
18349
|
+
*
|
|
18350
|
+
* @example
|
|
18351
|
+
* ```ts
|
|
18352
|
+
* for (const file of runtime.stream(element)) {
|
|
18353
|
+
* await writeFile(file)
|
|
18354
|
+
* }
|
|
18355
|
+
* ```
|
|
18356
|
+
*/
|
|
18357
|
+
*stream(element) {
|
|
18358
|
+
yield* walkFiles(element);
|
|
18359
|
+
}
|
|
18360
|
+
};
|
|
18361
|
+
//#endregion
|
|
18154
18362
|
//#region src/createRenderer.tsx
|
|
18155
18363
|
/**
|
|
18156
|
-
*
|
|
18364
|
+
* Renderer factory for generators that produce JSX output.
|
|
18157
18365
|
*
|
|
18158
|
-
* Pass
|
|
18159
|
-
* core can render the JSX element tree returned by your generator methods
|
|
18366
|
+
* Pass as the `renderer` property of `defineGenerator`. Core drives rendering
|
|
18160
18367
|
* without a hard dependency on `@kubb/renderer-jsx`.
|
|
18161
18368
|
*
|
|
18162
18369
|
* @example
|
|
18163
18370
|
* ```ts
|
|
18164
18371
|
* import { jsxRenderer } from '@kubb/renderer-jsx'
|
|
18165
|
-
* import { defineGenerator } from '@kubb/core'
|
|
18166
18372
|
*
|
|
18167
18373
|
* export const myGenerator = defineGenerator<PluginTs>({
|
|
18168
|
-
* name: 'my-generator',
|
|
18169
18374
|
* renderer: jsxRenderer,
|
|
18170
18375
|
* schema(node, options) {
|
|
18171
18376
|
* return <File baseName="output.ts" path="src/output.ts">...</File>
|
|
@@ -18187,7 +18392,48 @@ const jsxRenderer = () => {
|
|
|
18187
18392
|
}
|
|
18188
18393
|
};
|
|
18189
18394
|
};
|
|
18395
|
+
/**
|
|
18396
|
+
* Lightweight renderer factory with no React fiber, scheduler, or work loop.
|
|
18397
|
+
*
|
|
18398
|
+
* Walks the JSX element tree in a single recursive pass. All components must be
|
|
18399
|
+
* pure functions; hooks and class components are not supported. Drop-in
|
|
18400
|
+
* replacement for {@link jsxRenderer} at approximately 2–4× the speed.
|
|
18401
|
+
*
|
|
18402
|
+
* @example Drop-in replacement
|
|
18403
|
+
* ```ts
|
|
18404
|
+
* import { jsxRendererSync } from '@kubb/renderer-jsx'
|
|
18405
|
+
*
|
|
18406
|
+
* export const myGenerator = defineGenerator<PluginTs>({
|
|
18407
|
+
* renderer: jsxRendererSync,
|
|
18408
|
+
* schema(node, options) {
|
|
18409
|
+
* return <File baseName="output.ts" path="src/output.ts">...</File>
|
|
18410
|
+
* },
|
|
18411
|
+
* })
|
|
18412
|
+
* ```
|
|
18413
|
+
*
|
|
18414
|
+
* @example Stream files as they are produced
|
|
18415
|
+
* ```ts
|
|
18416
|
+
* for await (const file of jsxRendererSync().stream(element)) {
|
|
18417
|
+
* await writeFile(file)
|
|
18418
|
+
* }
|
|
18419
|
+
* ```
|
|
18420
|
+
*/
|
|
18421
|
+
const jsxRendererSync = () => {
|
|
18422
|
+
const runtime = new SyncRuntime();
|
|
18423
|
+
return {
|
|
18424
|
+
async render(element) {
|
|
18425
|
+
runtime.render(element);
|
|
18426
|
+
},
|
|
18427
|
+
get files() {
|
|
18428
|
+
return runtime.nodes;
|
|
18429
|
+
},
|
|
18430
|
+
async *stream(element) {
|
|
18431
|
+
yield* runtime.stream(element);
|
|
18432
|
+
},
|
|
18433
|
+
unmount(_error) {}
|
|
18434
|
+
};
|
|
18435
|
+
};
|
|
18190
18436
|
//#endregion
|
|
18191
|
-
export { Const, File, Function$1 as Function, Jsx, Root, Type, createContext, inject, jsxRenderer, provide, unprovide };
|
|
18437
|
+
export { Const, File, Function$1 as Function, Jsx, Root, Type, createContext, inject, jsxRenderer, jsxRendererSync, provide, unprovide };
|
|
18192
18438
|
|
|
18193
18439
|
//# sourceMappingURL=index.js.map
|