From 7e71b3a410302d39f9434c5c20b574a3f105f2f7 Mon Sep 17 00:00:00 2001 From: Liu Date: Sat, 2 Sep 2023 23:19:37 +0800 Subject: [PATCH] feat(compile): add support for pre rendering React components --- lib/compiler/react-tsx.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/compiler/react-tsx.js b/lib/compiler/react-tsx.js index 8236fd4..a12d5a3 100644 --- a/lib/compiler/react-tsx.js +++ b/lib/compiler/react-tsx.js @@ -104,7 +104,7 @@ function replaceCSSImportStatments(code, files) { } function transformReactElement(el) { - const node = { + let node = { type: "element", name: "", text: "", @@ -124,14 +124,21 @@ function transformReactElement(el) { break; } } else if (typeof el.type === "function") { + if (el.type.shouldPreRender) { + const newNode = transformReactElement(el.type(el.props)); + if (el.props.$ref) { + newNode.attributes.ref = el.props.$ref; + } + return newNode; + } node.name = el.type.constructor.name; } else { return; } const attrMap = { - className: 'class', - $ref: "ref" + className: "class", + $ref: "ref", }; Object.keys(el.props).forEach((propKey) => { let key = propKey; @@ -149,7 +156,9 @@ function transformReactElement(el) { if (key in attrMap) { key = attrMap[key]; } - node.attributes[key] = el.props[propKey]; + if (typeof el.props[propKey] !== 'undefined') { + node.attributes[key] = el.props[propKey]; + } }); return node; }