jsx-async-runtime
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -1,5 +0,3 @@ | ||
function T(t,r,...n){r??={};let s=[];for(let e of n)h(e,s);return r?.children&&h(r.children,s),r.children=s,Object.freeze(s),Object.freeze(r),{type:"tag",tag:t,props:r}}function h(t,r){switch(typeof t){case"string":r.push({type:"textNode",text:t});break;case"number":r.push({type:"textNode",text:t.toString()});break;case"object":if(Array.isArray(t))for(let n of t)h(n,r);else t!=null&&r.push(t);break}}function b(t){let r=[];for(let n of t){let s=x(n);s.length>0&&r.push(s)}return r.join(" ")}function x([t,r]){if(r===!0)return t;if(r===!1||r===null||r===void 0)return"";if(typeof r=="object")switch(t){case"style":let n=Object.entries(r).map(([e,i])=>`${e}: ${i}`);return`style="${g(n.join("; "))}"`;case"class":let s=Object.entries(r).filter(([e,i])=>i).map(([e,i])=>e);return`class="${g(s.join(" "))}"`;default:return`${t}="${g(JSON.stringify(r))}"`}return`${t}="${g(r.toString())}"`}function g(t){return t.replaceAll('"',""")}function S(t){return{tag:t.tag.toString(),attributes:J(t),children:X(t)}}function J(t){let r=[];for(let[n,s]of Object.entries(t.props))n!=="children"&&r.push([n,s]);return r}function X(t){return t.props.children?Array.isArray(t.props.children)?t.props.children:[t.props.children]:[]}var o=class extends Error{tags=[];constructor(r,n){super(r),this.name="RenderError",this.message=r,n&&this.pushTag(n)}pushTag(r){this.tags.unshift(r)}updateMessage(){this.message=` | ||
Error: ${this.tags.filter(r=>r).map(r=>`<${r}>`).join("")} | ||
Cause: ${this.message}`}};var A=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]);async function u(t,r){if(t===null)return"";switch(typeof t){case"string":return t;case"bigint":case"number":return String(t);case"boolean":case"function":case"symbol":case"undefined":return""}let{currentIndent:n=0,indent:s=2}=r??{};if(t.type==="textNode")return`${" ".repeat(n)}${t.text}`;if(typeof t.tag!="string"){if(typeof t.tag!="function")throw new o("Invalid element");try{let e=await t.tag(t.props);return await u(e,{indent:s,currentIndent:n})}catch(e){throw e instanceof o?(e.pushTag($(t.tag)),e):new o(l(e),$(t.tag))}}else{let e=S(t);if(e.tag==="")try{let i=[];for(let a of e.children){let c=await u(a,{indent:s,currentIndent:n+s});c.length>0&&i.push(c)}return i.join(` | ||
`)}catch(i){throw i instanceof o?(i.pushTag(e.tag),i):new o(l(i),e.tag)}else try{let i=" ".repeat(n),a=b(e.attributes),c=a.length?" ":"";if(e.children.length===0&&A.has(e.tag))return`${i}<${e.tag}${c}${a}>`;let f=e.children.length===0||e.children.every(E),d=`${i}<${e.tag}${c}${a}>`,m=`${f?"":i}</${e.tag}>`,p=[];for(let w of e.children){let y=await u(w,{indent:f?0:s,currentIndent:f?0:n+s});y.length>0&&p.push(y)}return f?`${d}${p.join("")}${m}`:[d,...p,m].join(` | ||
`)}catch(i){throw i instanceof o?(i.pushTag(e.tag),i):new o(l(i),e.tag)}}}function E(t){return typeof t=="object"&&t!=null&&"type"in t&&t.type==="textNode"}function $(t){return"name"in t&&typeof t.name=="string"?t.name:"Anonymous"}function l(t){return t instanceof Error?t.message:typeof t=="string"?t:String(t)}async function j(t,r){try{return await u(await t,r)}catch(n){throw n instanceof o&&n.updateMessage(),n}}export{T as createElement,j as renderToString}; | ||
function y(t,e,...i){e??={};let n=[];for(let r of i)g(r,n);return e?.children&&g(e.children,n),e.children=n,Object.freeze(n),Object.freeze(e),{type:"tag",tag:t,props:e}}function g(t,e){switch(typeof t){case"string":e.push({type:"textNode",text:t});break;case"number":e.push({type:"textNode",text:t.toString()});break;case"object":if(Array.isArray(t))for(let i of t)g(i,e);else t!=null&&e.push(t);break}}function h(t){let e=[];for(let i of t){let n=$(i);n.length>0&&e.push(n)}return e.join(" ")}function $([t,e]){if(e===!0)return t;if(e===!1||e===null||e===void 0)return"";if(typeof e=="object")switch(t){case"style":let i=Object.entries(e).map(([r,s])=>`${r}: ${s}`);return`style="${f(i.join("; "))}"`;case"class":let n=Object.entries(e).filter(([r,s])=>s).map(([r,s])=>r);return`class="${f(n.join(" "))}"`;default:return`${t}="${f(JSON.stringify(e))}"`}return`${t}="${f(e.toString())}"`}function f(t){return t.replaceAll('"',""")}function m(t){return{tag:t.tag.toString(),attributes:x(t),children:J(t)}}function x(t){let e=[];for(let[i,n]of Object.entries(t.props))i!=="children"&&e.push([i,n]);return e}function J(t){return t.props.children?Array.isArray(t.props.children)?t.props.children:[t.props.children]:[]}var X=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]);async function a(t,e){if(t===null)return"";switch(typeof t){case"string":return t;case"bigint":case"number":return String(t);case"boolean":case"function":case"symbol":case"undefined":return""}let{currentIndent:i=0,indent:n=2}=e??{};if(t.type==="textNode")return`${" ".repeat(i)}${t.text}`;if(typeof t.tag=="string"){let r=m(t);if(r.tag===""){let s=[];for(let o of r.children){let c=await a(o,{indent:n,currentIndent:i+n});c.length>0&&s.push(c)}return s.join(` | ||
`)}else{let s=" ".repeat(i),o=h(r.attributes),c=o.length?" ":"";if(r.children.length===0&&X.has(r.tag))return`${s}<${r.tag}${c}${o}>`;let u=r.children.length===0||r.children.every(T),l=`${s}<${r.tag}${c}${o}>`,d=`${u?"":s}</${r.tag}>`,p=[];for(let S of r.children){let b=await a(S,{indent:u?0:n,currentIndent:u?0:i+n});b.length>0&&p.push(b)}return u?`${l}${p.join("")}${d}`:[l,...p,d].join(` | ||
`)}}else{let r=await t.tag(t.props);return await a(r,{indent:n,currentIndent:i})}}function T(t){return typeof t=="object"&&t!=null&&"type"in t&&t.type==="textNode"}async function A(t,e){return await a(await t,e)}export{y as createElement,A as renderToString}; |
{ | ||
"name": "jsx-async-runtime", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "An asynchronous JSX runtime without dependencies to be used as server-side template engine.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -30,6 +30,4 @@ # jsx-async-runtime | ||
If you're using templates in a server app, you might want to include data from a database in the HTML you serve to the client. | ||
If you're using `jsx-async-runtime` as server-side template engine, you might want to include data from an asynchronous operation in the resulting markup. To simplify this process, you can make your components asynchronous and send async requests from within them. | ||
To simplify this process, you can make your components asynchronous and send async requests from within them. | ||
You can study the [example project](https://github.com/jeasx/jsx-async-runtime/tree/main/example) to learn about existing features. | ||
@@ -36,0 +34,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32263
19
754
97