@fluixi/compiler 1.0.0-alpha.74 → 1.0.0-alpha.76
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/analyze/intrinsics.cjs +1 -0
- package/dist/analyze/intrinsics.d.ts +67 -0
- package/dist/analyze/intrinsics.d.ts.map +1 -0
- package/dist/analyze/intrinsics.js +91 -0
- package/dist/analyze/intrinsics.mjs +1 -0
- package/dist/analyze/props-destructure.cjs +1 -0
- package/dist/analyze/props-destructure.d.ts +84 -0
- package/dist/analyze/props-destructure.d.ts.map +1 -0
- package/dist/analyze/props-destructure.js +272 -0
- package/dist/analyze/props-destructure.mjs +1 -0
- package/dist/analyze/reactive-bindings.cjs +1 -0
- package/dist/analyze/reactive-bindings.d.ts +34 -0
- package/dist/analyze/reactive-bindings.d.ts.map +1 -0
- package/dist/analyze/reactive-bindings.js +153 -0
- package/dist/analyze/reactive-bindings.mjs +1 -0
- package/dist/{babel-GXO3KAVI.mjs → babel-YQ42WUS3.mjs} +1 -1
- package/dist/chunk-545LMC5W.mjs +1 -0
- package/dist/{chunk-TIE4XNAM.mjs → chunk-L2QPV25P.mjs} +1 -1
- package/dist/frontend/babel/build-ir-lit.cjs +1 -1
- package/dist/frontend/babel/build-ir-lit.mjs +1 -1
- package/dist/frontend/babel/build-ir.cjs +1 -1
- package/dist/frontend/babel/build-ir.mjs +1 -1
- package/dist/frontend/babel/index.cjs +1 -1
- package/dist/frontend/babel/index.mjs +1 -1
- package/dist/frontend/babel/lower-template.cjs +1 -1
- package/dist/frontend/babel/lower-template.mjs +1 -1
- package/dist/frontend/babel/plugin.cjs +1 -1
- package/dist/frontend/babel/plugin.mjs +1 -1
- package/dist/index.cjs +8 -8
- package/dist/index.mjs +9 -9
- package/dist/integrations.cjs +18 -14
- package/dist/integrations.d.ts +14 -0
- package/dist/integrations.d.ts.map +1 -1
- package/dist/integrations.js +16 -1
- package/dist/integrations.mjs +11 -11
- package/dist/lower/compile-template.cjs +1 -1
- package/dist/lower/compile-template.mjs +1 -1
- package/dist/lower/template.cjs +1 -1
- package/dist/lower/template.d.ts.map +1 -1
- package/dist/lower/template.js +7 -5
- package/dist/lower/template.mjs +1 -1
- package/dist/resolve/component-globals.cjs +9 -9
- package/dist/resolve/component-globals.d.ts +8 -0
- package/dist/resolve/component-globals.d.ts.map +1 -1
- package/dist/resolve/component-globals.js +17 -3
- package/dist/resolve/component-globals.mjs +9 -9
- package/dist/resolve/write-globals.cjs +9 -9
- package/dist/resolve/write-globals.mjs +9 -9
- package/dist/transform/index.cjs +5 -1
- package/dist/transform/index.d.ts +4 -0
- package/dist/transform/index.d.ts.map +1 -1
- package/dist/transform/index.js +2 -0
- package/dist/transform/index.mjs +14 -10
- package/dist/transform/intrinsics.cjs +3 -0
- package/dist/transform/intrinsics.d.ts +22 -0
- package/dist/transform/intrinsics.d.ts.map +1 -0
- package/dist/transform/intrinsics.js +78 -0
- package/dist/transform/intrinsics.mjs +3 -0
- package/dist/transform/props.cjs +3 -0
- package/dist/transform/props.d.ts +46 -0
- package/dist/transform/props.d.ts.map +1 -0
- package/dist/transform/props.js +221 -0
- package/dist/transform/props.mjs +12 -0
- package/dist/transform/templates.cjs +4 -1
- package/dist/transform/templates.d.ts +20 -0
- package/dist/transform/templates.d.ts.map +1 -1
- package/dist/transform/templates.js +41 -6
- package/dist/transform/templates.mjs +13 -10
- package/dist/transform-ZWIHEZVN.mjs +7 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/package.json +8 -7
- package/dist/transform-PJX2GNSV.mjs +0 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var E=Object.defineProperty;var le=Object.getOwnPropertyDescriptor;var ae=Object.getOwnPropertyNames;var ce=Object.prototype.hasOwnProperty;var pe=(t,e)=>{for(var n in e)E(t,n,{get:e[n],enumerable:!0})},de=(t,e,n,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of ae(e))!ce.call(t,r)&&r!==n&&E(t,r,{get:()=>e[r],enumerable:!(o=le(e,r))||o.enumerable});return t};var ue=t=>de(E({},"__esModule",{value:!0}),t);var Be={};pe(Be,{compilePieces:()=>se,compileTemplate:()=>_e});module.exports=ue(Be);function me(t){switch(t.kind){case"event":return"event";case"ref":return"ref";case"spread":return"spread";case"class":case"style":return"class-or-style-directive";case"attr":case"prop":return t.reactive?"reactive-prop":t.expr!==void 0?"expression-prop":void 0;default:return"expression-prop"}}function L(t){let e=[],n=new Map,o=r=>{switch(r.kind){case"text":return!0;case"expr":return!1;case"component":case"control":for(let i of r.children)o(i);return!1;case"fragment":for(let i of r.children)o(i);return!1;case"element":{let i=!0;for(let s of r.children)o(s)||(i=!1);let a;for(let s of r.props)if(a=me(s),a)break;return!a&&!i&&(a=fe(r)),r.static=!a,a?n.set(r,a):e.push(r),r.static}default:return!1}};for(let r of Array.isArray(t)?t:[t])o(r);return{staticElements:e,reasons:n}}function fe(t){for(let e of t.children){if(e.kind==="expr")return"expression-child";if(e.kind==="component")return"component-child";if(e.kind==="control")return"control-child";if(e.kind==="element"&&!e.static||e.kind==="fragment")return"expression-child"}return"expression-child"}var k={kind:"eager"},he=["pointerdown","focusin","keydown"],j=new Set(["eager","idle","visible","interaction","media","never"]),f=class extends Error{};function _(t,e){let n=t.slice(5);if(!j.has(n))throw new f(`Unknown load strategy 'load:${n}'. Expected one of ${[...j].join(", ")}.`);switch(n){case"eager":return k;case"idle":return{kind:"idle"};case"never":return{kind:"never"};case"visible":return e?{kind:"visible",rootMargin:e}:{kind:"visible"};case"interaction":return{kind:"interaction",events:e?ge(e):[...he]};case"media":if(!e)throw new f(`'load:media' needs a query, e.g. load:media="(min-width: 1024px)".`);return{kind:"media",query:e};default:throw new f(`Unhandled load strategy '${n}'.`)}}function ge(t){let e=t.split(/[\s,]+/).map(n=>n.trim()).filter(Boolean);if(e.length===0)throw new f("'load:interaction' was given no event names.");return e}function B(t){return t.kind!=="eager"&&t.kind!=="never"}var w=["Show","For","Index","Switch","Match","Portal","Dynamic","ErrorBoundary"],$=["Suspense","SuspenseList","Await"],x=["Router","Outlet","Redirect","Link"],Je=[...w,...$,...x],T=new Set([...w,...$]);function H(t={}){let{controlFlowModule:e="@fluixi/dom",coreModule:n="@fluixi/core",routerModule:o="@fluixi/core/router-next"}=t,r={};for(let i of w)r[i]=e;for(let i of $)r[i]=n;for(let i of x)r[i]=o;return r}function F(t,e={}){let{resolver:n,isBound:o,modules:r,strategyFor:i}=e,a=H(r),s=new Map,l=new Set,c=p=>{let{name:u}=p;if(!u||u.includes(".")||o?.(u))return;let m=d(u);if(!m){l.add(u);return}let v=m.origin==="builtin"&&T.has(u)?k:i?.(p)??k;p.source={...m,loading:v},s.set(u,p.source)},d=p=>{if(T.has(p))return{module:a[p],export:p,origin:"builtin"};let u=n?.resolve(p);if(u)return{module:u.module,export:u.export,origin:"rule"};if(x.includes(p))return{module:a[p],export:p,origin:"builtin"}};return N(t,p=>{p.kind==="component"&&c(p)}),{resolved:s,unresolved:[...l]}}function N(t,e){let n=Array.isArray(t)?t:[t];for(let o of n){e(o);let r=o.children;r&&N(r,e)}}function z(t){let e=[];N(t,i=>{i.kind==="component"&&i.source&&e.push(i)});let n=new Set,o=new Set;for(let i of e){let{module:a,loading:s}=i.source;s.kind==="eager"&&n.add(a),s.kind==="never"&&o.add(a)}let r=new Map;for(let i of e){let a=i.source;B(a.loading)&&n.has(a.module)&&(a.loading={kind:"eager"},r.set(a.module,(r.get(a.module)??0)+1))}return{collapsed:r,conflicted:[...o].filter(i=>n.has(i))}}var Re=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]),J={className:"class",htmlFor:"for"},ve=new Set(["innerHTML","outerHTML","innerText","textContent","value","checked","selected","muted","defaultValue","defaultChecked","children","ref"]),ke=new Set(["script","style","textarea","title"]);function S(t){return ke.has(t)}function U(t){return J[t]??t}function h(t){return t.kind!=="attr"||t.expr!==void 0||t.name.includes(":")?!1:!ve.has(t.name)}function P(t){if(!t.static||S(t.tag))return!1;for(let e of t.props)if(!h(e))return!1;for(let e of t.children)if(e.kind!=="text"&&!(e.kind==="element"&&P(e)))return!1;return!0}function g(t){let e=t.props.map(Ne).filter(Boolean).join(""),n=`<${t.tag}${e}>`;return Re.has(t.tag)?n:`${n}${t.children.map(xe).join("")}</${t.tag}>`}function xe(t){if(t.kind==="text")return ye(t.value);if(t.kind!=="element")throw new Error(`serializeStatic: unexpected ${t.kind}`);return g(t)}function Ne(t){let e=J[t.name]??t.name,n=t.literal;return n===!0||n===void 0?` ${e}`:n===!1||n===null?"":` ${e}="${Ie(String(n))}"`}function Ie(t){return t.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function ye(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}var be="<!--fx-->",Ce="<!--fx/-->";function y(t){return t.kind==="expr"||t.kind==="component"||t.kind==="control"}function Ee(t){return t.kind==="text"||t.kind==="element"&&V(t)}function V(t){return t.svg||S(t.tag)||!t.props.every(e=>h(e))?!1:t.children.every(e=>Ee(e)||y(e))}function I(t){if(y(t))return!0;let e=t.children;return e?e.some(I):!1}function W(t){for(let e=0;e<t.children.length;e++){let n=t.children[e];if(n.kind==="text"&&(n.value===""||t.children[e+1]?.kind==="text"))return!1}return t.children.every(e=>e.kind!=="element"||W(e))}function q(t){if(!V(t)||!W(t)||!t.children.some(I))return null;let e=[],n=[],o=0,r=a=>{let s=`_n$${o++}`;return e.push({ref:s,expr:a}),s};return i(t,"_el$"),{html:we(t),tag:t.tag,steps:e,holes:n};function i(a,s){let l=-1;a.children.forEach((d,p)=>{I(d)&&(l=p)});let c=null;for(let d=0;d<=l;d++){let p=a.children[d],u=c?`${c}.nextSibling`:`${s}.firstChild`;if(y(p)){let v=r(u),D=r(`holeEnd(${v})`);n.push({parentRef:s,startRef:v,endRef:D,node:p}),c=D;continue}let m=r(u);p.kind==="element"&&I(p)&&i(p,m),c=m}}}function we(t){return g(G(t)).split(K).join(be+Ce)}function G(t){return{...t,children:t.children.map(e=>y(e)?{kind:"text",value:K}:e.kind==="element"?G(e):e)}}var K="\0fx-hole\0";var M={version:1,module:"@fluixi/dom",symbols:["createNativeElement","insert","spread","setAttribute","delegateEvents","createComponent","createMemo","untrack","Show","For","Index","Switch","Match","Dynamic","ErrorBoundary"]},et={version:2,module:"@fluixi/dom",symbols:[...M.symbols,"template","cloneTemplate","walk"]};var $e={show:"Show",for:"For",index:"Index",switch:"Switch",match:"Match",dynamic:"Dynamic",errorBoundary:"ErrorBoundary",suspense:"Suspense"};function Te(t){return/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(t)}function Z(t){return Te(t)?t:JSON.stringify(t)}function Se(t){return t.expr!==void 0?t.reactive?`() => (${t.expr})`:`(${t.expr})`:JSON.stringify(t.literal??!0)}function Pe(t){return t.expr!==void 0?`(${t.expr})`:JSON.stringify(t.literal??!0)}function Me(t,e,n){let o=t.filter(s=>s.kind==="spread"),i=t.filter(s=>s.kind!=="spread").map(s=>`${Z(s.name)}: ${Se(s)}`);e!=null&&i.push(`children: ${e}`);let a=`{ ${i.join(", ")} }`;return o.length>0?(n.add("mergeProps"),`mergeProps(${o.map(s=>s.expr).join(", ")}, ${a})`):a}function X(t,e,n){return t.length===1?R(t[0],e,n):`[${t.map(o=>R(o,e,n)).join(", ")}]`}function Y(t,e,n,o,r){o.add("createMemo"),o.add("createComponent");let i=e.filter(c=>c.kind==="spread"),s=e.filter(c=>c.kind!=="spread").map(c=>`get ${Z(c.name)}() { return ${Pe(c)}; }`);n.length>0&&s.push(`get children() { return ${X(n,o,r)}; }`);let l=`{ ${s.join(", ")} }`;return i.length>0&&(o.add("mergeProps"),l=`mergeProps(${i.map(c=>c.expr).join(", ")}, ${l})`),`createMemo(() => createComponent(${t}, ${l}))`}function Oe(t,e){let n=U(e.name),o=e.literal;return o===!0||o===void 0?`${t}.setAttribute(${JSON.stringify(n)}, "");`:o===!1||o===null?"":`${t}.setAttribute(${JSON.stringify(n)}, ${JSON.stringify(String(o))});`}var Q=!1;function R(t,e,n){switch(t.kind){case"text":return JSON.stringify(t.value);case"expr":return t.reactive?`() => (${t.code})`:`(${t.code})`;case"fragment":return t.children.length===0?"null":X(t.children,e,n);case"component":return Y(t.name,t.props,t.children,e,n);case"control":{let o=$e[t.control]??t.control;return e.add(o),Y(o,t.props,t.children,e,n)}case"element":{if(n&&t.static&&!t.svg&&P(t)){e.add("templateNode");let s=`_tmpl$${n.length}`;return n.push({id:s,html:g(t),tag:t.tag,svg:!1}),`templateNode(${s}, ${JSON.stringify(t.tag)})`}if(n&&Q){let s=q(t);if(s){e.add("templateNode"),e.add("insert"),e.add("holeEnd"),e.add("holeContent"),e.add("holeScope");let l=`_tmpl$${n.length}`;n.push({id:l,html:s.html,tag:s.tag,svg:!1});let c=[`const _el$ = templateNode(${l}, ${JSON.stringify(s.tag)}, true);`];for(let d of s.steps)c.push(`const ${d.ref} = ${d.expr};`);for(let d of s.holes)c.push(`insert(${d.parentRef}, holeScope(${d.startRef}, () => (${R(d.node,e,n)})), ${d.endRef}, holeContent(${d.startRef}, ${d.endRef}));`);return c.push("return _el$;"),`(() => { ${c.join(" ")} })()`}}e.add("createNativeElement");let o=JSON.stringify(t.tag),r="_el$",i=[],a=t.svg?`${o}, true`:o;if(i.push(`const ${r} = createNativeElement(${a});`),t.props.length>0)if(!t.svg&&t.props.every(h))for(let s of t.props)i.push(Oe(r,s));else{e.add("spread");let s=t.svg?", isSVG: true":"";i.push(`spread({ element: ${r}, props: ${Me(t.props,null,e)}${s} });`)}for(let s of t.children){e.add("insert");let l=R(s,e,n),c=s.kind==="expr"&&s.reactive||s.kind==="component"||s.kind==="control";i.push(c?`insert(${r}, ${l}, null);`:`insert(${r}, ${l});`)}return i.push(`return ${r};`),`(() => { ${i.join(" ")} })()`}}}function b(t,e){if(!e||e.length===0)return t;let n=new Map(e.map(o=>[o.id,JSON.stringify(o.html)]));return t.replace(/_tmpl\$\d+/g,o=>n.get(o)??o)}var C={name:"imperative",contract:M,emit(t,e){let n=new Set,o=e?.templateClone!==!1?[]:void 0;return Q=e?.partialTemplates===!0,{code:R(t,n,o),imports:Array.from(n),templates:o}}};var Ae=t=>"components"in t;function ee(t,e){return t.replace(/\$(\d+)/g,(n,o)=>e[Number(o)]??n)}function De(t,e){return typeof e=="string"?{module:e,export:t}:e}function te(t=[]){let e=new Map,n=[],o=[];for(let a of t){if(!Ae(a)){o.push(a);continue}for(let[s,l]of Object.entries(a.components)){let c=De(s,l),d=e.get(s);if(d&&d.module!==c.module){let p=n.find(u=>u.name===s);p?p.modules.push(c.module):n.push({name:s,modules:[d.module,c.module]});continue}e.set(s,c)}}let r=new Map;return{resolve:a=>{if(r.has(a))return r.get(a);let s=e.get(a);if(!s)for(let l of o){let c=a.match(new RegExp(l.match.source,l.match.flags.replace("g","")));if(c){s={module:ee(l.module,c),export:l.export?ee(l.export,c):a};break}}return r.set(a,s),s},names:()=>[...e.keys()],conflicts:()=>n}}var oe=require("@fluixi/template-parser");function O(t){let e=t.split(/\r\n|\n|\r/),n=0;for(let r=0;r<e.length;r++)/[^ \t]/.test(e[r])&&(n=r);let o="";for(let r=0;r<e.length;r++){let i=e[r].replace(/\t/g," ");r!==0&&(i=i.replace(/^ +/,"")),r!==e.length-1&&(i=i.replace(/ +$/,"")),i&&(r!==n&&(i+=" "),o+=i)}return o}var ne=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function Le(t){return t.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$\{/g,"\\${")}function re(t){return t.charAt(0).toUpperCase()+t.slice(1)}var A=class{constructor(e,n){this.holes=e;this.used=n}hole(e){return this.holes[e]??{code:"undefined",reactive:!1}}emit(e){let{code:n,imports:o,templates:r}=C.emit(e,{});for(let i of o)this.used.add(i);return b(n,r)}lowerRoot(e){let n=this.lowerChildren(e);return n.length===1?n[0]:{kind:"fragment",children:n}}lowerChildren(e){let n=[];for(let o=0;o<e.length;o++){let r=e[o];if(r.kind==="Element"||r.kind==="Component"){let a=r.attributes.find(s=>s.kind==="IfDirective");if(a&&a.kind==="IfDirective"){let s=o+1;s<e.length&&this.isBlankText(e[s])&&s++;let l=e[s],c=l&&(l.kind==="Element"||l.kind==="Component")&&l.attributes.some(d=>d.kind==="ElseDirective");n.push(this.lowerIf(r,a.hole,c?l:null)),c&&(o=s);continue}if(r.attributes.some(s=>s.kind==="EachDirective")){n.push(this.lowerEach(r));continue}}let i=this.lowerNode(r);i&&n.push(i)}return n}isBlankText(e){return e.kind==="Text"&&!e.raw&&O(e.value)===""}lowerNode(e){switch(e.kind){case"Text":{if(e.raw)return{kind:"text",value:e.value};let n=O(e.value);return n?{kind:"text",value:n}:null}case"Comment":return null;case"Expression":{let n=this.hole(e.hole);return{kind:"expr",code:n.code,reactive:n.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 n=e.attributes.find(o=>o.kind==="Attribute"&&o.name==="is");if(e.tag==="component"&&n&&n.value&&n.value.kind==="hole"){let o=e.attributes.filter(i=>i!==n),r=this.lowerComponent({...e,kind:"Component",tag:"Dynamic",tagHole:null,attributes:o});return r.props.unshift({name:"component",kind:"attr",expr:`() => (${this.hole(n.value.hole).code})`}),r}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 n=e.tagHole!=null?this.hole(e.tagHole).code:e.tag,o=this.lowerAttributes(e.attributes),{slots:r,rest:i}=this.partitionSlots(e.children);for(let[l,c]of r){let d=c.length===1?c[0]:{kind:"fragment",children:c},p={name:l,kind:"attr",expr:this.emit(d),jsxElement:!0},u=o.findIndex(m=>m.name===l);u>=0?o[u]=p:o.push(p)}let a=e.attributes.find(l=>l.kind==="LoadDirective"),s=a?_(`load:${a.strategy}`,a.modifier):void 0;return{kind:"component",name:n,props:o,children:this.lowerChildren(i),...s?{load:s}:{}}}partitionSlots(e){let n=new Map,o=[];for(let r of e){if(r.kind==="Element"||r.kind==="Component"){let i=r.attributes.find(a=>a.kind==="Attribute"&&a.name==="slot");if(i&&i.value&&i.value.kind==="static"){let a={...r,attributes:r.attributes.filter(c=>c!==i)},s=a.kind==="Element"?this.lowerElement(a):this.lowerComponent(a),l=n.get(i.value.value)??[];l.push(s),n.set(i.value.value,l);continue}}o.push(r)}return{slots:n,rest:o}}lowerIf(e,n,o){let r=this.hole(n),i=[{name:"when",kind:"attr",expr:r.code,reactive:r.reactive}];if(o){let s=this.stripAndLower(o,l=>l.kind==="ElseDirective");i.push({name:"fallback",kind:"attr",expr:this.emit(s),jsxElement:!0})}let a=this.stripAndLower(e,s=>s.kind==="IfDirective");return{kind:"component",name:"Show",props:i,children:[a]}}lowerEach(e){let n=e.attributes.find(l=>l.kind==="EachDirective");if(!n||n.kind!=="EachDirective")return this.lowerNode(e);let o=this.hole(n.hole),r=[{name:"each",kind:"attr",expr:o.code,reactive:o.reactive}];if(n.key){let l="static"in n.key?`(item) => item[${JSON.stringify(n.key.static)}]`:this.hole(n.key.hole).code;r.push({name:"by",kind:"attr",expr:l})}let i=this.itemArrow(e),a;if(i){let l={kind:"expr",code:i.body,reactive:i.bodyReactive},c=this.rebuildWithChildren(e,[l]);a=`(${i.params.join(", ")}) => (${this.emit(c)})`}else{let l=this.stripAndLower(e,c=>c.kind==="EachDirective");a=`() => (${this.emit(l)})`}return{kind:"component",name:"For",props:r,children:[{kind:"expr",code:a,reactive:!1}]}}itemArrow(e){let n=e.children.filter(o=>!this.isBlankText(o));return n.length!==1||n[0].kind!=="Expression"?null:this.hole(n[0].hole).arrow??null}stripAndLower(e,n){let o={...e,attributes:e.attributes.filter(r=>!n(r))};return o.kind==="Element"?this.lowerElement(o):this.lowerComponent(o)}rebuildWithChildren(e,n){let o=this.lowerAttributes(e.attributes.filter(r=>r.kind!=="EachDirective"));return e.kind==="Element"?{kind:"element",tag:e.tag,svg:e.namespace==="svg",props:o,children:n,static:!1}:{kind:"component",name:e.tag,props:o,children:n}}lowerAttributes(e){let n=[],o=[],r=!1,i=[],a=!1,s=[];for(let l of e)switch(l.kind){case"IfDirective":case"EachDirective":case"ElseDirective":break;case"Attribute":n.push(this.plainAttr(l));break;case"PropertyBinding":n.push({name:l.name,kind:"prop",expr:this.hole(l.hole).code,reactive:this.hole(l.hole).reactive});break;case"EventBinding":n.push(this.eventProp(l));break;case"RefBinding":n.push({name:"ref",kind:"ref",expr:this.hole(l.hole).code,reactive:this.hole(l.hole).reactive});break;case"Spread":n.push({name:"",kind:"spread",expr:this.hole(l.hole).code});break;case"ClassDirective":{let c=this.hole(l.hole);o.push(`${JSON.stringify(l.name)}: ${c.code}`),r=r||c.reactive;break}case"StyleDirective":{let c=this.hole(l.hole);i.push(`${JSON.stringify(l.name)}: ${c.code}`),a=a||c.reactive;break}case"BindDirective":n.push(...this.bindProps(l.name,this.hole(l.hole).code));break;case"UseDirective":{let c=l.name??(l.hole!=null?this.hole(l.hole).code:null);if(!c)break;s.push(l.name!=null&&l.hole!=null?`[${c}, () => (${this.hole(l.hole).code})]`:`[${c}]`);break}}return o.length>0&&n.push({name:"classList",kind:"attr",expr:`{ ${o.join(", ")} }`,reactive:r}),i.length>0&&n.push({name:"style",kind:"attr",expr:`{ ${i.join(", ")} }`,reactive:a}),s.length>0&&n.push({name:"use",kind:"attr",expr:`[${s.join(", ")}]`}),n}eventProp(e){let n=e.name.toLowerCase(),o=this.hole(e.hole).code;if(!(e.syntax==="colon"||e.modifiers.length>0))return{name:"on"+re(e.name),kind:"event",event:{name:n,delegated:ne.has(n)},expr:o};let i=this.wrapHandler(o,e.modifiers),a=this.eventOptions(e.modifiers),s=a?`[${i}, ${a}]`:i;return{name:"on:"+n,kind:"attr",expr:s}}wrapHandler(e,n){let o=n.includes("self")?"if (e.target !== e.currentTarget) return; ":"",r=[];return n.includes("prevent")&&r.push("e.preventDefault();"),n.includes("stop")&&r.push("e.stopPropagation();"),!o&&r.length===0?e:`(e) => { ${o}${r.join(" ")} return (${e})(e); }`}eventOptions(e){let n=[];return e.includes("capture")&&n.push("capture: true"),e.includes("once")&&n.push("once: true"),e.includes("passive")&&n.push("passive: true"),n.length?`{ ${n.join(", ")} }`:null}bindProps(e,n){let o=e==="checked",r=o?"change":"input",i=o?"checked":"value",a=`(${n})`;return[{name:e,kind:"attr",expr:`${a}[0]()`,reactive:!0},{name:"on"+re(r),kind:"event",event:{name:r,delegated:ne.has(r)},expr:`(e) => ${a}[1](e.target.${i})`}]}plainAttr(e){let n=e.value,o=e.name;e.name==="class"?o=n&&n.kind==="hole"&&this.hole(n.hole).object?"classList":"className":e.name==="html"&&(o="innerHTML");let i={name:o,kind:"attr"};if(n==null)return i.literal=!0,i;if(n.kind==="static")return i.literal=n.value,i;if(n.kind==="hole"){let l=this.hole(n.hole);return i.expr=l.code,i.reactive=l.reactive,i}let a=!1,s=n.parts.map(l=>{if("text"in l)return Le(l.text);let c=this.hole(l.hole);return a=a||c.reactive,"${"+c.code+"}"}).join("");return i.expr="`"+s+"`",i.reactive=a,i}};function ie(t,e,n,o={}){let{root:r}=(0,oe.parseTemplate)(t,{svg:o.svg});return new A(e,n).lowerRoot(r.children)}function je(t){let e=[],n=0;return t.statics.forEach((o,r)=>{if(e.push({kind:"static",text:o,start:n}),n+=o.length,r<t.holes.length){let i=n+t.holes[r].code.length;e.push({kind:"hole",index:r,start:n,end:i}),n=i}}),e}function _e(t,e={}){if(t.statics.length!==t.holes.length+1)throw new Error(`[fluixi] compileTemplate: ${t.statics.length} static runs for ${t.holes.length} holes — a template always has exactly one more.`);return se(je(t),t.holes,e)}function se(t,e,n={}){let o=new Set,r=ie(t,[...e],o,{svg:n.svg});L(r),F(r,{resolver:te(n.resolve??[]),modules:{controlFlowModule:n.controlFlowModule??"@fluixi/dom",coreModule:n.coreModule??"@fluixi/core",routerModule:n.routerModule??"@fluixi/core/router-next"},strategyFor:l=>l.load}),z(r);let{code:i,imports:a,templates:s}=C.emit(r,{templateClone:n.templateClone,partialTemplates:n.partialTemplates});for(let l of a)o.add(l);return n.hoistTemplates?{code:i,imports:[...o],ir:r,templates:s}:{code:b(i,s),imports:[...o],ir:r}}
|
|
1
|
+
"use strict";var E=Object.defineProperty;var le=Object.getOwnPropertyDescriptor;var ae=Object.getOwnPropertyNames;var ce=Object.prototype.hasOwnProperty;var pe=(t,e)=>{for(var n in e)E(t,n,{get:e[n],enumerable:!0})},de=(t,e,n,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of ae(e))!ce.call(t,r)&&r!==n&&E(t,r,{get:()=>e[r],enumerable:!(o=le(e,r))||o.enumerable});return t};var ue=t=>de(E({},"__esModule",{value:!0}),t);var Be={};pe(Be,{compilePieces:()=>se,compileTemplate:()=>_e});module.exports=ue(Be);function me(t){switch(t.kind){case"event":return"event";case"ref":return"ref";case"spread":return"spread";case"class":case"style":return"class-or-style-directive";case"attr":case"prop":return t.reactive?"reactive-prop":t.expr!==void 0?"expression-prop":void 0;default:return"expression-prop"}}function L(t){let e=[],n=new Map,o=r=>{switch(r.kind){case"text":return!0;case"expr":return!1;case"component":case"control":for(let i of r.children)o(i);return!1;case"fragment":for(let i of r.children)o(i);return!1;case"element":{let i=!0;for(let s of r.children)o(s)||(i=!1);let a;for(let s of r.props)if(a=me(s),a)break;return!a&&!i&&(a=fe(r)),r.static=!a,a?n.set(r,a):e.push(r),r.static}default:return!1}};for(let r of Array.isArray(t)?t:[t])o(r);return{staticElements:e,reasons:n}}function fe(t){for(let e of t.children){if(e.kind==="expr")return"expression-child";if(e.kind==="component")return"component-child";if(e.kind==="control")return"control-child";if(e.kind==="element"&&!e.static||e.kind==="fragment")return"expression-child"}return"expression-child"}var k={kind:"eager"},he=["pointerdown","focusin","keydown"],j=new Set(["eager","idle","visible","interaction","media","never"]),f=class extends Error{};function _(t,e){let n=t.slice(5);if(!j.has(n))throw new f(`Unknown load strategy 'load:${n}'. Expected one of ${[...j].join(", ")}.`);switch(n){case"eager":return k;case"idle":return{kind:"idle"};case"never":return{kind:"never"};case"visible":return e?{kind:"visible",rootMargin:e}:{kind:"visible"};case"interaction":return{kind:"interaction",events:e?ge(e):[...he]};case"media":if(!e)throw new f(`'load:media' needs a query, e.g. load:media="(min-width: 1024px)".`);return{kind:"media",query:e};default:throw new f(`Unhandled load strategy '${n}'.`)}}function ge(t){let e=t.split(/[\s,]+/).map(n=>n.trim()).filter(Boolean);if(e.length===0)throw new f("'load:interaction' was given no event names.");return e}function B(t){return t.kind!=="eager"&&t.kind!=="never"}var w=["Show","For","Index","Switch","Match","Portal","Dynamic","ErrorBoundary"],$=["Suspense","SuspenseList","Await"],x=["Router","Outlet","Redirect","Link"],Je=[...w,...$,...x],T=new Set([...w,...$]);function H(t={}){let{controlFlowModule:e="@fluixi/dom",coreModule:n="@fluixi/core",routerModule:o="@fluixi/core/router-next"}=t,r={};for(let i of w)r[i]=e;for(let i of $)r[i]=n;for(let i of x)r[i]=o;return r}function F(t,e={}){let{resolver:n,isBound:o,modules:r,strategyFor:i}=e,a=H(r),s=new Map,l=new Set,c=p=>{let{name:u}=p;if(!u||u.includes(".")||o?.(u))return;let m=d(u);if(!m){l.add(u);return}let v=m.origin==="builtin"&&T.has(u)?k:i?.(p)??k;p.source={...m,loading:v},s.set(u,p.source)},d=p=>{if(T.has(p))return{module:a[p],export:p,origin:"builtin"};let u=n?.resolve(p);if(u)return{module:u.module,export:u.export,origin:"rule"};if(x.includes(p))return{module:a[p],export:p,origin:"builtin"}};return N(t,p=>{p.kind==="component"&&c(p)}),{resolved:s,unresolved:[...l]}}function N(t,e){let n=Array.isArray(t)?t:[t];for(let o of n){e(o);let r=o.children;r&&N(r,e)}}function z(t){let e=[];N(t,i=>{i.kind==="component"&&i.source&&e.push(i)});let n=new Set,o=new Set;for(let i of e){let{module:a,loading:s}=i.source;s.kind==="eager"&&n.add(a),s.kind==="never"&&o.add(a)}let r=new Map;for(let i of e){let a=i.source;B(a.loading)&&n.has(a.module)&&(a.loading={kind:"eager"},r.set(a.module,(r.get(a.module)??0)+1))}return{collapsed:r,conflicted:[...o].filter(i=>n.has(i))}}var Re=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]),J={className:"class",htmlFor:"for"},ve=new Set(["innerHTML","outerHTML","innerText","textContent","value","checked","selected","muted","defaultValue","defaultChecked","children","ref"]),ke=new Set(["script","style","textarea","title"]);function S(t){return ke.has(t)}function U(t){return J[t]??t}function h(t){return t.kind!=="attr"||t.expr!==void 0||t.name.includes(":")?!1:!ve.has(t.name)}function P(t){if(!t.static||S(t.tag))return!1;for(let e of t.props)if(!h(e))return!1;for(let e of t.children)if(e.kind!=="text"&&!(e.kind==="element"&&P(e)))return!1;return!0}function g(t){let e=t.props.map(Ne).filter(Boolean).join(""),n=`<${t.tag}${e}>`;return Re.has(t.tag)?n:`${n}${t.children.map(xe).join("")}</${t.tag}>`}function xe(t){if(t.kind==="text")return be(t.value);if(t.kind!=="element")throw new Error(`serializeStatic: unexpected ${t.kind}`);return g(t)}function Ne(t){let e=J[t.name]??t.name,n=t.literal;return n===!0||n===void 0?` ${e}`:n===!1||n===null?"":` ${e}="${Ie(String(n))}"`}function Ie(t){return t.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function be(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}var ye="<!--fx-->",Ce="<!--fx/-->";function b(t){return t.kind==="expr"||t.kind==="component"||t.kind==="control"}function Ee(t){return t.kind==="text"||t.kind==="element"&&V(t)}function V(t){return t.svg||S(t.tag)||!t.props.every(e=>h(e))?!1:t.children.every(e=>Ee(e)||b(e))}function I(t){if(b(t))return!0;let e=t.children;return e?e.some(I):!1}function W(t){for(let e=0;e<t.children.length;e++){let n=t.children[e];if(n.kind==="text"&&(n.value===""||t.children[e+1]?.kind==="text"))return!1}return t.children.every(e=>e.kind!=="element"||W(e))}function q(t){if(!V(t)||!W(t)||!t.children.some(I))return null;let e=[],n=[],o=0,r=a=>{let s=`_n$${o++}`;return e.push({ref:s,expr:a}),s};return i(t,"_el$"),{html:we(t),tag:t.tag,steps:e,holes:n};function i(a,s){let l=-1;a.children.forEach((d,p)=>{I(d)&&(l=p)});let c=null;for(let d=0;d<=l;d++){let p=a.children[d],u=c?`${c}.nextSibling`:`${s}.firstChild`;if(b(p)){let v=r(u),D=r(`holeEnd(${v})`);n.push({parentRef:s,startRef:v,endRef:D,node:p}),c=D;continue}let m=r(u);p.kind==="element"&&I(p)&&i(p,m),c=m}}}function we(t){return g(G(t)).split(K).join(ye+Ce)}function G(t){return{...t,children:t.children.map(e=>b(e)?{kind:"text",value:K}:e.kind==="element"?G(e):e)}}var K="\0fx-hole\0";var M={version:1,module:"@fluixi/dom",symbols:["createNativeElement","insert","spread","setAttribute","delegateEvents","createComponent","createMemo","untrack","Show","For","Index","Switch","Match","Dynamic","ErrorBoundary"]},et={version:2,module:"@fluixi/dom",symbols:[...M.symbols,"template","cloneTemplate","walk"]};var $e={show:"Show",for:"For",index:"Index",switch:"Switch",match:"Match",dynamic:"Dynamic",errorBoundary:"ErrorBoundary",suspense:"Suspense"};function Te(t){return/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(t)}function Z(t){return Te(t)?t:JSON.stringify(t)}function Se(t){return t.expr!==void 0?t.reactive?`() => (${t.expr})`:`(${t.expr})`:JSON.stringify(t.literal??!0)}function Pe(t){return t.expr!==void 0?`(${t.expr})`:JSON.stringify(t.literal??!0)}function Me(t,e,n){let o=t.filter(s=>s.kind==="spread"),i=t.filter(s=>s.kind!=="spread").map(s=>`${Z(s.name)}: ${Se(s)}`);e!=null&&i.push(`children: ${e}`);let a=`{ ${i.join(", ")} }`;return o.length>0?(n.add("mergeProps"),`mergeProps(${o.map(s=>s.expr).join(", ")}, ${a})`):a}function X(t,e,n){return t.length===1?R(t[0],e,n):`[${t.map(o=>R(o,e,n)).join(", ")}]`}function Y(t,e,n,o,r){o.add("createMemo"),o.add("createComponent");let i=e.filter(c=>c.kind==="spread"),s=e.filter(c=>c.kind!=="spread").map(c=>`get ${Z(c.name)}() { return ${Pe(c)}; }`);n.length>0&&s.push(`get children() { return ${X(n,o,r)}; }`);let l=`{ ${s.join(", ")} }`;return i.length>0&&(o.add("mergeProps"),l=`mergeProps(${i.map(c=>c.expr).join(", ")}, ${l})`),`createMemo(() => createComponent(${t}, ${l}))`}function Oe(t,e){let n=U(e.name),o=e.literal;return o===!0||o===void 0?`${t}.setAttribute(${JSON.stringify(n)}, "");`:o===!1||o===null?"":`${t}.setAttribute(${JSON.stringify(n)}, ${JSON.stringify(String(o))});`}var Q=!1;function R(t,e,n){switch(t.kind){case"text":return JSON.stringify(t.value);case"expr":return t.reactive?`() => (${t.code})`:`(${t.code})`;case"fragment":return t.children.length===0?"null":X(t.children,e,n);case"component":return Y(t.name,t.props,t.children,e,n);case"control":{let o=$e[t.control]??t.control;return e.add(o),Y(o,t.props,t.children,e,n)}case"element":{if(n&&t.static&&!t.svg&&P(t)){e.add("templateNode");let s=`_tmpl$${n.length}`;return n.push({id:s,html:g(t),tag:t.tag,svg:!1}),`templateNode(${s}, ${JSON.stringify(t.tag)})`}if(n&&Q){let s=q(t);if(s){e.add("templateNode"),e.add("insert"),e.add("holeEnd"),e.add("holeContent"),e.add("holeScope");let l=`_tmpl$${n.length}`;n.push({id:l,html:s.html,tag:s.tag,svg:!1});let c=[`const _el$ = templateNode(${l}, ${JSON.stringify(s.tag)}, true);`];for(let d of s.steps)c.push(`const ${d.ref} = ${d.expr};`);for(let d of s.holes)c.push(`insert(${d.parentRef}, holeScope(${d.startRef}, () => (${R(d.node,e,n)})), ${d.endRef}, holeContent(${d.startRef}, ${d.endRef}));`);return c.push("return _el$;"),`(() => { ${c.join(" ")} })()`}}e.add("createNativeElement");let o=JSON.stringify(t.tag),r="_el$",i=[],a=t.svg?`${o}, true`:o;if(i.push(`const ${r} = createNativeElement(${a});`),t.props.length>0)if(!t.svg&&t.props.every(h))for(let s of t.props)i.push(Oe(r,s));else{e.add("spread");let s=t.svg?", isSVG: true":"";i.push(`spread({ element: ${r}, props: ${Me(t.props,null,e)}${s} });`)}for(let s of t.children){e.add("insert");let l=R(s,e,n),c=s.kind==="expr"&&s.reactive||s.kind==="component"||s.kind==="control";i.push(c?`insert(${r}, ${l}, null);`:`insert(${r}, ${l});`)}return i.push(`return ${r};`),`(() => { ${i.join(" ")} })()`}}}function y(t,e){if(!e||e.length===0)return t;let n=new Map(e.map(o=>[o.id,JSON.stringify(o.html)]));return t.replace(/_tmpl\$\d+/g,o=>n.get(o)??o)}var C={name:"imperative",contract:M,emit(t,e){let n=new Set,o=e?.templateClone!==!1?[]:void 0;return Q=e?.partialTemplates===!0,{code:R(t,n,o),imports:Array.from(n),templates:o}}};var Ae=t=>"components"in t;function ee(t,e){return t.replace(/\$(\d+)/g,(n,o)=>e[Number(o)]??n)}function De(t,e){return typeof e=="string"?{module:e,export:t}:e}function te(t=[]){let e=new Map,n=[],o=[];for(let a of t){if(!Ae(a)){o.push(a);continue}for(let[s,l]of Object.entries(a.components)){let c=De(s,l),d=e.get(s);if(d&&d.module!==c.module){let p=n.find(u=>u.name===s);p?p.modules.push(c.module):n.push({name:s,modules:[d.module,c.module]});continue}e.set(s,c)}}let r=new Map;return{resolve:a=>{if(r.has(a))return r.get(a);let s=e.get(a);if(!s)for(let l of o){let c=a.match(new RegExp(l.match.source,l.match.flags.replace("g","")));if(c){s={module:ee(l.module,c),export:l.export?ee(l.export,c):a};break}}return r.set(a,s),s},names:()=>[...e.keys()],conflicts:()=>n}}var oe=require("@fluixi/template-parser");function O(t){let e=t.split(/\r\n|\n|\r/),n=0;for(let r=0;r<e.length;r++)/[^ \t]/.test(e[r])&&(n=r);let o="";for(let r=0;r<e.length;r++){let i=e[r].replace(/\t/g," ");r!==0&&(i=i.replace(/^ +/,"")),r!==e.length-1&&(i=i.replace(/ +$/,"")),i&&(r!==n&&(i+=" "),o+=i)}return o}var ne=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function Le(t){return t.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$\{/g,"\\${")}function re(t){return t.charAt(0).toUpperCase()+t.slice(1)}var A=class{constructor(e,n){this.holes=e;this.used=n}hole(e){return this.holes[e]??{code:"undefined",reactive:!1}}emit(e){let{code:n,imports:o,templates:r}=C.emit(e,{});for(let i of o)this.used.add(i);return y(n,r)}lowerRoot(e){let n=this.lowerChildren(e);return n.length===1?n[0]:{kind:"fragment",children:n}}lowerChildren(e){let n=[];for(let o=0;o<e.length;o++){let r=e[o];if(r.kind==="Element"||r.kind==="Component"){let a=r.attributes.find(s=>s.kind==="IfDirective");if(a&&a.kind==="IfDirective"){let s=o+1;s<e.length&&this.isBlankText(e[s])&&s++;let l=e[s],c=l&&(l.kind==="Element"||l.kind==="Component")&&l.attributes.some(d=>d.kind==="ElseDirective");n.push(this.lowerIf(r,a.hole,c?l:null)),c&&(o=s);continue}if(r.attributes.some(s=>s.kind==="EachDirective")){n.push(this.lowerEach(r));continue}}let i=this.lowerNode(r);i&&n.push(i)}return n}isBlankText(e){return e.kind==="Text"&&!e.raw&&O(e.value)===""}lowerNode(e){switch(e.kind){case"Text":{if(e.raw)return{kind:"text",value:e.value};let n=O(e.value);return n?{kind:"text",value:n}:null}case"Comment":return null;case"Expression":{let n=this.hole(e.hole);return{kind:"expr",code:n.code,reactive:n.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 n=e.attributes.find(o=>o.kind==="Attribute"&&o.name==="is");if(e.tag==="component"&&n&&n.value&&n.value.kind==="hole"){let o=e.attributes.filter(i=>i!==n),r=this.lowerComponent({...e,kind:"Component",tag:"Dynamic",tagHole:null,attributes:o});return r.props.unshift({name:"component",kind:"attr",expr:`() => (${this.hole(n.value.hole).code})`}),r}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 n=e.tagHole!=null?this.hole(e.tagHole).code:e.tag,o=this.lowerAttributes(e.attributes),{slots:r,rest:i}=this.partitionSlots(e.children);for(let[l,c]of r){let d=c.length===1?c[0]:{kind:"fragment",children:c},p={name:l,kind:"attr",expr:this.emit(d),jsxElement:!0},u=o.findIndex(m=>m.name===l);u>=0?o[u]=p:o.push(p)}let a=e.attributes.find(l=>l.kind==="LoadDirective"),s=a?_(`load:${a.strategy}`,a.modifier):void 0;return{kind:"component",name:n,props:o,children:this.lowerChildren(i),...s?{load:s}:{}}}partitionSlots(e){let n=new Map,o=[];for(let r of e){if(r.kind==="Element"||r.kind==="Component"){let i=r.attributes.find(a=>a.kind==="Attribute"&&a.name==="slot");if(i&&i.value&&i.value.kind==="static"){let a={...r,attributes:r.attributes.filter(c=>c!==i)},s=a.kind==="Element"?this.lowerElement(a):this.lowerComponent(a),l=n.get(i.value.value)??[];l.push(s),n.set(i.value.value,l);continue}}o.push(r)}return{slots:n,rest:o}}lowerIf(e,n,o){let r=this.hole(n),i=[{name:"when",kind:"attr",expr:r.code,reactive:r.reactive}];if(o){let s=this.stripAndLower(o,l=>l.kind==="ElseDirective");i.push({name:"fallback",kind:"attr",expr:this.emit(s),jsxElement:!0})}let a=this.stripAndLower(e,s=>s.kind==="IfDirective");return{kind:"component",name:"Show",props:i,children:[a]}}lowerEach(e){let n=e.attributes.find(l=>l.kind==="EachDirective");if(!n||n.kind!=="EachDirective")return this.lowerNode(e);let o=this.hole(n.hole),r=[{name:"each",kind:"attr",expr:o.code,reactive:o.reactive}];if(n.key){let l="static"in n.key?`(item) => item[${JSON.stringify(n.key.static)}]`:this.hole(n.key.hole).code;r.push({name:"by",kind:"attr",expr:l})}let i=this.itemArrow(e),a;if(i){let l={kind:"expr",code:i.body,reactive:i.bodyReactive},c=this.rebuildWithChildren(e,[l]);a=`(${i.params.join(", ")}) => (${this.emit(c)})`}else{let l=this.stripAndLower(e,c=>c.kind==="EachDirective");a=`() => (${this.emit(l)})`}return{kind:"component",name:"For",props:r,children:[{kind:"expr",code:a,reactive:!1}]}}itemArrow(e){let n=e.children.filter(o=>!this.isBlankText(o));return n.length!==1||n[0].kind!=="Expression"?null:this.hole(n[0].hole).arrow??null}stripAndLower(e,n){let o={...e,attributes:e.attributes.filter(r=>!n(r))};return o.kind==="Element"?this.lowerElement(o):this.lowerComponent(o)}rebuildWithChildren(e,n){let o=this.lowerAttributes(e.attributes.filter(r=>r.kind!=="EachDirective"));return e.kind==="Element"?{kind:"element",tag:e.tag,svg:e.namespace==="svg",props:o,children:n,static:!1}:{kind:"component",name:e.tag,props:o,children:n}}lowerAttributes(e){let n=[],o=[],r=!1,i=[],a=!1,s=[];for(let l of e)switch(l.kind){case"IfDirective":case"EachDirective":case"ElseDirective":break;case"Attribute":n.push(this.plainAttr(l));break;case"PropertyBinding":n.push({name:l.name,kind:"prop",expr:this.hole(l.hole).code,reactive:this.hole(l.hole).reactive});break;case"EventBinding":n.push(this.eventProp(l));break;case"RefBinding":n.push({name:"ref",kind:"ref",expr:this.hole(l.hole).code,reactive:this.hole(l.hole).reactive});break;case"Spread":n.push({name:"",kind:"spread",expr:this.hole(l.hole).code});break;case"ClassDirective":{let c=this.hole(l.hole);o.push(`${JSON.stringify(l.name)}: ${c.code}`),r=r||c.reactive;break}case"StyleDirective":{let c=this.hole(l.hole);i.push(`${JSON.stringify(l.name)}: ${c.code}`),a=a||c.reactive;break}case"BindDirective":n.push(...this.bindProps(l.name,this.hole(l.hole).code));break;case"UseDirective":{let c=l.name??(l.hole!=null?this.hole(l.hole).code:null);if(!c)break;s.push(l.name!=null&&l.hole!=null?`[${c}, () => (${this.hole(l.hole).code})]`:`[${c}]`);break}}return o.length>0&&n.push({name:"classList",kind:"attr",expr:`{ ${o.join(", ")} }`,reactive:r}),i.length>0&&n.push({name:"style",kind:"attr",expr:`{ ${i.join(", ")} }`,reactive:a}),s.length>0&&n.push({name:"use",kind:"attr",expr:`[${s.join(", ")}]`}),n}eventProp(e){let n=e.name.toLowerCase(),o=this.hole(e.hole).code;if(!(e.syntax==="colon"||e.modifiers.length>0))return{name:"on"+re(e.name),kind:"event",event:{name:n,delegated:ne.has(n)},expr:o};let i=this.wrapHandler(o,e.modifiers),a=this.eventOptions(e.modifiers),s=a?`[${i}, ${a}]`:i;return{name:"on:"+n,kind:"attr",expr:s}}wrapHandler(e,n){let o=n.includes("self")?"if (e.target !== e.currentTarget) return; ":"",r=[];return n.includes("prevent")&&r.push("e.preventDefault();"),n.includes("stop")&&r.push("e.stopPropagation();"),!o&&r.length===0?e:`(e) => { ${o}${r.join(" ")} return (${e})(e); }`}eventOptions(e){let n=[];return e.includes("capture")&&n.push("capture: true"),e.includes("once")&&n.push("once: true"),e.includes("passive")&&n.push("passive: true"),n.length?`{ ${n.join(", ")} }`:null}bindProps(e,n){let o=e==="checked",r=o?"change":"input",i=o?"checked":"value";this.used.add("bindPair");let a=`bindPair(${n})`;return[{name:e,kind:"attr",expr:`${a}[0]()`,reactive:!0},{name:"on"+re(r),kind:"event",event:{name:r,delegated:ne.has(r)},expr:`(e) => ${a}[1](e.target.${i})`}]}plainAttr(e){let n=e.value,o=e.name;e.name==="class"?o=n&&n.kind==="hole"&&this.hole(n.hole).object?"classList":"className":e.name==="html"&&(o="innerHTML");let i={name:o,kind:"attr"};if(n==null)return i.literal=!0,i;if(n.kind==="static")return i.literal=n.value,i;if(n.kind==="hole"){let l=this.hole(n.hole);return i.expr=l.code,i.reactive=l.reactive,i}let a=!1,s=n.parts.map(l=>{if("text"in l)return Le(l.text);let c=this.hole(l.hole);return a=a||c.reactive,"${"+c.code+"}"}).join("");return i.expr="`"+s+"`",i.reactive=a,i}};function ie(t,e,n,o={}){let{root:r}=(0,oe.parseTemplate)(t,{svg:o.svg});return new A(e,n).lowerRoot(r.children)}function je(t){let e=[],n=0;return t.statics.forEach((o,r)=>{if(e.push({kind:"static",text:o,start:n}),n+=o.length,r<t.holes.length){let i=n+t.holes[r].code.length;e.push({kind:"hole",index:r,start:n,end:i}),n=i}}),e}function _e(t,e={}){if(t.statics.length!==t.holes.length+1)throw new Error(`[fluixi] compileTemplate: ${t.statics.length} static runs for ${t.holes.length} holes — a template always has exactly one more.`);return se(je(t),t.holes,e)}function se(t,e,n={}){let o=new Set,r=ie(t,[...e],o,{svg:n.svg});L(r),F(r,{resolver:te(n.resolve??[]),modules:{controlFlowModule:n.controlFlowModule??"@fluixi/dom",coreModule:n.coreModule??"@fluixi/core",routerModule:n.routerModule??"@fluixi/core/router-next"},strategyFor:l=>l.load}),z(r);let{code:i,imports:a,templates:s}=C.emit(r,{templateClone:n.templateClone,partialTemplates:n.partialTemplates});for(let l of a)o.add(l);return n.hoistTemplates?{code:i,imports:[...o],ir:r,templates:s}:{code:y(i,s),imports:[...o],ir:r}}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
function oe(t){switch(t.kind){case"event":return"event";case"ref":return"ref";case"spread":return"spread";case"class":case"style":return"class-or-style-directive";case"attr":case"prop":return t.reactive?"reactive-prop":t.expr!==void 0?"expression-prop":void 0;default:return"expression-prop"}}function D(t){let e=[],n=new Map,r=o=>{switch(o.kind){case"text":return!0;case"expr":return!1;case"component":case"control":for(let i of o.children)r(i);return!1;case"fragment":for(let i of o.children)r(i);return!1;case"element":{let i=!0;for(let s of o.children)r(s)||(i=!1);let a;for(let s of o.props)if(a=oe(s),a)break;return!a&&!i&&(a=ie(o)),o.static=!a,a?n.set(o,a):e.push(o),o.static}default:return!1}};for(let o of Array.isArray(t)?t:[t])r(o);return{staticElements:e,reasons:n}}function ie(t){for(let e of t.children){if(e.kind==="expr")return"expression-child";if(e.kind==="component")return"component-child";if(e.kind==="control")return"control-child";if(e.kind==="element"&&!e.static||e.kind==="fragment")return"expression-child"}return"expression-child"}var k={kind:"eager"},se=["pointerdown","focusin","keydown"],L=new Set(["eager","idle","visible","interaction","media","never"]),f=class extends Error{};function j(t,e){let n=t.slice(5);if(!L.has(n))throw new f(`Unknown load strategy 'load:${n}'. Expected one of ${[...L].join(", ")}.`);switch(n){case"eager":return k;case"idle":return{kind:"idle"};case"never":return{kind:"never"};case"visible":return e?{kind:"visible",rootMargin:e}:{kind:"visible"};case"interaction":return{kind:"interaction",events:e?le(e):[...se]};case"media":if(!e)throw new f(`'load:media' needs a query, e.g. load:media="(min-width: 1024px)".`);return{kind:"media",query:e};default:throw new f(`Unhandled load strategy '${n}'.`)}}function le(t){let e=t.split(/[\s,]+/).map(n=>n.trim()).filter(Boolean);if(e.length===0)throw new f("'load:interaction' was given no event names.");return e}function _(t){return t.kind!=="eager"&&t.kind!=="never"}var E=["Show","For","Index","Switch","Match","Portal","Dynamic","ErrorBoundary"],w=["Suspense","SuspenseList","Await"],x=["Router","Outlet","Redirect","Link"],Oe=[...E,...w,...x],$=new Set([...E,...w]);function B(t={}){let{controlFlowModule:e="@fluixi/dom",coreModule:n="@fluixi/core",routerModule:r="@fluixi/core/router-next"}=t,o={};for(let i of E)o[i]=e;for(let i of w)o[i]=n;for(let i of x)o[i]=r;return o}function H(t,e={}){let{resolver:n,isBound:r,modules:o,strategyFor:i}=e,a=B(o),s=new Map,l=new Set,c=p=>{let{name:u}=p;if(!u||u.includes(".")||r?.(u))return;let m=d(u);if(!m){l.add(u);return}let v=m.origin==="builtin"&&$.has(u)?k:i?.(p)??k;p.source={...m,loading:v},s.set(u,p.source)},d=p=>{if($.has(p))return{module:a[p],export:p,origin:"builtin"};let u=n?.resolve(p);if(u)return{module:u.module,export:u.export,origin:"rule"};if(x.includes(p))return{module:a[p],export:p,origin:"builtin"}};return N(t,p=>{p.kind==="component"&&c(p)}),{resolved:s,unresolved:[...l]}}function N(t,e){let n=Array.isArray(t)?t:[t];for(let r of n){e(r);let o=r.children;o&&N(o,e)}}function F(t){let e=[];N(t,i=>{i.kind==="component"&&i.source&&e.push(i)});let n=new Set,r=new Set;for(let i of e){let{module:a,loading:s}=i.source;s.kind==="eager"&&n.add(a),s.kind==="never"&&r.add(a)}let o=new Map;for(let i of e){let a=i.source;_(a.loading)&&n.has(a.module)&&(a.loading={kind:"eager"},o.set(a.module,(o.get(a.module)??0)+1))}return{collapsed:o,conflicted:[...r].filter(i=>n.has(i))}}var ae=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]),z={className:"class",htmlFor:"for"},ce=new Set(["innerHTML","outerHTML","innerText","textContent","value","checked","selected","muted","defaultValue","defaultChecked","children","ref"]),pe=new Set(["script","style","textarea","title"]);function T(t){return pe.has(t)}function J(t){return z[t]??t}function h(t){return t.kind!=="attr"||t.expr!==void 0||t.name.includes(":")?!1:!ce.has(t.name)}function S(t){if(!t.static||T(t.tag))return!1;for(let e of t.props)if(!h(e))return!1;for(let e of t.children)if(e.kind!=="text"&&!(e.kind==="element"&&S(e)))return!1;return!0}function g(t){let e=t.props.map(ue).filter(Boolean).join(""),n=`<${t.tag}${e}>`;return ae.has(t.tag)?n:`${n}${t.children.map(de).join("")}</${t.tag}>`}function de(t){if(t.kind==="text")return fe(t.value);if(t.kind!=="element")throw new Error(`serializeStatic: unexpected ${t.kind}`);return g(t)}function ue(t){let e=z[t.name]??t.name,n=t.literal;return n===!0||n===void 0?` ${e}`:n===!1||n===null?"":` ${e}="${me(String(n))}"`}function me(t){return t.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function fe(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}var he="<!--fx-->",ge="<!--fx/-->";function y(t){return t.kind==="expr"||t.kind==="component"||t.kind==="control"}function Re(t){return t.kind==="text"||t.kind==="element"&&U(t)}function U(t){return t.svg||T(t.tag)||!t.props.every(e=>h(e))?!1:t.children.every(e=>Re(e)||y(e))}function I(t){if(y(t))return!0;let e=t.children;return e?e.some(I):!1}function V(t){for(let e=0;e<t.children.length;e++){let n=t.children[e];if(n.kind==="text"&&(n.value===""||t.children[e+1]?.kind==="text"))return!1}return t.children.every(e=>e.kind!=="element"||V(e))}function W(t){if(!U(t)||!V(t)||!t.children.some(I))return null;let e=[],n=[],r=0,o=a=>{let s=`_n$${r++}`;return e.push({ref:s,expr:a}),s};return i(t,"_el$"),{html:ve(t),tag:t.tag,steps:e,holes:n};function i(a,s){let l=-1;a.children.forEach((d,p)=>{I(d)&&(l=p)});let c=null;for(let d=0;d<=l;d++){let p=a.children[d],u=c?`${c}.nextSibling`:`${s}.firstChild`;if(y(p)){let v=o(u),A=o(`holeEnd(${v})`);n.push({parentRef:s,startRef:v,endRef:A,node:p}),c=A;continue}let m=o(u);p.kind==="element"&&I(p)&&i(p,m),c=m}}}function ve(t){return g(q(t)).split(G).join(he+ge)}function q(t){return{...t,children:t.children.map(e=>y(e)?{kind:"text",value:G}:e.kind==="element"?q(e):e)}}var G="\0fx-hole\0";var P={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:[...P.symbols,"template","cloneTemplate","walk"]};var ke={show:"Show",for:"For",index:"Index",switch:"Switch",match:"Match",dynamic:"Dynamic",errorBoundary:"ErrorBoundary",suspense:"Suspense"};function xe(t){return/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(t)}function Y(t){return xe(t)?t:JSON.stringify(t)}function Ne(t){return t.expr!==void 0?t.reactive?`() => (${t.expr})`:`(${t.expr})`:JSON.stringify(t.literal??!0)}function Ie(t){return t.expr!==void 0?`(${t.expr})`:JSON.stringify(t.literal??!0)}function ye(t,e,n){let r=t.filter(s=>s.kind==="spread"),i=t.filter(s=>s.kind!=="spread").map(s=>`${Y(s.name)}: ${Ne(s)}`);e!=null&&i.push(`children: ${e}`);let a=`{ ${i.join(", ")} }`;return r.length>0?(n.add("mergeProps"),`mergeProps(${r.map(s=>s.expr).join(", ")}, ${a})`):a}function Z(t,e,n){return t.length===1?R(t[0],e,n):`[${t.map(r=>R(r,e,n)).join(", ")}]`}function K(t,e,n,r,o){r.add("createMemo"),r.add("createComponent");let i=e.filter(c=>c.kind==="spread"),s=e.filter(c=>c.kind!=="spread").map(c=>`get ${Y(c.name)}() { return ${Ie(c)}; }`);n.length>0&&s.push(`get children() { return ${Z(n,r,o)}; }`);let l=`{ ${s.join(", ")} }`;return i.length>0&&(r.add("mergeProps"),l=`mergeProps(${i.map(c=>c.expr).join(", ")}, ${l})`),`createMemo(() => createComponent(${t}, ${l}))`}function be(t,e){let n=J(e.name),r=e.literal;return r===!0||r===void 0?`${t}.setAttribute(${JSON.stringify(n)}, "");`:r===!1||r===null?"":`${t}.setAttribute(${JSON.stringify(n)}, ${JSON.stringify(String(r))});`}var X=!1;function R(t,e,n){switch(t.kind){case"text":return JSON.stringify(t.value);case"expr":return t.reactive?`() => (${t.code})`:`(${t.code})`;case"fragment":return t.children.length===0?"null":Z(t.children,e,n);case"component":return K(t.name,t.props,t.children,e,n);case"control":{let r=ke[t.control]??t.control;return e.add(r),K(r,t.props,t.children,e,n)}case"element":{if(n&&t.static&&!t.svg&&S(t)){e.add("templateNode");let s=`_tmpl$${n.length}`;return n.push({id:s,html:g(t),tag:t.tag,svg:!1}),`templateNode(${s}, ${JSON.stringify(t.tag)})`}if(n&&X){let s=W(t);if(s){e.add("templateNode"),e.add("insert"),e.add("holeEnd"),e.add("holeContent"),e.add("holeScope");let l=`_tmpl$${n.length}`;n.push({id:l,html:s.html,tag:s.tag,svg:!1});let c=[`const _el$ = templateNode(${l}, ${JSON.stringify(s.tag)}, true);`];for(let d of s.steps)c.push(`const ${d.ref} = ${d.expr};`);for(let d of s.holes)c.push(`insert(${d.parentRef}, holeScope(${d.startRef}, () => (${R(d.node,e,n)})), ${d.endRef}, holeContent(${d.startRef}, ${d.endRef}));`);return c.push("return _el$;"),`(() => { ${c.join(" ")} })()`}}e.add("createNativeElement");let r=JSON.stringify(t.tag),o="_el$",i=[],a=t.svg?`${r}, true`:r;if(i.push(`const ${o} = createNativeElement(${a});`),t.props.length>0)if(!t.svg&&t.props.every(h))for(let s of t.props)i.push(be(o,s));else{e.add("spread");let s=t.svg?", isSVG: true":"";i.push(`spread({ element: ${o}, props: ${ye(t.props,null,e)}${s} });`)}for(let s of t.children){e.add("insert");let l=R(s,e,n),c=s.kind==="expr"&&s.reactive||s.kind==="component"||s.kind==="control";i.push(c?`insert(${o}, ${l}, null);`:`insert(${o}, ${l});`)}return i.push(`return ${o};`),`(() => { ${i.join(" ")} })()`}}}function b(t,e){if(!e||e.length===0)return t;let n=new Map(e.map(r=>[r.id,JSON.stringify(r.html)]));return t.replace(/_tmpl\$\d+/g,r=>n.get(r)??r)}var C={name:"imperative",contract:P,emit(t,e){let n=new Set,r=e?.templateClone!==!1?[]:void 0;return X=e?.partialTemplates===!0,{code:R(t,n,r),imports:Array.from(n),templates:r}}};var Ce=t=>"components"in t;function Q(t,e){return t.replace(/\$(\d+)/g,(n,r)=>e[Number(r)]??n)}function Ee(t,e){return typeof e=="string"?{module:e,export:t}:e}function ee(t=[]){let e=new Map,n=[],r=[];for(let a of t){if(!Ce(a)){r.push(a);continue}for(let[s,l]of Object.entries(a.components)){let c=Ee(s,l),d=e.get(s);if(d&&d.module!==c.module){let p=n.find(u=>u.name===s);p?p.modules.push(c.module):n.push({name:s,modules:[d.module,c.module]});continue}e.set(s,c)}}let o=new Map;return{resolve:a=>{if(o.has(a))return o.get(a);let s=e.get(a);if(!s)for(let l of r){let c=a.match(new RegExp(l.match.source,l.match.flags.replace("g","")));if(c){s={module:Q(l.module,c),export:l.export?Q(l.export,c):a};break}}return o.set(a,s),s},names:()=>[...e.keys()],conflicts:()=>n}}import{parseTemplate as we}from"@fluixi/template-parser";function M(t){let e=t.split(/\r\n|\n|\r/),n=0;for(let o=0;o<e.length;o++)/[^ \t]/.test(e[o])&&(n=o);let r="";for(let o=0;o<e.length;o++){let i=e[o].replace(/\t/g," ");o!==0&&(i=i.replace(/^ +/,"")),o!==e.length-1&&(i=i.replace(/ +$/,"")),i&&(o!==n&&(i+=" "),r+=i)}return r}var te=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function $e(t){return t.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$\{/g,"\\${")}function ne(t){return t.charAt(0).toUpperCase()+t.slice(1)}var O=class{constructor(e,n){this.holes=e;this.used=n}hole(e){return this.holes[e]??{code:"undefined",reactive:!1}}emit(e){let{code:n,imports:r,templates:o}=C.emit(e,{});for(let i of r)this.used.add(i);return b(n,o)}lowerRoot(e){let n=this.lowerChildren(e);return n.length===1?n[0]:{kind:"fragment",children:n}}lowerChildren(e){let n=[];for(let r=0;r<e.length;r++){let o=e[r];if(o.kind==="Element"||o.kind==="Component"){let a=o.attributes.find(s=>s.kind==="IfDirective");if(a&&a.kind==="IfDirective"){let s=r+1;s<e.length&&this.isBlankText(e[s])&&s++;let l=e[s],c=l&&(l.kind==="Element"||l.kind==="Component")&&l.attributes.some(d=>d.kind==="ElseDirective");n.push(this.lowerIf(o,a.hole,c?l:null)),c&&(r=s);continue}if(o.attributes.some(s=>s.kind==="EachDirective")){n.push(this.lowerEach(o));continue}}let i=this.lowerNode(o);i&&n.push(i)}return n}isBlankText(e){return e.kind==="Text"&&!e.raw&&M(e.value)===""}lowerNode(e){switch(e.kind){case"Text":{if(e.raw)return{kind:"text",value:e.value};let n=M(e.value);return n?{kind:"text",value:n}:null}case"Comment":return null;case"Expression":{let n=this.hole(e.hole);return{kind:"expr",code:n.code,reactive:n.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 n=e.attributes.find(r=>r.kind==="Attribute"&&r.name==="is");if(e.tag==="component"&&n&&n.value&&n.value.kind==="hole"){let r=e.attributes.filter(i=>i!==n),o=this.lowerComponent({...e,kind:"Component",tag:"Dynamic",tagHole:null,attributes:r});return o.props.unshift({name:"component",kind:"attr",expr:`() => (${this.hole(n.value.hole).code})`}),o}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 n=e.tagHole!=null?this.hole(e.tagHole).code:e.tag,r=this.lowerAttributes(e.attributes),{slots:o,rest:i}=this.partitionSlots(e.children);for(let[l,c]of o){let d=c.length===1?c[0]:{kind:"fragment",children:c},p={name:l,kind:"attr",expr:this.emit(d),jsxElement:!0},u=r.findIndex(m=>m.name===l);u>=0?r[u]=p:r.push(p)}let a=e.attributes.find(l=>l.kind==="LoadDirective"),s=a?j(`load:${a.strategy}`,a.modifier):void 0;return{kind:"component",name:n,props:r,children:this.lowerChildren(i),...s?{load:s}:{}}}partitionSlots(e){let n=new Map,r=[];for(let o of e){if(o.kind==="Element"||o.kind==="Component"){let i=o.attributes.find(a=>a.kind==="Attribute"&&a.name==="slot");if(i&&i.value&&i.value.kind==="static"){let a={...o,attributes:o.attributes.filter(c=>c!==i)},s=a.kind==="Element"?this.lowerElement(a):this.lowerComponent(a),l=n.get(i.value.value)??[];l.push(s),n.set(i.value.value,l);continue}}r.push(o)}return{slots:n,rest:r}}lowerIf(e,n,r){let o=this.hole(n),i=[{name:"when",kind:"attr",expr:o.code,reactive:o.reactive}];if(r){let s=this.stripAndLower(r,l=>l.kind==="ElseDirective");i.push({name:"fallback",kind:"attr",expr:this.emit(s),jsxElement:!0})}let a=this.stripAndLower(e,s=>s.kind==="IfDirective");return{kind:"component",name:"Show",props:i,children:[a]}}lowerEach(e){let n=e.attributes.find(l=>l.kind==="EachDirective");if(!n||n.kind!=="EachDirective")return this.lowerNode(e);let r=this.hole(n.hole),o=[{name:"each",kind:"attr",expr:r.code,reactive:r.reactive}];if(n.key){let l="static"in n.key?`(item) => item[${JSON.stringify(n.key.static)}]`:this.hole(n.key.hole).code;o.push({name:"by",kind:"attr",expr:l})}let i=this.itemArrow(e),a;if(i){let l={kind:"expr",code:i.body,reactive:i.bodyReactive},c=this.rebuildWithChildren(e,[l]);a=`(${i.params.join(", ")}) => (${this.emit(c)})`}else{let l=this.stripAndLower(e,c=>c.kind==="EachDirective");a=`() => (${this.emit(l)})`}return{kind:"component",name:"For",props:o,children:[{kind:"expr",code:a,reactive:!1}]}}itemArrow(e){let n=e.children.filter(r=>!this.isBlankText(r));return n.length!==1||n[0].kind!=="Expression"?null:this.hole(n[0].hole).arrow??null}stripAndLower(e,n){let r={...e,attributes:e.attributes.filter(o=>!n(o))};return r.kind==="Element"?this.lowerElement(r):this.lowerComponent(r)}rebuildWithChildren(e,n){let r=this.lowerAttributes(e.attributes.filter(o=>o.kind!=="EachDirective"));return e.kind==="Element"?{kind:"element",tag:e.tag,svg:e.namespace==="svg",props:r,children:n,static:!1}:{kind:"component",name:e.tag,props:r,children:n}}lowerAttributes(e){let n=[],r=[],o=!1,i=[],a=!1,s=[];for(let l of e)switch(l.kind){case"IfDirective":case"EachDirective":case"ElseDirective":break;case"Attribute":n.push(this.plainAttr(l));break;case"PropertyBinding":n.push({name:l.name,kind:"prop",expr:this.hole(l.hole).code,reactive:this.hole(l.hole).reactive});break;case"EventBinding":n.push(this.eventProp(l));break;case"RefBinding":n.push({name:"ref",kind:"ref",expr:this.hole(l.hole).code,reactive:this.hole(l.hole).reactive});break;case"Spread":n.push({name:"",kind:"spread",expr:this.hole(l.hole).code});break;case"ClassDirective":{let c=this.hole(l.hole);r.push(`${JSON.stringify(l.name)}: ${c.code}`),o=o||c.reactive;break}case"StyleDirective":{let c=this.hole(l.hole);i.push(`${JSON.stringify(l.name)}: ${c.code}`),a=a||c.reactive;break}case"BindDirective":n.push(...this.bindProps(l.name,this.hole(l.hole).code));break;case"UseDirective":{let c=l.name??(l.hole!=null?this.hole(l.hole).code:null);if(!c)break;s.push(l.name!=null&&l.hole!=null?`[${c}, () => (${this.hole(l.hole).code})]`:`[${c}]`);break}}return r.length>0&&n.push({name:"classList",kind:"attr",expr:`{ ${r.join(", ")} }`,reactive:o}),i.length>0&&n.push({name:"style",kind:"attr",expr:`{ ${i.join(", ")} }`,reactive:a}),s.length>0&&n.push({name:"use",kind:"attr",expr:`[${s.join(", ")}]`}),n}eventProp(e){let n=e.name.toLowerCase(),r=this.hole(e.hole).code;if(!(e.syntax==="colon"||e.modifiers.length>0))return{name:"on"+ne(e.name),kind:"event",event:{name:n,delegated:te.has(n)},expr:r};let i=this.wrapHandler(r,e.modifiers),a=this.eventOptions(e.modifiers),s=a?`[${i}, ${a}]`:i;return{name:"on:"+n,kind:"attr",expr:s}}wrapHandler(e,n){let r=n.includes("self")?"if (e.target !== e.currentTarget) return; ":"",o=[];return n.includes("prevent")&&o.push("e.preventDefault();"),n.includes("stop")&&o.push("e.stopPropagation();"),!r&&o.length===0?e:`(e) => { ${r}${o.join(" ")} return (${e})(e); }`}eventOptions(e){let n=[];return e.includes("capture")&&n.push("capture: true"),e.includes("once")&&n.push("once: true"),e.includes("passive")&&n.push("passive: true"),n.length?`{ ${n.join(", ")} }`:null}bindProps(e,n){let r=e==="checked",o=r?"change":"input",i=r?"checked":"value",a=`(${n})`;return[{name:e,kind:"attr",expr:`${a}[0]()`,reactive:!0},{name:"on"+ne(o),kind:"event",event:{name:o,delegated:te.has(o)},expr:`(e) => ${a}[1](e.target.${i})`}]}plainAttr(e){let n=e.value,r=e.name;e.name==="class"?r=n&&n.kind==="hole"&&this.hole(n.hole).object?"classList":"className":e.name==="html"&&(r="innerHTML");let i={name:r,kind:"attr"};if(n==null)return i.literal=!0,i;if(n.kind==="static")return i.literal=n.value,i;if(n.kind==="hole"){let l=this.hole(n.hole);return i.expr=l.code,i.reactive=l.reactive,i}let a=!1,s=n.parts.map(l=>{if("text"in l)return $e(l.text);let c=this.hole(l.hole);return a=a||c.reactive,"${"+c.code+"}"}).join("");return i.expr="`"+s+"`",i.reactive=a,i}};function re(t,e,n,r={}){let{root:o}=we(t,{svg:r.svg});return new O(e,n).lowerRoot(o.children)}function Te(t){let e=[],n=0;return t.statics.forEach((r,o)=>{if(e.push({kind:"static",text:r,start:n}),n+=r.length,o<t.holes.length){let i=n+t.holes[o].code.length;e.push({kind:"hole",index:o,start:n,end:i}),n=i}}),e}function ct(t,e={}){if(t.statics.length!==t.holes.length+1)throw new Error(`[fluixi] compileTemplate: ${t.statics.length} static runs for ${t.holes.length} holes — a template always has exactly one more.`);return Se(Te(t),t.holes,e)}function Se(t,e,n={}){let r=new Set,o=re(t,[...e],r,{svg:n.svg});D(o),H(o,{resolver:ee(n.resolve??[]),modules:{controlFlowModule:n.controlFlowModule??"@fluixi/dom",coreModule:n.coreModule??"@fluixi/core",routerModule:n.routerModule??"@fluixi/core/router-next"},strategyFor:l=>l.load}),F(o);let{code:i,imports:a,templates:s}=C.emit(o,{templateClone:n.templateClone,partialTemplates:n.partialTemplates});for(let l of a)r.add(l);return n.hoistTemplates?{code:i,imports:[...r],ir:o,templates:s}:{code:b(i,s),imports:[...r],ir:o}}export{Se as compilePieces,ct as compileTemplate};
|
|
1
|
+
function oe(t){switch(t.kind){case"event":return"event";case"ref":return"ref";case"spread":return"spread";case"class":case"style":return"class-or-style-directive";case"attr":case"prop":return t.reactive?"reactive-prop":t.expr!==void 0?"expression-prop":void 0;default:return"expression-prop"}}function D(t){let e=[],n=new Map,r=o=>{switch(o.kind){case"text":return!0;case"expr":return!1;case"component":case"control":for(let i of o.children)r(i);return!1;case"fragment":for(let i of o.children)r(i);return!1;case"element":{let i=!0;for(let s of o.children)r(s)||(i=!1);let a;for(let s of o.props)if(a=oe(s),a)break;return!a&&!i&&(a=ie(o)),o.static=!a,a?n.set(o,a):e.push(o),o.static}default:return!1}};for(let o of Array.isArray(t)?t:[t])r(o);return{staticElements:e,reasons:n}}function ie(t){for(let e of t.children){if(e.kind==="expr")return"expression-child";if(e.kind==="component")return"component-child";if(e.kind==="control")return"control-child";if(e.kind==="element"&&!e.static||e.kind==="fragment")return"expression-child"}return"expression-child"}var k={kind:"eager"},se=["pointerdown","focusin","keydown"],L=new Set(["eager","idle","visible","interaction","media","never"]),f=class extends Error{};function j(t,e){let n=t.slice(5);if(!L.has(n))throw new f(`Unknown load strategy 'load:${n}'. Expected one of ${[...L].join(", ")}.`);switch(n){case"eager":return k;case"idle":return{kind:"idle"};case"never":return{kind:"never"};case"visible":return e?{kind:"visible",rootMargin:e}:{kind:"visible"};case"interaction":return{kind:"interaction",events:e?le(e):[...se]};case"media":if(!e)throw new f(`'load:media' needs a query, e.g. load:media="(min-width: 1024px)".`);return{kind:"media",query:e};default:throw new f(`Unhandled load strategy '${n}'.`)}}function le(t){let e=t.split(/[\s,]+/).map(n=>n.trim()).filter(Boolean);if(e.length===0)throw new f("'load:interaction' was given no event names.");return e}function _(t){return t.kind!=="eager"&&t.kind!=="never"}var E=["Show","For","Index","Switch","Match","Portal","Dynamic","ErrorBoundary"],w=["Suspense","SuspenseList","Await"],x=["Router","Outlet","Redirect","Link"],Oe=[...E,...w,...x],$=new Set([...E,...w]);function B(t={}){let{controlFlowModule:e="@fluixi/dom",coreModule:n="@fluixi/core",routerModule:r="@fluixi/core/router-next"}=t,o={};for(let i of E)o[i]=e;for(let i of w)o[i]=n;for(let i of x)o[i]=r;return o}function H(t,e={}){let{resolver:n,isBound:r,modules:o,strategyFor:i}=e,a=B(o),s=new Map,l=new Set,c=p=>{let{name:u}=p;if(!u||u.includes(".")||r?.(u))return;let m=d(u);if(!m){l.add(u);return}let v=m.origin==="builtin"&&$.has(u)?k:i?.(p)??k;p.source={...m,loading:v},s.set(u,p.source)},d=p=>{if($.has(p))return{module:a[p],export:p,origin:"builtin"};let u=n?.resolve(p);if(u)return{module:u.module,export:u.export,origin:"rule"};if(x.includes(p))return{module:a[p],export:p,origin:"builtin"}};return N(t,p=>{p.kind==="component"&&c(p)}),{resolved:s,unresolved:[...l]}}function N(t,e){let n=Array.isArray(t)?t:[t];for(let r of n){e(r);let o=r.children;o&&N(o,e)}}function F(t){let e=[];N(t,i=>{i.kind==="component"&&i.source&&e.push(i)});let n=new Set,r=new Set;for(let i of e){let{module:a,loading:s}=i.source;s.kind==="eager"&&n.add(a),s.kind==="never"&&r.add(a)}let o=new Map;for(let i of e){let a=i.source;_(a.loading)&&n.has(a.module)&&(a.loading={kind:"eager"},o.set(a.module,(o.get(a.module)??0)+1))}return{collapsed:o,conflicted:[...r].filter(i=>n.has(i))}}var ae=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]),z={className:"class",htmlFor:"for"},ce=new Set(["innerHTML","outerHTML","innerText","textContent","value","checked","selected","muted","defaultValue","defaultChecked","children","ref"]),pe=new Set(["script","style","textarea","title"]);function T(t){return pe.has(t)}function J(t){return z[t]??t}function h(t){return t.kind!=="attr"||t.expr!==void 0||t.name.includes(":")?!1:!ce.has(t.name)}function S(t){if(!t.static||T(t.tag))return!1;for(let e of t.props)if(!h(e))return!1;for(let e of t.children)if(e.kind!=="text"&&!(e.kind==="element"&&S(e)))return!1;return!0}function g(t){let e=t.props.map(ue).filter(Boolean).join(""),n=`<${t.tag}${e}>`;return ae.has(t.tag)?n:`${n}${t.children.map(de).join("")}</${t.tag}>`}function de(t){if(t.kind==="text")return fe(t.value);if(t.kind!=="element")throw new Error(`serializeStatic: unexpected ${t.kind}`);return g(t)}function ue(t){let e=z[t.name]??t.name,n=t.literal;return n===!0||n===void 0?` ${e}`:n===!1||n===null?"":` ${e}="${me(String(n))}"`}function me(t){return t.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function fe(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}var he="<!--fx-->",ge="<!--fx/-->";function b(t){return t.kind==="expr"||t.kind==="component"||t.kind==="control"}function Re(t){return t.kind==="text"||t.kind==="element"&&U(t)}function U(t){return t.svg||T(t.tag)||!t.props.every(e=>h(e))?!1:t.children.every(e=>Re(e)||b(e))}function I(t){if(b(t))return!0;let e=t.children;return e?e.some(I):!1}function V(t){for(let e=0;e<t.children.length;e++){let n=t.children[e];if(n.kind==="text"&&(n.value===""||t.children[e+1]?.kind==="text"))return!1}return t.children.every(e=>e.kind!=="element"||V(e))}function W(t){if(!U(t)||!V(t)||!t.children.some(I))return null;let e=[],n=[],r=0,o=a=>{let s=`_n$${r++}`;return e.push({ref:s,expr:a}),s};return i(t,"_el$"),{html:ve(t),tag:t.tag,steps:e,holes:n};function i(a,s){let l=-1;a.children.forEach((d,p)=>{I(d)&&(l=p)});let c=null;for(let d=0;d<=l;d++){let p=a.children[d],u=c?`${c}.nextSibling`:`${s}.firstChild`;if(b(p)){let v=o(u),A=o(`holeEnd(${v})`);n.push({parentRef:s,startRef:v,endRef:A,node:p}),c=A;continue}let m=o(u);p.kind==="element"&&I(p)&&i(p,m),c=m}}}function ve(t){return g(q(t)).split(G).join(he+ge)}function q(t){return{...t,children:t.children.map(e=>b(e)?{kind:"text",value:G}:e.kind==="element"?q(e):e)}}var G="\0fx-hole\0";var P={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:[...P.symbols,"template","cloneTemplate","walk"]};var ke={show:"Show",for:"For",index:"Index",switch:"Switch",match:"Match",dynamic:"Dynamic",errorBoundary:"ErrorBoundary",suspense:"Suspense"};function xe(t){return/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(t)}function Y(t){return xe(t)?t:JSON.stringify(t)}function Ne(t){return t.expr!==void 0?t.reactive?`() => (${t.expr})`:`(${t.expr})`:JSON.stringify(t.literal??!0)}function Ie(t){return t.expr!==void 0?`(${t.expr})`:JSON.stringify(t.literal??!0)}function be(t,e,n){let r=t.filter(s=>s.kind==="spread"),i=t.filter(s=>s.kind!=="spread").map(s=>`${Y(s.name)}: ${Ne(s)}`);e!=null&&i.push(`children: ${e}`);let a=`{ ${i.join(", ")} }`;return r.length>0?(n.add("mergeProps"),`mergeProps(${r.map(s=>s.expr).join(", ")}, ${a})`):a}function Z(t,e,n){return t.length===1?R(t[0],e,n):`[${t.map(r=>R(r,e,n)).join(", ")}]`}function K(t,e,n,r,o){r.add("createMemo"),r.add("createComponent");let i=e.filter(c=>c.kind==="spread"),s=e.filter(c=>c.kind!=="spread").map(c=>`get ${Y(c.name)}() { return ${Ie(c)}; }`);n.length>0&&s.push(`get children() { return ${Z(n,r,o)}; }`);let l=`{ ${s.join(", ")} }`;return i.length>0&&(r.add("mergeProps"),l=`mergeProps(${i.map(c=>c.expr).join(", ")}, ${l})`),`createMemo(() => createComponent(${t}, ${l}))`}function ye(t,e){let n=J(e.name),r=e.literal;return r===!0||r===void 0?`${t}.setAttribute(${JSON.stringify(n)}, "");`:r===!1||r===null?"":`${t}.setAttribute(${JSON.stringify(n)}, ${JSON.stringify(String(r))});`}var X=!1;function R(t,e,n){switch(t.kind){case"text":return JSON.stringify(t.value);case"expr":return t.reactive?`() => (${t.code})`:`(${t.code})`;case"fragment":return t.children.length===0?"null":Z(t.children,e,n);case"component":return K(t.name,t.props,t.children,e,n);case"control":{let r=ke[t.control]??t.control;return e.add(r),K(r,t.props,t.children,e,n)}case"element":{if(n&&t.static&&!t.svg&&S(t)){e.add("templateNode");let s=`_tmpl$${n.length}`;return n.push({id:s,html:g(t),tag:t.tag,svg:!1}),`templateNode(${s}, ${JSON.stringify(t.tag)})`}if(n&&X){let s=W(t);if(s){e.add("templateNode"),e.add("insert"),e.add("holeEnd"),e.add("holeContent"),e.add("holeScope");let l=`_tmpl$${n.length}`;n.push({id:l,html:s.html,tag:s.tag,svg:!1});let c=[`const _el$ = templateNode(${l}, ${JSON.stringify(s.tag)}, true);`];for(let d of s.steps)c.push(`const ${d.ref} = ${d.expr};`);for(let d of s.holes)c.push(`insert(${d.parentRef}, holeScope(${d.startRef}, () => (${R(d.node,e,n)})), ${d.endRef}, holeContent(${d.startRef}, ${d.endRef}));`);return c.push("return _el$;"),`(() => { ${c.join(" ")} })()`}}e.add("createNativeElement");let r=JSON.stringify(t.tag),o="_el$",i=[],a=t.svg?`${r}, true`:r;if(i.push(`const ${o} = createNativeElement(${a});`),t.props.length>0)if(!t.svg&&t.props.every(h))for(let s of t.props)i.push(ye(o,s));else{e.add("spread");let s=t.svg?", isSVG: true":"";i.push(`spread({ element: ${o}, props: ${be(t.props,null,e)}${s} });`)}for(let s of t.children){e.add("insert");let l=R(s,e,n),c=s.kind==="expr"&&s.reactive||s.kind==="component"||s.kind==="control";i.push(c?`insert(${o}, ${l}, null);`:`insert(${o}, ${l});`)}return i.push(`return ${o};`),`(() => { ${i.join(" ")} })()`}}}function y(t,e){if(!e||e.length===0)return t;let n=new Map(e.map(r=>[r.id,JSON.stringify(r.html)]));return t.replace(/_tmpl\$\d+/g,r=>n.get(r)??r)}var C={name:"imperative",contract:P,emit(t,e){let n=new Set,r=e?.templateClone!==!1?[]:void 0;return X=e?.partialTemplates===!0,{code:R(t,n,r),imports:Array.from(n),templates:r}}};var Ce=t=>"components"in t;function Q(t,e){return t.replace(/\$(\d+)/g,(n,r)=>e[Number(r)]??n)}function Ee(t,e){return typeof e=="string"?{module:e,export:t}:e}function ee(t=[]){let e=new Map,n=[],r=[];for(let a of t){if(!Ce(a)){r.push(a);continue}for(let[s,l]of Object.entries(a.components)){let c=Ee(s,l),d=e.get(s);if(d&&d.module!==c.module){let p=n.find(u=>u.name===s);p?p.modules.push(c.module):n.push({name:s,modules:[d.module,c.module]});continue}e.set(s,c)}}let o=new Map;return{resolve:a=>{if(o.has(a))return o.get(a);let s=e.get(a);if(!s)for(let l of r){let c=a.match(new RegExp(l.match.source,l.match.flags.replace("g","")));if(c){s={module:Q(l.module,c),export:l.export?Q(l.export,c):a};break}}return o.set(a,s),s},names:()=>[...e.keys()],conflicts:()=>n}}import{parseTemplate as we}from"@fluixi/template-parser";function M(t){let e=t.split(/\r\n|\n|\r/),n=0;for(let o=0;o<e.length;o++)/[^ \t]/.test(e[o])&&(n=o);let r="";for(let o=0;o<e.length;o++){let i=e[o].replace(/\t/g," ");o!==0&&(i=i.replace(/^ +/,"")),o!==e.length-1&&(i=i.replace(/ +$/,"")),i&&(o!==n&&(i+=" "),r+=i)}return r}var te=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function $e(t){return t.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$\{/g,"\\${")}function ne(t){return t.charAt(0).toUpperCase()+t.slice(1)}var O=class{constructor(e,n){this.holes=e;this.used=n}hole(e){return this.holes[e]??{code:"undefined",reactive:!1}}emit(e){let{code:n,imports:r,templates:o}=C.emit(e,{});for(let i of r)this.used.add(i);return y(n,o)}lowerRoot(e){let n=this.lowerChildren(e);return n.length===1?n[0]:{kind:"fragment",children:n}}lowerChildren(e){let n=[];for(let r=0;r<e.length;r++){let o=e[r];if(o.kind==="Element"||o.kind==="Component"){let a=o.attributes.find(s=>s.kind==="IfDirective");if(a&&a.kind==="IfDirective"){let s=r+1;s<e.length&&this.isBlankText(e[s])&&s++;let l=e[s],c=l&&(l.kind==="Element"||l.kind==="Component")&&l.attributes.some(d=>d.kind==="ElseDirective");n.push(this.lowerIf(o,a.hole,c?l:null)),c&&(r=s);continue}if(o.attributes.some(s=>s.kind==="EachDirective")){n.push(this.lowerEach(o));continue}}let i=this.lowerNode(o);i&&n.push(i)}return n}isBlankText(e){return e.kind==="Text"&&!e.raw&&M(e.value)===""}lowerNode(e){switch(e.kind){case"Text":{if(e.raw)return{kind:"text",value:e.value};let n=M(e.value);return n?{kind:"text",value:n}:null}case"Comment":return null;case"Expression":{let n=this.hole(e.hole);return{kind:"expr",code:n.code,reactive:n.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 n=e.attributes.find(r=>r.kind==="Attribute"&&r.name==="is");if(e.tag==="component"&&n&&n.value&&n.value.kind==="hole"){let r=e.attributes.filter(i=>i!==n),o=this.lowerComponent({...e,kind:"Component",tag:"Dynamic",tagHole:null,attributes:r});return o.props.unshift({name:"component",kind:"attr",expr:`() => (${this.hole(n.value.hole).code})`}),o}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 n=e.tagHole!=null?this.hole(e.tagHole).code:e.tag,r=this.lowerAttributes(e.attributes),{slots:o,rest:i}=this.partitionSlots(e.children);for(let[l,c]of o){let d=c.length===1?c[0]:{kind:"fragment",children:c},p={name:l,kind:"attr",expr:this.emit(d),jsxElement:!0},u=r.findIndex(m=>m.name===l);u>=0?r[u]=p:r.push(p)}let a=e.attributes.find(l=>l.kind==="LoadDirective"),s=a?j(`load:${a.strategy}`,a.modifier):void 0;return{kind:"component",name:n,props:r,children:this.lowerChildren(i),...s?{load:s}:{}}}partitionSlots(e){let n=new Map,r=[];for(let o of e){if(o.kind==="Element"||o.kind==="Component"){let i=o.attributes.find(a=>a.kind==="Attribute"&&a.name==="slot");if(i&&i.value&&i.value.kind==="static"){let a={...o,attributes:o.attributes.filter(c=>c!==i)},s=a.kind==="Element"?this.lowerElement(a):this.lowerComponent(a),l=n.get(i.value.value)??[];l.push(s),n.set(i.value.value,l);continue}}r.push(o)}return{slots:n,rest:r}}lowerIf(e,n,r){let o=this.hole(n),i=[{name:"when",kind:"attr",expr:o.code,reactive:o.reactive}];if(r){let s=this.stripAndLower(r,l=>l.kind==="ElseDirective");i.push({name:"fallback",kind:"attr",expr:this.emit(s),jsxElement:!0})}let a=this.stripAndLower(e,s=>s.kind==="IfDirective");return{kind:"component",name:"Show",props:i,children:[a]}}lowerEach(e){let n=e.attributes.find(l=>l.kind==="EachDirective");if(!n||n.kind!=="EachDirective")return this.lowerNode(e);let r=this.hole(n.hole),o=[{name:"each",kind:"attr",expr:r.code,reactive:r.reactive}];if(n.key){let l="static"in n.key?`(item) => item[${JSON.stringify(n.key.static)}]`:this.hole(n.key.hole).code;o.push({name:"by",kind:"attr",expr:l})}let i=this.itemArrow(e),a;if(i){let l={kind:"expr",code:i.body,reactive:i.bodyReactive},c=this.rebuildWithChildren(e,[l]);a=`(${i.params.join(", ")}) => (${this.emit(c)})`}else{let l=this.stripAndLower(e,c=>c.kind==="EachDirective");a=`() => (${this.emit(l)})`}return{kind:"component",name:"For",props:o,children:[{kind:"expr",code:a,reactive:!1}]}}itemArrow(e){let n=e.children.filter(r=>!this.isBlankText(r));return n.length!==1||n[0].kind!=="Expression"?null:this.hole(n[0].hole).arrow??null}stripAndLower(e,n){let r={...e,attributes:e.attributes.filter(o=>!n(o))};return r.kind==="Element"?this.lowerElement(r):this.lowerComponent(r)}rebuildWithChildren(e,n){let r=this.lowerAttributes(e.attributes.filter(o=>o.kind!=="EachDirective"));return e.kind==="Element"?{kind:"element",tag:e.tag,svg:e.namespace==="svg",props:r,children:n,static:!1}:{kind:"component",name:e.tag,props:r,children:n}}lowerAttributes(e){let n=[],r=[],o=!1,i=[],a=!1,s=[];for(let l of e)switch(l.kind){case"IfDirective":case"EachDirective":case"ElseDirective":break;case"Attribute":n.push(this.plainAttr(l));break;case"PropertyBinding":n.push({name:l.name,kind:"prop",expr:this.hole(l.hole).code,reactive:this.hole(l.hole).reactive});break;case"EventBinding":n.push(this.eventProp(l));break;case"RefBinding":n.push({name:"ref",kind:"ref",expr:this.hole(l.hole).code,reactive:this.hole(l.hole).reactive});break;case"Spread":n.push({name:"",kind:"spread",expr:this.hole(l.hole).code});break;case"ClassDirective":{let c=this.hole(l.hole);r.push(`${JSON.stringify(l.name)}: ${c.code}`),o=o||c.reactive;break}case"StyleDirective":{let c=this.hole(l.hole);i.push(`${JSON.stringify(l.name)}: ${c.code}`),a=a||c.reactive;break}case"BindDirective":n.push(...this.bindProps(l.name,this.hole(l.hole).code));break;case"UseDirective":{let c=l.name??(l.hole!=null?this.hole(l.hole).code:null);if(!c)break;s.push(l.name!=null&&l.hole!=null?`[${c}, () => (${this.hole(l.hole).code})]`:`[${c}]`);break}}return r.length>0&&n.push({name:"classList",kind:"attr",expr:`{ ${r.join(", ")} }`,reactive:o}),i.length>0&&n.push({name:"style",kind:"attr",expr:`{ ${i.join(", ")} }`,reactive:a}),s.length>0&&n.push({name:"use",kind:"attr",expr:`[${s.join(", ")}]`}),n}eventProp(e){let n=e.name.toLowerCase(),r=this.hole(e.hole).code;if(!(e.syntax==="colon"||e.modifiers.length>0))return{name:"on"+ne(e.name),kind:"event",event:{name:n,delegated:te.has(n)},expr:r};let i=this.wrapHandler(r,e.modifiers),a=this.eventOptions(e.modifiers),s=a?`[${i}, ${a}]`:i;return{name:"on:"+n,kind:"attr",expr:s}}wrapHandler(e,n){let r=n.includes("self")?"if (e.target !== e.currentTarget) return; ":"",o=[];return n.includes("prevent")&&o.push("e.preventDefault();"),n.includes("stop")&&o.push("e.stopPropagation();"),!r&&o.length===0?e:`(e) => { ${r}${o.join(" ")} return (${e})(e); }`}eventOptions(e){let n=[];return e.includes("capture")&&n.push("capture: true"),e.includes("once")&&n.push("once: true"),e.includes("passive")&&n.push("passive: true"),n.length?`{ ${n.join(", ")} }`:null}bindProps(e,n){let r=e==="checked",o=r?"change":"input",i=r?"checked":"value";this.used.add("bindPair");let a=`bindPair(${n})`;return[{name:e,kind:"attr",expr:`${a}[0]()`,reactive:!0},{name:"on"+ne(o),kind:"event",event:{name:o,delegated:te.has(o)},expr:`(e) => ${a}[1](e.target.${i})`}]}plainAttr(e){let n=e.value,r=e.name;e.name==="class"?r=n&&n.kind==="hole"&&this.hole(n.hole).object?"classList":"className":e.name==="html"&&(r="innerHTML");let i={name:r,kind:"attr"};if(n==null)return i.literal=!0,i;if(n.kind==="static")return i.literal=n.value,i;if(n.kind==="hole"){let l=this.hole(n.hole);return i.expr=l.code,i.reactive=l.reactive,i}let a=!1,s=n.parts.map(l=>{if("text"in l)return $e(l.text);let c=this.hole(l.hole);return a=a||c.reactive,"${"+c.code+"}"}).join("");return i.expr="`"+s+"`",i.reactive=a,i}};function re(t,e,n,r={}){let{root:o}=we(t,{svg:r.svg});return new O(e,n).lowerRoot(o.children)}function Te(t){let e=[],n=0;return t.statics.forEach((r,o)=>{if(e.push({kind:"static",text:r,start:n}),n+=r.length,o<t.holes.length){let i=n+t.holes[o].code.length;e.push({kind:"hole",index:o,start:n,end:i}),n=i}}),e}function ct(t,e={}){if(t.statics.length!==t.holes.length+1)throw new Error(`[fluixi] compileTemplate: ${t.statics.length} static runs for ${t.holes.length} holes — a template always has exactly one more.`);return Se(Te(t),t.holes,e)}function Se(t,e,n={}){let r=new Set,o=re(t,[...e],r,{svg:n.svg});D(o),H(o,{resolver:ee(n.resolve??[]),modules:{controlFlowModule:n.controlFlowModule??"@fluixi/dom",coreModule:n.coreModule??"@fluixi/core",routerModule:n.routerModule??"@fluixi/core/router-next"},strategyFor:l=>l.load}),F(o);let{code:i,imports:a,templates:s}=C.emit(o,{templateClone:n.templateClone,partialTemplates:n.partialTemplates});for(let l of a)r.add(l);return n.hoistTemplates?{code:i,imports:[...r],ir:o,templates:s}:{code:y(i,s),imports:[...r],ir:o}}export{Se as compilePieces,ct as compileTemplate};
|
package/dist/lower/template.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var R=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var G=Object.prototype.hasOwnProperty;var K=(t,e)=>{for(var n in e)R(t,n,{get:e[n],enumerable:!0})},Y=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of q(e))!G.call(t,i)&&i!==n&&R(t,i,{get:()=>e[i],enumerable:!(r=W(e,i))||r.enumerable});return t};var Z=t=>Y(R({},"__esModule",{value:!0}),t);var Re={};K(Re,{lowerTemplate:()=>Ne});module.exports=Z(Re);var U=require("@fluixi/template-parser");var X={kind:"eager"},Q=["pointerdown","focusin","keydown"],T=new Set(["eager","idle","visible","interaction","media","never"]),u=class extends Error{};function S(t,e){let n=t.slice(5);if(!T.has(n))throw new u(`Unknown load strategy 'load:${n}'. Expected one of ${[...T].join(", ")}.`);switch(n){case"eager":return X;case"idle":return{kind:"idle"};case"never":return{kind:"never"};case"visible":return e?{kind:"visible",rootMargin:e}:{kind:"visible"};case"interaction":return{kind:"interaction",events:e?ee(e):[...Q]};case"media":if(!e)throw new u(`'load:media' needs a query, e.g. load:media="(min-width: 1024px)".`);return{kind:"media",query:e};default:throw new u(`Unhandled load strategy '${n}'.`)}}function ee(t){let e=t.split(/[\s,]+/).map(n=>n.trim()).filter(Boolean);if(e.length===0)throw new u("'load:interaction' was given no event names.");return e}var te=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]),C={className:"class",htmlFor:"for"},ne=new Set(["innerHTML","outerHTML","innerText","textContent","value","checked","selected","muted","defaultValue","defaultChecked","children","ref"]),re=new Set(["script","style","textarea","title"]);function $(t){return re.has(t)}function A(t){return C[t]??t}function h(t){return t.kind!=="attr"||t.expr!==void 0||t.name.includes(":")?!1:!ne.has(t.name)}function b(t){if(!t.static||$(t.tag))return!1;for(let e of t.props)if(!h(e))return!1;for(let e of t.children)if(e.kind!=="text"&&!(e.kind==="element"&&b(e)))return!1;return!0}function f(t){let e=t.props.map(oe).filter(Boolean).join(""),n=`<${t.tag}${e}>`;return te.has(t.tag)?n:`${n}${t.children.map(ie).join("")}</${t.tag}>`}function ie(t){if(t.kind==="text")return ae(t.value);if(t.kind!=="element")throw new Error(`serializeStatic: unexpected ${t.kind}`);return f(t)}function oe(t){let e=C[t.name]??t.name,n=t.literal;return n===!0||n===void 0?` ${e}`:n===!1||n===null?"":` ${e}="${se(String(n))}"`}function se(t){return t.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function ae(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}var le="<!--fx-->",ce="<!--fx/-->";function N(t){return t.kind==="expr"||t.kind==="component"||t.kind==="control"}function de(t){return t.kind==="text"||t.kind==="element"&&P(t)}function P(t){return t.svg||$(t.tag)||!t.props.every(e=>h(e))?!1:t.children.every(e=>de(e)||N(e))}function k(t){if(N(t))return!0;let e=t.children;return e?e.some(k):!1}function L(t){for(let e=0;e<t.children.length;e++){let n=t.children[e];if(n.kind==="text"&&(n.value===""||t.children[e+1]?.kind==="text"))return!1}return t.children.every(e=>e.kind!=="element"||L(e))}function D(t){if(!P(t)||!L(t)||!t.children.some(k))return null;let e=[],n=[],r=0,i=c=>{let s=`_n$${r++}`;return e.push({ref:s,expr:c}),s};return o(t,"_el$"),{html:pe(t),tag:t.tag,steps:e,holes:n};function o(c,s){let a=-1;c.children.forEach((d,p)=>{k(d)&&(a=p)});let l=null;for(let d=0;d<=a;d++){let p=c.children[d],m=l?`${l}.nextSibling`:`${s}.firstChild`;if(N(p)){let w=i(m),y=i(`holeEnd(${w})`);n.push({parentRef:s,startRef:w,endRef:y,node:p}),l=y;continue}let v=i(m);p.kind==="element"&&k(p)&&o(p,v),l=v}}}function pe(t){return f(j(t)).split(O).join(le+ce)}function j(t){return{...t,children:t.children.map(e=>N(e)?{kind:"text",value:O}:e.kind==="element"?j(e):e)}}var O="\0fx-hole\0";var x={version:1,module:"@fluixi/dom",symbols:["createNativeElement","insert","spread","setAttribute","delegateEvents","createComponent","createMemo","untrack","Show","For","Index","Switch","Match","Dynamic","ErrorBoundary"]},we={version:2,module:"@fluixi/dom",symbols:[...x.symbols,"template","cloneTemplate","walk"]};var ue={show:"Show",for:"For",index:"Index",switch:"Switch",match:"Match",dynamic:"Dynamic",errorBoundary:"ErrorBoundary",suspense:"Suspense"};function me(t){return/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(t)}function M(t){return me(t)?t:JSON.stringify(t)}function he(t){return t.expr!==void 0?t.reactive?`() => (${t.expr})`:`(${t.expr})`:JSON.stringify(t.literal??!0)}function fe(t){return t.expr!==void 0?`(${t.expr})`:JSON.stringify(t.literal??!0)}function ge(t,e,n){let r=t.filter(s=>s.kind==="spread"),o=t.filter(s=>s.kind!=="spread").map(s=>`${M(s.name)}: ${he(s)}`);e!=null&&o.push(`children: ${e}`);let c=`{ ${o.join(", ")} }`;return r.length>0?(n.add("mergeProps"),`mergeProps(${r.map(s=>s.expr).join(", ")}, ${c})`):c}function H(t,e,n){return t.length===1?g(t[0],e,n):`[${t.map(r=>g(r,e,n)).join(", ")}]`}function _(t,e,n,r,i){r.add("createMemo"),r.add("createComponent");let o=e.filter(l=>l.kind==="spread"),s=e.filter(l=>l.kind!=="spread").map(l=>`get ${M(l.name)}() { return ${fe(l)}; }`);n.length>0&&s.push(`get children() { return ${H(n,r,i)}; }`);let a=`{ ${s.join(", ")} }`;return o.length>0&&(r.add("mergeProps"),a=`mergeProps(${o.map(l=>l.expr).join(", ")}, ${a})`),`createMemo(() => createComponent(${t}, ${a}))`}function ve(t,e){let n=A(e.name),r=e.literal;return r===!0||r===void 0?`${t}.setAttribute(${JSON.stringify(n)}, "");`:r===!1||r===null?"":`${t}.setAttribute(${JSON.stringify(n)}, ${JSON.stringify(String(r))});`}var B=!1;function g(t,e,n){switch(t.kind){case"text":return JSON.stringify(t.value);case"expr":return t.reactive?`() => (${t.code})`:`(${t.code})`;case"fragment":return t.children.length===0?"null":H(t.children,e,n);case"component":return _(t.name,t.props,t.children,e,n);case"control":{let r=ue[t.control]??t.control;return e.add(r),_(r,t.props,t.children,e,n)}case"element":{if(n&&t.static&&!t.svg&&b(t)){e.add("templateNode");let s=`_tmpl$${n.length}`;return n.push({id:s,html:f(t),tag:t.tag,svg:!1}),`templateNode(${s}, ${JSON.stringify(t.tag)})`}if(n&&B){let s=D(t);if(s){e.add("templateNode"),e.add("insert"),e.add("holeEnd"),e.add("holeContent"),e.add("holeScope");let a=`_tmpl$${n.length}`;n.push({id:a,html:s.html,tag:s.tag,svg:!1});let l=[`const _el$ = templateNode(${a}, ${JSON.stringify(s.tag)}, true);`];for(let d of s.steps)l.push(`const ${d.ref} = ${d.expr};`);for(let d of s.holes)l.push(`insert(${d.parentRef}, holeScope(${d.startRef}, () => (${g(d.node,e,n)})), ${d.endRef}, holeContent(${d.startRef}, ${d.endRef}));`);return l.push("return _el$;"),`(() => { ${l.join(" ")} })()`}}e.add("createNativeElement");let r=JSON.stringify(t.tag),i="_el$",o=[],c=t.svg?`${r}, true`:r;if(o.push(`const ${i} = createNativeElement(${c});`),t.props.length>0)if(!t.svg&&t.props.every(h))for(let s of t.props)o.push(ve(i,s));else{e.add("spread");let s=t.svg?", isSVG: true":"";o.push(`spread({ element: ${i}, props: ${ge(t.props,null,e)}${s} });`)}for(let s of t.children){e.add("insert");let a=g(s,e,n),l=s.kind==="expr"&&s.reactive||s.kind==="component"||s.kind==="control";o.push(l?`insert(${i}, ${a}, null);`:`insert(${i}, ${a});`)}return o.push(`return ${i};`),`(() => { ${o.join(" ")} })()`}}}function J(t,e){if(!e||e.length===0)return t;let n=new Map(e.map(r=>[r.id,JSON.stringify(r.html)]));return t.replace(/_tmpl\$\d+/g,r=>n.get(r)??r)}var z={name:"imperative",contract:x,emit(t,e){let n=new Set,r=e?.templateClone!==!1?[]:void 0;return B=e?.partialTemplates===!0,{code:g(t,n,r),imports:Array.from(n),templates:r}}};function E(t){let e=t.split(/\r\n|\n|\r/),n=0;for(let i=0;i<e.length;i++)/[^ \t]/.test(e[i])&&(n=i);let r="";for(let i=0;i<e.length;i++){let o=e[i].replace(/\t/g," ");i!==0&&(o=o.replace(/^ +/,"")),i!==e.length-1&&(o=o.replace(/ +$/,"")),o&&(i!==n&&(o+=" "),r+=o)}return r}var V=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function ke(t){return t.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$\{/g,"\\${")}function F(t){return t.charAt(0).toUpperCase()+t.slice(1)}var I=class{constructor(e,n){this.holes=e;this.used=n}hole(e){return this.holes[e]??{code:"undefined",reactive:!1}}emit(e){let{code:n,imports:r,templates:i}=z.emit(e,{});for(let o of r)this.used.add(o);return J(n,i)}lowerRoot(e){let n=this.lowerChildren(e);return n.length===1?n[0]:{kind:"fragment",children:n}}lowerChildren(e){let n=[];for(let r=0;r<e.length;r++){let i=e[r];if(i.kind==="Element"||i.kind==="Component"){let c=i.attributes.find(s=>s.kind==="IfDirective");if(c&&c.kind==="IfDirective"){let s=r+1;s<e.length&&this.isBlankText(e[s])&&s++;let a=e[s],l=a&&(a.kind==="Element"||a.kind==="Component")&&a.attributes.some(d=>d.kind==="ElseDirective");n.push(this.lowerIf(i,c.hole,l?a:null)),l&&(r=s);continue}if(i.attributes.some(s=>s.kind==="EachDirective")){n.push(this.lowerEach(i));continue}}let o=this.lowerNode(i);o&&n.push(o)}return n}isBlankText(e){return e.kind==="Text"&&!e.raw&&E(e.value)===""}lowerNode(e){switch(e.kind){case"Text":{if(e.raw)return{kind:"text",value:e.value};let n=E(e.value);return n?{kind:"text",value:n}:null}case"Comment":return null;case"Expression":{let n=this.hole(e.hole);return{kind:"expr",code:n.code,reactive:n.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 n=e.attributes.find(r=>r.kind==="Attribute"&&r.name==="is");if(e.tag==="component"&&n&&n.value&&n.value.kind==="hole"){let r=e.attributes.filter(o=>o!==n),i=this.lowerComponent({...e,kind:"Component",tag:"Dynamic",tagHole:null,attributes:r});return i.props.unshift({name:"component",kind:"attr",expr:`() => (${this.hole(n.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 n=e.tagHole!=null?this.hole(e.tagHole).code:e.tag,r=this.lowerAttributes(e.attributes),{slots:i,rest:o}=this.partitionSlots(e.children);for(let[a,l]of i){let d=l.length===1?l[0]:{kind:"fragment",children:l},p={name:a,kind:"attr",expr:this.emit(d),jsxElement:!0},m=r.findIndex(v=>v.name===a);m>=0?r[m]=p:r.push(p)}let c=e.attributes.find(a=>a.kind==="LoadDirective"),s=c?S(`load:${c.strategy}`,c.modifier):void 0;return{kind:"component",name:n,props:r,children:this.lowerChildren(o),...s?{load:s}:{}}}partitionSlots(e){let n=new Map,r=[];for(let i of e){if(i.kind==="Element"||i.kind==="Component"){let o=i.attributes.find(c=>c.kind==="Attribute"&&c.name==="slot");if(o&&o.value&&o.value.kind==="static"){let c={...i,attributes:i.attributes.filter(l=>l!==o)},s=c.kind==="Element"?this.lowerElement(c):this.lowerComponent(c),a=n.get(o.value.value)??[];a.push(s),n.set(o.value.value,a);continue}}r.push(i)}return{slots:n,rest:r}}lowerIf(e,n,r){let i=this.hole(n),o=[{name:"when",kind:"attr",expr:i.code,reactive:i.reactive}];if(r){let s=this.stripAndLower(r,a=>a.kind==="ElseDirective");o.push({name:"fallback",kind:"attr",expr:this.emit(s),jsxElement:!0})}let c=this.stripAndLower(e,s=>s.kind==="IfDirective");return{kind:"component",name:"Show",props:o,children:[c]}}lowerEach(e){let n=e.attributes.find(a=>a.kind==="EachDirective");if(!n||n.kind!=="EachDirective")return this.lowerNode(e);let r=this.hole(n.hole),i=[{name:"each",kind:"attr",expr:r.code,reactive:r.reactive}];if(n.key){let a="static"in n.key?`(item) => item[${JSON.stringify(n.key.static)}]`:this.hole(n.key.hole).code;i.push({name:"by",kind:"attr",expr:a})}let o=this.itemArrow(e),c;if(o){let a={kind:"expr",code:o.body,reactive:o.bodyReactive},l=this.rebuildWithChildren(e,[a]);c=`(${o.params.join(", ")}) => (${this.emit(l)})`}else{let a=this.stripAndLower(e,l=>l.kind==="EachDirective");c=`() => (${this.emit(a)})`}return{kind:"component",name:"For",props:i,children:[{kind:"expr",code:c,reactive:!1}]}}itemArrow(e){let n=e.children.filter(r=>!this.isBlankText(r));return n.length!==1||n[0].kind!=="Expression"?null:this.hole(n[0].hole).arrow??null}stripAndLower(e,n){let r={...e,attributes:e.attributes.filter(i=>!n(i))};return r.kind==="Element"?this.lowerElement(r):this.lowerComponent(r)}rebuildWithChildren(e,n){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:n,static:!1}:{kind:"component",name:e.tag,props:r,children:n}}lowerAttributes(e){let n=[],r=[],i=!1,o=[],c=!1,s=[];for(let a of e)switch(a.kind){case"IfDirective":case"EachDirective":case"ElseDirective":break;case"Attribute":n.push(this.plainAttr(a));break;case"PropertyBinding":n.push({name:a.name,kind:"prop",expr:this.hole(a.hole).code,reactive:this.hole(a.hole).reactive});break;case"EventBinding":n.push(this.eventProp(a));break;case"RefBinding":n.push({name:"ref",kind:"ref",expr:this.hole(a.hole).code,reactive:this.hole(a.hole).reactive});break;case"Spread":n.push({name:"",kind:"spread",expr:this.hole(a.hole).code});break;case"ClassDirective":{let l=this.hole(a.hole);r.push(`${JSON.stringify(a.name)}: ${l.code}`),i=i||l.reactive;break}case"StyleDirective":{let l=this.hole(a.hole);o.push(`${JSON.stringify(a.name)}: ${l.code}`),c=c||l.reactive;break}case"BindDirective":n.push(...this.bindProps(a.name,this.hole(a.hole).code));break;case"UseDirective":{let l=a.name??(a.hole!=null?this.hole(a.hole).code:null);if(!l)break;s.push(a.name!=null&&a.hole!=null?`[${l}, () => (${this.hole(a.hole).code})]`:`[${l}]`);break}}return r.length>0&&n.push({name:"classList",kind:"attr",expr:`{ ${r.join(", ")} }`,reactive:i}),o.length>0&&n.push({name:"style",kind:"attr",expr:`{ ${o.join(", ")} }`,reactive:c}),s.length>0&&n.push({name:"use",kind:"attr",expr:`[${s.join(", ")}]`}),n}eventProp(e){let n=e.name.toLowerCase(),r=this.hole(e.hole).code;if(!(e.syntax==="colon"||e.modifiers.length>0))return{name:"on"+F(e.name),kind:"event",event:{name:n,delegated:V.has(n)},expr:r};let o=this.wrapHandler(r,e.modifiers),c=this.eventOptions(e.modifiers),s=c?`[${o}, ${c}]`:o;return{name:"on:"+n,kind:"attr",expr:s}}wrapHandler(e,n){let r=n.includes("self")?"if (e.target !== e.currentTarget) return; ":"",i=[];return n.includes("prevent")&&i.push("e.preventDefault();"),n.includes("stop")&&i.push("e.stopPropagation();"),!r&&i.length===0?e:`(e) => { ${r}${i.join(" ")} return (${e})(e); }`}eventOptions(e){let n=[];return e.includes("capture")&&n.push("capture: true"),e.includes("once")&&n.push("once: true"),e.includes("passive")&&n.push("passive: true"),n.length?`{ ${n.join(", ")} }`:null}bindProps(e,n){let r=e==="checked",i=r?"change":"input",o=r?"checked":"value",c=`(${n})`;return[{name:e,kind:"attr",expr:`${c}[0]()`,reactive:!0},{name:"on"+F(i),kind:"event",event:{name:i,delegated:V.has(i)},expr:`(e) => ${c}[1](e.target.${o})`}]}plainAttr(e){let n=e.value,r=e.name;e.name==="class"?r=n&&n.kind==="hole"&&this.hole(n.hole).object?"classList":"className":e.name==="html"&&(r="innerHTML");let o={name:r,kind:"attr"};if(n==null)return o.literal=!0,o;if(n.kind==="static")return o.literal=n.value,o;if(n.kind==="hole"){let a=this.hole(n.hole);return o.expr=a.code,o.reactive=a.reactive,o}let c=!1,s=n.parts.map(a=>{if("text"in a)return ke(a.text);let l=this.hole(a.hole);return c=c||l.reactive,"${"+l.code+"}"}).join("");return o.expr="`"+s+"`",o.reactive=c,o}};function Ne(t,e,n,r={}){let{root:i}=(0,U.parseTemplate)(t,{svg:r.svg});return new I(e,n).lowerRoot(i.children)}
|
|
1
|
+
"use strict";var R=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var G=Object.prototype.hasOwnProperty;var K=(t,e)=>{for(var n in e)R(t,n,{get:e[n],enumerable:!0})},Y=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of q(e))!G.call(t,i)&&i!==n&&R(t,i,{get:()=>e[i],enumerable:!(r=W(e,i))||r.enumerable});return t};var Z=t=>Y(R({},"__esModule",{value:!0}),t);var Re={};K(Re,{lowerTemplate:()=>Ne});module.exports=Z(Re);var U=require("@fluixi/template-parser");var X={kind:"eager"},Q=["pointerdown","focusin","keydown"],T=new Set(["eager","idle","visible","interaction","media","never"]),u=class extends Error{};function S(t,e){let n=t.slice(5);if(!T.has(n))throw new u(`Unknown load strategy 'load:${n}'. Expected one of ${[...T].join(", ")}.`);switch(n){case"eager":return X;case"idle":return{kind:"idle"};case"never":return{kind:"never"};case"visible":return e?{kind:"visible",rootMargin:e}:{kind:"visible"};case"interaction":return{kind:"interaction",events:e?ee(e):[...Q]};case"media":if(!e)throw new u(`'load:media' needs a query, e.g. load:media="(min-width: 1024px)".`);return{kind:"media",query:e};default:throw new u(`Unhandled load strategy '${n}'.`)}}function ee(t){let e=t.split(/[\s,]+/).map(n=>n.trim()).filter(Boolean);if(e.length===0)throw new u("'load:interaction' was given no event names.");return e}var te=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]),C={className:"class",htmlFor:"for"},ne=new Set(["innerHTML","outerHTML","innerText","textContent","value","checked","selected","muted","defaultValue","defaultChecked","children","ref"]),re=new Set(["script","style","textarea","title"]);function $(t){return re.has(t)}function P(t){return C[t]??t}function h(t){return t.kind!=="attr"||t.expr!==void 0||t.name.includes(":")?!1:!ne.has(t.name)}function b(t){if(!t.static||$(t.tag))return!1;for(let e of t.props)if(!h(e))return!1;for(let e of t.children)if(e.kind!=="text"&&!(e.kind==="element"&&b(e)))return!1;return!0}function f(t){let e=t.props.map(oe).filter(Boolean).join(""),n=`<${t.tag}${e}>`;return te.has(t.tag)?n:`${n}${t.children.map(ie).join("")}</${t.tag}>`}function ie(t){if(t.kind==="text")return ae(t.value);if(t.kind!=="element")throw new Error(`serializeStatic: unexpected ${t.kind}`);return f(t)}function oe(t){let e=C[t.name]??t.name,n=t.literal;return n===!0||n===void 0?` ${e}`:n===!1||n===null?"":` ${e}="${se(String(n))}"`}function se(t){return t.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function ae(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}var le="<!--fx-->",ce="<!--fx/-->";function N(t){return t.kind==="expr"||t.kind==="component"||t.kind==="control"}function de(t){return t.kind==="text"||t.kind==="element"&&A(t)}function A(t){return t.svg||$(t.tag)||!t.props.every(e=>h(e))?!1:t.children.every(e=>de(e)||N(e))}function k(t){if(N(t))return!0;let e=t.children;return e?e.some(k):!1}function L(t){for(let e=0;e<t.children.length;e++){let n=t.children[e];if(n.kind==="text"&&(n.value===""||t.children[e+1]?.kind==="text"))return!1}return t.children.every(e=>e.kind!=="element"||L(e))}function D(t){if(!A(t)||!L(t)||!t.children.some(k))return null;let e=[],n=[],r=0,i=c=>{let s=`_n$${r++}`;return e.push({ref:s,expr:c}),s};return o(t,"_el$"),{html:pe(t),tag:t.tag,steps:e,holes:n};function o(c,s){let a=-1;c.children.forEach((d,p)=>{k(d)&&(a=p)});let l=null;for(let d=0;d<=a;d++){let p=c.children[d],m=l?`${l}.nextSibling`:`${s}.firstChild`;if(N(p)){let w=i(m),y=i(`holeEnd(${w})`);n.push({parentRef:s,startRef:w,endRef:y,node:p}),l=y;continue}let v=i(m);p.kind==="element"&&k(p)&&o(p,v),l=v}}}function pe(t){return f(j(t)).split(O).join(le+ce)}function j(t){return{...t,children:t.children.map(e=>N(e)?{kind:"text",value:O}:e.kind==="element"?j(e):e)}}var O="\0fx-hole\0";var x={version:1,module:"@fluixi/dom",symbols:["createNativeElement","insert","spread","setAttribute","delegateEvents","createComponent","createMemo","untrack","Show","For","Index","Switch","Match","Dynamic","ErrorBoundary"]},we={version:2,module:"@fluixi/dom",symbols:[...x.symbols,"template","cloneTemplate","walk"]};var ue={show:"Show",for:"For",index:"Index",switch:"Switch",match:"Match",dynamic:"Dynamic",errorBoundary:"ErrorBoundary",suspense:"Suspense"};function me(t){return/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(t)}function M(t){return me(t)?t:JSON.stringify(t)}function he(t){return t.expr!==void 0?t.reactive?`() => (${t.expr})`:`(${t.expr})`:JSON.stringify(t.literal??!0)}function fe(t){return t.expr!==void 0?`(${t.expr})`:JSON.stringify(t.literal??!0)}function ge(t,e,n){let r=t.filter(s=>s.kind==="spread"),o=t.filter(s=>s.kind!=="spread").map(s=>`${M(s.name)}: ${he(s)}`);e!=null&&o.push(`children: ${e}`);let c=`{ ${o.join(", ")} }`;return r.length>0?(n.add("mergeProps"),`mergeProps(${r.map(s=>s.expr).join(", ")}, ${c})`):c}function H(t,e,n){return t.length===1?g(t[0],e,n):`[${t.map(r=>g(r,e,n)).join(", ")}]`}function _(t,e,n,r,i){r.add("createMemo"),r.add("createComponent");let o=e.filter(l=>l.kind==="spread"),s=e.filter(l=>l.kind!=="spread").map(l=>`get ${M(l.name)}() { return ${fe(l)}; }`);n.length>0&&s.push(`get children() { return ${H(n,r,i)}; }`);let a=`{ ${s.join(", ")} }`;return o.length>0&&(r.add("mergeProps"),a=`mergeProps(${o.map(l=>l.expr).join(", ")}, ${a})`),`createMemo(() => createComponent(${t}, ${a}))`}function ve(t,e){let n=P(e.name),r=e.literal;return r===!0||r===void 0?`${t}.setAttribute(${JSON.stringify(n)}, "");`:r===!1||r===null?"":`${t}.setAttribute(${JSON.stringify(n)}, ${JSON.stringify(String(r))});`}var B=!1;function g(t,e,n){switch(t.kind){case"text":return JSON.stringify(t.value);case"expr":return t.reactive?`() => (${t.code})`:`(${t.code})`;case"fragment":return t.children.length===0?"null":H(t.children,e,n);case"component":return _(t.name,t.props,t.children,e,n);case"control":{let r=ue[t.control]??t.control;return e.add(r),_(r,t.props,t.children,e,n)}case"element":{if(n&&t.static&&!t.svg&&b(t)){e.add("templateNode");let s=`_tmpl$${n.length}`;return n.push({id:s,html:f(t),tag:t.tag,svg:!1}),`templateNode(${s}, ${JSON.stringify(t.tag)})`}if(n&&B){let s=D(t);if(s){e.add("templateNode"),e.add("insert"),e.add("holeEnd"),e.add("holeContent"),e.add("holeScope");let a=`_tmpl$${n.length}`;n.push({id:a,html:s.html,tag:s.tag,svg:!1});let l=[`const _el$ = templateNode(${a}, ${JSON.stringify(s.tag)}, true);`];for(let d of s.steps)l.push(`const ${d.ref} = ${d.expr};`);for(let d of s.holes)l.push(`insert(${d.parentRef}, holeScope(${d.startRef}, () => (${g(d.node,e,n)})), ${d.endRef}, holeContent(${d.startRef}, ${d.endRef}));`);return l.push("return _el$;"),`(() => { ${l.join(" ")} })()`}}e.add("createNativeElement");let r=JSON.stringify(t.tag),i="_el$",o=[],c=t.svg?`${r}, true`:r;if(o.push(`const ${i} = createNativeElement(${c});`),t.props.length>0)if(!t.svg&&t.props.every(h))for(let s of t.props)o.push(ve(i,s));else{e.add("spread");let s=t.svg?", isSVG: true":"";o.push(`spread({ element: ${i}, props: ${ge(t.props,null,e)}${s} });`)}for(let s of t.children){e.add("insert");let a=g(s,e,n),l=s.kind==="expr"&&s.reactive||s.kind==="component"||s.kind==="control";o.push(l?`insert(${i}, ${a}, null);`:`insert(${i}, ${a});`)}return o.push(`return ${i};`),`(() => { ${o.join(" ")} })()`}}}function J(t,e){if(!e||e.length===0)return t;let n=new Map(e.map(r=>[r.id,JSON.stringify(r.html)]));return t.replace(/_tmpl\$\d+/g,r=>n.get(r)??r)}var z={name:"imperative",contract:x,emit(t,e){let n=new Set,r=e?.templateClone!==!1?[]:void 0;return B=e?.partialTemplates===!0,{code:g(t,n,r),imports:Array.from(n),templates:r}}};function E(t){let e=t.split(/\r\n|\n|\r/),n=0;for(let i=0;i<e.length;i++)/[^ \t]/.test(e[i])&&(n=i);let r="";for(let i=0;i<e.length;i++){let o=e[i].replace(/\t/g," ");i!==0&&(o=o.replace(/^ +/,"")),i!==e.length-1&&(o=o.replace(/ +$/,"")),o&&(i!==n&&(o+=" "),r+=o)}return r}var V=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function ke(t){return t.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$\{/g,"\\${")}function F(t){return t.charAt(0).toUpperCase()+t.slice(1)}var I=class{constructor(e,n){this.holes=e;this.used=n}hole(e){return this.holes[e]??{code:"undefined",reactive:!1}}emit(e){let{code:n,imports:r,templates:i}=z.emit(e,{});for(let o of r)this.used.add(o);return J(n,i)}lowerRoot(e){let n=this.lowerChildren(e);return n.length===1?n[0]:{kind:"fragment",children:n}}lowerChildren(e){let n=[];for(let r=0;r<e.length;r++){let i=e[r];if(i.kind==="Element"||i.kind==="Component"){let c=i.attributes.find(s=>s.kind==="IfDirective");if(c&&c.kind==="IfDirective"){let s=r+1;s<e.length&&this.isBlankText(e[s])&&s++;let a=e[s],l=a&&(a.kind==="Element"||a.kind==="Component")&&a.attributes.some(d=>d.kind==="ElseDirective");n.push(this.lowerIf(i,c.hole,l?a:null)),l&&(r=s);continue}if(i.attributes.some(s=>s.kind==="EachDirective")){n.push(this.lowerEach(i));continue}}let o=this.lowerNode(i);o&&n.push(o)}return n}isBlankText(e){return e.kind==="Text"&&!e.raw&&E(e.value)===""}lowerNode(e){switch(e.kind){case"Text":{if(e.raw)return{kind:"text",value:e.value};let n=E(e.value);return n?{kind:"text",value:n}:null}case"Comment":return null;case"Expression":{let n=this.hole(e.hole);return{kind:"expr",code:n.code,reactive:n.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 n=e.attributes.find(r=>r.kind==="Attribute"&&r.name==="is");if(e.tag==="component"&&n&&n.value&&n.value.kind==="hole"){let r=e.attributes.filter(o=>o!==n),i=this.lowerComponent({...e,kind:"Component",tag:"Dynamic",tagHole:null,attributes:r});return i.props.unshift({name:"component",kind:"attr",expr:`() => (${this.hole(n.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 n=e.tagHole!=null?this.hole(e.tagHole).code:e.tag,r=this.lowerAttributes(e.attributes),{slots:i,rest:o}=this.partitionSlots(e.children);for(let[a,l]of i){let d=l.length===1?l[0]:{kind:"fragment",children:l},p={name:a,kind:"attr",expr:this.emit(d),jsxElement:!0},m=r.findIndex(v=>v.name===a);m>=0?r[m]=p:r.push(p)}let c=e.attributes.find(a=>a.kind==="LoadDirective"),s=c?S(`load:${c.strategy}`,c.modifier):void 0;return{kind:"component",name:n,props:r,children:this.lowerChildren(o),...s?{load:s}:{}}}partitionSlots(e){let n=new Map,r=[];for(let i of e){if(i.kind==="Element"||i.kind==="Component"){let o=i.attributes.find(c=>c.kind==="Attribute"&&c.name==="slot");if(o&&o.value&&o.value.kind==="static"){let c={...i,attributes:i.attributes.filter(l=>l!==o)},s=c.kind==="Element"?this.lowerElement(c):this.lowerComponent(c),a=n.get(o.value.value)??[];a.push(s),n.set(o.value.value,a);continue}}r.push(i)}return{slots:n,rest:r}}lowerIf(e,n,r){let i=this.hole(n),o=[{name:"when",kind:"attr",expr:i.code,reactive:i.reactive}];if(r){let s=this.stripAndLower(r,a=>a.kind==="ElseDirective");o.push({name:"fallback",kind:"attr",expr:this.emit(s),jsxElement:!0})}let c=this.stripAndLower(e,s=>s.kind==="IfDirective");return{kind:"component",name:"Show",props:o,children:[c]}}lowerEach(e){let n=e.attributes.find(a=>a.kind==="EachDirective");if(!n||n.kind!=="EachDirective")return this.lowerNode(e);let r=this.hole(n.hole),i=[{name:"each",kind:"attr",expr:r.code,reactive:r.reactive}];if(n.key){let a="static"in n.key?`(item) => item[${JSON.stringify(n.key.static)}]`:this.hole(n.key.hole).code;i.push({name:"by",kind:"attr",expr:a})}let o=this.itemArrow(e),c;if(o){let a={kind:"expr",code:o.body,reactive:o.bodyReactive},l=this.rebuildWithChildren(e,[a]);c=`(${o.params.join(", ")}) => (${this.emit(l)})`}else{let a=this.stripAndLower(e,l=>l.kind==="EachDirective");c=`() => (${this.emit(a)})`}return{kind:"component",name:"For",props:i,children:[{kind:"expr",code:c,reactive:!1}]}}itemArrow(e){let n=e.children.filter(r=>!this.isBlankText(r));return n.length!==1||n[0].kind!=="Expression"?null:this.hole(n[0].hole).arrow??null}stripAndLower(e,n){let r={...e,attributes:e.attributes.filter(i=>!n(i))};return r.kind==="Element"?this.lowerElement(r):this.lowerComponent(r)}rebuildWithChildren(e,n){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:n,static:!1}:{kind:"component",name:e.tag,props:r,children:n}}lowerAttributes(e){let n=[],r=[],i=!1,o=[],c=!1,s=[];for(let a of e)switch(a.kind){case"IfDirective":case"EachDirective":case"ElseDirective":break;case"Attribute":n.push(this.plainAttr(a));break;case"PropertyBinding":n.push({name:a.name,kind:"prop",expr:this.hole(a.hole).code,reactive:this.hole(a.hole).reactive});break;case"EventBinding":n.push(this.eventProp(a));break;case"RefBinding":n.push({name:"ref",kind:"ref",expr:this.hole(a.hole).code,reactive:this.hole(a.hole).reactive});break;case"Spread":n.push({name:"",kind:"spread",expr:this.hole(a.hole).code});break;case"ClassDirective":{let l=this.hole(a.hole);r.push(`${JSON.stringify(a.name)}: ${l.code}`),i=i||l.reactive;break}case"StyleDirective":{let l=this.hole(a.hole);o.push(`${JSON.stringify(a.name)}: ${l.code}`),c=c||l.reactive;break}case"BindDirective":n.push(...this.bindProps(a.name,this.hole(a.hole).code));break;case"UseDirective":{let l=a.name??(a.hole!=null?this.hole(a.hole).code:null);if(!l)break;s.push(a.name!=null&&a.hole!=null?`[${l}, () => (${this.hole(a.hole).code})]`:`[${l}]`);break}}return r.length>0&&n.push({name:"classList",kind:"attr",expr:`{ ${r.join(", ")} }`,reactive:i}),o.length>0&&n.push({name:"style",kind:"attr",expr:`{ ${o.join(", ")} }`,reactive:c}),s.length>0&&n.push({name:"use",kind:"attr",expr:`[${s.join(", ")}]`}),n}eventProp(e){let n=e.name.toLowerCase(),r=this.hole(e.hole).code;if(!(e.syntax==="colon"||e.modifiers.length>0))return{name:"on"+F(e.name),kind:"event",event:{name:n,delegated:V.has(n)},expr:r};let o=this.wrapHandler(r,e.modifiers),c=this.eventOptions(e.modifiers),s=c?`[${o}, ${c}]`:o;return{name:"on:"+n,kind:"attr",expr:s}}wrapHandler(e,n){let r=n.includes("self")?"if (e.target !== e.currentTarget) return; ":"",i=[];return n.includes("prevent")&&i.push("e.preventDefault();"),n.includes("stop")&&i.push("e.stopPropagation();"),!r&&i.length===0?e:`(e) => { ${r}${i.join(" ")} return (${e})(e); }`}eventOptions(e){let n=[];return e.includes("capture")&&n.push("capture: true"),e.includes("once")&&n.push("once: true"),e.includes("passive")&&n.push("passive: true"),n.length?`{ ${n.join(", ")} }`:null}bindProps(e,n){let r=e==="checked",i=r?"change":"input",o=r?"checked":"value";this.used.add("bindPair");let c=`bindPair(${n})`;return[{name:e,kind:"attr",expr:`${c}[0]()`,reactive:!0},{name:"on"+F(i),kind:"event",event:{name:i,delegated:V.has(i)},expr:`(e) => ${c}[1](e.target.${o})`}]}plainAttr(e){let n=e.value,r=e.name;e.name==="class"?r=n&&n.kind==="hole"&&this.hole(n.hole).object?"classList":"className":e.name==="html"&&(r="innerHTML");let o={name:r,kind:"attr"};if(n==null)return o.literal=!0,o;if(n.kind==="static")return o.literal=n.value,o;if(n.kind==="hole"){let a=this.hole(n.hole);return o.expr=a.code,o.reactive=a.reactive,o}let c=!1,s=n.parts.map(a=>{if("text"in a)return ke(a.text);let l=this.hole(a.hole);return c=c||l.reactive,"${"+l.code+"}"}).join("");return o.expr="`"+s+"`",o.reactive=c,o}};function Ne(t,e,n,r={}){let{root:i}=(0,U.parseTemplate)(t,{svg:r.svg});return new I(e,n).lowerRoot(i.children)}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"template.d.ts","sourceRoot":"","sources":["../../src/lower/template.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAML,KAAK,KAAK,EAGX,MAAM,yBAAyB,CAAC;AACjC,OAAO,KAAK,EAAE,MAAM,EAAsB,MAAM,gBAAgB,CAAC;AAKjE;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC3B,sFAAsF;IACtF,IAAI,EAAE,MAAM,CAAC;IACb,0EAA0E;IAC1E,QAAQ,EAAE,OAAO,CAAC;IAClB;;;;OAIG;IACH,KAAK,CAAC,EAAE;QAAE,MAAM,EAAE,MAAM,EAAE,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,OAAO,CAAA;KAAE,CAAC;IAClE,kFAAkF;IAClF,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;
|
|
1
|
+
{"version":3,"file":"template.d.ts","sourceRoot":"","sources":["../../src/lower/template.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAML,KAAK,KAAK,EAGX,MAAM,yBAAyB,CAAC;AACjC,OAAO,KAAK,EAAE,MAAM,EAAsB,MAAM,gBAAgB,CAAC;AAKjE;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC3B,sFAAsF;IACtF,IAAI,EAAE,MAAM,CAAC;IACb,0EAA0E;IAC1E,QAAQ,EAAE,OAAO,CAAC;IAClB;;;;OAIG;IACH,KAAK,CAAC,EAAE;QAAE,MAAM,EAAE,MAAM,EAAE,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,OAAO,CAAA;KAAE,CAAC;IAClE,kFAAkF;IAClF,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AA4bD;;;;GAIG;AACH,wBAAgB,aAAa,CAC3B,MAAM,EAAE,KAAK,EAAE,EACf,KAAK,EAAE,YAAY,EAAE,EACrB,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,EACjB,IAAI,GAAE;IAAE,GAAG,CAAC,EAAE,OAAO,CAAA;CAAO,GAC3B,MAAM,CAGR"}
|
package/dist/lower/template.js
CHANGED
|
@@ -368,15 +368,17 @@ class Lowerer {
|
|
|
368
368
|
opts.push('passive: true');
|
|
369
369
|
return opts.length ? `{ ${opts.join(', ')} }` : null;
|
|
370
370
|
}
|
|
371
|
-
/** `bind:value=${sig}` → `value={
|
|
371
|
+
/** `bind:value=${sig}` → `value={read()}` + `onInput={e => write(e…value)}`. */
|
|
372
372
|
bindProps(prop, sigCode) {
|
|
373
373
|
const checked = prop === 'checked';
|
|
374
374
|
const evt = checked ? 'change' : 'input';
|
|
375
375
|
const accessor = checked ? 'checked' : 'value';
|
|
376
|
-
//
|
|
377
|
-
//
|
|
378
|
-
//
|
|
379
|
-
const
|
|
376
|
+
// Through `bindPair`, so both spellings of a signal work here: the `[get, set]`
|
|
377
|
+
// pair from `createSignal` and the `{ get, set }` object from `signal`.
|
|
378
|
+
// Parenthesized because the expression may be more than an identifier
|
|
379
|
+
// (`[get, set] as const`, `props.model`, a call).
|
|
380
|
+
this.used.add('bindPair');
|
|
381
|
+
const sig = `bindPair(${sigCode})`;
|
|
380
382
|
return [
|
|
381
383
|
{ name: prop, kind: 'attr', expr: `${sig}[0]()`, reactive: true },
|
|
382
384
|
{
|
package/dist/lower/template.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{parseTemplate as de}from"@fluixi/template-parser";var F={kind:"eager"},U=["pointerdown","focusin","keydown"],y=new Set(["eager","idle","visible","interaction","media","never"]),u=class extends Error{};function T(t,e){let n=t.slice(5);if(!y.has(n))throw new u(`Unknown load strategy 'load:${n}'. Expected one of ${[...y].join(", ")}.`);switch(n){case"eager":return F;case"idle":return{kind:"idle"};case"never":return{kind:"never"};case"visible":return e?{kind:"visible",rootMargin:e}:{kind:"visible"};case"interaction":return{kind:"interaction",events:e?W(e):[...U]};case"media":if(!e)throw new u(`'load:media' needs a query, e.g. load:media="(min-width: 1024px)".`);return{kind:"media",query:e};default:throw new u(`Unhandled load strategy '${n}'.`)}}function W(t){let e=t.split(/[\s,]+/).map(n=>n.trim()).filter(Boolean);if(e.length===0)throw new u("'load:interaction' was given no event names.");return e}var q=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]),S={className:"class",htmlFor:"for"},G=new Set(["innerHTML","outerHTML","innerText","textContent","value","checked","selected","muted","defaultValue","defaultChecked","children","ref"]),K=new Set(["script","style","textarea","title"]);function R(t){return K.has(t)}function C(t){return S[t]??t}function h(t){return t.kind!=="attr"||t.expr!==void 0||t.name.includes(":")?!1:!G.has(t.name)}function $(t){if(!t.static||R(t.tag))return!1;for(let e of t.props)if(!h(e))return!1;for(let e of t.children)if(e.kind!=="text"&&!(e.kind==="element"&&$(e)))return!1;return!0}function f(t){let e=t.props.map(Z).filter(Boolean).join(""),n=`<${t.tag}${e}>`;return q.has(t.tag)?n:`${n}${t.children.map(Y).join("")}</${t.tag}>`}function Y(t){if(t.kind==="text")return Q(t.value);if(t.kind!=="element")throw new Error(`serializeStatic: unexpected ${t.kind}`);return f(t)}function Z(t){let e=S[t.name]??t.name,n=t.literal;return n===!0||n===void 0?` ${e}`:n===!1||n===null?"":` ${e}="${X(String(n))}"`}function X(t){return t.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function Q(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}var ee="<!--fx-->",te="<!--fx/-->";function N(t){return t.kind==="expr"||t.kind==="component"||t.kind==="control"}function ne(t){return t.kind==="text"||t.kind==="element"&&A(t)}function A(t){return t.svg||R(t.tag)||!t.props.every(e=>h(e))?!1:t.children.every(e=>ne(e)||N(e))}function k(t){if(N(t))return!0;let e=t.children;return e?e.some(k):!1}function P(t){for(let e=0;e<t.children.length;e++){let n=t.children[e];if(n.kind==="text"&&(n.value===""||t.children[e+1]?.kind==="text"))return!1}return t.children.every(e=>e.kind!=="element"||P(e))}function L(t){if(!A(t)||!P(t)||!t.children.some(k))return null;let e=[],n=[],r=0,i=c=>{let s=`_n$${r++}`;return e.push({ref:s,expr:c}),s};return o(t,"_el$"),{html:re(t),tag:t.tag,steps:e,holes:n};function o(c,s){let a=-1;c.children.forEach((d,p)=>{k(d)&&(a=p)});let l=null;for(let d=0;d<=a;d++){let p=c.children[d],m=l?`${l}.nextSibling`:`${s}.firstChild`;if(N(p)){let I=i(m),w=i(`holeEnd(${I})`);n.push({parentRef:s,startRef:I,endRef:w,node:p}),l=w;continue}let v=i(m);p.kind==="element"&&k(p)&&o(p,v),l=v}}}function re(t){return f(D(t)).split(j).join(ee+te)}function D(t){return{...t,children:t.children.map(e=>N(e)?{kind:"text",value:j}:e.kind==="element"?D(e):e)}}var j="\0fx-hole\0";var b={version:1,module:"@fluixi/dom",symbols:["createNativeElement","insert","spread","setAttribute","delegateEvents","createComponent","createMemo","untrack","Show","For","Index","Switch","Match","Dynamic","ErrorBoundary"]},ge={version:2,module:"@fluixi/dom",symbols:[...b.symbols,"template","cloneTemplate","walk"]};var ie={show:"Show",for:"For",index:"Index",switch:"Switch",match:"Match",dynamic:"Dynamic",errorBoundary:"ErrorBoundary",suspense:"Suspense"};function oe(t){return/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(t)}function _(t){return oe(t)?t:JSON.stringify(t)}function se(t){return t.expr!==void 0?t.reactive?`() => (${t.expr})`:`(${t.expr})`:JSON.stringify(t.literal??!0)}function ae(t){return t.expr!==void 0?`(${t.expr})`:JSON.stringify(t.literal??!0)}function le(t,e,n){let r=t.filter(s=>s.kind==="spread"),o=t.filter(s=>s.kind!=="spread").map(s=>`${_(s.name)}: ${se(s)}`);e!=null&&o.push(`children: ${e}`);let c=`{ ${o.join(", ")} }`;return r.length>0?(n.add("mergeProps"),`mergeProps(${r.map(s=>s.expr).join(", ")}, ${c})`):c}function M(t,e,n){return t.length===1?g(t[0],e,n):`[${t.map(r=>g(r,e,n)).join(", ")}]`}function O(t,e,n,r,i){r.add("createMemo"),r.add("createComponent");let o=e.filter(l=>l.kind==="spread"),s=e.filter(l=>l.kind!=="spread").map(l=>`get ${_(l.name)}() { return ${ae(l)}; }`);n.length>0&&s.push(`get children() { return ${M(n,r,i)}; }`);let a=`{ ${s.join(", ")} }`;return o.length>0&&(r.add("mergeProps"),a=`mergeProps(${o.map(l=>l.expr).join(", ")}, ${a})`),`createMemo(() => createComponent(${t}, ${a}))`}function ce(t,e){let n=C(e.name),r=e.literal;return r===!0||r===void 0?`${t}.setAttribute(${JSON.stringify(n)}, "");`:r===!1||r===null?"":`${t}.setAttribute(${JSON.stringify(n)}, ${JSON.stringify(String(r))});`}var H=!1;function g(t,e,n){switch(t.kind){case"text":return JSON.stringify(t.value);case"expr":return t.reactive?`() => (${t.code})`:`(${t.code})`;case"fragment":return t.children.length===0?"null":M(t.children,e,n);case"component":return O(t.name,t.props,t.children,e,n);case"control":{let r=ie[t.control]??t.control;return e.add(r),O(r,t.props,t.children,e,n)}case"element":{if(n&&t.static&&!t.svg&&$(t)){e.add("templateNode");let s=`_tmpl$${n.length}`;return n.push({id:s,html:f(t),tag:t.tag,svg:!1}),`templateNode(${s}, ${JSON.stringify(t.tag)})`}if(n&&H){let s=L(t);if(s){e.add("templateNode"),e.add("insert"),e.add("holeEnd"),e.add("holeContent"),e.add("holeScope");let a=`_tmpl$${n.length}`;n.push({id:a,html:s.html,tag:s.tag,svg:!1});let l=[`const _el$ = templateNode(${a}, ${JSON.stringify(s.tag)}, true);`];for(let d of s.steps)l.push(`const ${d.ref} = ${d.expr};`);for(let d of s.holes)l.push(`insert(${d.parentRef}, holeScope(${d.startRef}, () => (${g(d.node,e,n)})), ${d.endRef}, holeContent(${d.startRef}, ${d.endRef}));`);return l.push("return _el$;"),`(() => { ${l.join(" ")} })()`}}e.add("createNativeElement");let r=JSON.stringify(t.tag),i="_el$",o=[],c=t.svg?`${r}, true`:r;if(o.push(`const ${i} = createNativeElement(${c});`),t.props.length>0)if(!t.svg&&t.props.every(h))for(let s of t.props)o.push(ce(i,s));else{e.add("spread");let s=t.svg?", isSVG: true":"";o.push(`spread({ element: ${i}, props: ${le(t.props,null,e)}${s} });`)}for(let s of t.children){e.add("insert");let a=g(s,e,n),l=s.kind==="expr"&&s.reactive||s.kind==="component"||s.kind==="control";o.push(l?`insert(${i}, ${a}, null);`:`insert(${i}, ${a});`)}return o.push(`return ${i};`),`(() => { ${o.join(" ")} })()`}}}function B(t,e){if(!e||e.length===0)return t;let n=new Map(e.map(r=>[r.id,JSON.stringify(r.html)]));return t.replace(/_tmpl\$\d+/g,r=>n.get(r)??r)}var J={name:"imperative",contract:b,emit(t,e){let n=new Set,r=e?.templateClone!==!1?[]:void 0;return H=e?.partialTemplates===!0,{code:g(t,n,r),imports:Array.from(n),templates:r}}};function x(t){let e=t.split(/\r\n|\n|\r/),n=0;for(let i=0;i<e.length;i++)/[^ \t]/.test(e[i])&&(n=i);let r="";for(let i=0;i<e.length;i++){let o=e[i].replace(/\t/g," ");i!==0&&(o=o.replace(/^ +/,"")),i!==e.length-1&&(o=o.replace(/ +$/,"")),o&&(i!==n&&(o+=" "),r+=o)}return r}var z=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function pe(t){return t.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$\{/g,"\\${")}function V(t){return t.charAt(0).toUpperCase()+t.slice(1)}var E=class{constructor(e,n){this.holes=e;this.used=n}hole(e){return this.holes[e]??{code:"undefined",reactive:!1}}emit(e){let{code:n,imports:r,templates:i}=J.emit(e,{});for(let o of r)this.used.add(o);return B(n,i)}lowerRoot(e){let n=this.lowerChildren(e);return n.length===1?n[0]:{kind:"fragment",children:n}}lowerChildren(e){let n=[];for(let r=0;r<e.length;r++){let i=e[r];if(i.kind==="Element"||i.kind==="Component"){let c=i.attributes.find(s=>s.kind==="IfDirective");if(c&&c.kind==="IfDirective"){let s=r+1;s<e.length&&this.isBlankText(e[s])&&s++;let a=e[s],l=a&&(a.kind==="Element"||a.kind==="Component")&&a.attributes.some(d=>d.kind==="ElseDirective");n.push(this.lowerIf(i,c.hole,l?a:null)),l&&(r=s);continue}if(i.attributes.some(s=>s.kind==="EachDirective")){n.push(this.lowerEach(i));continue}}let o=this.lowerNode(i);o&&n.push(o)}return n}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 n=x(e.value);return n?{kind:"text",value:n}:null}case"Comment":return null;case"Expression":{let n=this.hole(e.hole);return{kind:"expr",code:n.code,reactive:n.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 n=e.attributes.find(r=>r.kind==="Attribute"&&r.name==="is");if(e.tag==="component"&&n&&n.value&&n.value.kind==="hole"){let r=e.attributes.filter(o=>o!==n),i=this.lowerComponent({...e,kind:"Component",tag:"Dynamic",tagHole:null,attributes:r});return i.props.unshift({name:"component",kind:"attr",expr:`() => (${this.hole(n.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 n=e.tagHole!=null?this.hole(e.tagHole).code:e.tag,r=this.lowerAttributes(e.attributes),{slots:i,rest:o}=this.partitionSlots(e.children);for(let[a,l]of i){let d=l.length===1?l[0]:{kind:"fragment",children:l},p={name:a,kind:"attr",expr:this.emit(d),jsxElement:!0},m=r.findIndex(v=>v.name===a);m>=0?r[m]=p:r.push(p)}let c=e.attributes.find(a=>a.kind==="LoadDirective"),s=c?T(`load:${c.strategy}`,c.modifier):void 0;return{kind:"component",name:n,props:r,children:this.lowerChildren(o),...s?{load:s}:{}}}partitionSlots(e){let n=new Map,r=[];for(let i of e){if(i.kind==="Element"||i.kind==="Component"){let o=i.attributes.find(c=>c.kind==="Attribute"&&c.name==="slot");if(o&&o.value&&o.value.kind==="static"){let c={...i,attributes:i.attributes.filter(l=>l!==o)},s=c.kind==="Element"?this.lowerElement(c):this.lowerComponent(c),a=n.get(o.value.value)??[];a.push(s),n.set(o.value.value,a);continue}}r.push(i)}return{slots:n,rest:r}}lowerIf(e,n,r){let i=this.hole(n),o=[{name:"when",kind:"attr",expr:i.code,reactive:i.reactive}];if(r){let s=this.stripAndLower(r,a=>a.kind==="ElseDirective");o.push({name:"fallback",kind:"attr",expr:this.emit(s),jsxElement:!0})}let c=this.stripAndLower(e,s=>s.kind==="IfDirective");return{kind:"component",name:"Show",props:o,children:[c]}}lowerEach(e){let n=e.attributes.find(a=>a.kind==="EachDirective");if(!n||n.kind!=="EachDirective")return this.lowerNode(e);let r=this.hole(n.hole),i=[{name:"each",kind:"attr",expr:r.code,reactive:r.reactive}];if(n.key){let a="static"in n.key?`(item) => item[${JSON.stringify(n.key.static)}]`:this.hole(n.key.hole).code;i.push({name:"by",kind:"attr",expr:a})}let o=this.itemArrow(e),c;if(o){let a={kind:"expr",code:o.body,reactive:o.bodyReactive},l=this.rebuildWithChildren(e,[a]);c=`(${o.params.join(", ")}) => (${this.emit(l)})`}else{let a=this.stripAndLower(e,l=>l.kind==="EachDirective");c=`() => (${this.emit(a)})`}return{kind:"component",name:"For",props:i,children:[{kind:"expr",code:c,reactive:!1}]}}itemArrow(e){let n=e.children.filter(r=>!this.isBlankText(r));return n.length!==1||n[0].kind!=="Expression"?null:this.hole(n[0].hole).arrow??null}stripAndLower(e,n){let r={...e,attributes:e.attributes.filter(i=>!n(i))};return r.kind==="Element"?this.lowerElement(r):this.lowerComponent(r)}rebuildWithChildren(e,n){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:n,static:!1}:{kind:"component",name:e.tag,props:r,children:n}}lowerAttributes(e){let n=[],r=[],i=!1,o=[],c=!1,s=[];for(let a of e)switch(a.kind){case"IfDirective":case"EachDirective":case"ElseDirective":break;case"Attribute":n.push(this.plainAttr(a));break;case"PropertyBinding":n.push({name:a.name,kind:"prop",expr:this.hole(a.hole).code,reactive:this.hole(a.hole).reactive});break;case"EventBinding":n.push(this.eventProp(a));break;case"RefBinding":n.push({name:"ref",kind:"ref",expr:this.hole(a.hole).code,reactive:this.hole(a.hole).reactive});break;case"Spread":n.push({name:"",kind:"spread",expr:this.hole(a.hole).code});break;case"ClassDirective":{let l=this.hole(a.hole);r.push(`${JSON.stringify(a.name)}: ${l.code}`),i=i||l.reactive;break}case"StyleDirective":{let l=this.hole(a.hole);o.push(`${JSON.stringify(a.name)}: ${l.code}`),c=c||l.reactive;break}case"BindDirective":n.push(...this.bindProps(a.name,this.hole(a.hole).code));break;case"UseDirective":{let l=a.name??(a.hole!=null?this.hole(a.hole).code:null);if(!l)break;s.push(a.name!=null&&a.hole!=null?`[${l}, () => (${this.hole(a.hole).code})]`:`[${l}]`);break}}return r.length>0&&n.push({name:"classList",kind:"attr",expr:`{ ${r.join(", ")} }`,reactive:i}),o.length>0&&n.push({name:"style",kind:"attr",expr:`{ ${o.join(", ")} }`,reactive:c}),s.length>0&&n.push({name:"use",kind:"attr",expr:`[${s.join(", ")}]`}),n}eventProp(e){let n=e.name.toLowerCase(),r=this.hole(e.hole).code;if(!(e.syntax==="colon"||e.modifiers.length>0))return{name:"on"+V(e.name),kind:"event",event:{name:n,delegated:z.has(n)},expr:r};let o=this.wrapHandler(r,e.modifiers),c=this.eventOptions(e.modifiers),s=c?`[${o}, ${c}]`:o;return{name:"on:"+n,kind:"attr",expr:s}}wrapHandler(e,n){let r=n.includes("self")?"if (e.target !== e.currentTarget) return; ":"",i=[];return n.includes("prevent")&&i.push("e.preventDefault();"),n.includes("stop")&&i.push("e.stopPropagation();"),!r&&i.length===0?e:`(e) => { ${r}${i.join(" ")} return (${e})(e); }`}eventOptions(e){let n=[];return e.includes("capture")&&n.push("capture: true"),e.includes("once")&&n.push("once: true"),e.includes("passive")&&n.push("passive: true"),n.length?`{ ${n.join(", ")} }`:null}bindProps(e,n){let r=e==="checked",i=r?"change":"input",o=r?"checked":"value",c=`(${n})`;return[{name:e,kind:"attr",expr:`${c}[0]()`,reactive:!0},{name:"on"+V(i),kind:"event",event:{name:i,delegated:z.has(i)},expr:`(e) => ${c}[1](e.target.${o})`}]}plainAttr(e){let n=e.value,r=e.name;e.name==="class"?r=n&&n.kind==="hole"&&this.hole(n.hole).object?"classList":"className":e.name==="html"&&(r="innerHTML");let o={name:r,kind:"attr"};if(n==null)return o.literal=!0,o;if(n.kind==="static")return o.literal=n.value,o;if(n.kind==="hole"){let a=this.hole(n.hole);return o.expr=a.code,o.reactive=a.reactive,o}let c=!1,s=n.parts.map(a=>{if("text"in a)return pe(a.text);let l=this.hole(a.hole);return c=c||l.reactive,"${"+l.code+"}"}).join("");return o.expr="`"+s+"`",o.reactive=c,o}};function ye(t,e,n,r={}){let{root:i}=de(t,{svg:r.svg});return new E(e,n).lowerRoot(i.children)}export{ye as lowerTemplate};
|
|
1
|
+
import{parseTemplate as de}from"@fluixi/template-parser";var F={kind:"eager"},U=["pointerdown","focusin","keydown"],y=new Set(["eager","idle","visible","interaction","media","never"]),u=class extends Error{};function T(t,e){let n=t.slice(5);if(!y.has(n))throw new u(`Unknown load strategy 'load:${n}'. Expected one of ${[...y].join(", ")}.`);switch(n){case"eager":return F;case"idle":return{kind:"idle"};case"never":return{kind:"never"};case"visible":return e?{kind:"visible",rootMargin:e}:{kind:"visible"};case"interaction":return{kind:"interaction",events:e?W(e):[...U]};case"media":if(!e)throw new u(`'load:media' needs a query, e.g. load:media="(min-width: 1024px)".`);return{kind:"media",query:e};default:throw new u(`Unhandled load strategy '${n}'.`)}}function W(t){let e=t.split(/[\s,]+/).map(n=>n.trim()).filter(Boolean);if(e.length===0)throw new u("'load:interaction' was given no event names.");return e}var q=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]),S={className:"class",htmlFor:"for"},G=new Set(["innerHTML","outerHTML","innerText","textContent","value","checked","selected","muted","defaultValue","defaultChecked","children","ref"]),K=new Set(["script","style","textarea","title"]);function R(t){return K.has(t)}function C(t){return S[t]??t}function h(t){return t.kind!=="attr"||t.expr!==void 0||t.name.includes(":")?!1:!G.has(t.name)}function $(t){if(!t.static||R(t.tag))return!1;for(let e of t.props)if(!h(e))return!1;for(let e of t.children)if(e.kind!=="text"&&!(e.kind==="element"&&$(e)))return!1;return!0}function f(t){let e=t.props.map(Z).filter(Boolean).join(""),n=`<${t.tag}${e}>`;return q.has(t.tag)?n:`${n}${t.children.map(Y).join("")}</${t.tag}>`}function Y(t){if(t.kind==="text")return Q(t.value);if(t.kind!=="element")throw new Error(`serializeStatic: unexpected ${t.kind}`);return f(t)}function Z(t){let e=S[t.name]??t.name,n=t.literal;return n===!0||n===void 0?` ${e}`:n===!1||n===null?"":` ${e}="${X(String(n))}"`}function X(t){return t.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function Q(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}var ee="<!--fx-->",te="<!--fx/-->";function N(t){return t.kind==="expr"||t.kind==="component"||t.kind==="control"}function ne(t){return t.kind==="text"||t.kind==="element"&&P(t)}function P(t){return t.svg||R(t.tag)||!t.props.every(e=>h(e))?!1:t.children.every(e=>ne(e)||N(e))}function k(t){if(N(t))return!0;let e=t.children;return e?e.some(k):!1}function A(t){for(let e=0;e<t.children.length;e++){let n=t.children[e];if(n.kind==="text"&&(n.value===""||t.children[e+1]?.kind==="text"))return!1}return t.children.every(e=>e.kind!=="element"||A(e))}function L(t){if(!P(t)||!A(t)||!t.children.some(k))return null;let e=[],n=[],r=0,i=c=>{let s=`_n$${r++}`;return e.push({ref:s,expr:c}),s};return o(t,"_el$"),{html:re(t),tag:t.tag,steps:e,holes:n};function o(c,s){let a=-1;c.children.forEach((d,p)=>{k(d)&&(a=p)});let l=null;for(let d=0;d<=a;d++){let p=c.children[d],m=l?`${l}.nextSibling`:`${s}.firstChild`;if(N(p)){let I=i(m),w=i(`holeEnd(${I})`);n.push({parentRef:s,startRef:I,endRef:w,node:p}),l=w;continue}let v=i(m);p.kind==="element"&&k(p)&&o(p,v),l=v}}}function re(t){return f(D(t)).split(j).join(ee+te)}function D(t){return{...t,children:t.children.map(e=>N(e)?{kind:"text",value:j}:e.kind==="element"?D(e):e)}}var j="\0fx-hole\0";var b={version:1,module:"@fluixi/dom",symbols:["createNativeElement","insert","spread","setAttribute","delegateEvents","createComponent","createMemo","untrack","Show","For","Index","Switch","Match","Dynamic","ErrorBoundary"]},ge={version:2,module:"@fluixi/dom",symbols:[...b.symbols,"template","cloneTemplate","walk"]};var ie={show:"Show",for:"For",index:"Index",switch:"Switch",match:"Match",dynamic:"Dynamic",errorBoundary:"ErrorBoundary",suspense:"Suspense"};function oe(t){return/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(t)}function _(t){return oe(t)?t:JSON.stringify(t)}function se(t){return t.expr!==void 0?t.reactive?`() => (${t.expr})`:`(${t.expr})`:JSON.stringify(t.literal??!0)}function ae(t){return t.expr!==void 0?`(${t.expr})`:JSON.stringify(t.literal??!0)}function le(t,e,n){let r=t.filter(s=>s.kind==="spread"),o=t.filter(s=>s.kind!=="spread").map(s=>`${_(s.name)}: ${se(s)}`);e!=null&&o.push(`children: ${e}`);let c=`{ ${o.join(", ")} }`;return r.length>0?(n.add("mergeProps"),`mergeProps(${r.map(s=>s.expr).join(", ")}, ${c})`):c}function M(t,e,n){return t.length===1?g(t[0],e,n):`[${t.map(r=>g(r,e,n)).join(", ")}]`}function O(t,e,n,r,i){r.add("createMemo"),r.add("createComponent");let o=e.filter(l=>l.kind==="spread"),s=e.filter(l=>l.kind!=="spread").map(l=>`get ${_(l.name)}() { return ${ae(l)}; }`);n.length>0&&s.push(`get children() { return ${M(n,r,i)}; }`);let a=`{ ${s.join(", ")} }`;return o.length>0&&(r.add("mergeProps"),a=`mergeProps(${o.map(l=>l.expr).join(", ")}, ${a})`),`createMemo(() => createComponent(${t}, ${a}))`}function ce(t,e){let n=C(e.name),r=e.literal;return r===!0||r===void 0?`${t}.setAttribute(${JSON.stringify(n)}, "");`:r===!1||r===null?"":`${t}.setAttribute(${JSON.stringify(n)}, ${JSON.stringify(String(r))});`}var H=!1;function g(t,e,n){switch(t.kind){case"text":return JSON.stringify(t.value);case"expr":return t.reactive?`() => (${t.code})`:`(${t.code})`;case"fragment":return t.children.length===0?"null":M(t.children,e,n);case"component":return O(t.name,t.props,t.children,e,n);case"control":{let r=ie[t.control]??t.control;return e.add(r),O(r,t.props,t.children,e,n)}case"element":{if(n&&t.static&&!t.svg&&$(t)){e.add("templateNode");let s=`_tmpl$${n.length}`;return n.push({id:s,html:f(t),tag:t.tag,svg:!1}),`templateNode(${s}, ${JSON.stringify(t.tag)})`}if(n&&H){let s=L(t);if(s){e.add("templateNode"),e.add("insert"),e.add("holeEnd"),e.add("holeContent"),e.add("holeScope");let a=`_tmpl$${n.length}`;n.push({id:a,html:s.html,tag:s.tag,svg:!1});let l=[`const _el$ = templateNode(${a}, ${JSON.stringify(s.tag)}, true);`];for(let d of s.steps)l.push(`const ${d.ref} = ${d.expr};`);for(let d of s.holes)l.push(`insert(${d.parentRef}, holeScope(${d.startRef}, () => (${g(d.node,e,n)})), ${d.endRef}, holeContent(${d.startRef}, ${d.endRef}));`);return l.push("return _el$;"),`(() => { ${l.join(" ")} })()`}}e.add("createNativeElement");let r=JSON.stringify(t.tag),i="_el$",o=[],c=t.svg?`${r}, true`:r;if(o.push(`const ${i} = createNativeElement(${c});`),t.props.length>0)if(!t.svg&&t.props.every(h))for(let s of t.props)o.push(ce(i,s));else{e.add("spread");let s=t.svg?", isSVG: true":"";o.push(`spread({ element: ${i}, props: ${le(t.props,null,e)}${s} });`)}for(let s of t.children){e.add("insert");let a=g(s,e,n),l=s.kind==="expr"&&s.reactive||s.kind==="component"||s.kind==="control";o.push(l?`insert(${i}, ${a}, null);`:`insert(${i}, ${a});`)}return o.push(`return ${i};`),`(() => { ${o.join(" ")} })()`}}}function B(t,e){if(!e||e.length===0)return t;let n=new Map(e.map(r=>[r.id,JSON.stringify(r.html)]));return t.replace(/_tmpl\$\d+/g,r=>n.get(r)??r)}var J={name:"imperative",contract:b,emit(t,e){let n=new Set,r=e?.templateClone!==!1?[]:void 0;return H=e?.partialTemplates===!0,{code:g(t,n,r),imports:Array.from(n),templates:r}}};function x(t){let e=t.split(/\r\n|\n|\r/),n=0;for(let i=0;i<e.length;i++)/[^ \t]/.test(e[i])&&(n=i);let r="";for(let i=0;i<e.length;i++){let o=e[i].replace(/\t/g," ");i!==0&&(o=o.replace(/^ +/,"")),i!==e.length-1&&(o=o.replace(/ +$/,"")),o&&(i!==n&&(o+=" "),r+=o)}return r}var z=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function pe(t){return t.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$\{/g,"\\${")}function V(t){return t.charAt(0).toUpperCase()+t.slice(1)}var E=class{constructor(e,n){this.holes=e;this.used=n}hole(e){return this.holes[e]??{code:"undefined",reactive:!1}}emit(e){let{code:n,imports:r,templates:i}=J.emit(e,{});for(let o of r)this.used.add(o);return B(n,i)}lowerRoot(e){let n=this.lowerChildren(e);return n.length===1?n[0]:{kind:"fragment",children:n}}lowerChildren(e){let n=[];for(let r=0;r<e.length;r++){let i=e[r];if(i.kind==="Element"||i.kind==="Component"){let c=i.attributes.find(s=>s.kind==="IfDirective");if(c&&c.kind==="IfDirective"){let s=r+1;s<e.length&&this.isBlankText(e[s])&&s++;let a=e[s],l=a&&(a.kind==="Element"||a.kind==="Component")&&a.attributes.some(d=>d.kind==="ElseDirective");n.push(this.lowerIf(i,c.hole,l?a:null)),l&&(r=s);continue}if(i.attributes.some(s=>s.kind==="EachDirective")){n.push(this.lowerEach(i));continue}}let o=this.lowerNode(i);o&&n.push(o)}return n}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 n=x(e.value);return n?{kind:"text",value:n}:null}case"Comment":return null;case"Expression":{let n=this.hole(e.hole);return{kind:"expr",code:n.code,reactive:n.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 n=e.attributes.find(r=>r.kind==="Attribute"&&r.name==="is");if(e.tag==="component"&&n&&n.value&&n.value.kind==="hole"){let r=e.attributes.filter(o=>o!==n),i=this.lowerComponent({...e,kind:"Component",tag:"Dynamic",tagHole:null,attributes:r});return i.props.unshift({name:"component",kind:"attr",expr:`() => (${this.hole(n.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 n=e.tagHole!=null?this.hole(e.tagHole).code:e.tag,r=this.lowerAttributes(e.attributes),{slots:i,rest:o}=this.partitionSlots(e.children);for(let[a,l]of i){let d=l.length===1?l[0]:{kind:"fragment",children:l},p={name:a,kind:"attr",expr:this.emit(d),jsxElement:!0},m=r.findIndex(v=>v.name===a);m>=0?r[m]=p:r.push(p)}let c=e.attributes.find(a=>a.kind==="LoadDirective"),s=c?T(`load:${c.strategy}`,c.modifier):void 0;return{kind:"component",name:n,props:r,children:this.lowerChildren(o),...s?{load:s}:{}}}partitionSlots(e){let n=new Map,r=[];for(let i of e){if(i.kind==="Element"||i.kind==="Component"){let o=i.attributes.find(c=>c.kind==="Attribute"&&c.name==="slot");if(o&&o.value&&o.value.kind==="static"){let c={...i,attributes:i.attributes.filter(l=>l!==o)},s=c.kind==="Element"?this.lowerElement(c):this.lowerComponent(c),a=n.get(o.value.value)??[];a.push(s),n.set(o.value.value,a);continue}}r.push(i)}return{slots:n,rest:r}}lowerIf(e,n,r){let i=this.hole(n),o=[{name:"when",kind:"attr",expr:i.code,reactive:i.reactive}];if(r){let s=this.stripAndLower(r,a=>a.kind==="ElseDirective");o.push({name:"fallback",kind:"attr",expr:this.emit(s),jsxElement:!0})}let c=this.stripAndLower(e,s=>s.kind==="IfDirective");return{kind:"component",name:"Show",props:o,children:[c]}}lowerEach(e){let n=e.attributes.find(a=>a.kind==="EachDirective");if(!n||n.kind!=="EachDirective")return this.lowerNode(e);let r=this.hole(n.hole),i=[{name:"each",kind:"attr",expr:r.code,reactive:r.reactive}];if(n.key){let a="static"in n.key?`(item) => item[${JSON.stringify(n.key.static)}]`:this.hole(n.key.hole).code;i.push({name:"by",kind:"attr",expr:a})}let o=this.itemArrow(e),c;if(o){let a={kind:"expr",code:o.body,reactive:o.bodyReactive},l=this.rebuildWithChildren(e,[a]);c=`(${o.params.join(", ")}) => (${this.emit(l)})`}else{let a=this.stripAndLower(e,l=>l.kind==="EachDirective");c=`() => (${this.emit(a)})`}return{kind:"component",name:"For",props:i,children:[{kind:"expr",code:c,reactive:!1}]}}itemArrow(e){let n=e.children.filter(r=>!this.isBlankText(r));return n.length!==1||n[0].kind!=="Expression"?null:this.hole(n[0].hole).arrow??null}stripAndLower(e,n){let r={...e,attributes:e.attributes.filter(i=>!n(i))};return r.kind==="Element"?this.lowerElement(r):this.lowerComponent(r)}rebuildWithChildren(e,n){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:n,static:!1}:{kind:"component",name:e.tag,props:r,children:n}}lowerAttributes(e){let n=[],r=[],i=!1,o=[],c=!1,s=[];for(let a of e)switch(a.kind){case"IfDirective":case"EachDirective":case"ElseDirective":break;case"Attribute":n.push(this.plainAttr(a));break;case"PropertyBinding":n.push({name:a.name,kind:"prop",expr:this.hole(a.hole).code,reactive:this.hole(a.hole).reactive});break;case"EventBinding":n.push(this.eventProp(a));break;case"RefBinding":n.push({name:"ref",kind:"ref",expr:this.hole(a.hole).code,reactive:this.hole(a.hole).reactive});break;case"Spread":n.push({name:"",kind:"spread",expr:this.hole(a.hole).code});break;case"ClassDirective":{let l=this.hole(a.hole);r.push(`${JSON.stringify(a.name)}: ${l.code}`),i=i||l.reactive;break}case"StyleDirective":{let l=this.hole(a.hole);o.push(`${JSON.stringify(a.name)}: ${l.code}`),c=c||l.reactive;break}case"BindDirective":n.push(...this.bindProps(a.name,this.hole(a.hole).code));break;case"UseDirective":{let l=a.name??(a.hole!=null?this.hole(a.hole).code:null);if(!l)break;s.push(a.name!=null&&a.hole!=null?`[${l}, () => (${this.hole(a.hole).code})]`:`[${l}]`);break}}return r.length>0&&n.push({name:"classList",kind:"attr",expr:`{ ${r.join(", ")} }`,reactive:i}),o.length>0&&n.push({name:"style",kind:"attr",expr:`{ ${o.join(", ")} }`,reactive:c}),s.length>0&&n.push({name:"use",kind:"attr",expr:`[${s.join(", ")}]`}),n}eventProp(e){let n=e.name.toLowerCase(),r=this.hole(e.hole).code;if(!(e.syntax==="colon"||e.modifiers.length>0))return{name:"on"+V(e.name),kind:"event",event:{name:n,delegated:z.has(n)},expr:r};let o=this.wrapHandler(r,e.modifiers),c=this.eventOptions(e.modifiers),s=c?`[${o}, ${c}]`:o;return{name:"on:"+n,kind:"attr",expr:s}}wrapHandler(e,n){let r=n.includes("self")?"if (e.target !== e.currentTarget) return; ":"",i=[];return n.includes("prevent")&&i.push("e.preventDefault();"),n.includes("stop")&&i.push("e.stopPropagation();"),!r&&i.length===0?e:`(e) => { ${r}${i.join(" ")} return (${e})(e); }`}eventOptions(e){let n=[];return e.includes("capture")&&n.push("capture: true"),e.includes("once")&&n.push("once: true"),e.includes("passive")&&n.push("passive: true"),n.length?`{ ${n.join(", ")} }`:null}bindProps(e,n){let r=e==="checked",i=r?"change":"input",o=r?"checked":"value";this.used.add("bindPair");let c=`bindPair(${n})`;return[{name:e,kind:"attr",expr:`${c}[0]()`,reactive:!0},{name:"on"+V(i),kind:"event",event:{name:i,delegated:z.has(i)},expr:`(e) => ${c}[1](e.target.${o})`}]}plainAttr(e){let n=e.value,r=e.name;e.name==="class"?r=n&&n.kind==="hole"&&this.hole(n.hole).object?"classList":"className":e.name==="html"&&(r="innerHTML");let o={name:r,kind:"attr"};if(n==null)return o.literal=!0,o;if(n.kind==="static")return o.literal=n.value,o;if(n.kind==="hole"){let a=this.hole(n.hole);return o.expr=a.code,o.reactive=a.reactive,o}let c=!1,s=n.parts.map(a=>{if("text"in a)return pe(a.text);let l=this.hole(a.hole);return c=c||l.reactive,"${"+l.code+"}"}).join("");return o.expr="`"+s+"`",o.reactive=c,o}};function ye(t,e,n,r={}){let{root:i}=de(t,{svg:r.svg});return new E(e,n).lowerRoot(i.children)}export{ye as lowerTemplate};
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
"use strict";var C=Object.defineProperty;var
|
|
1
|
+
"use strict";var C=Object.defineProperty;var _=Object.getOwnPropertyDescriptor;var D=Object.getOwnPropertyNames;var P=Object.prototype.hasOwnProperty;var k=(e,t)=>{for(var r in t)C(e,r,{get:t[r],enumerable:!0})},A=(e,t,r,c)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of D(t))!P.call(e,n)&&n!==r&&C(e,n,{get:()=>t[n],enumerable:!(c=_(t,n))||c.enumerable});return e};var B=e=>A(C({},"__esModule",{value:!0}),e);var H={};k(H,{componentGlobals:()=>V});module.exports=B(H);var G=e=>"components"in e;function S(e,t){return e.replace(/\$(\d+)/g,(r,c)=>t[Number(c)]??r)}function W(e,t){return typeof t=="string"?{module:t,export:e}:t}function $(e=[]){let t=new Map,r=[],c=[];for(let s of e){if(!G(s)){c.push(s);continue}for(let[i,a]of Object.entries(s.components)){let u=W(i,a),m=t.get(i);if(m&&m.module!==u.module){let d=r.find(R=>R.name===i);d?d.modules.push(u.module):r.push({name:i,modules:[m.module,u.module]});continue}t.set(i,u)}}let n=new Map;return{resolve:s=>{if(n.has(s))return n.get(s);let i=t.get(s);if(!i)for(let a of c){let u=s.match(new RegExp(a.match.source,a.match.flags.replace("g","")));if(u){i={module:S(a.module,u),export:a.export?S(a.export,u):s};break}}return n.set(s,i),i},names:()=>[...t.keys()],conflicts:()=>r}}var b=["Show","For","Index","Switch","Match","Portal","Dynamic","ErrorBoundary"],M=["Suspense","SuspenseList","Await"],E=["Router","Outlet","Redirect","Link"],J=[...b,...M,...E],Q=new Set([...b,...M]);function I(e={}){let{controlFlowModule:t="@fluixi/dom",coreModule:r="@fluixi/core",routerModule:c="@fluixi/core/router-next"}=e,n={};for(let p of b)n[p]=t;for(let p of M)n[p]=r;for(let p of E)n[p]=c;return n}var f="@fluixi/reactive/signal",v={$signal:{export:"signal",module:f,returns:"signal-handle"},$memo:{export:"memo",module:f,returns:"memo-handle"},$effect:{export:"effect",module:f,returns:"effect"},$store:{export:"store",module:"@fluixi/reactive/store",returns:"store-handle"},$resource:{export:"resource",module:f,returns:"resource-handle"},$selector:{export:"createSelector",module:f,returns:"memo-accessor"},$deferred:{export:"createDeferred",module:f,returns:"memo-accessor"},$untrack:{export:"untrack",module:f,returns:"plain"},$untrackStore:{export:"untrackStore",module:"@fluixi/reactive/store",returns:"plain"}};var Y=new RegExp(`\\$(?:${Object.keys(v).map(e=>e.slice(1)).join("|")})\\s*\\(`);var F=new Set(["Text","Image","Option","Audio","Comment","Range","Selection","Notification","Event","Document","Element","Node","Screen","Window","Location","History","Storage","Response","Request","Headers"]);function U(e){if(e.startsWith(".")||e.startsWith("/")||/^[~@]\//.test(e))return;let t=e.split("/");return e.startsWith("@")?t.slice(0,2).join("/"):t[0]}function y(e){return`_${e.replace(/[^A-Za-z0-9]/g,"_")}`}function V(e,t={}){let{available:r,builtins:c=!0,intrinsics:n=!0,generator:p="@fluixi/compiler",specifier:s=o=>o}=t,i=[...e];c&&i.push({components:I()});let a=$(i),u=r?new Set(r):void 0,m=[],d=[],R=[],h=new Map;for(let o of[...a.names()].sort()){let l=a.resolve(o);if(!l)continue;let g=U(l.module);F.has(o)?d.push(o):u&&g&&!u.has(g)?R.push(o):(m.push(o),h.set(o,l))}let w=n?[...new Set(Object.values(v).map(o=>o.module))]:[],O=[...new Set([...m.map(o=>h.get(o).module),...w])].sort().map(o=>`import * as ${y(o)} from '${s(o)}';`),N=m.map(o=>{let l=h.get(o),g=y(l.module);return l.export==="default"?` const ${o}: typeof ${g}.default;`:` export import ${o} = ${g}.${l.export};`}),T=d.length>0?`//
|
|
2
2
|
// Import these explicitly — lib.dom already declares the name, so an ambient
|
|
3
3
|
// declaration cannot win:
|
|
4
|
-
`+
|
|
5
|
-
`}).join(""):"",
|
|
6
|
-
// ${
|
|
4
|
+
`+d.map(o=>{let l=a.resolve(o)?.module;return`// import { ${o} } from '${l?s(l):""}';
|
|
5
|
+
`}).join(""):"",L=n?Object.entries(v).map(([o,l])=>` const ${o}: typeof ${y(l.module)}.${l.export};`):[],x=e.filter(o=>"match"in o).length,j=x>0?`//
|
|
6
|
+
// ${x} pattern rule${x===1?"":"s"} cannot be declared:
|
|
7
7
|
// a pattern matches unboundedly many names, so there is no list to emit. Those
|
|
8
8
|
// tags still resolve at build time; declare them by hand if the editor needs them.
|
|
9
|
-
`:"";return{source:`// GENERATED by ${
|
|
9
|
+
`:"";return{source:`// GENERATED by ${p} — do not edit.
|
|
10
10
|
//
|
|
11
11
|
// The components \`resolve\` supplies without an import. This describes what the
|
|
12
12
|
// compiler already does; it does not enable it.
|
|
13
|
-
`+
|
|
14
|
-
${
|
|
13
|
+
`+T+j+(O.length>0?`
|
|
14
|
+
${O.join(`
|
|
15
15
|
`)}
|
|
16
16
|
`:`
|
|
17
17
|
`)+`
|
|
18
18
|
declare global {
|
|
19
|
-
${
|
|
19
|
+
${[...N,...L].join(`
|
|
20
20
|
`)}
|
|
21
21
|
}
|
|
22
|
-
`,declared:
|
|
22
|
+
`,declared:m,shadowedByDom:d,unavailable:R,patternRules:x}}
|
|
@@ -38,6 +38,14 @@ export interface ComponentGlobalsOptions {
|
|
|
38
38
|
* declaration, so this stays safe for code that imports them explicitly.
|
|
39
39
|
*/
|
|
40
40
|
builtins?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Declare the `$` reactive intrinsics (`$signal`, `$memo`, …).
|
|
43
|
+
*
|
|
44
|
+
* On by default, matching the compiler: it rewrites those calls whether or not
|
|
45
|
+
* anything declares them, and without the declaration the editor reports
|
|
46
|
+
* "Cannot find name '$signal'" for code that builds correctly.
|
|
47
|
+
*/
|
|
48
|
+
intrinsics?: boolean;
|
|
41
49
|
/** Attribution written into the header. */
|
|
42
50
|
generator?: string;
|
|
43
51
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"component-globals.d.ts","sourceRoot":"","sources":["../../src/resolve/component-globals.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,EAEL,KAAK,qBAAqB,EAE3B,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"component-globals.d.ts","sourceRoot":"","sources":["../../src/resolve/component-globals.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,EAEL,KAAK,qBAAqB,EAE3B,MAAM,yBAAyB,CAAC;AAmCjC,MAAM,WAAW,uBAAuB;IACtC;;;;;;;;;;OAUG;IACH,SAAS,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;IAC7B;;;;;;OAMG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB;;;;;;OAMG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,2CAA2C;IAC3C,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;;;;OAOG;IACH,SAAS,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,MAAM,CAAC;CACxC;AAED,MAAM,WAAW,sBAAsB;IACrC,mCAAmC;IACnC,MAAM,EAAE,MAAM,CAAC;IACf,2BAA2B;IAC3B,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,0DAA0D;IAC1D,aAAa,EAAE,MAAM,EAAE,CAAC;IACxB,qEAAqE;IACrE,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB;;;;;;OAMG;IACH,YAAY,EAAE,MAAM,CAAC;CACtB;AAgBD,0FAA0F;AAC1F,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,SAAS,qBAAqB,EAAE,EACvC,OAAO,GAAE,uBAA4B,GACpC,sBAAsB,CA2GxB"}
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
*/
|
|
19
19
|
import { createComponentResolver, } from './component-resolver.js';
|
|
20
20
|
import { builtinComponentMap } from './builtins.js';
|
|
21
|
+
import { INTRINSICS } from '../analyze/intrinsics.js';
|
|
21
22
|
/**
|
|
22
23
|
* Names `lib.dom` already declares as globals.
|
|
23
24
|
*
|
|
@@ -64,7 +65,7 @@ function namespaceAlias(module) {
|
|
|
64
65
|
}
|
|
65
66
|
/** Build the ambient `.d.ts` for the components a project's `resolve` config supplies. */
|
|
66
67
|
export function componentGlobals(rules, options = {}) {
|
|
67
|
-
const { available: availableList, builtins = true, generator = '@fluixi/compiler', specifier = (module) => module, } = options;
|
|
68
|
+
const { available: availableList, builtins = true, intrinsics = true, generator = '@fluixi/compiler', specifier = (module) => module, } = options;
|
|
68
69
|
const all = [...rules];
|
|
69
70
|
if (builtins) {
|
|
70
71
|
// Appended, so an explicit rule for the same name still wins — the resolver is
|
|
@@ -92,7 +93,14 @@ export function componentGlobals(rules, options = {}) {
|
|
|
92
93
|
resolved.set(name, target);
|
|
93
94
|
}
|
|
94
95
|
}
|
|
95
|
-
|
|
96
|
+
// The `$` intrinsics need declaring too, or the editor says "Cannot find name
|
|
97
|
+
// '$signal'" for code that compiles. tsc doesn't load language-service plugins, so an
|
|
98
|
+
// ambient declaration is the only thing that answers for both the editor and the
|
|
99
|
+
// command line.
|
|
100
|
+
const intrinsicModules = intrinsics ? [...new Set(Object.values(INTRINSICS).map((i) => i.module))] : [];
|
|
101
|
+
const modules = [
|
|
102
|
+
...new Set([...declared.map((name) => resolved.get(name).module), ...intrinsicModules]),
|
|
103
|
+
].sort();
|
|
96
104
|
const imports = modules.map((m) => `import * as ${namespaceAlias(m)} from '${specifier(m)}';`);
|
|
97
105
|
// `export import X = ns.X` rather than `const X: typeof import('…').X`: an alias
|
|
98
106
|
// resolves to the original symbol, so hover shows the real declaration
|
|
@@ -119,6 +127,12 @@ export function componentGlobals(rules, options = {}) {
|
|
|
119
127
|
})
|
|
120
128
|
.join('')
|
|
121
129
|
: '';
|
|
130
|
+
// `typeof ns.signal` rather than an alias: the intrinsic is a global that stands for
|
|
131
|
+
// the runtime call, so it takes the runtime's signature without claiming to be that
|
|
132
|
+
// symbol — go-to-definition on `$signal` should land on what it compiles to.
|
|
133
|
+
const intrinsicLines = intrinsics
|
|
134
|
+
? Object.entries(INTRINSICS).map(([name, intrinsic]) => ` const ${name}: typeof ${namespaceAlias(intrinsic.module)}.${intrinsic.export};`)
|
|
135
|
+
: [];
|
|
122
136
|
const patternRules = rules.filter((rule) => 'match' in rule).length;
|
|
123
137
|
const patternNote = patternRules > 0
|
|
124
138
|
? `//\n// ${patternRules} pattern rule${patternRules === 1 ? '' : 's'} cannot be declared:\n` +
|
|
@@ -132,6 +146,6 @@ export function componentGlobals(rules, options = {}) {
|
|
|
132
146
|
note +
|
|
133
147
|
patternNote +
|
|
134
148
|
(imports.length > 0 ? `\n${imports.join('\n')}\n` : '\n') +
|
|
135
|
-
`\ndeclare global {\n${lines.join('\n')}\n}\n`;
|
|
149
|
+
`\ndeclare global {\n${[...lines, ...intrinsicLines].join('\n')}\n}\n`;
|
|
136
150
|
return { source, declared, shadowedByDom, unavailable, patternRules };
|
|
137
151
|
}
|