@fluixi/compiler 1.0.0-alpha.55 → 1.0.0-alpha.56
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/babel-DZ7BC6T2.mjs +2 -0
- package/dist/frontend/babel/build-ir-lit.cjs +1 -0
- package/dist/frontend/babel/build-ir-lit.d.ts +26 -0
- package/dist/frontend/babel/build-ir-lit.d.ts.map +1 -0
- package/dist/frontend/babel/build-ir-lit.js +9 -0
- package/dist/frontend/babel/build-ir-lit.mjs +1 -0
- package/dist/frontend/babel/build-ir.cjs +1 -1
- package/dist/frontend/babel/build-ir.d.ts +11 -0
- package/dist/frontend/babel/build-ir.d.ts.map +1 -1
- package/dist/frontend/babel/build-ir.js +72 -13
- package/dist/frontend/babel/build-ir.mjs +1 -1
- package/dist/frontend/babel/index.cjs +2 -2
- package/dist/frontend/babel/index.mjs +2 -2
- package/dist/frontend/babel/lower-template.cjs +1 -0
- package/dist/frontend/babel/lower-template.d.ts +21 -0
- package/dist/frontend/babel/lower-template.d.ts.map +1 -0
- package/dist/frontend/babel/lower-template.js +447 -0
- package/dist/frontend/babel/lower-template.mjs +1 -0
- package/dist/frontend/babel/plugin.cjs +2 -2
- package/dist/frontend/babel/plugin.d.ts +21 -0
- package/dist/frontend/babel/plugin.d.ts.map +1 -1
- package/dist/frontend/babel/plugin.js +144 -206
- package/dist/frontend/babel/plugin.mjs +2 -2
- package/dist/integrations.cjs +7 -7
- package/dist/integrations.d.ts +19 -0
- package/dist/integrations.d.ts.map +1 -1
- package/dist/integrations.js +3 -0
- package/dist/integrations.mjs +4 -4
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/package.json +4 -4
- package/dist/babel-OGRQKOZA.mjs +0 -2
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import"./chunk-3SAEGOMQ.mjs";import{declare as tt}from"@babel/helper-plugin-utils";import{types as t,template as Se}from"@babel/core";import{types as m,traverse as ze,template as We}from"@babel/core";import ue from"@babel/generator";var q={version:1,module:"@fluixi/dom",symbols:["createNativeElement","insert","spread","setAttribute","delegateEvents","createComponent","createMemo","untrack","Show","For","Index","Switch","Match","Dynamic","ErrorBoundary"]},wt={version:2,module:"@fluixi/dom",symbols:[...q.symbols,"template","cloneTemplate","walk"]};var je={show:"Show",for:"For",index:"Index",switch:"Switch",match:"Match",dynamic:"Dynamic",errorBoundary:"ErrorBoundary",suspense:"Suspense"};function Ae(e){return/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(e)}function se(e){return Ae(e)?e:JSON.stringify(e)}function Me(e){return e.expr!==void 0?e.reactive?`() => (${e.expr})`:`(${e.expr})`:JSON.stringify(e.literal??!0)}function Te(e){return e.expr!==void 0?`(${e.expr})`:JSON.stringify(e.literal??!0)}function $e(e,n,r){let i=e.filter(c=>c.kind==="spread"),o=e.filter(c=>c.kind!=="spread").map(c=>`${se(c.name)}: ${Me(c)}`);n!=null&&o.push(`children: ${n}`);let a=`{ ${o.join(", ")} }`;return i.length>0?(r.add("mergeProps"),`mergeProps(${i.map(c=>c.expr).join(", ")}, ${a})`):a}function oe(e,n){return e.length===1?V(e[0],n):`[${e.map(r=>V(r,n)).join(", ")}]`}function ie(e,n,r,i){i.add("createMemo"),i.add("createComponent");let s=n.filter(l=>l.kind==="spread"),a=n.filter(l=>l.kind!=="spread").map(l=>`get ${se(l.name)}() { return ${Te(l)}; }`);r.length>0&&a.push(`get children() { return ${oe(r,i)}; }`);let c=`{ ${a.join(", ")} }`;return s.length>0&&(i.add("mergeProps"),c=`mergeProps(${s.map(l=>l.expr).join(", ")}, ${c})`),`createMemo(() => createComponent(${e}, ${c}))`}function V(e,n){switch(e.kind){case"text":return JSON.stringify(e.value);case"expr":return e.reactive?`() => (${e.code})`:`(${e.code})`;case"fragment":return e.children.length===0?"null":oe(e.children,n);case"component":return ie(e.name,e.props,e.children,n);case"control":{let r=je[e.control]??e.control;return n.add(r),ie(r,e.props,e.children,n)}case"element":{n.add("createNativeElement");let r=JSON.stringify(e.tag),i="_el$",s=[],o=e.svg?`${r}, true`:r;if(s.push(`const ${i} = createNativeElement(${o});`),e.props.length>0){n.add("spread");let a=e.svg?", isSVG: true":"";s.push(`spread({ element: ${i}, props: ${$e(e.props,null,n)}${a} });`)}for(let a of e.children){n.add("insert");let c=V(a,n),l=a.kind==="expr"&&a.reactive||a.kind==="component"||a.kind==="control";s.push(l?`insert(${i}, ${c}, null);`:`insert(${i}, ${c});`)}return s.push(`return ${i};`),`(() => { ${s.join(" ")} })()`}}}var X={name:"imperative",contract:q,emit(e,n){let r=new Set;return{code:V(e,r),imports:Array.from(r)}}};import{types as z}from"@babel/core";import ae from"@babel/generator";import{parseTemplate as Oe}from"@fluixi/template-parser";var De=ae.default??ae;function Be(e){return De(e,{concise:!0}).code}var le=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function _e(e){return e.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$\{/g,"\\${")}function ce(e){return e.charAt(0).toUpperCase()+e.slice(1)}var U=class{constructor(n,r){this.holes=n;this.used=r}hole(n){return this.holes[n]??{node:z.identifier("undefined"),code:"undefined",reactive:!1}}emit(n){let{code:r,imports:i}=X.emit(n,{});for(let s of i)this.used.add(s);return r}lowerRoot(n){let r=this.lowerChildren(n);return r.length===1?r[0]:{kind:"fragment",children:r}}lowerChildren(n){let r=[];for(let i=0;i<n.length;i++){let s=n[i];if(s.kind==="Element"||s.kind==="Component"){let a=s.attributes.find(c=>c.kind==="IfDirective");if(a&&a.kind==="IfDirective"){let c=i+1;c<n.length&&this.isBlankText(n[c])&&c++;let l=n[c],p=l&&(l.kind==="Element"||l.kind==="Component")&&l.attributes.some(E=>E.kind==="ElseDirective");r.push(this.lowerIf(s,a.hole,p?l:null)),p&&(i=c);continue}if(s.attributes.some(c=>c.kind==="EachDirective")){r.push(this.lowerEach(s));continue}}let o=this.lowerNode(s);o&&r.push(o)}return r}isBlankText(n){return n.kind==="Text"&&!n.raw&&W(n.value)===""}lowerNode(n){switch(n.kind){case"Text":{if(n.raw)return{kind:"text",value:n.value};let r=W(n.value);return r?{kind:"text",value:r}:null}case"Comment":return null;case"Expression":{let r=this.hole(n.hole);return{kind:"expr",code:r.code,reactive:r.reactive}}case"Fragment":return{kind:"fragment",children:this.lowerChildren(n.children)};case"Element":return this.lowerElement(n);case"Component":return this.lowerComponent(n);default:return null}}lowerElement(n){let r=n.attributes.find(i=>i.kind==="Attribute"&&i.name==="is");if(n.tag==="component"&&r&&r.value&&r.value.kind==="hole"){let i=n.attributes.filter(o=>o!==r),s=this.lowerComponent({...n,kind:"Component",tag:"Dynamic",tagHole:null,attributes:i});return s.props.unshift({name:"component",kind:"attr",expr:`() => (${this.hole(r.value.hole).code})`}),s}return{kind:"element",tag:n.tag,svg:n.namespace==="svg",props:this.lowerAttributes(n.attributes),children:this.lowerChildren(n.children),static:!1}}lowerComponent(n){let r=n.tagHole!=null?this.hole(n.tagHole).code:n.tag,i=this.lowerAttributes(n.attributes),{slots:s,rest:o}=this.partitionSlots(n.children);for(let[a,c]of s){let l=c.length===1?c[0]:{kind:"fragment",children:c};i.push({name:a,kind:"attr",expr:this.emit(l),jsxElement:!0})}return{kind:"component",name:r,props:i,children:this.lowerChildren(o)}}partitionSlots(n){let r=new Map,i=[];for(let s of n){if(s.kind==="Element"||s.kind==="Component"){let o=s.attributes.find(a=>a.kind==="Attribute"&&a.name==="slot");if(o&&o.value&&o.value.kind==="static"){let a={...s,attributes:s.attributes.filter(p=>p!==o)},c=a.kind==="Element"?this.lowerElement(a):this.lowerComponent(a),l=r.get(o.value.value)??[];l.push(c),r.set(o.value.value,l);continue}}i.push(s)}return{slots:r,rest:i}}lowerIf(n,r,i){let s=this.hole(r),o=[{name:"when",kind:"attr",expr:s.code,reactive:s.reactive}];if(i){let c=this.stripAndLower(i,l=>l.kind==="ElseDirective");o.push({name:"fallback",kind:"attr",expr:this.emit(c),jsxElement:!0})}let a=this.stripAndLower(n,c=>c.kind==="IfDirective");return{kind:"component",name:"Show",props:o,children:[a]}}lowerEach(n){let r=n.attributes.find(l=>l.kind==="EachDirective");if(!r||r.kind!=="EachDirective")return this.lowerNode(n);let i=this.hole(r.hole),s=[{name:"each",kind:"attr",expr:i.code,reactive:i.reactive}];if(r.key){let l="static"in r.key?`(item) => item[${JSON.stringify(r.key.static)}]`:this.hole(r.key.hole).code;s.push({name:"by",kind:"attr",expr:l})}let o=this.itemArrow(n),a;if(o){let l=o.body,p={kind:"expr",code:R(l,this.used),reactive:y(l)},E=this.rebuildWithChildren(n,[p]);a=`(${o.params.map(f=>Be(f)).join(", ")}) => (${this.emit(E)})`}else{let l=this.stripAndLower(n,p=>p.kind==="EachDirective");a=`() => (${this.emit(l)})`}return{kind:"component",name:"For",props:s,children:[{kind:"expr",code:a,reactive:!1}]}}itemArrow(n){let r=n.children.filter(s=>!this.isBlankText(s));if(r.length!==1||r[0].kind!=="Expression")return null;let i=this.hole(r[0].hole).node;return z.isArrowFunctionExpression(i)&&!z.isBlockStatement(i.body)?i:null}stripAndLower(n,r){let i={...n,attributes:n.attributes.filter(s=>!r(s))};return i.kind==="Element"?this.lowerElement(i):this.lowerComponent(i)}rebuildWithChildren(n,r){let i=this.lowerAttributes(n.attributes.filter(s=>s.kind!=="EachDirective"));return n.kind==="Element"?{kind:"element",tag:n.tag,svg:n.namespace==="svg",props:i,children:r,static:!1}:{kind:"component",name:n.tag,props:i,children:r}}lowerAttributes(n){let r=[],i=[],s=!1,o=[],a=!1,c=[];for(let l of n)switch(l.kind){case"IfDirective":case"EachDirective":case"ElseDirective":break;case"Attribute":r.push(this.plainAttr(l));break;case"PropertyBinding":r.push({name:l.name,kind:"prop",expr:this.hole(l.hole).code,reactive:this.hole(l.hole).reactive});break;case"EventBinding":r.push(this.eventProp(l));break;case"RefBinding":r.push({name:"ref",kind:"ref",expr:this.hole(l.hole).code,reactive:this.hole(l.hole).reactive});break;case"Spread":r.push({name:"",kind:"spread",expr:this.hole(l.hole).code});break;case"ClassDirective":{let p=this.hole(l.hole);i.push(`${JSON.stringify(l.name)}: ${p.code}`),s=s||p.reactive;break}case"StyleDirective":{let p=this.hole(l.hole);o.push(`${JSON.stringify(l.name)}: ${p.code}`),a=a||p.reactive;break}case"BindDirective":r.push(...this.bindProps(l.name,this.hole(l.hole).code));break;case"UseDirective":{let p=l.name??(l.hole!=null?this.hole(l.hole).code:null);if(!p)break;c.push(l.name!=null&&l.hole!=null?`[${p}, () => (${this.hole(l.hole).code})]`:`[${p}]`);break}}return i.length>0&&r.push({name:"classList",kind:"attr",expr:`{ ${i.join(", ")} }`,reactive:s}),o.length>0&&r.push({name:"style",kind:"attr",expr:`{ ${o.join(", ")} }`,reactive:a}),c.length>0&&r.push({name:"use",kind:"attr",expr:`[${c.join(", ")}]`}),r}eventProp(n){let r=n.name.toLowerCase(),i=this.hole(n.hole).code;if(!(n.syntax==="colon"||n.modifiers.length>0))return{name:"on"+ce(n.name),kind:"event",event:{name:r,delegated:le.has(r)},expr:i};let o=this.wrapHandler(i,n.modifiers),a=this.eventOptions(n.modifiers),c=a?`[${o}, ${a}]`:o;return{name:"on:"+r,kind:"attr",expr:c}}wrapHandler(n,r){let i=r.includes("self")?"if (e.target !== e.currentTarget) return; ":"",s=[];return r.includes("prevent")&&s.push("e.preventDefault();"),r.includes("stop")&&s.push("e.stopPropagation();"),!i&&s.length===0?n:`(e) => { ${i}${s.join(" ")} return (${n})(e); }`}eventOptions(n){let r=[];return n.includes("capture")&&r.push("capture: true"),n.includes("once")&&r.push("once: true"),n.includes("passive")&&r.push("passive: true"),r.length?`{ ${r.join(", ")} }`:null}bindProps(n,r){let i=n==="checked",s=i?"change":"input",o=i?"checked":"value",a=`(${r})`;return[{name:n,kind:"attr",expr:`${a}[0]()`,reactive:!0},{name:"on"+ce(s),kind:"event",event:{name:s,delegated:le.has(s)},expr:`(e) => ${a}[1](e.target.${o})`}]}plainAttr(n){let r=n.value,i=n.name;n.name==="class"?i=r&&r.kind==="hole"&&z.isObjectExpression(this.hole(r.hole).node)?"classList":"className":n.name==="html"&&(i="innerHTML");let o={name:i,kind:"attr"};if(r==null)return o.literal=!0,o;if(r.kind==="static")return o.literal=r.value,o;if(r.kind==="hole"){let l=this.hole(r.hole);return o.expr=l.code,o.reactive=l.reactive,o}let a=!1,c=r.parts.map(l=>{if("text"in l)return _e(l.text);let p=this.hole(l.hole);return a=a||p.reactive,"${"+p.code+"}"}).join("");return o.expr="`"+c+"`",o.reactive=a,o}};function pe(e,n,r={}){let i=e.quasi,s=i.expressions.map(c=>{let l=c;return{node:l,code:R(l,n),reactive:y(l)}}),o=Ve(i),{root:a}=Oe(o,{svg:r.svg});return new U(s,n).lowerRoot(a.children)}function Ve(e){let n=[];return e.quasis.forEach((r,i)=>{let s=r.value.cooked??r.value.raw;if(n.push({kind:"static",text:s,start:r.start??0}),i<e.expressions.length){let o=e.expressions[i];n.push({kind:"hole",index:i,start:o.start??0,end:o.end??0})}}),n}function H(e,n=new Set,r={}){return pe(e,n,r)}var He=ue.default??ue;function fe(e){return He(e,{concise:!0}).code}function Ge(e,n){let r=m.cloneNode(e,!0),i=m.file(m.program([m.expressionStatement(r)])),s=!1,o=(a,c,l)=>{for(let p of l)n.add(p);a.replaceWith(We.expression(c,{placeholderPattern:!1,plugins:["typescript"]})()),a.skip(),s=!0};return ze(i,{"JSXElement|JSXFragment"(a){let c=a.node,{code:l,imports:p}=X.emit(M(c,n),{});o(a,l,p)},TaggedTemplateExpression(a){let c=a.node.tag;if(!m.isIdentifier(c)||c.name!=="html"&&c.name!=="svg")return;let l=H(a.node,n,{svg:c.name==="svg"}),{code:p,imports:E}=X.emit(l,{});o(a,p,E)}}),s?i.program.body[0].expression:e}function R(e,n){return fe(Ge(e,n))}var qe=new Set(["svg","path","circle","rect","line","polygon","polyline","ellipse","g","defs","clipPath","text"]),Ue=/^on[A-Z]/,Ze=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function Ke(e){if(m.isJSXIdentifier(e)){let n=e.name[0]!==e.name[0].toLowerCase();return{tag:e.name,component:n}}return m.isJSXMemberExpression(e)?{tag:fe(e),component:!0}:m.isJSXNamespacedName(e)?{tag:`${e.namespace.name}:${e.name.name}`,component:!1}:{tag:"div",component:!1}}function Ye(e){return m.isJSXIdentifier(e)?e.name==="class"?"className":e.name:m.isJSXNamespacedName(e)?`${e.namespace.name}:${e.name.name}`:"unknown"}function y(e){return m.isCallExpression(e)||m.isOptionalCallExpression(e)?!0:m.isMemberExpression(e)||m.isOptionalMemberExpression(e)?!(!e.computed&&m.isIdentifier(e.property,{name:"children"})):m.isConditionalExpression(e)?y(e.test)||y(e.consequent)||y(e.alternate):m.isLogicalExpression(e)||m.isBinaryExpression(e)?y(e.left)||y(e.right):m.isTemplateLiteral(e)?e.expressions.some(n=>y(n)):m.isObjectExpression(e)?e.properties.some(n=>m.isObjectProperty(n)&&!n.computed&&y(n.value)):m.isArrayExpression(e)?e.elements.some(n=>n!=null&&!m.isSpreadElement(n)&&y(n)):!1}function Qe(e,n){let r=Ye(e.name),i=Ue.test(r),s=i||r.startsWith("on:"),a={name:r,kind:i?"event":"attr"};i&&(a.event={name:r.slice(2).toLowerCase(),delegated:Ze.has(r.slice(2).toLowerCase())});let c=e.value;if(c==null)return a.literal=!0,a;if(m.isStringLiteral(c))return a.literal=c.value,a;if(m.isJSXExpressionContainer(c)&&!m.isJSXEmptyExpression(c.expression)){let l=c.expression;return m.isStringLiteral(l)||m.isNumericLiteral(l)||m.isBooleanLiteral(l)?(a.literal=l.value,a):(a.expr=R(l,n),a.reactive=s?!1:y(l),(m.isJSXElement(l)||m.isJSXFragment(l))&&(a.jsxElement=!0),a)}return a.literal=!0,a}function me(e,n){let r=e.value;return r==null?null:m.isStringLiteral(r)?JSON.stringify(r.value):m.isJSXExpressionContainer(r)&&!m.isJSXEmptyExpression(r.expression)?R(r.expression,n):null}function et(e,n){let r=[],i=[];for(let s of e){if(m.isJSXSpreadAttribute(s)){r.push({name:"",kind:"spread",expr:R(s.argument,n)});continue}if(!m.isJSXAttribute(s))continue;let o=m.isJSXNamespacedName(s.name)?s.name.namespace.name:null;if(o==="use"){let a=s.name.name.name;n.add(a);let c=me(s,n);i.push(c!=null?`[${a}, () => (${c})]`:`[${a}]`);continue}if(o==="oncapture"){let a=s.name.name.name.toLowerCase(),c=me(s,n)??"undefined";r.push({name:"on:"+a,kind:"attr",expr:`[${c}, { capture: true }]`});continue}r.push(Qe(s,n))}return i.length>0&&r.push({name:"use",kind:"attr",expr:`[${i.join(", ")}]`}),r}function W(e){let n=e.split(/\r\n|\n|\r/),r=0;for(let s=0;s<n.length;s++)/[^ \t]/.test(n[s])&&(r=s);let i="";for(let s=0;s<n.length;s++){let o=n[s].replace(/\t/g," ");s!==0&&(o=o.replace(/^ +/,"")),s!==n.length-1&&(o=o.replace(/ +$/,"")),o&&(s!==r&&(o+=" "),i+=o)}return i}function de(e,n){let r=[];for(let i of e)if(m.isJSXText(i)){let s=W(i.value);s&&r.push({kind:"text",value:s})}else if(m.isJSXExpressionContainer(i)){if(!m.isJSXEmptyExpression(i.expression)){let s=i.expression;r.push({kind:"expr",code:R(s,n),reactive:y(s)})}}else m.isJSXElement(i)||m.isJSXFragment(i)?r.push(M(i,n)):m.isJSXSpreadChild(i)&&r.push({kind:"expr",code:R(i.expression,n),reactive:!1});return r}function M(e,n=new Set){if(m.isJSXFragment(e))return{kind:"fragment",children:de(e.children,n)};let{tag:r,component:i}=Ke(e.openingElement.name),s=et(e.openingElement.attributes,n),o=de(e.children,n);return i?{kind:"component",name:r,props:s,children:o}:{kind:"element",tag:r,svg:qe.has(r),props:s,children:o,static:!1}}var K=new Set(["Show","For","Index","Switch","Match","Portal","Dynamic","ErrorBoundary","Suspense","Await"]),ge=new Set(["Suspense","SuspenseList","Await"]),T=new Set(["Router","Outlet","Redirect","Link"]),nt={Show:"when",For:"each",Index:"each",Match:"when"};var be=/^on[A-Z]/,ve=["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup","mouseover","mouseout","mouseenter","mouseleave"],O=tt((e,n={})=>{e.assertVersion(7);let{runtime:r="automatic",importSource:i="@fluixi/jsx",pragma:s="jsx",pragmaFrag:o="Fragment",development:a=!1,detectReactivity:c=!0,useLitHTML:l=!0,hoistStatics:p=!0,delegateEvents:E=!0,delegatedEvents:f=ve,optimizeControlFlow:w=!0,sourceMaps:L=!0,signalModule:C="@fluixi/dom",controlFlowModule:J="@fluixi/dom",routerModule:B="@fluixi/core/router-next",reactiveModule:te="@fluixi/reactive/signal",autoShowTransform:ne=!1,backend:N="imperative",codegen:A="inline",format:re="both",litTag:Pe="html"}=n,Le=re==="lit"||re==="both",Fe={backend:N,codegen:A,irImports:new Set,libModule:"@fluixi/core",hasJSX:!1,hasReactivity:!1,hasLitHTML:!1,needsSignalImport:!1,needsControlFlowImport:!1,needsCreateMemo:!1,controlFlowComponents:new Set,delegatedEvents:new Set,staticElements:new Map,staticCounter:0};return{name:"@fluixi/babel-plugin-jsx",manipulateOptions(d,u){u.plugins.push("jsx","typescript")},pre(d){Object.assign(d,Fe)},visitor:{ImportDeclaration(d,u){d.node.source.value==="@fluixi/core/rx"&&(d.node.source=t.stringLiteral("@fluixi/reactive"))},Program:{enter(d,u){Object.assign(u,{hasJSX:!1,hasReactivity:!1,hasLitHTML:!1,hasLitIR:!1,needsSignalImport:!1,needsControlFlowImport:!1,needsCreateMemo:!1,controlFlowComponents:new Set,delegatedEvents:new Set,staticElements:new Map,autoShow:ne,staticCounter:0,libModule:"@fluixi/core",backend:N,codegen:A,irImports:new Set,needsCreateNativeElement:!1,needsInsert:!1,needsSpread:!1,needsCreateComponent:!1,needsMergeProps:!1})},exit(d,u){let g=[];if(u.hasJSX||u.needsSignalImport&&g.push(t.importDeclaration([t.importSpecifier(t.identifier("signal"),t.identifier("signal"))],t.stringLiteral(C))),u.hasJSX||u.hasLitIR){if(u.irImports.size>0){let h=new Set(["Show","For","Index","Switch","Match","Dynamic","ErrorBoundary","Portal","Suspense"]),b=new Set(["createMemo","createEffect","createRenderEffect","createRoot","createSignal","onCleanup","batch","untrack"]),x=Array.from(u.irImports),I=S=>t.importSpecifier(t.identifier(S),t.identifier(S)),F=S=>!d.scope.hasBinding(S),j=x.filter(S=>b.has(S)&&F(S)),v=x.filter(S=>!h.has(S)&&!b.has(S)&&F(S)),_=x.filter(S=>h.has(S)&&F(S));j.length>0&&g.push(t.importDeclaration(j.map(I),t.stringLiteral(te))),v.length>0&&g.push(t.importDeclaration(v.map(I),t.stringLiteral(C))),_.length>0&&g.push(t.importDeclaration(_.map(I),t.stringLiteral(J)))}if(u.hasJSX&&N==="imperative"&&A!=="ir"){let b=[[!!u.needsCreateNativeElement,"createNativeElement"],[!!u.needsSpread,"spread"],[!!u.needsInsert,"insert"],[!!u.needsCreateComponent,"createComponent"],[!!u.needsMergeProps,"mergeProps"]].filter(([x,I])=>x&&!d.scope.hasBinding(I)).map(([,x])=>t.importSpecifier(t.identifier(x),t.identifier(x)));b.length>0&&g.push(t.importDeclaration(b,t.stringLiteral(C)))}else if(u.hasJSX&&r==="automatic"){let h=[t.importSpecifier(t.identifier("jsx"),t.identifier("jsx")),t.importSpecifier(t.identifier("jsxs"),t.identifier("jsxs")),t.importSpecifier(t.identifier("Fragment"),t.identifier("Fragment"))];a&&h.push(t.importSpecifier(t.identifier("jsxDEV"),t.identifier("jsxDEV"))),u.needsMergeProps&&h.push(t.importSpecifier(t.identifier("mergeProps"),t.identifier("mergeProps"))),g.push(t.importDeclaration(h,t.stringLiteral(i)))}if(u.needsCreateMemo&&!d.scope.hasBinding("createMemo")&&g.push(t.importDeclaration([t.importSpecifier(t.identifier("createMemo"),t.identifier("createMemo"))],t.stringLiteral(N==="imperative"?te:u.libModule??"@fluixi/core"))),u.needsControlFlowImport&&u.controlFlowComponents.size>0){let h=Array.from(u.controlFlowComponents).filter(v=>!d.scope.hasBinding(v)),b=u.libModule??"@fluixi/core",x=h.filter(v=>T.has(v)),I=h.filter(v=>ge.has(v)),F=h.filter(v=>!ge.has(v)&&!T.has(v)),j=(v,_)=>t.importDeclaration(v.map(S=>t.importSpecifier(t.identifier(S),t.identifier(S))),t.stringLiteral(_));F.length>0&&g.push(j(F,J)),I.length>0&&g.push(j(I,b)),x.length>0&&g.push(j(x,B))}if(E&&u.delegatedEvents.size>0){g.push(t.importDeclaration([t.importSpecifier(t.identifier("delegateEvents"),t.identifier("delegateEvents"))],t.stringLiteral(N==="imperative"?C:"@fluixi/jsx")));let h=t.arrayExpression(Array.from(u.delegatedEvents).map(x=>t.stringLiteral(x))),b=t.expressionStatement(t.callExpression(t.identifier("delegateEvents"),[h]));d.node.body.unshift(b)}if(p&&u.staticElements.size>0){let h=[];u.staticElements.forEach((b,x)=>{h.push(t.variableDeclaration("const",[t.variableDeclarator(t.identifier(x),b)]))}),d.node.body.unshift(...h)}}g.length>0&&d.node.body.unshift(...ot(g))}},JSXElement(d,u){if(u.hasJSX=!0,N==="imperative"&&A==="ir"){d.replaceWith(Ee(d.node,u));return}let g=d.node,h=g.openingElement;if(t.isJSXIdentifier(h.name)&&T.has(h.name.name)&&(u.controlFlowComponents.add(h.name.name),u.needsControlFlowImport=!0),t.isJSXIdentifier(h.name)&&K.has(h.name.name)&&(u.controlFlowComponents.add(h.name.name),w)){let x=bt(g,u);if(x){d.replaceWith(x);return}}if(p&&N!=="imperative"&&Re(g,u)){let x=St(g,u);if(x){d.replaceWith(x);return}}let b=rt(g,u,{runtime:r,pragma:s,development:a,detectReactivity:c,useLitHTML:l,autoShow:ne});d.replaceWith(b)},JSXFragment(d,u){if(u.hasJSX=!0,N==="imperative"&&A==="ir"){d.replaceWith(Ee(d.node,u));return}let g=G(d.node,u,{runtime:r,pragmaFrag:o,development:a});d.replaceWith(g)},CallExpression(d,u){if(!c)return;let g=d.node.callee;g.name,t.isIdentifier(g)&&(g.name==="createSignal"||g.name==="createStore"||g.name==="useContext"||g.name==="useLocation")&&(u.hasReactivity=!0)},JSXExpressionContainer(d){},TaggedTemplateExpression(d,u){if(Le&&N==="imperative"&&t.isIdentifier(d.node.tag)&&(d.node.tag.name===Pe||d.node.tag.name==="svg")){d.replaceWith(at(d.node,u));return}}}}});function rt(e,n,r){let{runtime:i,pragma:s,development:o,autoShow:a=!0}=r;return i==="automatic"?D(e,n,o,a):ct(e,n,s)}var it=new Set(["svg","path","circle","rect","line","polygon","polyline","ellipse","g","defs","clipPath","text"]);function he(e,n,r){return n.length>0?(r.needsMergeProps=!0,t.callExpression(t.identifier("mergeProps"),[...n,t.objectExpression(e)])):t.objectExpression(e)}function ye(e){return e.length===1?e[0]:t.arrayExpression(e)}function st(e,n,r,i,s,o){if(!n){let f=[...r];return s.length>0&&f.push(t.objectMethod("get",t.identifier("children"),[],t.blockStatement([t.returnStatement(ye(s))]))),we(e,he(f,i,o),o)}let a=e.value,c=it.has(a),l=t.identifier("_el$"),p=[];o.needsCreateNativeElement=!0;let E=[e];if(c&&E.push(t.booleanLiteral(!0)),p.push(t.variableDeclaration("const",[t.variableDeclarator(l,t.callExpression(t.identifier("createNativeElement"),E))])),r.length>0||i.length>0){o.needsSpread=!0;let f=[t.objectProperty(t.identifier("element"),l),t.objectProperty(t.identifier("props"),he(r,i,o))];c&&f.push(t.objectProperty(t.identifier("isSVG"),t.booleanLiteral(!0))),p.push(t.expressionStatement(t.callExpression(t.identifier("spread"),[t.objectExpression(f)])))}if(s.length>0){o.needsInsert=!0;for(let f of s){let L=t.isArrowFunctionExpression(f)||t.isFunctionExpression(f)||t.isCallExpression(f)&&t.isIdentifier(f.callee)&&f.callee.name==="createMemo"?[t.cloneNode(l),f,t.nullLiteral()]:[t.cloneNode(l),f];p.push(t.expressionStatement(t.callExpression(t.identifier("insert"),L)))}}return p.push(t.returnStatement(l)),t.callExpression(t.arrowFunctionExpression([],t.blockStatement(p)),[])}function we(e,n,r){return t.isIdentifier(e)&&T.has(e.name)&&(r.controlFlowComponents.add(e.name),r.needsControlFlowImport=!0),r.backend==="imperative"?(r.needsCreateComponent=!0,r.needsCreateMemo=!0,t.callExpression(t.identifier("createMemo"),[t.arrowFunctionExpression([],t.callExpression(t.identifier("createComponent"),[e,n]))])):t.callExpression(t.identifier("jsx"),[e,n])}function Ee(e,n){let r=new Set,i=M(e,r),{code:s,imports:o}=X.emit(i,{});for(let a of o)r.add(a);for(let a of r)n.irImports.add(a);return Se.expression(s,{placeholderPattern:!1,plugins:["typescript"]})()}function ot(e){let n=new Map,r=[];for(let i of e){let s=i.source.value;if(!i.specifiers.every(c=>t.isImportSpecifier(c))){r.push(i);continue}let a=n.get(s);if(!a){let c=new Set(i.specifiers.map(l=>l.local.name));n.set(s,{decl:i,names:c}),r.push(i);continue}for(let c of i.specifiers)a.names.has(c.local.name)||(a.names.add(c.local.name),a.decl.specifiers.push(c))}return r}function at(e,n){let r=new Set,i=t.isIdentifier(e.tag)&&e.tag.name==="svg",s=H(e,r,{svg:i}),{code:o,imports:a}=X.emit(s,{});for(let c of a)r.add(c);for(let c of r)n.irImports.add(c);return Ce(s,n),n.hasLitIR=!0,Se.expression(o,{placeholderPattern:!1,plugins:["typescript"]})()}function Ce(e,n){if(e.kind==="component"&&(K.has(e.name)||T.has(e.name))&&(n.controlFlowComponents.add(e.name),n.needsControlFlowImport=!0),(e.kind==="element"||e.kind==="component"||e.kind==="control")&&"props"in e)for(let r of e.props)r.kind==="event"&&r.event?.delegated&&n.delegatedEvents.add(r.event.name);if("children"in e&&e.children)for(let r of e.children)Ce(r,n)}function lt(e){return e.map(n=>{if(t.isObjectProperty(n)){let r=n.key,i=n.computed,s=n.value;return t.objectMethod("get",r,[],t.blockStatement([t.returnStatement(s)]),i)}return n})}function D(e,n,r,i=!0){let s=e.openingElement,o=e.children,a=ke(s.name),c=t.isJSXIdentifier(s.name)&&/^[a-z]/.test(s.name.name),l=Je(s.attributes,n,c,t.isJSXIdentifier(s.name)?s.name.name:""),p=l.props,E=l.spreads;c||(p=lt(p));let f=Y(o,n,i);if(n.backend==="imperative")return st(a,c,p,E,f,n);if(f.length>0){let J=f.length===1?f[0]:t.arrayExpression(f);p.push(t.objectProperty(t.identifier("children"),J))}let w;if(E.length>0){let J=p.length>0?t.objectExpression(p):t.objectExpression([]);n.needsMergeProps=!0,w=t.callExpression(t.identifier("mergeProps"),[...E,J])}else w=t.objectExpression(p);let L=r?"jsxDEV":f.length>1?"jsxs":"jsx",C=[a,w];return r&&C.push(t.identifier("undefined"),t.booleanLiteral(!1),t.identifier("undefined"),t.identifier("undefined")),t.callExpression(t.identifier(L),C)}function ct(e,n,r){let i=e.openingElement,s=e.children,o=ke(i.name),a=t.isJSXIdentifier(i.name)&&/^[a-z]/.test(i.name.name),{props:c,spreads:l}=Je(i.attributes,n,a,t.isJSXIdentifier(i.name)?i.name.name:""),p=Y(s,n,n.autoShow??!0),E;if(l.length>0){let w=c.length>0?t.objectExpression(c):t.objectExpression([]);n.needsMergeProps=!0,E=t.callExpression(t.identifier("mergeProps"),[...l,w])}else E=c.length>0?t.objectExpression(c):t.nullLiteral();let f=[o,E,...p];return t.callExpression(t.identifier(r),f)}function G(e,n,r){let{runtime:i,pragmaFrag:s,development:o}=r,a=Y(e.children,n,n.autoShow??!0);if(n.backend==="imperative")return a.length===0?t.nullLiteral():ye(a);if(i==="automatic"){let c=o?"jsxDEV":a.length>1?"jsxs":"jsx",l=t.objectExpression([t.objectProperty(t.identifier("children"),a.length===1?a[0]:t.arrayExpression(a))]),p=[t.identifier("Fragment"),l];return o&&p.push(t.identifier("undefined"),t.booleanLiteral(!1),t.identifier("undefined"),t.identifier("undefined")),t.callExpression(t.identifier(c),p)}else return t.callExpression(t.identifier(s),a)}function ke(e){if(t.isJSXIdentifier(e)){let n=e.name;return n[0]===n[0].toLowerCase()?t.stringLiteral(n):t.identifier(n)}return t.isJSXMemberExpression(e)?Ne(e):t.isJSXNamespacedName(e)?t.stringLiteral(`${e.namespace.name}:${e.name.name}`):t.stringLiteral("div")}function Ne(e){let n;return t.isJSXIdentifier(e.object)?n=t.identifier(e.object.name):n=Ne(e.object),t.memberExpression(n,t.identifier(e.property.name))}function pt(e){return/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(e)}function Je(e,n,r=!0,i=""){let s=[],o=[],a=nt[i];for(let c of e)if(t.isJSXSpreadAttribute(c))o.push(c.argument);else if(t.isJSXAttribute(c)){let l=ut(c.name),p=mt(c.value,n,l,r);if(be.test(l)){let B=l.slice(2).toLowerCase();ve.includes(B)&&n.delegatedEvents.add(B)}let E=pt(l)?t.identifier(l):t.stringLiteral(l),f=c.value,w=t.isJSXElement(f)||t.isJSXFragment(f)||t.isJSXExpressionContainer(f)&&(t.isJSXElement(f.expression)||t.isJSXFragment(f.expression)),L=!r&&l===a,C=t.isJSXExpressionContainer(f)&&!t.isJSXEmptyExpression(f.expression)?f.expression:null,J=!r&&!!C&&Ie(C,l);!r&&w||L||J?s.push(t.objectMethod("get",E,[],t.blockStatement([t.returnStatement(p)]))):s.push(t.objectProperty(E,p))}return{props:s,spreads:o}}function ut(e){return t.isJSXIdentifier(e)?e.name==="class"?"className":e.name:t.isJSXNamespacedName(e)?`${e.namespace.name}:${e.name.name}`:"unknown"}function mt(e,n,r="",i=!0){if(e===null)return t.booleanLiteral(!0);if(t.isStringLiteral(e))return e.value.includes(`
|
|
2
|
+
`)?t.stringLiteral(e.value.replace(/\s+/g," ").trim()):e;if(t.isJSXExpressionContainer(e)){if(t.isJSXEmptyExpression(e.expression))return t.booleanLiteral(!0);let s=e.expression;return i&&Ie(s,r)?t.arrowFunctionExpression([],s):s}return t.isJSXElement(e)?D(e,n,!1,n.autoShow??!0):t.isJSXFragment(e)?G(e,n,{runtime:"automatic",pragmaFrag:"Fragment",development:!1}):t.booleanLiteral(!0)}function Ie(e,n){return be.test(n)||n.startsWith("on:")||n==="ref"||n.startsWith("use:")||t.isArrowFunctionExpression(e)||t.isFunctionExpression(e)||t.isIdentifier(e)||t.isStringLiteral(e)||t.isNumericLiteral(e)||t.isBooleanLiteral(e)||t.isNullLiteral(e)?!1:!!(t.isCallExpression(e)||t.isOptionalCallExpression(e)||t.isMemberExpression(e)||t.isOptionalMemberExpression(e)||t.isLogicalExpression(e)||t.isConditionalExpression(e)||t.isBinaryExpression(e)||t.isUnaryExpression(e)||t.isTemplateLiteral(e)||t.isObjectExpression(e)||t.isArrayExpression(e))}function dt(e){let n=e.split(/\r\n|\n|\r/),r=0;for(let s=0;s<n.length;s++)/[^ \t]/.test(n[s])&&(r=s);let i="";for(let s=0;s<n.length;s++){let o=n[s].replace(/\t/g," ");s!==0&&(o=o.replace(/^ +/,"")),s!==n.length-1&&(o=o.replace(/ +$/,"")),o&&(s!==r&&(o+=" "),i+=o)}return i}function Y(e,n,r=!0){let i=[];for(let s of e)if(t.isJSXText(s)){let o=dt(s.value);o&&i.push(t.stringLiteral(o))}else if(t.isJSXExpressionContainer(s)){if(!t.isJSXEmptyExpression(s.expression)){let o=s.expression;if(r){let a=ft(o,n)??gt(o,n);if(a){i.push(a);continue}}else{let a=ht(o,n);if(a){i.push(a);continue}}xt(o)?i.push(t.arrowFunctionExpression([],o)):i.push(o)}}else t.isJSXElement(s)?i.push(D(s,n,!1,n.autoShow??!0)):t.isJSXFragment(s)?i.push(G(s,n,{runtime:"automatic",pragmaFrag:"Fragment",development:!1})):t.isJSXSpreadChild(s)&&i.push(s.expression);return i}function ft(e,n){if(!t.isArrowFunctionExpression(e)||e.params.length!==0||e.async)return null;let r=e.body;if(t.isLogicalExpression(r)&&r.operator==="&&"){let i=k(r.right);if(i)return $(P(r.left),i,null,n)}if(t.isConditionalExpression(r)){let i=k(r.consequent);if(i){let s=Q(r.alternate,n);return $(P(r.test),i,s,n,!0)}}return null}function gt(e,n){if(t.isLogicalExpression(e)&&e.operator==="&&"){let r=k(e.right);if(r)return $(P(e.left),r,null,n)}if(t.isConditionalExpression(e)){let r=k(e.consequent);if(r){let i=Q(e.alternate,n);return $(P(e.test),r,i,n)}}return null}function ht(e,n){let r=t.isArrowFunctionExpression(e)&&e.params.length===0&&!e.async&&t.isExpression(e.body)?e.body:e;if(t.isConditionalExpression(r)){let i=P(r.test),o=k(r.consequent)??r.consequent,c=k(r.alternate)??r.alternate,l=t.conditionalExpression(i,o,c);return Z(r.consequent)||Z(r.alternate)?null:(n.needsCreateMemo=!0,t.callExpression(t.identifier("createMemo"),[t.arrowFunctionExpression([],l)]))}if(t.isLogicalExpression(r)&&r.operator==="&&"){let i=P(r.left),o=k(r.right)??r.right,a=t.logicalExpression("&&",i,o);return Z(r.right)?null:(n.needsCreateMemo=!0,t.callExpression(t.identifier("createMemo"),[t.arrowFunctionExpression([],a)]))}return null}function Q(e,n){if(Et(e))return null;let r=k(e);if(r)return Xe(r,n);if(t.isConditionalExpression(e)){let i=e,s=k(i.consequent);if(s){let o=Q(i.alternate,n);return $(P(i.test),s,o,n)}}return e}function xe(e){return t.isJSXElement(e)||t.isJSXFragment(e)}function Z(e){let n=k(e);if(!n||!t.isJSXElement(n))return!1;let r=n.openingElement.name;return t.isJSXIdentifier(r)&&K.has(r.name)}function k(e){return xe(e)?e:t.isArrowFunctionExpression(e)&&e.params.length===0&&!e.async&&xe(e.body)?e.body:null}function P(e){return t.isArrowFunctionExpression(e)&&e.params.length===0&&!e.async&&t.isExpression(e.body)?e.body:e}function Et(e){return t.isNullLiteral(e)||t.isIdentifier(e)&&e.name==="undefined"||t.isBooleanLiteral(e)&&e.value===!1}function Xe(e,n){return t.isJSXElement(e)?D(e,n,!1,n.autoShow??!0):G(e,n,{runtime:"automatic",pragmaFrag:"Fragment",development:!1})}function $(e,n,r,i,s=!1){i.controlFlowComponents.add("Show"),i.needsControlFlowImport=!0;let o=Xe(n,i),a=[t.objectProperty(t.identifier("when"),t.arrowFunctionExpression([],e)),t.objectProperty(t.identifier("children"),o)];return r&&a.push(t.objectProperty(t.identifier("fallback"),r)),we(t.identifier("Show"),t.objectExpression(a),i)}function xt(e){return t.isMemberExpression(e)||t.isOptionalMemberExpression(e)?!(!e.computed&&t.isIdentifier(e.property,{name:"children"})):t.isCallExpression(e)||t.isOptionalCallExpression(e)?!0:t.isIdentifier(e)?!1:!!(t.isLogicalExpression(e)||t.isConditionalExpression(e)||t.isBinaryExpression(e)||t.isUnaryExpression(e)||t.isTemplateLiteral(e))}function Re(e,n){let r=e.openingElement;if(!t.isJSXIdentifier(r.name))return!1;let i=r.name.name;if(i[0]!==i[0].toLowerCase())return!1;for(let s of r.attributes){if(t.isJSXSpreadAttribute(s))return!1;if(t.isJSXAttribute(s)){let o=s.value;if(t.isJSXExpressionContainer(o)&&!t.isStringLiteral(o.expression)&&!t.isNumericLiteral(o.expression))return!1}}for(let s of e.children)if(t.isJSXElement(s)&&!Re(s,n)||t.isJSXExpressionContainer(s))return!1;return!0}function St(e,n){let r=`_$static${n.staticCounter++}`,i=D(e,n,!1);return n.staticElements.set(r,i),t.identifier(r)}function bt(e,n){let r=e.openingElement.name.name;return r==="Show"?vt(e,n):r==="For"?yt(e,n):null}function vt(e,n){return null}function yt(e,n){return null}var Gt=O;function qt(e){return[O,e]}function ee(e={}){return{plugins:[[O,{runtime:"automatic",importSource:"@fluixi/jsx",development:!1,detectReactivity:!0,useLitHTML:!0,hoistStatics:!0,delegateEvents:!0,optimizeControlFlow:!0,sourceMaps:!0,...e}]]}}var Ut={production:ee({development:!1,hoistStatics:!0,delegateEvents:!0,optimizeControlFlow:!0}),development:ee({development:!0,hoistStatics:!1,sourceMaps:!0}),minimal:ee({detectReactivity:!1,useLitHTML:!1,hoistStatics:!1,delegateEvents:!1,optimizeControlFlow:!1})};export{Gt as babelPluginReactiveJSX,qt as createPlugin,ee as createPreset,O as default,Ut as presets};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var O=Object.create;var g=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames;var M=Object.getPrototypeOf,_=Object.prototype.hasOwnProperty;var H=(n,e)=>{for(var t in e)g(n,t,{get:e[t],enumerable:!0})},R=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of F(e))!_.call(n,i)&&i!==t&&g(n,i,{get:()=>e[i],enumerable:!(r=B(e,i))||r.enumerable});return n};var I=(n,e,t)=>(t=n!=null?O(M(n)):{},R(e||!n||!n.__esModule?g(t,"default",{value:n,enumerable:!0}):t,n)),V=n=>R(g({},"__esModule",{value:!0}),n);var ce={};H(ce,{buildIRFromLit:()=>b});module.exports=V(ce);var f=require("@babel/core"),y=I(require("@babel/generator"),1),j=require("@fluixi/template-parser");var k={version:1,module:"@fluixi/dom",symbols:["createNativeElement","insert","spread","setAttribute","delegateEvents","createComponent","createMemo","untrack","Show","For","Index","Switch","Match","Dynamic","ErrorBoundary"]},ue={version:2,module:"@fluixi/dom",symbols:[...k.symbols,"template","cloneTemplate","walk"]};var G={show:"Show",for:"For",index:"Index",switch:"Switch",match:"Match",dynamic:"Dynamic",errorBoundary:"ErrorBoundary",suspense:"Suspense"};function K(n){return/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(n)}function w(n){return K(n)?n:JSON.stringify(n)}function U(n){return n.expr!==void 0?n.reactive?`() => (${n.expr})`:`(${n.expr})`:JSON.stringify(n.literal??!0)}function W(n){return n.expr!==void 0?`(${n.expr})`:JSON.stringify(n.literal??!0)}function z(n,e,t){let r=n.filter(l=>l.kind==="spread"),a=n.filter(l=>l.kind!=="spread").map(l=>`${w(l.name)}: ${U(l)}`);e!=null&&a.push(`children: ${e}`);let o=`{ ${a.join(", ")} }`;return r.length>0?(t.add("mergeProps"),`mergeProps(${r.map(l=>l.expr).join(", ")}, ${o})`):o}function C(n,e){return n.length===1?v(n[0],e):`[${n.map(t=>v(t,e)).join(", ")}]`}function $(n,e,t,r){r.add("createMemo"),r.add("createComponent");let i=e.filter(s=>s.kind==="spread"),o=e.filter(s=>s.kind!=="spread").map(s=>`get ${w(s.name)}() { return ${W(s)}; }`);t.length>0&&o.push(`get children() { return ${C(t,r)}; }`);let l=`{ ${o.join(", ")} }`;return i.length>0&&(r.add("mergeProps"),l=`mergeProps(${i.map(s=>s.expr).join(", ")}, ${l})`),`createMemo(() => createComponent(${n}, ${l}))`}function v(n,e){switch(n.kind){case"text":return JSON.stringify(n.value);case"expr":return n.reactive?`() => (${n.code})`:`(${n.code})`;case"fragment":return n.children.length===0?"null":C(n.children,e);case"component":return $(n.name,n.props,n.children,e);case"control":{let t=G[n.control]??n.control;return e.add(t),$(t,n.props,n.children,e)}case"element":{e.add("createNativeElement");let t=JSON.stringify(n.tag),r="_el$",i=[],a=n.svg?`${t}, true`:t;if(i.push(`const ${r} = createNativeElement(${a});`),n.props.length>0){e.add("spread");let o=n.svg?", isSVG: true":"";i.push(`spread({ element: ${r}, props: ${z(n.props,null,e)}${o} });`)}for(let o of n.children){e.add("insert");let l=v(o,e),s=o.kind==="expr"&&o.reactive||o.kind==="component"||o.kind==="control";i.push(s?`insert(${r}, ${l}, null);`:`insert(${r}, ${l});`)}return i.push(`return ${r};`),`(() => { ${i.join(" ")} })()`}}}var h={name:"imperative",contract:k,emit(n,e){let t=new Set;return{code:v(n,t),imports:Array.from(t)}}};var c=require("@babel/core"),E=I(require("@babel/generator"),1);var Z=E.default.default??E.default;function P(n){return Z(n,{concise:!0}).code}function q(n,e){let t=c.types.cloneNode(n,!0),r=c.types.file(c.types.program([c.types.expressionStatement(t)])),i=!1,a=(o,l,s)=>{for(let p of s)e.add(p);o.replaceWith(c.template.expression(l,{placeholderPattern:!1,plugins:["typescript"]})()),o.skip(),i=!0};return(0,c.traverse)(r,{"JSXElement|JSXFragment"(o){let l=o.node,{code:s,imports:p}=h.emit(N(l,e),{});a(o,s,p)},TaggedTemplateExpression(o){let l=o.node.tag;if(!c.types.isIdentifier(l)||l.name!=="html"&&l.name!=="svg")return;let s=b(o.node,e,{svg:l.name==="svg"}),{code:p,imports:m}=h.emit(s,{});a(o,p,m)}}),i?r.program.body[0].expression:n}function d(n,e){return P(q(n,e))}var Q=new Set(["svg","path","circle","rect","line","polygon","polyline","ellipse","g","defs","clipPath","text"]),Y=/^on[A-Z]/,ee=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function te(n){if(c.types.isJSXIdentifier(n)){let e=n.name[0]!==n.name[0].toLowerCase();return{tag:n.name,component:e}}return c.types.isJSXMemberExpression(n)?{tag:P(n),component:!0}:c.types.isJSXNamespacedName(n)?{tag:`${n.namespace.name}:${n.name.name}`,component:!1}:{tag:"div",component:!1}}function ne(n){return c.types.isJSXIdentifier(n)?n.name==="class"?"className":n.name:c.types.isJSXNamespacedName(n)?`${n.namespace.name}:${n.name.name}`:"unknown"}function u(n){return c.types.isCallExpression(n)||c.types.isOptionalCallExpression(n)?!0:c.types.isMemberExpression(n)||c.types.isOptionalMemberExpression(n)?!(!n.computed&&c.types.isIdentifier(n.property,{name:"children"})):c.types.isConditionalExpression(n)?u(n.test)||u(n.consequent)||u(n.alternate):c.types.isLogicalExpression(n)||c.types.isBinaryExpression(n)?u(n.left)||u(n.right):c.types.isTemplateLiteral(n)?n.expressions.some(e=>u(e)):c.types.isObjectExpression(n)?n.properties.some(e=>c.types.isObjectProperty(e)&&!e.computed&&u(e.value)):c.types.isArrayExpression(n)?n.elements.some(e=>e!=null&&!c.types.isSpreadElement(e)&&u(e)):!1}function re(n,e){let t=ne(n.name),r=Y.test(t),i=r||t.startsWith("on:"),o={name:t,kind:r?"event":"attr"};r&&(o.event={name:t.slice(2).toLowerCase(),delegated:ee.has(t.slice(2).toLowerCase())});let l=n.value;if(l==null)return o.literal=!0,o;if(c.types.isStringLiteral(l))return o.literal=l.value,o;if(c.types.isJSXExpressionContainer(l)&&!c.types.isJSXEmptyExpression(l.expression)){let s=l.expression;return c.types.isStringLiteral(s)||c.types.isNumericLiteral(s)||c.types.isBooleanLiteral(s)?(o.literal=s.value,o):(o.expr=d(s,e),o.reactive=i?!1:u(s),(c.types.isJSXElement(s)||c.types.isJSXFragment(s))&&(o.jsxElement=!0),o)}return o.literal=!0,o}function A(n,e){let t=n.value;return t==null?null:c.types.isStringLiteral(t)?JSON.stringify(t.value):c.types.isJSXExpressionContainer(t)&&!c.types.isJSXEmptyExpression(t.expression)?d(t.expression,e):null}function ie(n,e){let t=[],r=[];for(let i of n){if(c.types.isJSXSpreadAttribute(i)){t.push({name:"",kind:"spread",expr:d(i.argument,e)});continue}if(!c.types.isJSXAttribute(i))continue;let a=c.types.isJSXNamespacedName(i.name)?i.name.namespace.name:null;if(a==="use"){let o=i.name.name.name;e.add(o);let l=A(i,e);r.push(l!=null?`[${o}, () => (${l})]`:`[${o}]`);continue}if(a==="oncapture"){let o=i.name.name.name.toLowerCase(),l=A(i,e)??"undefined";t.push({name:"on:"+o,kind:"attr",expr:`[${l}, { capture: true }]`});continue}t.push(re(i,e))}return r.length>0&&t.push({name:"use",kind:"attr",expr:`[${r.join(", ")}]`}),t}function x(n){let e=n.split(/\r\n|\n|\r/),t=0;for(let i=0;i<e.length;i++)/[^ \t]/.test(e[i])&&(t=i);let r="";for(let i=0;i<e.length;i++){let a=e[i].replace(/\t/g," ");i!==0&&(a=a.replace(/^ +/,"")),i!==e.length-1&&(a=a.replace(/ +$/,"")),a&&(i!==t&&(a+=" "),r+=a)}return r}function J(n,e){let t=[];for(let r of n)if(c.types.isJSXText(r)){let i=x(r.value);i&&t.push({kind:"text",value:i})}else if(c.types.isJSXExpressionContainer(r)){if(!c.types.isJSXEmptyExpression(r.expression)){let i=r.expression;t.push({kind:"expr",code:d(i,e),reactive:u(i)})}}else c.types.isJSXElement(r)||c.types.isJSXFragment(r)?t.push(N(r,e)):c.types.isJSXSpreadChild(r)&&t.push({kind:"expr",code:d(r.expression,e),reactive:!1});return t}function N(n,e=new Set){if(c.types.isJSXFragment(n))return{kind:"fragment",children:J(n.children,e)};let{tag:t,component:r}=te(n.openingElement.name),i=ie(n.openingElement.attributes,e),a=J(n.children,e);return r?{kind:"component",name:t,props:i,children:a}:{kind:"element",tag:t,svg:Q.has(t),props:i,children:a,static:!1}}var se=y.default.default??y.default;function oe(n){return se(n,{concise:!0}).code}var T=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function ae(n){return n.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$\{/g,"\\${")}function X(n){return n.charAt(0).toUpperCase()+n.slice(1)}var S=class{constructor(e,t){this.holes=e;this.used=t}hole(e){return this.holes[e]??{node:f.types.identifier("undefined"),code:"undefined",reactive:!1}}emit(e){let{code:t,imports:r}=h.emit(e,{});for(let i of r)this.used.add(i);return t}lowerRoot(e){let t=this.lowerChildren(e);return t.length===1?t[0]:{kind:"fragment",children:t}}lowerChildren(e){let t=[];for(let r=0;r<e.length;r++){let i=e[r];if(i.kind==="Element"||i.kind==="Component"){let o=i.attributes.find(l=>l.kind==="IfDirective");if(o&&o.kind==="IfDirective"){let l=r+1;l<e.length&&this.isBlankText(e[l])&&l++;let s=e[l],p=s&&(s.kind==="Element"||s.kind==="Component")&&s.attributes.some(m=>m.kind==="ElseDirective");t.push(this.lowerIf(i,o.hole,p?s:null)),p&&(r=l);continue}if(i.attributes.some(l=>l.kind==="EachDirective")){t.push(this.lowerEach(i));continue}}let a=this.lowerNode(i);a&&t.push(a)}return t}isBlankText(e){return e.kind==="Text"&&!e.raw&&x(e.value)===""}lowerNode(e){switch(e.kind){case"Text":{if(e.raw)return{kind:"text",value:e.value};let t=x(e.value);return t?{kind:"text",value:t}:null}case"Comment":return null;case"Expression":{let t=this.hole(e.hole);return{kind:"expr",code:t.code,reactive:t.reactive}}case"Fragment":return{kind:"fragment",children:this.lowerChildren(e.children)};case"Element":return this.lowerElement(e);case"Component":return this.lowerComponent(e);default:return null}}lowerElement(e){let t=e.attributes.find(r=>r.kind==="Attribute"&&r.name==="is");if(e.tag==="component"&&t&&t.value&&t.value.kind==="hole"){let r=e.attributes.filter(a=>a!==t),i=this.lowerComponent({...e,kind:"Component",tag:"Dynamic",tagHole:null,attributes:r});return i.props.unshift({name:"component",kind:"attr",expr:`() => (${this.hole(t.value.hole).code})`}),i}return{kind:"element",tag:e.tag,svg:e.namespace==="svg",props:this.lowerAttributes(e.attributes),children:this.lowerChildren(e.children),static:!1}}lowerComponent(e){let t=e.tagHole!=null?this.hole(e.tagHole).code:e.tag,r=this.lowerAttributes(e.attributes),{slots:i,rest:a}=this.partitionSlots(e.children);for(let[o,l]of i){let s=l.length===1?l[0]:{kind:"fragment",children:l};r.push({name:o,kind:"attr",expr:this.emit(s),jsxElement:!0})}return{kind:"component",name:t,props:r,children:this.lowerChildren(a)}}partitionSlots(e){let t=new Map,r=[];for(let i of e){if(i.kind==="Element"||i.kind==="Component"){let a=i.attributes.find(o=>o.kind==="Attribute"&&o.name==="slot");if(a&&a.value&&a.value.kind==="static"){let o={...i,attributes:i.attributes.filter(p=>p!==a)},l=o.kind==="Element"?this.lowerElement(o):this.lowerComponent(o),s=t.get(a.value.value)??[];s.push(l),t.set(a.value.value,s);continue}}r.push(i)}return{slots:t,rest:r}}lowerIf(e,t,r){let i=this.hole(t),a=[{name:"when",kind:"attr",expr:i.code,reactive:i.reactive}];if(r){let l=this.stripAndLower(r,s=>s.kind==="ElseDirective");a.push({name:"fallback",kind:"attr",expr:this.emit(l),jsxElement:!0})}let o=this.stripAndLower(e,l=>l.kind==="IfDirective");return{kind:"component",name:"Show",props:a,children:[o]}}lowerEach(e){let t=e.attributes.find(s=>s.kind==="EachDirective");if(!t||t.kind!=="EachDirective")return this.lowerNode(e);let r=this.hole(t.hole),i=[{name:"each",kind:"attr",expr:r.code,reactive:r.reactive}];if(t.key){let s="static"in t.key?`(item) => item[${JSON.stringify(t.key.static)}]`:this.hole(t.key.hole).code;i.push({name:"by",kind:"attr",expr:s})}let a=this.itemArrow(e),o;if(a){let s=a.body,p={kind:"expr",code:d(s,this.used),reactive:u(s)},m=this.rebuildWithChildren(e,[p]);o=`(${a.params.map(D=>oe(D)).join(", ")}) => (${this.emit(m)})`}else{let s=this.stripAndLower(e,p=>p.kind==="EachDirective");o=`() => (${this.emit(s)})`}return{kind:"component",name:"For",props:i,children:[{kind:"expr",code:o,reactive:!1}]}}itemArrow(e){let t=e.children.filter(i=>!this.isBlankText(i));if(t.length!==1||t[0].kind!=="Expression")return null;let r=this.hole(t[0].hole).node;return f.types.isArrowFunctionExpression(r)&&!f.types.isBlockStatement(r.body)?r:null}stripAndLower(e,t){let r={...e,attributes:e.attributes.filter(i=>!t(i))};return r.kind==="Element"?this.lowerElement(r):this.lowerComponent(r)}rebuildWithChildren(e,t){let r=this.lowerAttributes(e.attributes.filter(i=>i.kind!=="EachDirective"));return e.kind==="Element"?{kind:"element",tag:e.tag,svg:e.namespace==="svg",props:r,children:t,static:!1}:{kind:"component",name:e.tag,props:r,children:t}}lowerAttributes(e){let t=[],r=[],i=!1,a=[],o=!1,l=[];for(let s of e)switch(s.kind){case"IfDirective":case"EachDirective":case"ElseDirective":break;case"Attribute":t.push(this.plainAttr(s));break;case"PropertyBinding":t.push({name:s.name,kind:"prop",expr:this.hole(s.hole).code,reactive:this.hole(s.hole).reactive});break;case"EventBinding":t.push(this.eventProp(s));break;case"RefBinding":t.push({name:"ref",kind:"ref",expr:this.hole(s.hole).code,reactive:this.hole(s.hole).reactive});break;case"Spread":t.push({name:"",kind:"spread",expr:this.hole(s.hole).code});break;case"ClassDirective":{let p=this.hole(s.hole);r.push(`${JSON.stringify(s.name)}: ${p.code}`),i=i||p.reactive;break}case"StyleDirective":{let p=this.hole(s.hole);a.push(`${JSON.stringify(s.name)}: ${p.code}`),o=o||p.reactive;break}case"BindDirective":t.push(...this.bindProps(s.name,this.hole(s.hole).code));break;case"UseDirective":{let p=s.name??(s.hole!=null?this.hole(s.hole).code:null);if(!p)break;l.push(s.name!=null&&s.hole!=null?`[${p}, () => (${this.hole(s.hole).code})]`:`[${p}]`);break}}return r.length>0&&t.push({name:"classList",kind:"attr",expr:`{ ${r.join(", ")} }`,reactive:i}),a.length>0&&t.push({name:"style",kind:"attr",expr:`{ ${a.join(", ")} }`,reactive:o}),l.length>0&&t.push({name:"use",kind:"attr",expr:`[${l.join(", ")}]`}),t}eventProp(e){let t=e.name.toLowerCase(),r=this.hole(e.hole).code;if(!(e.syntax==="colon"||e.modifiers.length>0))return{name:"on"+X(e.name),kind:"event",event:{name:t,delegated:T.has(t)},expr:r};let a=this.wrapHandler(r,e.modifiers),o=this.eventOptions(e.modifiers),l=o?`[${a}, ${o}]`:a;return{name:"on:"+t,kind:"attr",expr:l}}wrapHandler(e,t){let r=t.includes("self")?"if (e.target !== e.currentTarget) return; ":"",i=[];return t.includes("prevent")&&i.push("e.preventDefault();"),t.includes("stop")&&i.push("e.stopPropagation();"),!r&&i.length===0?e:`(e) => { ${r}${i.join(" ")} return (${e})(e); }`}eventOptions(e){let t=[];return e.includes("capture")&&t.push("capture: true"),e.includes("once")&&t.push("once: true"),e.includes("passive")&&t.push("passive: true"),t.length?`{ ${t.join(", ")} }`:null}bindProps(e,t){let r=e==="checked",i=r?"change":"input",a=r?"checked":"value",o=`(${t})`;return[{name:e,kind:"attr",expr:`${o}[0]()`,reactive:!0},{name:"on"+X(i),kind:"event",event:{name:i,delegated:T.has(i)},expr:`(e) => ${o}[1](e.target.${a})`}]}plainAttr(e){let t=e.value,r=e.name;e.name==="class"?r=t&&t.kind==="hole"&&f.types.isObjectExpression(this.hole(t.hole).node)?"classList":"className":e.name==="html"&&(r="innerHTML");let a={name:r,kind:"attr"};if(t==null)return a.literal=!0,a;if(t.kind==="static")return a.literal=t.value,a;if(t.kind==="hole"){let s=this.hole(t.hole);return a.expr=s.code,a.reactive=s.reactive,a}let o=!1,l=t.parts.map(s=>{if("text"in s)return ae(s.text);let p=this.hole(s.hole);return o=o||p.reactive,"${"+p.code+"}"}).join("");return a.expr="`"+l+"`",a.reactive=o,a}};function L(n,e,t={}){let r=n.quasi,i=r.expressions.map(l=>{let s=l;return{node:s,code:d(s,e),reactive:u(s)}}),a=le(r),{root:o}=(0,j.parseTemplate)(a,{svg:t.svg});return new S(i,e).lowerRoot(o.children)}function le(n){let e=[];return n.quasis.forEach((t,r)=>{let i=t.value.cooked??t.value.raw;if(e.push({kind:"static",text:i,start:t.start??0}),r<n.expressions.length){let a=n.expressions[r];e.push({kind:"hole",index:r,start:a.start??0,end:a.end??0})}}),e}function b(n,e=new Set,t={}){return L(n,e,t)}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Babel front-end entry for lit `` html`...` `` templates.
|
|
3
|
+
*
|
|
4
|
+
* Historically this file held a single-pass scanner that went straight from
|
|
5
|
+
* template text to compiler IR. That fused parsing, lowering, and Babel
|
|
6
|
+
* concerns into one place with no source spans, no diagnostics, and no reuse
|
|
7
|
+
* outside the compiler. It has been split:
|
|
8
|
+
*
|
|
9
|
+
* `@fluixi/template-parser` — Babel-free tokenizer + parser → Template AST
|
|
10
|
+
* `./lower-template` — Babel-aware lowering: Template AST → IR
|
|
11
|
+
*
|
|
12
|
+
* `buildIRFromLit` keeps its exact signature so every caller (build-ir's nested
|
|
13
|
+
* composition, plugin's `emitLitViaIR`) is unchanged; the shared Template AST
|
|
14
|
+
* now also powers the language server, TS plugin, and Biome plugin.
|
|
15
|
+
*/
|
|
16
|
+
import type { types as t } from '@babel/core';
|
|
17
|
+
import type { IRNode } from '../../ir/nodes.js';
|
|
18
|
+
/**
|
|
19
|
+
* Build IR from a lit `` html`...` `` (or `` svg`...` ``) TaggedTemplateExpression.
|
|
20
|
+
* `used` collects runtime symbols emitted for anything nested inside a hole.
|
|
21
|
+
* Pass `svg: true` for an `` svg`...` `` root.
|
|
22
|
+
*/
|
|
23
|
+
export declare function buildIRFromLit(node: t.TaggedTemplateExpression, used?: Set<string>, opts?: {
|
|
24
|
+
svg?: boolean;
|
|
25
|
+
}): IRNode;
|
|
26
|
+
//# sourceMappingURL=build-ir-lit.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build-ir-lit.d.ts","sourceRoot":"","sources":["../../../src/frontend/babel/build-ir-lit.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,KAAK,EAAE,KAAK,IAAI,CAAC,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAGhD;;;;GAIG;AACH,wBAAgB,cAAc,CAC5B,IAAI,EAAE,CAAC,CAAC,wBAAwB,EAChC,IAAI,GAAE,GAAG,CAAC,MAAM,CAAa,EAC7B,IAAI,GAAE;IAAE,GAAG,CAAC,EAAE,OAAO,CAAA;CAAO,GAC3B,MAAM,CAER"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { lowerLitTemplate } from './lower-template.js';
|
|
2
|
+
/**
|
|
3
|
+
* Build IR from a lit `` html`...` `` (or `` svg`...` ``) TaggedTemplateExpression.
|
|
4
|
+
* `used` collects runtime symbols emitted for anything nested inside a hole.
|
|
5
|
+
* Pass `svg: true` for an `` svg`...` `` root.
|
|
6
|
+
*/
|
|
7
|
+
export function buildIRFromLit(node, used = new Set(), opts = {}) {
|
|
8
|
+
return lowerLitTemplate(node, used, opts);
|
|
9
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{types as v}from"@babel/core";import C from"@babel/generator";import{parseTemplate as Z}from"@fluixi/template-parser";var x={version:1,module:"@fluixi/dom",symbols:["createNativeElement","insert","spread","setAttribute","delegateEvents","createComponent","createMemo","untrack","Show","For","Index","Switch","Match","Dynamic","ErrorBoundary"]},te={version:2,module:"@fluixi/dom",symbols:[...x.symbols,"template","cloneTemplate","walk"]};var X={show:"Show",for:"For",index:"Index",switch:"Switch",match:"Match",dynamic:"Dynamic",errorBoundary:"ErrorBoundary",suspense:"Suspense"};function j(n){return/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(n)}function b(n){return j(n)?n:JSON.stringify(n)}function L(n){return n.expr!==void 0?n.reactive?`() => (${n.expr})`:`(${n.expr})`:JSON.stringify(n.literal??!0)}function D(n){return n.expr!==void 0?`(${n.expr})`:JSON.stringify(n.literal??!0)}function O(n,e,t){let r=n.filter(l=>l.kind==="spread"),a=n.filter(l=>l.kind!=="spread").map(l=>`${b(l.name)}: ${L(l)}`);e!=null&&a.push(`children: ${e}`);let o=`{ ${a.join(", ")} }`;return r.length>0?(t.add("mergeProps"),`mergeProps(${r.map(l=>l.expr).join(", ")}, ${o})`):o}function y(n,e){return n.length===1?f(n[0],e):`[${n.map(t=>f(t,e)).join(", ")}]`}function N(n,e,t,r){r.add("createMemo"),r.add("createComponent");let i=e.filter(s=>s.kind==="spread"),o=e.filter(s=>s.kind!=="spread").map(s=>`get ${b(s.name)}() { return ${D(s)}; }`);t.length>0&&o.push(`get children() { return ${y(t,r)}; }`);let l=`{ ${o.join(", ")} }`;return i.length>0&&(r.add("mergeProps"),l=`mergeProps(${i.map(s=>s.expr).join(", ")}, ${l})`),`createMemo(() => createComponent(${n}, ${l}))`}function f(n,e){switch(n.kind){case"text":return JSON.stringify(n.value);case"expr":return n.reactive?`() => (${n.code})`:`(${n.code})`;case"fragment":return n.children.length===0?"null":y(n.children,e);case"component":return N(n.name,n.props,n.children,e);case"control":{let t=X[n.control]??n.control;return e.add(t),N(t,n.props,n.children,e)}case"element":{e.add("createNativeElement");let t=JSON.stringify(n.tag),r="_el$",i=[],a=n.svg?`${t}, true`:t;if(i.push(`const ${r} = createNativeElement(${a});`),n.props.length>0){e.add("spread");let o=n.svg?", isSVG: true":"";i.push(`spread({ element: ${r}, props: ${O(n.props,null,e)}${o} });`)}for(let o of n.children){e.add("insert");let l=f(o,e),s=o.kind==="expr"&&o.reactive||o.kind==="component"||o.kind==="control";i.push(s?`insert(${r}, ${l}, null);`:`insert(${r}, ${l});`)}return i.push(`return ${r};`),`(() => { ${i.join(" ")} })()`}}}var h={name:"imperative",contract:x,emit(n,e){let t=new Set;return{code:f(n,t),imports:Array.from(t)}}};import{types as c,traverse as B,template as F}from"@babel/core";import S from"@babel/generator";var M=S.default??S;function $(n){return M(n,{concise:!0}).code}function _(n,e){let t=c.cloneNode(n,!0),r=c.file(c.program([c.expressionStatement(t)])),i=!1,a=(o,l,s)=>{for(let p of s)e.add(p);o.replaceWith(F.expression(l,{placeholderPattern:!1,plugins:["typescript"]})()),o.skip(),i=!0};return B(r,{"JSXElement|JSXFragment"(o){let l=o.node,{code:s,imports:p}=h.emit(k(l,e),{});a(o,s,p)},TaggedTemplateExpression(o){let l=o.node.tag;if(!c.isIdentifier(l)||l.name!=="html"&&l.name!=="svg")return;let s=w(o.node,e,{svg:l.name==="svg"}),{code:p,imports:m}=h.emit(s,{});a(o,p,m)}}),i?r.program.body[0].expression:n}function d(n,e){return $(_(n,e))}var H=new Set(["svg","path","circle","rect","line","polygon","polyline","ellipse","g","defs","clipPath","text"]),V=/^on[A-Z]/,G=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function K(n){if(c.isJSXIdentifier(n)){let e=n.name[0]!==n.name[0].toLowerCase();return{tag:n.name,component:e}}return c.isJSXMemberExpression(n)?{tag:$(n),component:!0}:c.isJSXNamespacedName(n)?{tag:`${n.namespace.name}:${n.name.name}`,component:!1}:{tag:"div",component:!1}}function U(n){return c.isJSXIdentifier(n)?n.name==="class"?"className":n.name:c.isJSXNamespacedName(n)?`${n.namespace.name}:${n.name.name}`:"unknown"}function u(n){return c.isCallExpression(n)||c.isOptionalCallExpression(n)?!0:c.isMemberExpression(n)||c.isOptionalMemberExpression(n)?!(!n.computed&&c.isIdentifier(n.property,{name:"children"})):c.isConditionalExpression(n)?u(n.test)||u(n.consequent)||u(n.alternate):c.isLogicalExpression(n)||c.isBinaryExpression(n)?u(n.left)||u(n.right):c.isTemplateLiteral(n)?n.expressions.some(e=>u(e)):c.isObjectExpression(n)?n.properties.some(e=>c.isObjectProperty(e)&&!e.computed&&u(e.value)):c.isArrayExpression(n)?n.elements.some(e=>e!=null&&!c.isSpreadElement(e)&&u(e)):!1}function W(n,e){let t=U(n.name),r=V.test(t),i=r||t.startsWith("on:"),o={name:t,kind:r?"event":"attr"};r&&(o.event={name:t.slice(2).toLowerCase(),delegated:G.has(t.slice(2).toLowerCase())});let l=n.value;if(l==null)return o.literal=!0,o;if(c.isStringLiteral(l))return o.literal=l.value,o;if(c.isJSXExpressionContainer(l)&&!c.isJSXEmptyExpression(l.expression)){let s=l.expression;return c.isStringLiteral(s)||c.isNumericLiteral(s)||c.isBooleanLiteral(s)?(o.literal=s.value,o):(o.expr=d(s,e),o.reactive=i?!1:u(s),(c.isJSXElement(s)||c.isJSXFragment(s))&&(o.jsxElement=!0),o)}return o.literal=!0,o}function R(n,e){let t=n.value;return t==null?null:c.isStringLiteral(t)?JSON.stringify(t.value):c.isJSXExpressionContainer(t)&&!c.isJSXEmptyExpression(t.expression)?d(t.expression,e):null}function z(n,e){let t=[],r=[];for(let i of n){if(c.isJSXSpreadAttribute(i)){t.push({name:"",kind:"spread",expr:d(i.argument,e)});continue}if(!c.isJSXAttribute(i))continue;let a=c.isJSXNamespacedName(i.name)?i.name.namespace.name:null;if(a==="use"){let o=i.name.name.name;e.add(o);let l=R(i,e);r.push(l!=null?`[${o}, () => (${l})]`:`[${o}]`);continue}if(a==="oncapture"){let o=i.name.name.name.toLowerCase(),l=R(i,e)??"undefined";t.push({name:"on:"+o,kind:"attr",expr:`[${l}, { capture: true }]`});continue}t.push(W(i,e))}return r.length>0&&t.push({name:"use",kind:"attr",expr:`[${r.join(", ")}]`}),t}function g(n){let e=n.split(/\r\n|\n|\r/),t=0;for(let i=0;i<e.length;i++)/[^ \t]/.test(e[i])&&(t=i);let r="";for(let i=0;i<e.length;i++){let a=e[i].replace(/\t/g," ");i!==0&&(a=a.replace(/^ +/,"")),i!==e.length-1&&(a=a.replace(/ +$/,"")),a&&(i!==t&&(a+=" "),r+=a)}return r}function I(n,e){let t=[];for(let r of n)if(c.isJSXText(r)){let i=g(r.value);i&&t.push({kind:"text",value:i})}else if(c.isJSXExpressionContainer(r)){if(!c.isJSXEmptyExpression(r.expression)){let i=r.expression;t.push({kind:"expr",code:d(i,e),reactive:u(i)})}}else c.isJSXElement(r)||c.isJSXFragment(r)?t.push(k(r,e)):c.isJSXSpreadChild(r)&&t.push({kind:"expr",code:d(r.expression,e),reactive:!1});return t}function k(n,e=new Set){if(c.isJSXFragment(n))return{kind:"fragment",children:I(n.children,e)};let{tag:t,component:r}=K(n.openingElement.name),i=z(n.openingElement.attributes,e),a=I(n.children,e);return r?{kind:"component",name:t,props:i,children:a}:{kind:"element",tag:t,svg:H.has(t),props:i,children:a,static:!1}}var q=C.default??C;function Q(n){return q(n,{concise:!0}).code}var A=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function Y(n){return n.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$\{/g,"\\${")}function J(n){return n.charAt(0).toUpperCase()+n.slice(1)}var E=class{constructor(e,t){this.holes=e;this.used=t}hole(e){return this.holes[e]??{node:v.identifier("undefined"),code:"undefined",reactive:!1}}emit(e){let{code:t,imports:r}=h.emit(e,{});for(let i of r)this.used.add(i);return t}lowerRoot(e){let t=this.lowerChildren(e);return t.length===1?t[0]:{kind:"fragment",children:t}}lowerChildren(e){let t=[];for(let r=0;r<e.length;r++){let i=e[r];if(i.kind==="Element"||i.kind==="Component"){let o=i.attributes.find(l=>l.kind==="IfDirective");if(o&&o.kind==="IfDirective"){let l=r+1;l<e.length&&this.isBlankText(e[l])&&l++;let s=e[l],p=s&&(s.kind==="Element"||s.kind==="Component")&&s.attributes.some(m=>m.kind==="ElseDirective");t.push(this.lowerIf(i,o.hole,p?s:null)),p&&(r=l);continue}if(i.attributes.some(l=>l.kind==="EachDirective")){t.push(this.lowerEach(i));continue}}let a=this.lowerNode(i);a&&t.push(a)}return t}isBlankText(e){return e.kind==="Text"&&!e.raw&&g(e.value)===""}lowerNode(e){switch(e.kind){case"Text":{if(e.raw)return{kind:"text",value:e.value};let t=g(e.value);return t?{kind:"text",value:t}:null}case"Comment":return null;case"Expression":{let t=this.hole(e.hole);return{kind:"expr",code:t.code,reactive:t.reactive}}case"Fragment":return{kind:"fragment",children:this.lowerChildren(e.children)};case"Element":return this.lowerElement(e);case"Component":return this.lowerComponent(e);default:return null}}lowerElement(e){let t=e.attributes.find(r=>r.kind==="Attribute"&&r.name==="is");if(e.tag==="component"&&t&&t.value&&t.value.kind==="hole"){let r=e.attributes.filter(a=>a!==t),i=this.lowerComponent({...e,kind:"Component",tag:"Dynamic",tagHole:null,attributes:r});return i.props.unshift({name:"component",kind:"attr",expr:`() => (${this.hole(t.value.hole).code})`}),i}return{kind:"element",tag:e.tag,svg:e.namespace==="svg",props:this.lowerAttributes(e.attributes),children:this.lowerChildren(e.children),static:!1}}lowerComponent(e){let t=e.tagHole!=null?this.hole(e.tagHole).code:e.tag,r=this.lowerAttributes(e.attributes),{slots:i,rest:a}=this.partitionSlots(e.children);for(let[o,l]of i){let s=l.length===1?l[0]:{kind:"fragment",children:l};r.push({name:o,kind:"attr",expr:this.emit(s),jsxElement:!0})}return{kind:"component",name:t,props:r,children:this.lowerChildren(a)}}partitionSlots(e){let t=new Map,r=[];for(let i of e){if(i.kind==="Element"||i.kind==="Component"){let a=i.attributes.find(o=>o.kind==="Attribute"&&o.name==="slot");if(a&&a.value&&a.value.kind==="static"){let o={...i,attributes:i.attributes.filter(p=>p!==a)},l=o.kind==="Element"?this.lowerElement(o):this.lowerComponent(o),s=t.get(a.value.value)??[];s.push(l),t.set(a.value.value,s);continue}}r.push(i)}return{slots:t,rest:r}}lowerIf(e,t,r){let i=this.hole(t),a=[{name:"when",kind:"attr",expr:i.code,reactive:i.reactive}];if(r){let l=this.stripAndLower(r,s=>s.kind==="ElseDirective");a.push({name:"fallback",kind:"attr",expr:this.emit(l),jsxElement:!0})}let o=this.stripAndLower(e,l=>l.kind==="IfDirective");return{kind:"component",name:"Show",props:a,children:[o]}}lowerEach(e){let t=e.attributes.find(s=>s.kind==="EachDirective");if(!t||t.kind!=="EachDirective")return this.lowerNode(e);let r=this.hole(t.hole),i=[{name:"each",kind:"attr",expr:r.code,reactive:r.reactive}];if(t.key){let s="static"in t.key?`(item) => item[${JSON.stringify(t.key.static)}]`:this.hole(t.key.hole).code;i.push({name:"by",kind:"attr",expr:s})}let a=this.itemArrow(e),o;if(a){let s=a.body,p={kind:"expr",code:d(s,this.used),reactive:u(s)},m=this.rebuildWithChildren(e,[p]);o=`(${a.params.map(T=>Q(T)).join(", ")}) => (${this.emit(m)})`}else{let s=this.stripAndLower(e,p=>p.kind==="EachDirective");o=`() => (${this.emit(s)})`}return{kind:"component",name:"For",props:i,children:[{kind:"expr",code:o,reactive:!1}]}}itemArrow(e){let t=e.children.filter(i=>!this.isBlankText(i));if(t.length!==1||t[0].kind!=="Expression")return null;let r=this.hole(t[0].hole).node;return v.isArrowFunctionExpression(r)&&!v.isBlockStatement(r.body)?r:null}stripAndLower(e,t){let r={...e,attributes:e.attributes.filter(i=>!t(i))};return r.kind==="Element"?this.lowerElement(r):this.lowerComponent(r)}rebuildWithChildren(e,t){let r=this.lowerAttributes(e.attributes.filter(i=>i.kind!=="EachDirective"));return e.kind==="Element"?{kind:"element",tag:e.tag,svg:e.namespace==="svg",props:r,children:t,static:!1}:{kind:"component",name:e.tag,props:r,children:t}}lowerAttributes(e){let t=[],r=[],i=!1,a=[],o=!1,l=[];for(let s of e)switch(s.kind){case"IfDirective":case"EachDirective":case"ElseDirective":break;case"Attribute":t.push(this.plainAttr(s));break;case"PropertyBinding":t.push({name:s.name,kind:"prop",expr:this.hole(s.hole).code,reactive:this.hole(s.hole).reactive});break;case"EventBinding":t.push(this.eventProp(s));break;case"RefBinding":t.push({name:"ref",kind:"ref",expr:this.hole(s.hole).code,reactive:this.hole(s.hole).reactive});break;case"Spread":t.push({name:"",kind:"spread",expr:this.hole(s.hole).code});break;case"ClassDirective":{let p=this.hole(s.hole);r.push(`${JSON.stringify(s.name)}: ${p.code}`),i=i||p.reactive;break}case"StyleDirective":{let p=this.hole(s.hole);a.push(`${JSON.stringify(s.name)}: ${p.code}`),o=o||p.reactive;break}case"BindDirective":t.push(...this.bindProps(s.name,this.hole(s.hole).code));break;case"UseDirective":{let p=s.name??(s.hole!=null?this.hole(s.hole).code:null);if(!p)break;l.push(s.name!=null&&s.hole!=null?`[${p}, () => (${this.hole(s.hole).code})]`:`[${p}]`);break}}return r.length>0&&t.push({name:"classList",kind:"attr",expr:`{ ${r.join(", ")} }`,reactive:i}),a.length>0&&t.push({name:"style",kind:"attr",expr:`{ ${a.join(", ")} }`,reactive:o}),l.length>0&&t.push({name:"use",kind:"attr",expr:`[${l.join(", ")}]`}),t}eventProp(e){let t=e.name.toLowerCase(),r=this.hole(e.hole).code;if(!(e.syntax==="colon"||e.modifiers.length>0))return{name:"on"+J(e.name),kind:"event",event:{name:t,delegated:A.has(t)},expr:r};let a=this.wrapHandler(r,e.modifiers),o=this.eventOptions(e.modifiers),l=o?`[${a}, ${o}]`:a;return{name:"on:"+t,kind:"attr",expr:l}}wrapHandler(e,t){let r=t.includes("self")?"if (e.target !== e.currentTarget) return; ":"",i=[];return t.includes("prevent")&&i.push("e.preventDefault();"),t.includes("stop")&&i.push("e.stopPropagation();"),!r&&i.length===0?e:`(e) => { ${r}${i.join(" ")} return (${e})(e); }`}eventOptions(e){let t=[];return e.includes("capture")&&t.push("capture: true"),e.includes("once")&&t.push("once: true"),e.includes("passive")&&t.push("passive: true"),t.length?`{ ${t.join(", ")} }`:null}bindProps(e,t){let r=e==="checked",i=r?"change":"input",a=r?"checked":"value",o=`(${t})`;return[{name:e,kind:"attr",expr:`${o}[0]()`,reactive:!0},{name:"on"+J(i),kind:"event",event:{name:i,delegated:A.has(i)},expr:`(e) => ${o}[1](e.target.${a})`}]}plainAttr(e){let t=e.value,r=e.name;e.name==="class"?r=t&&t.kind==="hole"&&v.isObjectExpression(this.hole(t.hole).node)?"classList":"className":e.name==="html"&&(r="innerHTML");let a={name:r,kind:"attr"};if(t==null)return a.literal=!0,a;if(t.kind==="static")return a.literal=t.value,a;if(t.kind==="hole"){let s=this.hole(t.hole);return a.expr=s.code,a.reactive=s.reactive,a}let o=!1,l=t.parts.map(s=>{if("text"in s)return Y(s.text);let p=this.hole(s.hole);return o=o||p.reactive,"${"+p.code+"}"}).join("");return a.expr="`"+l+"`",a.reactive=o,a}};function P(n,e,t={}){let r=n.quasi,i=r.expressions.map(l=>{let s=l;return{node:s,code:d(s,e),reactive:u(s)}}),a=ee(r),{root:o}=Z(a,{svg:t.svg});return new E(i,e).lowerRoot(o.children)}function ee(n){let e=[];return n.quasis.forEach((t,r)=>{let i=t.value.cooked??t.value.raw;if(e.push({kind:"static",text:i,start:t.start??0}),r<n.expressions.length){let a=n.expressions[r];e.push({kind:"hole",index:r,start:a.start??0,end:a.end??0})}}),e}function w(n,e=new Set,t={}){return P(n,e,t)}export{w as buildIRFromLit};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var C=Object.create;var m=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var X=Object.getOwnPropertyNames;var b=Object.getPrototypeOf,k=Object.prototype.hasOwnProperty;var P=(e,t)=>{for(var r in t)m(e,r,{get:t[r],enumerable:!0})},S=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of X(t))!k.call(e,i)&&i!==r&&m(e,i,{get:()=>t[i],enumerable:!(n=I(t,i))||n.enumerable});return e};var A=(e,t,r)=>(r=e!=null?C(b(e)):{},S(t||!e||!e.__esModule?m(r,"default",{value:e,enumerable:!0}):r,e)),w=e=>S(m({},"__esModule",{value:!0}),e);var q={};P(q,{buildIR:()=>g});module.exports=w(q);var s=require("@babel/core"),x=A(require("@babel/generator"),1);var d={version:1,module:"@fluixi/dom",symbols:["createNativeElement","insert","spread","setAttribute","delegateEvents","createComponent","createMemo","untrack","Show","For","Index","Switch","Match","Dynamic","ErrorBoundary"]},H={version:2,module:"@fluixi/dom",symbols:[...d.symbols,"template","cloneTemplate","walk"]};var T={show:"Show",for:"For",index:"Index",switch:"Switch",match:"Match",dynamic:"Dynamic",errorBoundary:"ErrorBoundary",suspense:"Suspense"};function O(e){return/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(e)}function h(e){return O(e)?e:JSON.stringify(e)}function j(e){return e.expr!==void 0?e.reactive?`() => (${e.expr})`:`(${e.expr})`:JSON.stringify(e.literal??!0)}function _(e){return e.expr!==void 0?`(${e.expr})`:JSON.stringify(e.literal??!0)}function M(e,t,r){let n=e.filter(a=>a.kind==="spread"),o=e.filter(a=>a.kind!=="spread").map(a=>`${h(a.name)}: ${j(a)}`);t!=null&&o.push(`children: ${t}`);let p=`{ ${o.join(", ")} }`;return n.length>0?(r.add("mergeProps"),`mergeProps(${n.map(a=>a.expr).join(", ")}, ${p})`):p}function y(e,t){return e.length===1?u(e[0],t):`[${e.map(r=>u(r,t)).join(", ")}]`}function E(e,t,r,n){n.add("createMemo"),n.add("createComponent");let i=t.filter(c=>c.kind==="spread"),p=t.filter(c=>c.kind!=="spread").map(c=>`get ${h(c.name)}() { return ${_(c)}; }`);r.length>0&&p.push(`get children() { return ${y(r,n)}; }`);let a=`{ ${p.join(", ")} }`;return i.length>0&&(n.add("mergeProps"),a=`mergeProps(${i.map(c=>c.expr).join(", ")}, ${a})`),`createMemo(() => createComponent(${e}, ${a}))`}function u(e,t){switch(e.kind){case"text":return JSON.stringify(e.value);case"expr":return e.reactive?`() => (${e.code})`:`(${e.code})`;case"fragment":return e.children.length===0?"null":y(e.children,t);case"component":return E(e.name,e.props,e.children,t);case"control":{let r=T[e.control]??e.control;return t.add(r),E(r,e.props,e.children,t)}case"element":{t.add("createNativeElement");let r=JSON.stringify(e.tag),n="_el$",i=[],o=e.svg?`${r}, true`:r;if(i.push(`const ${n} = createNativeElement(${o});`),e.props.length>0){t.add("spread");let p=e.svg?", isSVG: true":"";i.push(`spread({ element: ${n}, props: ${M(e.props,null,t)}${p} });`)}for(let p of e.children){t.add("insert");let a=u(p,t),c=p.kind==="expr"&&p.reactive||p.kind==="component"||p.kind==="control";i.push(c?`insert(${n}, ${a}, null);`:`insert(${n}, ${a});`)}return i.push(`return ${n};`),`(() => { ${i.join(" ")} })()`}}}var $={name:"imperative",contract:d,emit(e,t){let r=new Set;return{code:u(e,r),imports:Array.from(r)}}};var L=x.default.default??x.default;function J(e){return L(e,{concise:!0}).code}function F(e,t){let r=s.types.cloneNode(e,!0),n=s.types.file(s.types.program([s.types.expressionStatement(r)])),i=!1;return(0,s.traverse)(n,{"JSXElement|JSXFragment"(o){let p=o.node,{code:a,imports:c}=$.emit(g(p,t),{});for(let v of c)t.add(v);let R=s.template.expression(a,{placeholderPattern:!1})();o.replaceWith(R),o.skip(),i=!0}}),i?n.program.body[0].expression:e}function f(e,t){return J(F(e,t))}var B=new Set(["svg","path","circle","rect","line","polygon","polyline","ellipse","g","defs","clipPath","text"]),V=/^on[A-Z]/,D=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function G(e){if(s.types.isJSXIdentifier(e)){let t=e.name[0]!==e.name[0].toLowerCase();return{tag:e.name,component:t}}return s.types.isJSXMemberExpression(e)?{tag:J(e),component:!0}:s.types.isJSXNamespacedName(e)?{tag:`${e.namespace.name}:${e.name.name}`,component:!1}:{tag:"div",component:!1}}function K(e){return s.types.isJSXIdentifier(e)?e.name==="class"?"className":e.name:s.types.isJSXNamespacedName(e)?`${e.namespace.name}:${e.name.name}`:"unknown"}function l(e){return s.types.isCallExpression(e)||s.types.isOptionalCallExpression(e)?!0:s.types.isMemberExpression(e)||s.types.isOptionalMemberExpression(e)?!(!e.computed&&s.types.isIdentifier(e.property,{name:"children"})):s.types.isConditionalExpression(e)?l(e.test)||l(e.consequent)||l(e.alternate):s.types.isLogicalExpression(e)||s.types.isBinaryExpression(e)?l(e.left)||l(e.right):s.types.isTemplateLiteral(e)?e.expressions.some(t=>l(t)):!1}function U(e,t){let r=K(e.name),n=V.test(r),o={name:r,kind:n?"event":"attr"};n&&(o.event={name:r.slice(2).toLowerCase(),delegated:D.has(r.slice(2).toLowerCase())});let p=e.value;if(p==null)return o.literal=!0,o;if(s.types.isStringLiteral(p))return o.literal=p.value,o;if(s.types.isJSXExpressionContainer(p)&&!s.types.isJSXEmptyExpression(p.expression)){let a=p.expression;return s.types.isStringLiteral(a)||s.types.isNumericLiteral(a)||s.types.isBooleanLiteral(a)?(o.literal=a.value,o):(o.expr=f(a,t),o.reactive=n?!1:l(a),(s.types.isJSXElement(a)||s.types.isJSXFragment(a))&&(o.jsxElement=!0),o)}return o.literal=!0,o}function Z(e,t){let r=[];for(let n of e)s.types.isJSXSpreadAttribute(n)?r.push({name:"",kind:"spread",expr:f(n.argument,t)}):s.types.isJSXAttribute(n)&&r.push(U(n,t));return r}function z(e){let t=e.split(/\r\n|\n|\r/),r=0;for(let i=0;i<t.length;i++)/[^ \t]/.test(t[i])&&(r=i);let n="";for(let i=0;i<t.length;i++){let o=t[i].replace(/\t/g," ");i!==0&&(o=o.replace(/^ +/,"")),i!==t.length-1&&(o=o.replace(/ +$/,"")),o&&(i!==r&&(o+=" "),n+=o)}return n}function N(e,t){let r=[];for(let n of e)if(s.types.isJSXText(n)){let i=z(n.value);i&&r.push({kind:"text",value:i})}else if(s.types.isJSXExpressionContainer(n)){if(!s.types.isJSXEmptyExpression(n.expression)){let i=n.expression;r.push({kind:"expr",code:f(i,t),reactive:l(i)})}}else s.types.isJSXElement(n)||s.types.isJSXFragment(n)?r.push(g(n,t)):s.types.isJSXSpreadChild(n)&&r.push({kind:"expr",code:f(n.expression,t),reactive:!1});return r}function g(e,t=new Set){if(s.types.isJSXFragment(e))return{kind:"fragment",children:N(e.children,t)};let{tag:r,component:n}=G(e.openingElement.name),i=Z(e.openingElement.attributes,t),o=N(e.children,t);return n?{kind:"component",name:r,props:i,children:o}:{kind:"element",tag:r,svg:B.has(r),props:i,children:o,static:!1}}
|
|
1
|
+
"use strict";var B=Object.create;var v=Object.defineProperty;var F=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var _=Object.getPrototypeOf,H=Object.prototype.hasOwnProperty;var V=(n,e)=>{for(var t in e)v(n,t,{get:e[t],enumerable:!0})},S=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of M(e))!H.call(n,i)&&i!==t&&v(n,i,{get:()=>e[i],enumerable:!(r=F(e,i))||r.enumerable});return n};var R=(n,e,t)=>(t=n!=null?B(_(n)):{},S(e||!n||!n.__esModule?v(t,"default",{value:n,enumerable:!0}):t,n)),G=n=>S(v({},"__esModule",{value:!0}),n);var ce={};V(ce,{SVG_TAGS:()=>D,buildIR:()=>k,cleanJSXText:()=>g,genExpr:()=>d,isReactiveExpr:()=>u});module.exports=G(ce);var c=require("@babel/core"),y=R(require("@babel/generator"),1);var E={version:1,module:"@fluixi/dom",symbols:["createNativeElement","insert","spread","setAttribute","delegateEvents","createComponent","createMemo","untrack","Show","For","Index","Switch","Match","Dynamic","ErrorBoundary"]},ue={version:2,module:"@fluixi/dom",symbols:[...E.symbols,"template","cloneTemplate","walk"]};var K={show:"Show",for:"For",index:"Index",switch:"Switch",match:"Match",dynamic:"Dynamic",errorBoundary:"ErrorBoundary",suspense:"Suspense"};function U(n){return/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(n)}function $(n){return U(n)?n:JSON.stringify(n)}function W(n){return n.expr!==void 0?n.reactive?`() => (${n.expr})`:`(${n.expr})`:JSON.stringify(n.literal??!0)}function z(n){return n.expr!==void 0?`(${n.expr})`:JSON.stringify(n.literal??!0)}function Z(n,e,t){let r=n.filter(l=>l.kind==="spread"),a=n.filter(l=>l.kind!=="spread").map(l=>`${$(l.name)}: ${W(l)}`);e!=null&&a.push(`children: ${e}`);let o=`{ ${a.join(", ")} }`;return r.length>0?(t.add("mergeProps"),`mergeProps(${r.map(l=>l.expr).join(", ")}, ${o})`):o}function w(n,e){return n.length===1?x(n[0],e):`[${n.map(t=>x(t,e)).join(", ")}]`}function I(n,e,t,r){r.add("createMemo"),r.add("createComponent");let i=e.filter(s=>s.kind==="spread"),o=e.filter(s=>s.kind!=="spread").map(s=>`get ${$(s.name)}() { return ${z(s)}; }`);t.length>0&&o.push(`get children() { return ${w(t,r)}; }`);let l=`{ ${o.join(", ")} }`;return i.length>0&&(r.add("mergeProps"),l=`mergeProps(${i.map(s=>s.expr).join(", ")}, ${l})`),`createMemo(() => createComponent(${n}, ${l}))`}function x(n,e){switch(n.kind){case"text":return JSON.stringify(n.value);case"expr":return n.reactive?`() => (${n.code})`:`(${n.code})`;case"fragment":return n.children.length===0?"null":w(n.children,e);case"component":return I(n.name,n.props,n.children,e);case"control":{let t=K[n.control]??n.control;return e.add(t),I(t,n.props,n.children,e)}case"element":{e.add("createNativeElement");let t=JSON.stringify(n.tag),r="_el$",i=[],a=n.svg?`${t}, true`:t;if(i.push(`const ${r} = createNativeElement(${a});`),n.props.length>0){e.add("spread");let o=n.svg?", isSVG: true":"";i.push(`spread({ element: ${r}, props: ${Z(n.props,null,e)}${o} });`)}for(let o of n.children){e.add("insert");let l=x(o,e),s=o.kind==="expr"&&o.reactive||o.kind==="component"||o.kind==="control";i.push(s?`insert(${r}, ${l}, null);`:`insert(${r}, ${l});`)}return i.push(`return ${r};`),`(() => { ${i.join(" ")} })()`}}}var h={name:"imperative",contract:E,emit(n,e){let t=new Set;return{code:x(n,t),imports:Array.from(t)}}};var f=require("@babel/core"),N=R(require("@babel/generator"),1),J=require("@fluixi/template-parser");var q=N.default.default??N.default;function Q(n){return q(n,{concise:!0}).code}var C=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function Y(n){return n.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$\{/g,"\\${")}function A(n){return n.charAt(0).toUpperCase()+n.slice(1)}var b=class{constructor(e,t){this.holes=e;this.used=t}hole(e){return this.holes[e]??{node:f.types.identifier("undefined"),code:"undefined",reactive:!1}}emit(e){let{code:t,imports:r}=h.emit(e,{});for(let i of r)this.used.add(i);return t}lowerRoot(e){let t=this.lowerChildren(e);return t.length===1?t[0]:{kind:"fragment",children:t}}lowerChildren(e){let t=[];for(let r=0;r<e.length;r++){let i=e[r];if(i.kind==="Element"||i.kind==="Component"){let o=i.attributes.find(l=>l.kind==="IfDirective");if(o&&o.kind==="IfDirective"){let l=r+1;l<e.length&&this.isBlankText(e[l])&&l++;let s=e[l],p=s&&(s.kind==="Element"||s.kind==="Component")&&s.attributes.some(m=>m.kind==="ElseDirective");t.push(this.lowerIf(i,o.hole,p?s:null)),p&&(r=l);continue}if(i.attributes.some(l=>l.kind==="EachDirective")){t.push(this.lowerEach(i));continue}}let a=this.lowerNode(i);a&&t.push(a)}return t}isBlankText(e){return e.kind==="Text"&&!e.raw&&g(e.value)===""}lowerNode(e){switch(e.kind){case"Text":{if(e.raw)return{kind:"text",value:e.value};let t=g(e.value);return t?{kind:"text",value:t}:null}case"Comment":return null;case"Expression":{let t=this.hole(e.hole);return{kind:"expr",code:t.code,reactive:t.reactive}}case"Fragment":return{kind:"fragment",children:this.lowerChildren(e.children)};case"Element":return this.lowerElement(e);case"Component":return this.lowerComponent(e);default:return null}}lowerElement(e){let t=e.attributes.find(r=>r.kind==="Attribute"&&r.name==="is");if(e.tag==="component"&&t&&t.value&&t.value.kind==="hole"){let r=e.attributes.filter(a=>a!==t),i=this.lowerComponent({...e,kind:"Component",tag:"Dynamic",tagHole:null,attributes:r});return i.props.unshift({name:"component",kind:"attr",expr:`() => (${this.hole(t.value.hole).code})`}),i}return{kind:"element",tag:e.tag,svg:e.namespace==="svg",props:this.lowerAttributes(e.attributes),children:this.lowerChildren(e.children),static:!1}}lowerComponent(e){let t=e.tagHole!=null?this.hole(e.tagHole).code:e.tag,r=this.lowerAttributes(e.attributes),{slots:i,rest:a}=this.partitionSlots(e.children);for(let[o,l]of i){let s=l.length===1?l[0]:{kind:"fragment",children:l};r.push({name:o,kind:"attr",expr:this.emit(s),jsxElement:!0})}return{kind:"component",name:t,props:r,children:this.lowerChildren(a)}}partitionSlots(e){let t=new Map,r=[];for(let i of e){if(i.kind==="Element"||i.kind==="Component"){let a=i.attributes.find(o=>o.kind==="Attribute"&&o.name==="slot");if(a&&a.value&&a.value.kind==="static"){let o={...i,attributes:i.attributes.filter(p=>p!==a)},l=o.kind==="Element"?this.lowerElement(o):this.lowerComponent(o),s=t.get(a.value.value)??[];s.push(l),t.set(a.value.value,s);continue}}r.push(i)}return{slots:t,rest:r}}lowerIf(e,t,r){let i=this.hole(t),a=[{name:"when",kind:"attr",expr:i.code,reactive:i.reactive}];if(r){let l=this.stripAndLower(r,s=>s.kind==="ElseDirective");a.push({name:"fallback",kind:"attr",expr:this.emit(l),jsxElement:!0})}let o=this.stripAndLower(e,l=>l.kind==="IfDirective");return{kind:"component",name:"Show",props:a,children:[o]}}lowerEach(e){let t=e.attributes.find(s=>s.kind==="EachDirective");if(!t||t.kind!=="EachDirective")return this.lowerNode(e);let r=this.hole(t.hole),i=[{name:"each",kind:"attr",expr:r.code,reactive:r.reactive}];if(t.key){let s="static"in t.key?`(item) => item[${JSON.stringify(t.key.static)}]`:this.hole(t.key.hole).code;i.push({name:"by",kind:"attr",expr:s})}let a=this.itemArrow(e),o;if(a){let s=a.body,p={kind:"expr",code:d(s,this.used),reactive:u(s)},m=this.rebuildWithChildren(e,[p]);o=`(${a.params.map(O=>Q(O)).join(", ")}) => (${this.emit(m)})`}else{let s=this.stripAndLower(e,p=>p.kind==="EachDirective");o=`() => (${this.emit(s)})`}return{kind:"component",name:"For",props:i,children:[{kind:"expr",code:o,reactive:!1}]}}itemArrow(e){let t=e.children.filter(i=>!this.isBlankText(i));if(t.length!==1||t[0].kind!=="Expression")return null;let r=this.hole(t[0].hole).node;return f.types.isArrowFunctionExpression(r)&&!f.types.isBlockStatement(r.body)?r:null}stripAndLower(e,t){let r={...e,attributes:e.attributes.filter(i=>!t(i))};return r.kind==="Element"?this.lowerElement(r):this.lowerComponent(r)}rebuildWithChildren(e,t){let r=this.lowerAttributes(e.attributes.filter(i=>i.kind!=="EachDirective"));return e.kind==="Element"?{kind:"element",tag:e.tag,svg:e.namespace==="svg",props:r,children:t,static:!1}:{kind:"component",name:e.tag,props:r,children:t}}lowerAttributes(e){let t=[],r=[],i=!1,a=[],o=!1,l=[];for(let s of e)switch(s.kind){case"IfDirective":case"EachDirective":case"ElseDirective":break;case"Attribute":t.push(this.plainAttr(s));break;case"PropertyBinding":t.push({name:s.name,kind:"prop",expr:this.hole(s.hole).code,reactive:this.hole(s.hole).reactive});break;case"EventBinding":t.push(this.eventProp(s));break;case"RefBinding":t.push({name:"ref",kind:"ref",expr:this.hole(s.hole).code,reactive:this.hole(s.hole).reactive});break;case"Spread":t.push({name:"",kind:"spread",expr:this.hole(s.hole).code});break;case"ClassDirective":{let p=this.hole(s.hole);r.push(`${JSON.stringify(s.name)}: ${p.code}`),i=i||p.reactive;break}case"StyleDirective":{let p=this.hole(s.hole);a.push(`${JSON.stringify(s.name)}: ${p.code}`),o=o||p.reactive;break}case"BindDirective":t.push(...this.bindProps(s.name,this.hole(s.hole).code));break;case"UseDirective":{let p=s.name??(s.hole!=null?this.hole(s.hole).code:null);if(!p)break;l.push(s.name!=null&&s.hole!=null?`[${p}, () => (${this.hole(s.hole).code})]`:`[${p}]`);break}}return r.length>0&&t.push({name:"classList",kind:"attr",expr:`{ ${r.join(", ")} }`,reactive:i}),a.length>0&&t.push({name:"style",kind:"attr",expr:`{ ${a.join(", ")} }`,reactive:o}),l.length>0&&t.push({name:"use",kind:"attr",expr:`[${l.join(", ")}]`}),t}eventProp(e){let t=e.name.toLowerCase(),r=this.hole(e.hole).code;if(!(e.syntax==="colon"||e.modifiers.length>0))return{name:"on"+A(e.name),kind:"event",event:{name:t,delegated:C.has(t)},expr:r};let a=this.wrapHandler(r,e.modifiers),o=this.eventOptions(e.modifiers),l=o?`[${a}, ${o}]`:a;return{name:"on:"+t,kind:"attr",expr:l}}wrapHandler(e,t){let r=t.includes("self")?"if (e.target !== e.currentTarget) return; ":"",i=[];return t.includes("prevent")&&i.push("e.preventDefault();"),t.includes("stop")&&i.push("e.stopPropagation();"),!r&&i.length===0?e:`(e) => { ${r}${i.join(" ")} return (${e})(e); }`}eventOptions(e){let t=[];return e.includes("capture")&&t.push("capture: true"),e.includes("once")&&t.push("once: true"),e.includes("passive")&&t.push("passive: true"),t.length?`{ ${t.join(", ")} }`:null}bindProps(e,t){let r=e==="checked",i=r?"change":"input",a=r?"checked":"value",o=`(${t})`;return[{name:e,kind:"attr",expr:`${o}[0]()`,reactive:!0},{name:"on"+A(i),kind:"event",event:{name:i,delegated:C.has(i)},expr:`(e) => ${o}[1](e.target.${a})`}]}plainAttr(e){let t=e.value,r=e.name;e.name==="class"?r=t&&t.kind==="hole"&&f.types.isObjectExpression(this.hole(t.hole).node)?"classList":"className":e.name==="html"&&(r="innerHTML");let a={name:r,kind:"attr"};if(t==null)return a.literal=!0,a;if(t.kind==="static")return a.literal=t.value,a;if(t.kind==="hole"){let s=this.hole(t.hole);return a.expr=s.code,a.reactive=s.reactive,a}let o=!1,l=t.parts.map(s=>{if("text"in s)return Y(s.text);let p=this.hole(s.hole);return o=o||p.reactive,"${"+p.code+"}"}).join("");return a.expr="`"+l+"`",a.reactive=o,a}};function P(n,e,t={}){let r=n.quasi,i=r.expressions.map(l=>{let s=l;return{node:s,code:d(s,e),reactive:u(s)}}),a=ee(r),{root:o}=(0,J.parseTemplate)(a,{svg:t.svg});return new b(i,e).lowerRoot(o.children)}function ee(n){let e=[];return n.quasis.forEach((t,r)=>{let i=t.value.cooked??t.value.raw;if(e.push({kind:"static",text:i,start:t.start??0}),r<n.expressions.length){let a=n.expressions[r];e.push({kind:"hole",index:r,start:a.start??0,end:a.end??0})}}),e}function T(n,e=new Set,t={}){return P(n,e,t)}var te=y.default.default??y.default;function L(n){return te(n,{concise:!0}).code}function ne(n,e){let t=c.types.cloneNode(n,!0),r=c.types.file(c.types.program([c.types.expressionStatement(t)])),i=!1,a=(o,l,s)=>{for(let p of s)e.add(p);o.replaceWith(c.template.expression(l,{placeholderPattern:!1,plugins:["typescript"]})()),o.skip(),i=!0};return(0,c.traverse)(r,{"JSXElement|JSXFragment"(o){let l=o.node,{code:s,imports:p}=h.emit(k(l,e),{});a(o,s,p)},TaggedTemplateExpression(o){let l=o.node.tag;if(!c.types.isIdentifier(l)||l.name!=="html"&&l.name!=="svg")return;let s=T(o.node,e,{svg:l.name==="svg"}),{code:p,imports:m}=h.emit(s,{});a(o,p,m)}}),i?r.program.body[0].expression:n}function d(n,e){return L(ne(n,e))}var D=new Set(["svg","path","circle","rect","line","polygon","polyline","ellipse","g","defs","clipPath","text"]),re=/^on[A-Z]/,ie=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function se(n){if(c.types.isJSXIdentifier(n)){let e=n.name[0]!==n.name[0].toLowerCase();return{tag:n.name,component:e}}return c.types.isJSXMemberExpression(n)?{tag:L(n),component:!0}:c.types.isJSXNamespacedName(n)?{tag:`${n.namespace.name}:${n.name.name}`,component:!1}:{tag:"div",component:!1}}function oe(n){return c.types.isJSXIdentifier(n)?n.name==="class"?"className":n.name:c.types.isJSXNamespacedName(n)?`${n.namespace.name}:${n.name.name}`:"unknown"}function u(n){return c.types.isCallExpression(n)||c.types.isOptionalCallExpression(n)?!0:c.types.isMemberExpression(n)||c.types.isOptionalMemberExpression(n)?!(!n.computed&&c.types.isIdentifier(n.property,{name:"children"})):c.types.isConditionalExpression(n)?u(n.test)||u(n.consequent)||u(n.alternate):c.types.isLogicalExpression(n)||c.types.isBinaryExpression(n)?u(n.left)||u(n.right):c.types.isTemplateLiteral(n)?n.expressions.some(e=>u(e)):c.types.isObjectExpression(n)?n.properties.some(e=>c.types.isObjectProperty(e)&&!e.computed&&u(e.value)):c.types.isArrayExpression(n)?n.elements.some(e=>e!=null&&!c.types.isSpreadElement(e)&&u(e)):!1}function ae(n,e){let t=oe(n.name),r=re.test(t),i=r||t.startsWith("on:"),o={name:t,kind:r?"event":"attr"};r&&(o.event={name:t.slice(2).toLowerCase(),delegated:ie.has(t.slice(2).toLowerCase())});let l=n.value;if(l==null)return o.literal=!0,o;if(c.types.isStringLiteral(l))return o.literal=l.value,o;if(c.types.isJSXExpressionContainer(l)&&!c.types.isJSXEmptyExpression(l.expression)){let s=l.expression;return c.types.isStringLiteral(s)||c.types.isNumericLiteral(s)||c.types.isBooleanLiteral(s)?(o.literal=s.value,o):(o.expr=d(s,e),o.reactive=i?!1:u(s),(c.types.isJSXElement(s)||c.types.isJSXFragment(s))&&(o.jsxElement=!0),o)}return o.literal=!0,o}function X(n,e){let t=n.value;return t==null?null:c.types.isStringLiteral(t)?JSON.stringify(t.value):c.types.isJSXExpressionContainer(t)&&!c.types.isJSXEmptyExpression(t.expression)?d(t.expression,e):null}function le(n,e){let t=[],r=[];for(let i of n){if(c.types.isJSXSpreadAttribute(i)){t.push({name:"",kind:"spread",expr:d(i.argument,e)});continue}if(!c.types.isJSXAttribute(i))continue;let a=c.types.isJSXNamespacedName(i.name)?i.name.namespace.name:null;if(a==="use"){let o=i.name.name.name;e.add(o);let l=X(i,e);r.push(l!=null?`[${o}, () => (${l})]`:`[${o}]`);continue}if(a==="oncapture"){let o=i.name.name.name.toLowerCase(),l=X(i,e)??"undefined";t.push({name:"on:"+o,kind:"attr",expr:`[${l}, { capture: true }]`});continue}t.push(ae(i,e))}return r.length>0&&t.push({name:"use",kind:"attr",expr:`[${r.join(", ")}]`}),t}function g(n){let e=n.split(/\r\n|\n|\r/),t=0;for(let i=0;i<e.length;i++)/[^ \t]/.test(e[i])&&(t=i);let r="";for(let i=0;i<e.length;i++){let a=e[i].replace(/\t/g," ");i!==0&&(a=a.replace(/^ +/,"")),i!==e.length-1&&(a=a.replace(/ +$/,"")),a&&(i!==t&&(a+=" "),r+=a)}return r}function j(n,e){let t=[];for(let r of n)if(c.types.isJSXText(r)){let i=g(r.value);i&&t.push({kind:"text",value:i})}else if(c.types.isJSXExpressionContainer(r)){if(!c.types.isJSXEmptyExpression(r.expression)){let i=r.expression;t.push({kind:"expr",code:d(i,e),reactive:u(i)})}}else c.types.isJSXElement(r)||c.types.isJSXFragment(r)?t.push(k(r,e)):c.types.isJSXSpreadChild(r)&&t.push({kind:"expr",code:d(r.expression,e),reactive:!1});return t}function k(n,e=new Set){if(c.types.isJSXFragment(n))return{kind:"fragment",children:j(n.children,e)};let{tag:t,component:r}=se(n.openingElement.name),i=le(n.openingElement.attributes,e),a=j(n.children,e);return r?{kind:"component",name:t,props:i,children:a}:{kind:"element",tag:t,svg:D.has(t),props:i,children:a,static:!1}}
|
|
@@ -14,6 +14,17 @@
|
|
|
14
14
|
*/
|
|
15
15
|
import { types as t } from '@babel/core';
|
|
16
16
|
import type { IRNode } from '../../ir/nodes.js';
|
|
17
|
+
/** Stringify an expression, compiling any JSX nested inside it first. */
|
|
18
|
+
export declare function genExpr(node: t.Expression, used: Set<string>): string;
|
|
19
|
+
export declare const SVG_TAGS: Set<string>;
|
|
20
|
+
/**
|
|
21
|
+
* Reactivity heuristic, aligned with the babel transform: calls and member
|
|
22
|
+
* accesses (e.g. `count()`, `store.value`) are wired reactively; bare
|
|
23
|
+
* identifiers and literals are passed through as-is.
|
|
24
|
+
*/
|
|
25
|
+
export declare function isReactiveExpr(node: t.Expression): boolean;
|
|
26
|
+
/** Canonical JSX text cleaning — kept identical to plugin.ts cleanJSXText. */
|
|
27
|
+
export declare function cleanJSXText(value: string): string;
|
|
17
28
|
/**
|
|
18
29
|
* Build IR from a JSXElement or JSXFragment. `used` accumulates the runtime
|
|
19
30
|
* symbols emitted for any JSX nested inside expressions (so the caller can
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"build-ir.d.ts","sourceRoot":"","sources":["../../../src/frontend/babel/build-ir.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,EAAE,KAAK,IAAI,CAAC,EAAsB,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"build-ir.d.ts","sourceRoot":"","sources":["../../../src/frontend/babel/build-ir.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,EAAE,KAAK,IAAI,CAAC,EAAsB,MAAM,aAAa,CAAC;AAI7D,OAAO,KAAK,EACV,MAAM,EAGP,MAAM,mBAAmB,CAAC;AAgD3B,yEAAyE;AACzE,wBAAgB,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,UAAU,EAAE,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,GAAG,MAAM,CAErE;AAED,eAAO,MAAM,QAAQ,aAGnB,CAAC;AA2BH;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,CAAC,CAAC,UAAU,GAAG,OAAO,CA+B1D;AAsFD,8EAA8E;AAC9E,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAgBlD;AA2BD;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,UAAU,GAAG,CAAC,CAAC,WAAW,EAAE,IAAI,GAAE,GAAG,CAAC,MAAM,CAAa,GAAG,MAAM,CAYjG"}
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
import { types as t, traverse, template } from '@babel/core';
|
|
16
16
|
import _generate from '@babel/generator';
|
|
17
17
|
import { imperativeBackend } from '../../codegen/backends/imperative.js';
|
|
18
|
+
import { buildIRFromLit } from './build-ir-lit.js';
|
|
18
19
|
// @babel/generator's ESM/CJS interop default (its types aren't callable under nodenext).
|
|
19
20
|
const generate = _generate.default ?? _generate;
|
|
20
21
|
/** Stringify an AST node back to source. */
|
|
@@ -30,16 +31,30 @@ function transformNestedJSX(node, used) {
|
|
|
30
31
|
const clone = t.cloneNode(node, true);
|
|
31
32
|
const file = t.file(t.program([t.expressionStatement(clone)]));
|
|
32
33
|
let changed = false;
|
|
34
|
+
const emitInto = (path, code, imports) => {
|
|
35
|
+
for (const s of imports)
|
|
36
|
+
used.add(s);
|
|
37
|
+
// parse as TS: a hole/arrow param may carry a type annotation ((x: T) => …);
|
|
38
|
+
// preset-typescript strips it downstream, but the reparse must accept it.
|
|
39
|
+
path.replaceWith(template.expression(code, { placeholderPattern: false, plugins: ['typescript'] })());
|
|
40
|
+
path.skip(); // build-ir already handled this subtree (incl. anything nested)
|
|
41
|
+
changed = true;
|
|
42
|
+
};
|
|
33
43
|
traverse(file, {
|
|
34
44
|
'JSXElement|JSXFragment'(path) {
|
|
35
45
|
const jsx = path.node;
|
|
36
46
|
const { code, imports } = imperativeBackend.emit(buildIR(jsx, used), {});
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
47
|
+
emitInto(path, code, imports);
|
|
48
|
+
},
|
|
49
|
+
// Nested lit templates compose: ${cond ? html`…` : html`…`},
|
|
50
|
+
// ${list.map(x => html`<li>${x}</li>`)}. Compile them the same way.
|
|
51
|
+
TaggedTemplateExpression(path) {
|
|
52
|
+
const tag = path.node.tag;
|
|
53
|
+
if (!t.isIdentifier(tag) || (tag.name !== 'html' && tag.name !== 'svg'))
|
|
54
|
+
return;
|
|
55
|
+
const ir = buildIRFromLit(path.node, used, { svg: tag.name === 'svg' });
|
|
56
|
+
const { code, imports } = imperativeBackend.emit(ir, {});
|
|
57
|
+
emitInto(path, code, imports);
|
|
43
58
|
},
|
|
44
59
|
});
|
|
45
60
|
return changed
|
|
@@ -47,10 +62,10 @@ function transformNestedJSX(node, used) {
|
|
|
47
62
|
: node;
|
|
48
63
|
}
|
|
49
64
|
/** Stringify an expression, compiling any JSX nested inside it first. */
|
|
50
|
-
function genExpr(node, used) {
|
|
65
|
+
export function genExpr(node, used) {
|
|
51
66
|
return gen(transformNestedJSX(node, used));
|
|
52
67
|
}
|
|
53
|
-
const SVG_TAGS = new Set([
|
|
68
|
+
export const SVG_TAGS = new Set([
|
|
54
69
|
'svg', 'path', 'circle', 'rect', 'line', 'polygon', 'polyline',
|
|
55
70
|
'ellipse', 'g', 'defs', 'clipPath', 'text',
|
|
56
71
|
]);
|
|
@@ -84,7 +99,7 @@ function attrName(name) {
|
|
|
84
99
|
* accesses (e.g. `count()`, `store.value`) are wired reactively; bare
|
|
85
100
|
* identifiers and literals are passed through as-is.
|
|
86
101
|
*/
|
|
87
|
-
function isReactiveExpr(node) {
|
|
102
|
+
export function isReactiveExpr(node) {
|
|
88
103
|
if (t.isCallExpression(node) || t.isOptionalCallExpression(node))
|
|
89
104
|
return true;
|
|
90
105
|
if (t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) {
|
|
@@ -107,11 +122,21 @@ function isReactiveExpr(node) {
|
|
|
107
122
|
}
|
|
108
123
|
if (t.isTemplateLiteral(node))
|
|
109
124
|
return node.expressions.some((e) => isReactiveExpr(e));
|
|
125
|
+
// Object/array literals are reactive when any value inside is — so
|
|
126
|
+
// `class=${{ active: active() }}` / `style=${{ width: w() }}` react per update.
|
|
127
|
+
if (t.isObjectExpression(node)) {
|
|
128
|
+
return node.properties.some((p) => t.isObjectProperty(p) && !p.computed && isReactiveExpr(p.value));
|
|
129
|
+
}
|
|
130
|
+
if (t.isArrayExpression(node)) {
|
|
131
|
+
return node.elements.some((e) => e != null && !t.isSpreadElement(e) && isReactiveExpr(e));
|
|
132
|
+
}
|
|
110
133
|
return false;
|
|
111
134
|
}
|
|
112
135
|
function buildAttr(attr, used) {
|
|
113
136
|
const name = attrName(attr.name);
|
|
114
137
|
const isEvent = EVENT_RE.test(name);
|
|
138
|
+
// on: passes through to the runtime as a native listener; a handler, never reactive
|
|
139
|
+
const isHandler = isEvent || name.startsWith('on:');
|
|
115
140
|
const kind = isEvent ? 'event' : 'attr';
|
|
116
141
|
const base = { name, kind };
|
|
117
142
|
if (isEvent) {
|
|
@@ -133,7 +158,7 @@ function buildAttr(attr, used) {
|
|
|
133
158
|
return base;
|
|
134
159
|
}
|
|
135
160
|
base.expr = genExpr(expr, used);
|
|
136
|
-
base.reactive =
|
|
161
|
+
base.reactive = isHandler ? false : isReactiveExpr(expr);
|
|
137
162
|
if (t.isJSXElement(expr) || t.isJSXFragment(expr))
|
|
138
163
|
base.jsxElement = true;
|
|
139
164
|
return base;
|
|
@@ -141,20 +166,54 @@ function buildAttr(attr, used) {
|
|
|
141
166
|
base.literal = true;
|
|
142
167
|
return base;
|
|
143
168
|
}
|
|
169
|
+
/** Attribute value as JS code (expr or string), or null when valueless. */
|
|
170
|
+
function attrExprCode(attr, used) {
|
|
171
|
+
const v = attr.value;
|
|
172
|
+
if (v == null)
|
|
173
|
+
return null;
|
|
174
|
+
if (t.isStringLiteral(v))
|
|
175
|
+
return JSON.stringify(v.value);
|
|
176
|
+
if (t.isJSXExpressionContainer(v) && !t.isJSXEmptyExpression(v.expression)) {
|
|
177
|
+
return genExpr(v.expression, used);
|
|
178
|
+
}
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
144
181
|
function buildProps(attrs, used) {
|
|
145
182
|
const props = [];
|
|
183
|
+
const usePairs = []; // Solid `use:name` directives → one `use` prop
|
|
146
184
|
for (const a of attrs) {
|
|
147
185
|
if (t.isJSXSpreadAttribute(a)) {
|
|
148
186
|
props.push({ name: '', kind: 'spread', expr: genExpr(a.argument, used) });
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
if (!t.isJSXAttribute(a))
|
|
190
|
+
continue;
|
|
191
|
+
const ns = t.isJSXNamespacedName(a.name) ? a.name.namespace.name : null;
|
|
192
|
+
// use:name / use:name={opts} -> [dir, () => opts] pair; dir is an in-scope fn
|
|
193
|
+
if (ns === 'use') {
|
|
194
|
+
const dir = a.name.name.name;
|
|
195
|
+
used.add(dir); // keep the import
|
|
196
|
+
const opts = attrExprCode(a, used);
|
|
197
|
+
// parens around the body so an object literal isn't read as a block
|
|
198
|
+
usePairs.push(opts != null ? `[${dir}, () => (${opts})]` : `[${dir}]`);
|
|
199
|
+
continue;
|
|
149
200
|
}
|
|
150
|
-
|
|
151
|
-
|
|
201
|
+
// oncapture:name -> native on:name with { capture: true }
|
|
202
|
+
if (ns === 'oncapture') {
|
|
203
|
+
const evt = a.name.name.name.toLowerCase();
|
|
204
|
+
const h = attrExprCode(a, used) ?? 'undefined';
|
|
205
|
+
props.push({ name: 'on:' + evt, kind: 'attr', expr: `[${h}, { capture: true }]` });
|
|
206
|
+
continue;
|
|
152
207
|
}
|
|
208
|
+
props.push(buildAttr(a, used));
|
|
153
209
|
}
|
|
210
|
+
// one `use` prop = array of [dir, accessor?] pairs
|
|
211
|
+
if (usePairs.length > 0)
|
|
212
|
+
props.push({ name: 'use', kind: 'attr', expr: `[${usePairs.join(', ')}]` });
|
|
154
213
|
return props;
|
|
155
214
|
}
|
|
156
215
|
/** Canonical JSX text cleaning — kept identical to plugin.ts cleanJSXText. */
|
|
157
|
-
function cleanJSXText(value) {
|
|
216
|
+
export function cleanJSXText(value) {
|
|
158
217
|
const lines = value.split(/\r\n|\n|\r/);
|
|
159
218
|
let lastNonEmpty = 0;
|
|
160
219
|
for (let i = 0; i < lines.length; i++) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{types as i,traverse as b,template as k}from"@babel/core";import h from"@babel/generator";var f={version:1,module:"@fluixi/dom",symbols:["createNativeElement","insert","spread","setAttribute","delegateEvents","createComponent","createMemo","untrack","Show","For","Index","Switch","Match","Dynamic","ErrorBoundary"]},B={version:2,module:"@fluixi/dom",symbols:[...f.symbols,"template","cloneTemplate","walk"]};var R={show:"Show",for:"For",index:"Index",switch:"Switch",match:"Match",dynamic:"Dynamic",errorBoundary:"ErrorBoundary",suspense:"Suspense"};function v(e){return/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(e)}function x(e){return v(e)?e:JSON.stringify(e)}function C(e){return e.expr!==void 0?e.reactive?`() => (${e.expr})`:`(${e.expr})`:JSON.stringify(e.literal??!0)}function I(e){return e.expr!==void 0?`(${e.expr})`:JSON.stringify(e.literal??!0)}function X(e,t,n){let r=e.filter(a=>a.kind==="spread"),o=e.filter(a=>a.kind!=="spread").map(a=>`${x(a.name)}: ${C(a)}`);t!=null&&o.push(`children: ${t}`);let p=`{ ${o.join(", ")} }`;return r.length>0?(n.add("mergeProps"),`mergeProps(${r.map(a=>a.expr).join(", ")}, ${p})`):p}function S(e,t){return e.length===1?m(e[0],t):`[${e.map(n=>m(n,t)).join(", ")}]`}function d(e,t,n,r){r.add("createMemo"),r.add("createComponent");let s=t.filter(c=>c.kind==="spread"),p=t.filter(c=>c.kind!=="spread").map(c=>`get ${x(c.name)}() { return ${I(c)}; }`);n.length>0&&p.push(`get children() { return ${S(n,r)}; }`);let a=`{ ${p.join(", ")} }`;return s.length>0&&(r.add("mergeProps"),a=`mergeProps(${s.map(c=>c.expr).join(", ")}, ${a})`),`createMemo(() => createComponent(${e}, ${a}))`}function m(e,t){switch(e.kind){case"text":return JSON.stringify(e.value);case"expr":return e.reactive?`() => (${e.code})`:`(${e.code})`;case"fragment":return e.children.length===0?"null":S(e.children,t);case"component":return d(e.name,e.props,e.children,t);case"control":{let n=R[e.control]??e.control;return t.add(n),d(n,e.props,e.children,t)}case"element":{t.add("createNativeElement");let n=JSON.stringify(e.tag),r="_el$",s=[],o=e.svg?`${n}, true`:n;if(s.push(`const ${r} = createNativeElement(${o});`),e.props.length>0){t.add("spread");let p=e.svg?", isSVG: true":"";s.push(`spread({ element: ${r}, props: ${X(e.props,null,t)}${p} });`)}for(let p of e.children){t.add("insert");let a=m(p,t),c=p.kind==="expr"&&p.reactive||p.kind==="component"||p.kind==="control";s.push(c?`insert(${r}, ${a}, null);`:`insert(${r}, ${a});`)}return s.push(`return ${r};`),`(() => { ${s.join(" ")} })()`}}}var E={name:"imperative",contract:f,emit(e,t){let n=new Set;return{code:m(e,n),imports:Array.from(n)}}};var P=h.default??h;function $(e){return P(e,{concise:!0}).code}function A(e,t){let n=i.cloneNode(e,!0),r=i.file(i.program([i.expressionStatement(n)])),s=!1;return b(r,{"JSXElement|JSXFragment"(o){let p=o.node,{code:a,imports:c}=E.emit(g(p,t),{});for(let J of c)t.add(J);let N=k.expression(a,{placeholderPattern:!1})();o.replaceWith(N),o.skip(),s=!0}}),s?r.program.body[0].expression:e}function u(e,t){return $(A(e,t))}var w=new Set(["svg","path","circle","rect","line","polygon","polyline","ellipse","g","defs","clipPath","text"]),T=/^on[A-Z]/,O=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function j(e){if(i.isJSXIdentifier(e)){let t=e.name[0]!==e.name[0].toLowerCase();return{tag:e.name,component:t}}return i.isJSXMemberExpression(e)?{tag:$(e),component:!0}:i.isJSXNamespacedName(e)?{tag:`${e.namespace.name}:${e.name.name}`,component:!1}:{tag:"div",component:!1}}function _(e){return i.isJSXIdentifier(e)?e.name==="class"?"className":e.name:i.isJSXNamespacedName(e)?`${e.namespace.name}:${e.name.name}`:"unknown"}function l(e){return i.isCallExpression(e)||i.isOptionalCallExpression(e)?!0:i.isMemberExpression(e)||i.isOptionalMemberExpression(e)?!(!e.computed&&i.isIdentifier(e.property,{name:"children"})):i.isConditionalExpression(e)?l(e.test)||l(e.consequent)||l(e.alternate):i.isLogicalExpression(e)||i.isBinaryExpression(e)?l(e.left)||l(e.right):i.isTemplateLiteral(e)?e.expressions.some(t=>l(t)):!1}function M(e,t){let n=_(e.name),r=T.test(n),o={name:n,kind:r?"event":"attr"};r&&(o.event={name:n.slice(2).toLowerCase(),delegated:O.has(n.slice(2).toLowerCase())});let p=e.value;if(p==null)return o.literal=!0,o;if(i.isStringLiteral(p))return o.literal=p.value,o;if(i.isJSXExpressionContainer(p)&&!i.isJSXEmptyExpression(p.expression)){let a=p.expression;return i.isStringLiteral(a)||i.isNumericLiteral(a)||i.isBooleanLiteral(a)?(o.literal=a.value,o):(o.expr=u(a,t),o.reactive=r?!1:l(a),(i.isJSXElement(a)||i.isJSXFragment(a))&&(o.jsxElement=!0),o)}return o.literal=!0,o}function L(e,t){let n=[];for(let r of e)i.isJSXSpreadAttribute(r)?n.push({name:"",kind:"spread",expr:u(r.argument,t)}):i.isJSXAttribute(r)&&n.push(M(r,t));return n}function F(e){let t=e.split(/\r\n|\n|\r/),n=0;for(let s=0;s<t.length;s++)/[^ \t]/.test(t[s])&&(n=s);let r="";for(let s=0;s<t.length;s++){let o=t[s].replace(/\t/g," ");s!==0&&(o=o.replace(/^ +/,"")),s!==t.length-1&&(o=o.replace(/ +$/,"")),o&&(s!==n&&(o+=" "),r+=o)}return r}function y(e,t){let n=[];for(let r of e)if(i.isJSXText(r)){let s=F(r.value);s&&n.push({kind:"text",value:s})}else if(i.isJSXExpressionContainer(r)){if(!i.isJSXEmptyExpression(r.expression)){let s=r.expression;n.push({kind:"expr",code:u(s,t),reactive:l(s)})}}else i.isJSXElement(r)||i.isJSXFragment(r)?n.push(g(r,t)):i.isJSXSpreadChild(r)&&n.push({kind:"expr",code:u(r.expression,t),reactive:!1});return n}function g(e,t=new Set){if(i.isJSXFragment(e))return{kind:"fragment",children:y(e.children,t)};let{tag:n,component:r}=j(e.openingElement.name),s=L(e.openingElement.attributes,t),o=y(e.children,t);return r?{kind:"component",name:n,props:s,children:o}:{kind:"element",tag:n,svg:w.has(n),props:s,children:o,static:!1}}export{g as buildIR};
|
|
1
|
+
import{types as c,traverse as V,template as G}from"@babel/core";import C from"@babel/generator";var x={version:1,module:"@fluixi/dom",symbols:["createNativeElement","insert","spread","setAttribute","delegateEvents","createComponent","createMemo","untrack","Show","For","Index","Switch","Match","Dynamic","ErrorBoundary"]},te={version:2,module:"@fluixi/dom",symbols:[...x.symbols,"template","cloneTemplate","walk"]};var X={show:"Show",for:"For",index:"Index",switch:"Switch",match:"Match",dynamic:"Dynamic",errorBoundary:"ErrorBoundary",suspense:"Suspense"};function j(n){return/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(n)}function b(n){return j(n)?n:JSON.stringify(n)}function L(n){return n.expr!==void 0?n.reactive?`() => (${n.expr})`:`(${n.expr})`:JSON.stringify(n.literal??!0)}function D(n){return n.expr!==void 0?`(${n.expr})`:JSON.stringify(n.literal??!0)}function O(n,e,t){let r=n.filter(l=>l.kind==="spread"),a=n.filter(l=>l.kind!=="spread").map(l=>`${b(l.name)}: ${L(l)}`);e!=null&&a.push(`children: ${e}`);let o=`{ ${a.join(", ")} }`;return r.length>0?(t.add("mergeProps"),`mergeProps(${r.map(l=>l.expr).join(", ")}, ${o})`):o}function y(n,e){return n.length===1?f(n[0],e):`[${n.map(t=>f(t,e)).join(", ")}]`}function N(n,e,t,r){r.add("createMemo"),r.add("createComponent");let i=e.filter(s=>s.kind==="spread"),o=e.filter(s=>s.kind!=="spread").map(s=>`get ${b(s.name)}() { return ${D(s)}; }`);t.length>0&&o.push(`get children() { return ${y(t,r)}; }`);let l=`{ ${o.join(", ")} }`;return i.length>0&&(r.add("mergeProps"),l=`mergeProps(${i.map(s=>s.expr).join(", ")}, ${l})`),`createMemo(() => createComponent(${n}, ${l}))`}function f(n,e){switch(n.kind){case"text":return JSON.stringify(n.value);case"expr":return n.reactive?`() => (${n.code})`:`(${n.code})`;case"fragment":return n.children.length===0?"null":y(n.children,e);case"component":return N(n.name,n.props,n.children,e);case"control":{let t=X[n.control]??n.control;return e.add(t),N(t,n.props,n.children,e)}case"element":{e.add("createNativeElement");let t=JSON.stringify(n.tag),r="_el$",i=[],a=n.svg?`${t}, true`:t;if(i.push(`const ${r} = createNativeElement(${a});`),n.props.length>0){e.add("spread");let o=n.svg?", isSVG: true":"";i.push(`spread({ element: ${r}, props: ${O(n.props,null,e)}${o} });`)}for(let o of n.children){e.add("insert");let l=f(o,e),s=o.kind==="expr"&&o.reactive||o.kind==="component"||o.kind==="control";i.push(s?`insert(${r}, ${l}, null);`:`insert(${r}, ${l});`)}return i.push(`return ${r};`),`(() => { ${i.join(" ")} })()`}}}var h={name:"imperative",contract:x,emit(n,e){let t=new Set;return{code:f(n,t),imports:Array.from(t)}}};import{types as g}from"@babel/core";import S from"@babel/generator";import{parseTemplate as B}from"@fluixi/template-parser";var F=S.default??S;function M(n){return F(n,{concise:!0}).code}var R=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function _(n){return n.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$\{/g,"\\${")}function I(n){return n.charAt(0).toUpperCase()+n.slice(1)}var k=class{constructor(e,t){this.holes=e;this.used=t}hole(e){return this.holes[e]??{node:g.identifier("undefined"),code:"undefined",reactive:!1}}emit(e){let{code:t,imports:r}=h.emit(e,{});for(let i of r)this.used.add(i);return t}lowerRoot(e){let t=this.lowerChildren(e);return t.length===1?t[0]:{kind:"fragment",children:t}}lowerChildren(e){let t=[];for(let r=0;r<e.length;r++){let i=e[r];if(i.kind==="Element"||i.kind==="Component"){let o=i.attributes.find(l=>l.kind==="IfDirective");if(o&&o.kind==="IfDirective"){let l=r+1;l<e.length&&this.isBlankText(e[l])&&l++;let s=e[l],p=s&&(s.kind==="Element"||s.kind==="Component")&&s.attributes.some(m=>m.kind==="ElseDirective");t.push(this.lowerIf(i,o.hole,p?s:null)),p&&(r=l);continue}if(i.attributes.some(l=>l.kind==="EachDirective")){t.push(this.lowerEach(i));continue}}let a=this.lowerNode(i);a&&t.push(a)}return t}isBlankText(e){return e.kind==="Text"&&!e.raw&&v(e.value)===""}lowerNode(e){switch(e.kind){case"Text":{if(e.raw)return{kind:"text",value:e.value};let t=v(e.value);return t?{kind:"text",value:t}:null}case"Comment":return null;case"Expression":{let t=this.hole(e.hole);return{kind:"expr",code:t.code,reactive:t.reactive}}case"Fragment":return{kind:"fragment",children:this.lowerChildren(e.children)};case"Element":return this.lowerElement(e);case"Component":return this.lowerComponent(e);default:return null}}lowerElement(e){let t=e.attributes.find(r=>r.kind==="Attribute"&&r.name==="is");if(e.tag==="component"&&t&&t.value&&t.value.kind==="hole"){let r=e.attributes.filter(a=>a!==t),i=this.lowerComponent({...e,kind:"Component",tag:"Dynamic",tagHole:null,attributes:r});return i.props.unshift({name:"component",kind:"attr",expr:`() => (${this.hole(t.value.hole).code})`}),i}return{kind:"element",tag:e.tag,svg:e.namespace==="svg",props:this.lowerAttributes(e.attributes),children:this.lowerChildren(e.children),static:!1}}lowerComponent(e){let t=e.tagHole!=null?this.hole(e.tagHole).code:e.tag,r=this.lowerAttributes(e.attributes),{slots:i,rest:a}=this.partitionSlots(e.children);for(let[o,l]of i){let s=l.length===1?l[0]:{kind:"fragment",children:l};r.push({name:o,kind:"attr",expr:this.emit(s),jsxElement:!0})}return{kind:"component",name:t,props:r,children:this.lowerChildren(a)}}partitionSlots(e){let t=new Map,r=[];for(let i of e){if(i.kind==="Element"||i.kind==="Component"){let a=i.attributes.find(o=>o.kind==="Attribute"&&o.name==="slot");if(a&&a.value&&a.value.kind==="static"){let o={...i,attributes:i.attributes.filter(p=>p!==a)},l=o.kind==="Element"?this.lowerElement(o):this.lowerComponent(o),s=t.get(a.value.value)??[];s.push(l),t.set(a.value.value,s);continue}}r.push(i)}return{slots:t,rest:r}}lowerIf(e,t,r){let i=this.hole(t),a=[{name:"when",kind:"attr",expr:i.code,reactive:i.reactive}];if(r){let l=this.stripAndLower(r,s=>s.kind==="ElseDirective");a.push({name:"fallback",kind:"attr",expr:this.emit(l),jsxElement:!0})}let o=this.stripAndLower(e,l=>l.kind==="IfDirective");return{kind:"component",name:"Show",props:a,children:[o]}}lowerEach(e){let t=e.attributes.find(s=>s.kind==="EachDirective");if(!t||t.kind!=="EachDirective")return this.lowerNode(e);let r=this.hole(t.hole),i=[{name:"each",kind:"attr",expr:r.code,reactive:r.reactive}];if(t.key){let s="static"in t.key?`(item) => item[${JSON.stringify(t.key.static)}]`:this.hole(t.key.hole).code;i.push({name:"by",kind:"attr",expr:s})}let a=this.itemArrow(e),o;if(a){let s=a.body,p={kind:"expr",code:d(s,this.used),reactive:u(s)},m=this.rebuildWithChildren(e,[p]);o=`(${a.params.map(T=>M(T)).join(", ")}) => (${this.emit(m)})`}else{let s=this.stripAndLower(e,p=>p.kind==="EachDirective");o=`() => (${this.emit(s)})`}return{kind:"component",name:"For",props:i,children:[{kind:"expr",code:o,reactive:!1}]}}itemArrow(e){let t=e.children.filter(i=>!this.isBlankText(i));if(t.length!==1||t[0].kind!=="Expression")return null;let r=this.hole(t[0].hole).node;return g.isArrowFunctionExpression(r)&&!g.isBlockStatement(r.body)?r:null}stripAndLower(e,t){let r={...e,attributes:e.attributes.filter(i=>!t(i))};return r.kind==="Element"?this.lowerElement(r):this.lowerComponent(r)}rebuildWithChildren(e,t){let r=this.lowerAttributes(e.attributes.filter(i=>i.kind!=="EachDirective"));return e.kind==="Element"?{kind:"element",tag:e.tag,svg:e.namespace==="svg",props:r,children:t,static:!1}:{kind:"component",name:e.tag,props:r,children:t}}lowerAttributes(e){let t=[],r=[],i=!1,a=[],o=!1,l=[];for(let s of e)switch(s.kind){case"IfDirective":case"EachDirective":case"ElseDirective":break;case"Attribute":t.push(this.plainAttr(s));break;case"PropertyBinding":t.push({name:s.name,kind:"prop",expr:this.hole(s.hole).code,reactive:this.hole(s.hole).reactive});break;case"EventBinding":t.push(this.eventProp(s));break;case"RefBinding":t.push({name:"ref",kind:"ref",expr:this.hole(s.hole).code,reactive:this.hole(s.hole).reactive});break;case"Spread":t.push({name:"",kind:"spread",expr:this.hole(s.hole).code});break;case"ClassDirective":{let p=this.hole(s.hole);r.push(`${JSON.stringify(s.name)}: ${p.code}`),i=i||p.reactive;break}case"StyleDirective":{let p=this.hole(s.hole);a.push(`${JSON.stringify(s.name)}: ${p.code}`),o=o||p.reactive;break}case"BindDirective":t.push(...this.bindProps(s.name,this.hole(s.hole).code));break;case"UseDirective":{let p=s.name??(s.hole!=null?this.hole(s.hole).code:null);if(!p)break;l.push(s.name!=null&&s.hole!=null?`[${p}, () => (${this.hole(s.hole).code})]`:`[${p}]`);break}}return r.length>0&&t.push({name:"classList",kind:"attr",expr:`{ ${r.join(", ")} }`,reactive:i}),a.length>0&&t.push({name:"style",kind:"attr",expr:`{ ${a.join(", ")} }`,reactive:o}),l.length>0&&t.push({name:"use",kind:"attr",expr:`[${l.join(", ")}]`}),t}eventProp(e){let t=e.name.toLowerCase(),r=this.hole(e.hole).code;if(!(e.syntax==="colon"||e.modifiers.length>0))return{name:"on"+I(e.name),kind:"event",event:{name:t,delegated:R.has(t)},expr:r};let a=this.wrapHandler(r,e.modifiers),o=this.eventOptions(e.modifiers),l=o?`[${a}, ${o}]`:a;return{name:"on:"+t,kind:"attr",expr:l}}wrapHandler(e,t){let r=t.includes("self")?"if (e.target !== e.currentTarget) return; ":"",i=[];return t.includes("prevent")&&i.push("e.preventDefault();"),t.includes("stop")&&i.push("e.stopPropagation();"),!r&&i.length===0?e:`(e) => { ${r}${i.join(" ")} return (${e})(e); }`}eventOptions(e){let t=[];return e.includes("capture")&&t.push("capture: true"),e.includes("once")&&t.push("once: true"),e.includes("passive")&&t.push("passive: true"),t.length?`{ ${t.join(", ")} }`:null}bindProps(e,t){let r=e==="checked",i=r?"change":"input",a=r?"checked":"value",o=`(${t})`;return[{name:e,kind:"attr",expr:`${o}[0]()`,reactive:!0},{name:"on"+I(i),kind:"event",event:{name:i,delegated:R.has(i)},expr:`(e) => ${o}[1](e.target.${a})`}]}plainAttr(e){let t=e.value,r=e.name;e.name==="class"?r=t&&t.kind==="hole"&&g.isObjectExpression(this.hole(t.hole).node)?"classList":"className":e.name==="html"&&(r="innerHTML");let a={name:r,kind:"attr"};if(t==null)return a.literal=!0,a;if(t.kind==="static")return a.literal=t.value,a;if(t.kind==="hole"){let s=this.hole(t.hole);return a.expr=s.code,a.reactive=s.reactive,a}let o=!1,l=t.parts.map(s=>{if("text"in s)return _(s.text);let p=this.hole(s.hole);return o=o||p.reactive,"${"+p.code+"}"}).join("");return a.expr="`"+l+"`",a.reactive=o,a}};function $(n,e,t={}){let r=n.quasi,i=r.expressions.map(l=>{let s=l;return{node:s,code:d(s,e),reactive:u(s)}}),a=H(r),{root:o}=B(a,{svg:t.svg});return new k(i,e).lowerRoot(o.children)}function H(n){let e=[];return n.quasis.forEach((t,r)=>{let i=t.value.cooked??t.value.raw;if(e.push({kind:"static",text:i,start:t.start??0}),r<n.expressions.length){let a=n.expressions[r];e.push({kind:"hole",index:r,start:a.start??0,end:a.end??0})}}),e}function w(n,e=new Set,t={}){return $(n,e,t)}var K=C.default??C;function P(n){return K(n,{concise:!0}).code}function U(n,e){let t=c.cloneNode(n,!0),r=c.file(c.program([c.expressionStatement(t)])),i=!1,a=(o,l,s)=>{for(let p of s)e.add(p);o.replaceWith(G.expression(l,{placeholderPattern:!1,plugins:["typescript"]})()),o.skip(),i=!0};return V(r,{"JSXElement|JSXFragment"(o){let l=o.node,{code:s,imports:p}=h.emit(E(l,e),{});a(o,s,p)},TaggedTemplateExpression(o){let l=o.node.tag;if(!c.isIdentifier(l)||l.name!=="html"&&l.name!=="svg")return;let s=w(o.node,e,{svg:l.name==="svg"}),{code:p,imports:m}=h.emit(s,{});a(o,p,m)}}),i?r.program.body[0].expression:n}function d(n,e){return P(U(n,e))}var W=new Set(["svg","path","circle","rect","line","polygon","polyline","ellipse","g","defs","clipPath","text"]),z=/^on[A-Z]/,Z=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function q(n){if(c.isJSXIdentifier(n)){let e=n.name[0]!==n.name[0].toLowerCase();return{tag:n.name,component:e}}return c.isJSXMemberExpression(n)?{tag:P(n),component:!0}:c.isJSXNamespacedName(n)?{tag:`${n.namespace.name}:${n.name.name}`,component:!1}:{tag:"div",component:!1}}function Q(n){return c.isJSXIdentifier(n)?n.name==="class"?"className":n.name:c.isJSXNamespacedName(n)?`${n.namespace.name}:${n.name.name}`:"unknown"}function u(n){return c.isCallExpression(n)||c.isOptionalCallExpression(n)?!0:c.isMemberExpression(n)||c.isOptionalMemberExpression(n)?!(!n.computed&&c.isIdentifier(n.property,{name:"children"})):c.isConditionalExpression(n)?u(n.test)||u(n.consequent)||u(n.alternate):c.isLogicalExpression(n)||c.isBinaryExpression(n)?u(n.left)||u(n.right):c.isTemplateLiteral(n)?n.expressions.some(e=>u(e)):c.isObjectExpression(n)?n.properties.some(e=>c.isObjectProperty(e)&&!e.computed&&u(e.value)):c.isArrayExpression(n)?n.elements.some(e=>e!=null&&!c.isSpreadElement(e)&&u(e)):!1}function Y(n,e){let t=Q(n.name),r=z.test(t),i=r||t.startsWith("on:"),o={name:t,kind:r?"event":"attr"};r&&(o.event={name:t.slice(2).toLowerCase(),delegated:Z.has(t.slice(2).toLowerCase())});let l=n.value;if(l==null)return o.literal=!0,o;if(c.isStringLiteral(l))return o.literal=l.value,o;if(c.isJSXExpressionContainer(l)&&!c.isJSXEmptyExpression(l.expression)){let s=l.expression;return c.isStringLiteral(s)||c.isNumericLiteral(s)||c.isBooleanLiteral(s)?(o.literal=s.value,o):(o.expr=d(s,e),o.reactive=i?!1:u(s),(c.isJSXElement(s)||c.isJSXFragment(s))&&(o.jsxElement=!0),o)}return o.literal=!0,o}function A(n,e){let t=n.value;return t==null?null:c.isStringLiteral(t)?JSON.stringify(t.value):c.isJSXExpressionContainer(t)&&!c.isJSXEmptyExpression(t.expression)?d(t.expression,e):null}function ee(n,e){let t=[],r=[];for(let i of n){if(c.isJSXSpreadAttribute(i)){t.push({name:"",kind:"spread",expr:d(i.argument,e)});continue}if(!c.isJSXAttribute(i))continue;let a=c.isJSXNamespacedName(i.name)?i.name.namespace.name:null;if(a==="use"){let o=i.name.name.name;e.add(o);let l=A(i,e);r.push(l!=null?`[${o}, () => (${l})]`:`[${o}]`);continue}if(a==="oncapture"){let o=i.name.name.name.toLowerCase(),l=A(i,e)??"undefined";t.push({name:"on:"+o,kind:"attr",expr:`[${l}, { capture: true }]`});continue}t.push(Y(i,e))}return r.length>0&&t.push({name:"use",kind:"attr",expr:`[${r.join(", ")}]`}),t}function v(n){let e=n.split(/\r\n|\n|\r/),t=0;for(let i=0;i<e.length;i++)/[^ \t]/.test(e[i])&&(t=i);let r="";for(let i=0;i<e.length;i++){let a=e[i].replace(/\t/g," ");i!==0&&(a=a.replace(/^ +/,"")),i!==e.length-1&&(a=a.replace(/ +$/,"")),a&&(i!==t&&(a+=" "),r+=a)}return r}function J(n,e){let t=[];for(let r of n)if(c.isJSXText(r)){let i=v(r.value);i&&t.push({kind:"text",value:i})}else if(c.isJSXExpressionContainer(r)){if(!c.isJSXEmptyExpression(r.expression)){let i=r.expression;t.push({kind:"expr",code:d(i,e),reactive:u(i)})}}else c.isJSXElement(r)||c.isJSXFragment(r)?t.push(E(r,e)):c.isJSXSpreadChild(r)&&t.push({kind:"expr",code:d(r.expression,e),reactive:!1});return t}function E(n,e=new Set){if(c.isJSXFragment(n))return{kind:"fragment",children:J(n.children,e)};let{tag:t,component:r}=q(n.openingElement.name),i=ee(n.openingElement.attributes,e),a=J(n.children,e);return r?{kind:"component",name:t,props:i,children:a}:{kind:"element",tag:t,svg:W.has(t),props:i,children:a,static:!1}}export{W as SVG_TAGS,E as buildIR,v as cleanJSXText,d as genExpr,u as isReactiveExpr};
|