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