@fluixi/compiler 1.0.0-alpha.83 → 1.0.0-alpha.85
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/expr-shape.cjs +62 -1
- package/dist/analyze/expr-shape.d.cts +14 -0
- package/dist/analyze/expr-shape.d.ts +2 -2
- package/dist/analyze/expr-shape.js +1 -1
- package/dist/analyze/expr-shape.mjs +41 -1
- package/dist/analyze/intrinsics.cjs +80 -1
- package/dist/analyze/intrinsics.d.cts +66 -0
- package/dist/analyze/intrinsics.d.ts +7 -7
- package/dist/analyze/intrinsics.d.ts.map +1 -1
- package/dist/analyze/intrinsics.js +3 -3
- package/dist/analyze/intrinsics.mjs +59 -1
- package/dist/analyze/paren.cjs +134 -0
- package/dist/analyze/paren.d.cts +23 -0
- package/dist/analyze/paren.d.ts +24 -0
- package/dist/analyze/paren.d.ts.map +1 -0
- package/dist/analyze/paren.js +168 -0
- package/dist/analyze/paren.mjs +113 -0
- package/dist/analyze/props-destructure.cjs +226 -1
- package/dist/analyze/props-destructure.d.cts +83 -0
- package/dist/analyze/props-destructure.d.ts +3 -3
- package/dist/analyze/props-destructure.d.ts.map +1 -1
- package/dist/analyze/props-destructure.js +4 -4
- package/dist/analyze/props-destructure.mjs +205 -1
- package/dist/analyze/reactive-bindings.cjs +190 -1
- package/dist/analyze/reactive-bindings.d.cts +42 -0
- package/dist/analyze/reactive-bindings.d.ts +1 -1
- package/dist/analyze/reactive-bindings.js +2 -2
- package/dist/analyze/reactive-bindings.mjs +167 -1
- package/dist/analyze/reactive-expr.cjs +38 -1
- package/dist/analyze/reactive-expr.d.cts +38 -0
- package/dist/analyze/reactive-expr.d.ts +4 -4
- package/dist/analyze/reactive-expr.d.ts.map +1 -1
- package/dist/analyze/reactive-expr.js +2 -2
- package/dist/analyze/reactive-expr.mjs +17 -1
- package/dist/analyze/static-graph.cjs +811 -1
- package/dist/analyze/static-graph.d.cts +102 -0
- package/dist/analyze/static-graph.d.ts +1 -1
- package/dist/analyze/static-graph.d.ts.map +1 -1
- package/dist/analyze/static-graph.js +11 -11
- package/dist/analyze/static-graph.mjs +793 -1
- package/dist/babel-TGY76XJ7.mjs +636 -0
- package/dist/chunk-4PLHS2FP.mjs +158 -0
- package/dist/chunk-6DB5FXS4.mjs +1718 -0
- package/dist/chunk-F74GFKSL.mjs +16 -0
- package/dist/chunk-G6QC2WRJ.mjs +11 -0
- package/dist/chunk-RUDJDZAA.mjs +1078 -0
- package/dist/codegen/backend.cjs +18 -1
- package/dist/codegen/backend.d.cts +35 -0
- package/dist/codegen/backends/imperative.cjs +542 -1
- package/dist/codegen/backends/imperative.d.cts +11 -0
- package/dist/codegen/backends/imperative.d.ts +1 -1
- package/dist/codegen/backends/imperative.d.ts.map +1 -1
- package/dist/codegen/backends/imperative.js +24 -20
- package/dist/codegen/backends/imperative.mjs +519 -1
- package/dist/codegen/contract.cjs +52 -1
- package/dist/codegen/contract.d.cts +24 -0
- package/dist/codegen/contract.d.ts +2 -2
- package/dist/codegen/contract.js +2 -2
- package/dist/codegen/contract.mjs +31 -1
- package/dist/codegen/partial-template.cjs +176 -1
- package/dist/codegen/partial-template.d.cts +61 -0
- package/dist/codegen/partial-template.d.ts +4 -4
- package/dist/codegen/partial-template.js +0 -0
- package/dist/codegen/partial-template.mjs +153 -1
- package/dist/codegen/serialize-static.cjs +125 -1
- package/dist/codegen/serialize-static.d.cts +38 -0
- package/dist/codegen/serialize-static.d.ts +3 -3
- package/dist/codegen/serialize-static.js +6 -6
- package/dist/codegen/serialize-static.mjs +104 -1
- package/dist/frontend/babel/build-ir-lit.cjs +1509 -1
- package/dist/frontend/babel/build-ir-lit.d.cts +26 -0
- package/dist/frontend/babel/build-ir-lit.d.ts +2 -2
- package/dist/frontend/babel/build-ir-lit.mjs +1478 -1
- package/dist/frontend/babel/build-ir.cjs +1524 -1
- package/dist/frontend/babel/build-ir.d.cts +35 -0
- package/dist/frontend/babel/build-ir.d.ts +2 -2
- package/dist/frontend/babel/build-ir.js +2 -2
- package/dist/frontend/babel/build-ir.mjs +1495 -1
- package/dist/frontend/babel/index.cjs +2186 -1
- package/dist/frontend/babel/index.d.cts +33 -0
- package/dist/frontend/babel/index.d.ts +1 -1
- package/dist/frontend/babel/index.js +1 -1
- package/dist/frontend/babel/index.mjs +2156 -1
- package/dist/frontend/babel/lower-template.cjs +1507 -1
- package/dist/frontend/babel/lower-template.d.cts +23 -0
- package/dist/frontend/babel/lower-template.d.ts +1 -1
- package/dist/frontend/babel/lower-template.js +2 -2
- package/dist/frontend/babel/lower-template.mjs +1478 -1
- package/dist/frontend/babel/plugin.cjs +2132 -1
- package/dist/frontend/babel/plugin.d.cts +95 -0
- package/dist/frontend/babel/plugin.d.ts +5 -5
- package/dist/frontend/babel/plugin.d.ts.map +1 -1
- package/dist/frontend/babel/plugin.js +14 -14
- package/dist/frontend/babel/plugin.mjs +2103 -1
- package/dist/frontend/babel/server-functions.cjs +110 -1
- package/dist/frontend/babel/server-functions.d.cts +10 -0
- package/dist/frontend/babel/server-functions.mjs +89 -1
- package/dist/frontend/babel/types.cjs +18 -1
- package/dist/frontend/babel/types.d.cts +33 -0
- package/dist/frontend/types.cjs +18 -1
- package/dist/frontend/types.d.cts +25 -0
- package/dist/frontend/vocabulary.cjs +98 -0
- package/dist/frontend/vocabulary.d.cts +79 -0
- package/dist/frontend/vocabulary.d.ts +80 -0
- package/dist/frontend/vocabulary.d.ts.map +1 -0
- package/dist/frontend/vocabulary.js +124 -0
- package/dist/frontend/vocabulary.mjs +77 -0
- package/dist/index.cjs +1595 -14
- package/dist/index.d.cts +27 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +4 -4
- package/dist/index.mjs +1575 -14
- package/dist/integrations.cjs +4312 -26
- package/dist/integrations.d.cts +215 -0
- package/dist/integrations.d.ts +9 -9
- package/dist/integrations.d.ts.map +1 -1
- package/dist/integrations.js +29 -8
- package/dist/integrations.mjs +536 -18
- package/dist/ir/nodes.cjs +18 -1
- package/dist/ir/nodes.d.cts +168 -0
- package/dist/ir/nodes.d.ts +6 -6
- package/dist/lower/compile-template.cjs +1356 -1
- package/dist/lower/compile-template.d.cts +69 -0
- package/dist/lower/compile-template.d.ts +3 -3
- package/dist/lower/compile-template.js +1 -1
- package/dist/lower/compile-template.mjs +1336 -1
- package/dist/lower/jsx.cjs +529 -1
- package/dist/lower/jsx.d.cts +25 -0
- package/dist/lower/jsx.d.ts +1 -1
- package/dist/lower/jsx.d.ts.map +1 -1
- package/dist/lower/jsx.js +6 -5
- package/dist/lower/jsx.mjs +506 -1
- package/dist/lower/template.cjs +1061 -1
- package/dist/lower/template.d.cts +53 -0
- package/dist/lower/template.d.ts +3 -3
- package/dist/lower/template.d.ts.map +1 -1
- package/dist/lower/template.js +12 -11
- package/dist/lower/template.mjs +1042 -1
- package/dist/lower/text.cjs +42 -1
- package/dist/lower/text.d.cts +10 -0
- package/dist/lower/text.d.ts +2 -2
- package/dist/lower/text.d.ts.map +1 -1
- package/dist/lower/text.js +2 -2
- package/dist/lower/text.mjs +21 -1
- package/dist/optimize/analyze-static.cjs +112 -1
- package/dist/optimize/analyze-static.d.cts +39 -0
- package/dist/optimize/analyze-static.d.ts +2 -2
- package/dist/optimize/analyze-static.js +2 -2
- package/dist/optimize/analyze-static.mjs +91 -1
- package/dist/optimize/collapse-strategies.cjs +90 -1
- package/dist/optimize/collapse-strategies.d.cts +30 -0
- package/dist/optimize/collapse-strategies.d.ts +3 -3
- package/dist/optimize/collapse-strategies.mjs +67 -1
- package/dist/options.cjs +33 -1
- package/dist/options.d.cts +39 -0
- package/dist/options.d.ts +2 -2
- package/dist/options.mjs +12 -1
- package/dist/resolve/builtins.cjs +73 -1
- package/dist/resolve/builtins.d.cts +68 -0
- package/dist/resolve/builtins.d.ts +8 -8
- package/dist/resolve/builtins.d.ts.map +1 -1
- package/dist/resolve/builtins.js +4 -4
- package/dist/resolve/builtins.mjs +53 -1
- package/dist/resolve/component-globals.cjs +234 -14
- package/dist/resolve/component-globals.d.cts +80 -0
- package/dist/resolve/component-globals.d.ts +5 -5
- package/dist/resolve/component-globals.js +10 -10
- package/dist/resolve/component-globals.mjs +211 -14
- package/dist/resolve/component-resolver.cjs +80 -1
- package/dist/resolve/component-resolver.d.cts +68 -0
- package/dist/resolve/component-resolver.d.ts +3 -3
- package/dist/resolve/component-resolver.d.ts.map +1 -1
- package/dist/resolve/component-resolver.js +3 -3
- package/dist/resolve/component-resolver.mjs +59 -1
- package/dist/resolve/load-directive.cjs +93 -1
- package/dist/resolve/load-directive.d.cts +53 -0
- package/dist/resolve/load-directive.d.ts +5 -5
- package/dist/resolve/load-directive.js +6 -6
- package/dist/resolve/load-directive.mjs +72 -1
- package/dist/resolve/resolve-ir.cjs +108 -1
- package/dist/resolve/resolve-ir.d.cts +53 -0
- package/dist/resolve/resolve-ir.d.ts +6 -6
- package/dist/resolve/resolve-ir.d.ts.map +1 -1
- package/dist/resolve/resolve-ir.js +1 -1
- package/dist/resolve/resolve-ir.mjs +85 -1
- package/dist/resolve/write-globals.cjs +319 -14
- package/dist/resolve/write-globals.d.cts +30 -0
- package/dist/resolve/write-globals.js +2 -2
- package/dist/resolve/write-globals.mjs +298 -14
- package/dist/server-fns-ZYWQTSZU.mjs +78 -0
- package/dist/server-functions-PCERFA7A.mjs +82 -0
- package/dist/transform/bindings.cjs +71 -1
- package/dist/transform/bindings.d.cts +16 -0
- package/dist/transform/bindings.d.ts +1 -1
- package/dist/transform/bindings.js +1 -1
- package/dist/transform/bindings.mjs +50 -1
- package/dist/transform/index.cjs +2732 -8
- package/dist/transform/index.d.cts +15 -0
- package/dist/transform/index.mjs +3773 -17
- package/dist/transform/intrinsics.cjs +216 -3
- package/dist/transform/intrinsics.d.cts +21 -0
- package/dist/transform/intrinsics.js +1 -1
- package/dist/transform/intrinsics.mjs +193 -3
- package/dist/transform/props.cjs +441 -3
- package/dist/transform/props.d.cts +45 -0
- package/dist/transform/props.d.ts +1 -1
- package/dist/transform/props.d.ts.map +1 -1
- package/dist/transform/props.js +4 -4
- package/dist/transform/props.mjs +1482 -12
- package/dist/transform/server-fn-id.cjs +35 -1
- package/dist/transform/server-fn-id.d.cts +9 -0
- package/dist/transform/server-fn-id.d.ts +1 -1
- package/dist/transform/server-fn-id.js +2 -2
- package/dist/transform/server-fn-id.mjs +14 -1
- package/dist/transform/server-fns.cjs +114 -2
- package/dist/transform/server-fns.d.cts +12 -0
- package/dist/transform/server-fns.js +3 -3
- package/dist/transform/server-fns.mjs +1155 -11
- package/dist/transform/source-locations.cjs +330 -1
- package/dist/transform/source-locations.d.cts +43 -0
- package/dist/transform/source-locations.d.ts +1 -1
- package/dist/transform/source-locations.js +5 -5
- package/dist/transform/source-locations.mjs +307 -1
- package/dist/transform/templates.cjs +2701 -7
- package/dist/transform/templates.d.cts +69 -0
- package/dist/transform/templates.d.ts +2 -2
- package/dist/transform/templates.d.ts.map +1 -1
- package/dist/transform/templates.js +7 -7
- package/dist/transform/templates.mjs +3743 -16
- package/dist/transform-RWDTQUF5.mjs +893 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/package.json +6 -5
- package/dist/babel-VXKH6MRQ.mjs +0 -1
- package/dist/chunk-3SAEGOMQ.mjs +0 -1
- package/dist/chunk-5KMMN5QP.mjs +0 -1
- package/dist/chunk-IBRM4W7I.mjs +0 -10
- package/dist/chunk-OHHZCYIQ.mjs +0 -1
- package/dist/chunk-YKZ54NVE.mjs +0 -1
- package/dist/server-fns-6QM243HC.mjs +0 -2
- package/dist/server-functions-E3LKCZ4R.mjs +0 -1
- package/dist/transform-C4ZXRVGP.mjs +0 -8
package/dist/index.mjs
CHANGED
|
@@ -1,22 +1,1583 @@
|
|
|
1
|
-
var we="children";function Te(t){switch(t.kind){case"call":return!0;case"member":return t.property!==we;case"compound":return t.parts.some(Te);case"opaque":return!1}}import{parseTemplate as Xe}from"@fluixi/template-parser";var T={kind:"eager"},Ae=["pointerdown","focusin","keydown"],Z=new Set(["eager","idle","visible","interaction","media","never"]),x=class extends Error{};function Y(t,e){let n=t.slice(5);if(!Z.has(n))throw new x(`Unknown load strategy 'load:${n}'. Expected one of ${[...Z].join(", ")}.`);switch(n){case"eager":return T;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?Oe(e):[...Ae]};case"media":if(!e)throw new x(`'load:media' needs a query, e.g. load:media="(min-width: 1024px)".`);return{kind:"media",query:e};default:throw new x(`Unhandled load strategy '${n}'.`)}}function Oe(t){let e=t.split(/[\s,]+/).map(n=>n.trim()).filter(Boolean);if(e.length===0)throw new x("'load:interaction' was given no event names.");return e}function X(t){return t.kind!=="eager"&&t.kind!=="never"}var Pe=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]),Q={className:"class",htmlFor:"for"},Me=new Set(["innerHTML","outerHTML","innerText","textContent","value","checked","selected","muted","defaultValue","defaultChecked","children","ref"]),De=new Set(["script","style","textarea","title"]);function L(t){return De.has(t)}function ee(t){return Q[t]??t}function k(t){return t.kind!=="attr"||t.expr!==void 0||t.name.includes(":")?!1:!Me.has(t.name)}function B(t){if(!t.static||L(t.tag))return!1;for(let e of t.props)if(!k(e))return!1;for(let e of t.children)if(e.kind!=="text"&&!(e.kind==="element"&&B(e)))return!1;return!0}function I(t){let e=t.props.map(_e).filter(Boolean).join(""),n=`<${t.tag}${e}>`;return Pe.has(t.tag)?n:`${n}${t.children.map(je).join("")}</${t.tag}>`}function je(t){if(t.kind==="text")return Be(t.value);if(t.kind!=="element")throw new Error(`serializeStatic: unexpected ${t.kind}`);return I(t)}function _e(t){let e=Q[t.name]??t.name,n=t.literal;return n===!0||n===void 0?` ${e}`:n===!1||n===null?"":` ${e}="${Le(String(n))}"`}function Le(t){return t.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function Be(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}function F(t){let e=[],n,o=r=>{n??=r.at?.file,e.push(r.at?.line??0,r.at?.column??0);for(let i of r.children)i.kind==="element"&&o(i)};return o(t),n&&e.some(r=>r!==0)?{file:n,pos:e}:void 0}var Fe="<!--fx-->",He="<!--fx/-->";function O(t){return t.kind==="expr"||t.kind==="component"||t.kind==="control"}function Je(t){return t.kind==="text"||t.kind==="element"&&te(t)}function te(t){return t.svg||L(t.tag)||!t.props.every(e=>k(e))?!1:t.children.every(e=>Je(e)||O(e))}function A(t){if(O(t))return!0;let e=t.children;return e?e.some(A):!1}function ne(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"||ne(e))}function re(t){if(!te(t)||!ne(t)||!t.children.some(A))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 p=-1;a.children.forEach((l,u)=>{A(l)&&(p=u)});let c=null;for(let l=0;l<=p;l++){let u=a.children[l],d=c?`${c}.nextSibling`:`${s}.firstChild`;if(O(u)){let h=r(d),b=r(`holeEnd(${h})`);n.push({parentRef:s,startRef:h,endRef:b,node:u}),c=b;continue}let f=r(d);u.kind==="element"&&A(u)&&i(u,f),c=f}}}function We(t){return I(oe(t)).split(ie).join(Fe+He)}function oe(t){return{...t,children:t.children.map(e=>O(e)?{kind:"text",value:ie}:e.kind==="element"?oe(e):e)}}var ie="\0fx-hole\0";var H={version:1,module:"@fluixi/dom",symbols:["createNativeElement","insert","spread","setAttribute","delegateEvents","createComponent","createMemo","untrack","Show","For","Index","Switch","Match","Dynamic","ErrorBoundary"]},bt={version:2,module:"@fluixi/dom",symbols:[...H.symbols,"template","cloneTemplate","walk"]};var R="@fluixi/reactive/signal",y={$signal:{export:"signal",module:R,returns:"signal-handle"},$memo:{export:"memo",module:R,returns:"memo-handle"},$effect:{export:"effect",module:R,returns:"effect"},$store:{export:"store",module:"@fluixi/reactive/store",returns:"store-handle"},$resource:{export:"resource",module:R,returns:"resource-handle"},$selector:{export:"createSelector",module:R,returns:"memo-accessor"},$deferred:{export:"createDeferred",module:R,returns:"memo-accessor"},$untrack:{export:"untrack",module:R,returns:"plain"},$untrackStore:{export:"untrackStore",module:"@fluixi/reactive/store",returns:"plain"}};var kt=new RegExp(`\\$(?:${Object.keys(y).map(t=>t.slice(1)).join("|")})\\s*\\(`);var se="__fx_source";var Ue="__fx_hole",E=(t,e,n,o)=>`(globalThis.${Ue} ?? ((f) => f))(${t},${e},${n}${o===void 0?"":`,${JSON.stringify(o)}`})`,Ve="__fx_props",ae=(t,e)=>`(globalThis.${Ve} ?? ((p) => p))(${t},${JSON.stringify(e)})`;var Ge={show:"Show",for:"For",index:"Index",switch:"Switch",match:"Match",dynamic:"Dynamic",errorBoundary:"ErrorBoundary",suspense:"Suspense"};function Ke(t){return/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(t)}function pe(t){return Ke(t)?t:JSON.stringify(t)}function ze(t){if(t.expr!==void 0){if(t.name==="ref"&&t.at)return E(`(${t.expr})`,t.at.line,t.at.column,t.expr);if(!t.reactive)return`(${t.expr})`;let e=`() => (${t.expr})`;return t.at?E(e,t.at.line,t.at.column,t.directive):e}return JSON.stringify(t.literal??!0)}function qe(t){return t.expr!==void 0?`(${t.expr})`:JSON.stringify(t.literal??!0)}function Ze(t,e,n){let o=t.filter(s=>s.kind==="spread"),i=t.filter(s=>s.kind!=="spread").map(s=>`${pe(s.name)}: ${ze(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 le(t,e){return e?E(t,e.line,e.column):t}function ue(t,e,n){return t.length===1?S(t[0],e,n):`[${t.map(o=>S(o,e,n)).join(", ")}]`}function ce(t,e,n,o,r,i){o.add("createMemo"),o.add("createComponent");let a=e.filter(d=>d.kind==="spread"),s=e.filter(d=>d.kind!=="spread"),p=s.map(d=>`get ${pe(d.name)}() { return ${qe(d)}; }`);n.length>0&&p.push(`get children() { return ${ue(n,o,r)}; }`);let c=`{ ${p.join(", ")} }`;a.length>0&&(o.add("mergeProps"),c=`mergeProps(${a.map(d=>d.expr).join(", ")}, ${c})`);let l={};for(let d of s)d.at&&(l[d.name]=[d.at.line,d.at.column]);return Object.keys(l).length&&(c=ae(c,l)),`createMemo((${i?`globalThis.${se}?.(${JSON.stringify(i.file)},${i.line},${i.column},${JSON.stringify(t)}), `:""}() => createComponent(${t}, ${c})))`}function Ye(t,e){let n=ee(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 de=!1;function S(t,e,n){switch(t.kind){case"text":return JSON.stringify(t.value);case"expr":{if(!t.reactive)return`(${t.code})`;let o=`() => (${t.code})`;return t.at?E(o,t.at.line,t.at.column):o}case"fragment":return t.children.length===0?"null":ue(t.children,e,n);case"component":return le(ce(t.name,t.props,t.children,e,n,t.at),t.bindAt??t.at);case"control":{let o=Ge[t.control]??t.control;return e.add(o),le(ce(o,t.props,t.children,e,n),t.bindAt)}case"element":{if(n&&t.static&&!t.svg&&B(t)){e.add("templateNode");let s=`_tmpl$${n.length}`;n.push({id:s,html:I(t),tag:t.tag,svg:!1});let p=F(t);return p?`templateNode(${s}, ${JSON.stringify(t.tag)}, false, false, ${JSON.stringify(p)})`:`templateNode(${s}, ${JSON.stringify(t.tag)})`}if(n&&de){let s=re(t);if(s){e.add("templateNode"),e.add("insert"),e.add("holeEnd"),e.add("holeContent"),e.add("holeScope");let p=`_tmpl$${n.length}`;n.push({id:p,html:s.html,tag:s.tag,svg:!1});let c=F(t),l=[`const _el$ = templateNode(${p}, ${JSON.stringify(s.tag)}, true${c?`, false, ${JSON.stringify(c)}`:""});`];for(let u of s.steps)l.push(`const ${u.ref} = ${u.expr};`);for(let u of s.holes)l.push(`insert(${u.parentRef}, holeScope(${u.startRef}, () => (${S(u.node,e,n)})), ${u.endRef}, holeContent(${u.startRef}, ${u.endRef}));`);return l.push("return _el$;"),`(() => { ${l.join(" ")} })()`}}e.add("createNativeElement");let o=JSON.stringify(t.tag),r="_el$",i=[],a=t.at?`${o}, ${t.svg}, ${JSON.stringify(t.at)}`:t.svg?`${o}, true`:o;if(i.push(`const ${r} = createNativeElement(${a});`),t.props.length>0)if(!t.svg&&t.props.every(k))for(let s of t.props)i.push(Ye(r,s));else{e.add("spread");let s=t.svg?", isSVG: true":"";i.push(`spread({ element: ${r}, props: ${Ze(t.props,null,e)}${s} });`)}for(let s of t.children){e.add("insert");let p=S(s,e,n),c=s.kind==="expr"&&s.reactive||s.kind==="component"||s.kind==="control";i.push(c?`insert(${r}, ${p}, null);`:`insert(${r}, ${p});`)}return i.push(`return ${r};`),`(() => { ${i.join(" ")} })()`}}}function P(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 v={name:"imperative",contract:H,emit(t,e){let n=new Set,o=e?.templateClone!==!1?[]:void 0;return de=e?.partialTemplates===!0,{code:S(t,n,o),imports:Array.from(n),templates:o}}};function M(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 me=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function Qe(t){return t.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$\{/g,"\\${")}function fe(t){return t.charAt(0).toUpperCase()+t.slice(1)}var J=class{constructor(e,n,o,r){this.holes=e;this.used=n;this.sourceFile=o;this.positionAt=r}tagAt(e){let n=e.loc?.start;if(!this.sourceFile||!this.positionAt||n==null)return;let{line:o,column:r}=this.positionAt(n);return{file:this.sourceFile,line:o,column:r}}hole(e){return this.holes[e]??{code:"undefined",reactive:!1}}emit(e){let{code:n,imports:o,templates:r}=v.emit(e,{});for(let i of o)this.used.add(i);return P(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 p=e[s],c=p&&(p.kind==="Element"||p.kind==="Component")&&p.attributes.some(l=>l.kind==="ElseDirective");n.push(this.lowerIf(r,a.hole,c?p: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&&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,...n.at?{at:n.at}:{}}}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(a=>a!==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}let o=this.tagAt(e);return{kind:"element",tag:e.tag,svg:e.namespace==="svg",props:this.lowerAttributes(e.attributes),children:this.lowerChildren(e.children),static:!1,...o?{at:o}:{}}}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,u]of r){let d=u.length===1?u[0]:{kind:"fragment",children:u},f={name:l,kind:"attr",expr:this.emit(d),jsxElement:!0},h=o.findIndex(b=>b.name===l);h>=0?o[h]=f:o.push(f)}let a=e.attributes.find(l=>l.kind==="LoadDirective"),s=a?Y(`load:${a.strategy}`,a.modifier):void 0,p=o.find(l=>l.at&&l.reactive)?.at,c=this.tagAt(e);return{kind:"component",name:n,props:o,children:this.lowerChildren(i),...c?{at:c}:{},...p?{bindAt:p}:{},...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),p=n.get(i.value.value)??[];p.push(s),n.set(i.value.value,p);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,...r.at?{at:r.at}:{}}];if(o){let p=this.stripAndLower(o,c=>c.kind==="ElseDirective");i.push({name:"fallback",kind:"attr",expr:this.emit(p),jsxElement:!0})}let a=this.stripAndLower(e,p=>p.kind==="IfDirective"),s=this.tagAt(e);return{kind:"component",name:"Show",props:i,children:[a],...s?{at:s}:{},...r.at?{bindAt:r.at}:{}}}lowerEach(e){let n=e.attributes.find(c=>c.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,...o.at?{at:o.at}:{}}];if(n.key){let c="static"in n.key?`(item) => item[${JSON.stringify(n.key.static)}]`:this.hole(n.key.hole).code;r.push({name:"by",kind:"attr",expr:c})}let i=this.itemArrow(e),a;if(i){let c={kind:"expr",code:i.body,reactive:i.bodyReactive},l=this.rebuildWithChildren(e,[c]);a=`(${i.params.join(", ")}) => (${this.emit(l)})`}else{let c=this.stripAndLower(e,l=>l.kind==="EachDirective");a=`() => (${this.emit(c)})`}let s=[{kind:"expr",code:a,reactive:!1}],p=this.tagAt(e);return{kind:"component",name:"For",props:r,children:s,...p?{at:p}:{},...o.at?{bindAt:o.at}:{}}}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"));if(e.kind==="Element"){let r=this.tagAt(e);return{kind:"element",tag:e.tag,svg:e.namespace==="svg",props:o,children:n,static:!1,...r?{at:r}:{}}}return{kind:"component",name:e.tag,props:o,children:n}}lowerAttributes(e){let n=[],o=[],r=!1,i,a=[],s=!1,p,c=[];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,...this.hole(l.hole).at?{at:this.hole(l.hole).at}:{}});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,...this.hole(l.hole).at?{at:this.hole(l.hole).at}:{}});break;case"Spread":n.push({name:"",kind:"spread",expr:this.hole(l.hole).code});break;case"ClassDirective":{let u=this.hole(l.hole);o.push(`${JSON.stringify(l.name)}: ${u.code}`),r=r||u.reactive,i??=u.at;break}case"StyleDirective":{let u=this.hole(l.hole);a.push(`${JSON.stringify(l.name)}: ${u.code}`),s=s||u.reactive,p??=u.at;break}case"BindDirective":n.push(...this.bindProps(l.name,this.hole(l.hole)));break;case"UseDirective":{let u=l.name??(l.hole!=null?this.hole(l.hole).code:null);if(!u)break;c.push(l.name!=null&&l.hole!=null?`[${u}, () => (${this.hole(l.hole).code})]`:`[${u}]`);break}}return o.length>0&&n.push({name:"classList",kind:"attr",expr:`{ ${o.join(", ")} }`,reactive:r,...i?{at:i}:{}}),a.length>0&&n.push({name:"style",kind:"attr",expr:`{ ${a.join(", ")} }`,reactive:s,...p?{at:p}:{}}),c.length>0&&n.push({name:"use",kind:"attr",expr:`[${c.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"+fe(e.name),kind:"event",event:{name:n,delegated:me.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=n.code,r=e==="checked",i=r?"change":"input",a=r?"checked":"value";this.used.add("bindPair");let s=`bindPair(${o})`;return[{name:e,kind:"attr",expr:`${s}[0]()`,reactive:!0,directive:`bind:${e}`,...n.at?{at:n.at}:{}},{name:"on"+fe(i),kind:"event",event:{name:i,delegated:me.has(i)},expr:`(e) => ${s}[1](e.target.${a})`}]}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 c=this.hole(n.hole);return i.expr=c.code,i.reactive=c.reactive,c.at&&(i.at=c.at),i}let a=!1,s,p=n.parts.map(c=>{if("text"in c)return Qe(c.text);let l=this.hole(c.hole);return a=a||l.reactive,s??=l.at,"${"+l.code+"}"}).join("");return i.expr="`"+p+"`",i.reactive=a,s&&(i.at=s),i}};function W(t,e,n,o={}){let{root:r}=Xe(t,{svg:o.svg});return new J(e,n,o.sourceFile,o.positionAt).lowerRoot(r.children)}function et(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 ge(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=et(s),a)break;return!a&&!i&&(a=tt(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 tt(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 U=["Show","For","Index","Switch","Match","Portal","Dynamic","ErrorBoundary"],V=["Suspense","SuspenseList","Await"],$=["Router","Outlet","Redirect","Link"],Gt=[...U,...V,...$],G=new Set([...U,...V]);function C(t={}){let{controlFlowModule:e="@fluixi/dom",coreModule:n="@fluixi/core",routerModule:o="@fluixi/core/router"}=t,r={};for(let i of U)r[i]=e;for(let i of V)r[i]=n;for(let i of $)r[i]=o;return r}function Kt(t={}){return{components:C(t)}}function zt(t){let e=t??"@fluixi/core/router";return{components:Object.fromEntries($.map(n=>[n,e]))}}function he(t,e={}){let{resolver:n,isBound:o,modules:r,strategyFor:i}=e,a=C(r),s=new Map,p=new Set,c=u=>{let{name:d}=u;if(!d||d.includes(".")||o?.(d))return;let f=l(d);if(!f){p.add(d);return}let h=f.origin==="builtin"&&G.has(d)?T:i?.(u)??T;u.source={...f,loading:h},s.set(d,u.source)},l=u=>{if(G.has(u))return{module:a[u],export:u,origin:"builtin"};let d=n?.resolve(u);if(d)return{module:d.module,export:d.export,origin:"rule"};if($.includes(u))return{module:a[u],export:u,origin:"builtin"}};return D(t,u=>{u.kind==="component"&&c(u)}),{resolved:s,unresolved:[...p]}}function D(t,e){let n=Array.isArray(t)?t:[t];for(let o of n){e(o);let r=o.children;r&&D(r,e)}}function Re(t){let e=[];D(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;X(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 nt=t=>"components"in t;function ve(t,e){return t.replace(/\$(\d+)/g,(n,o)=>e[Number(o)]??n)}function rt(t,e){return typeof e=="string"?{module:e,export:t}:e}function j(t=[]){let e=new Map,n=[],o=[];for(let a of t){if(!nt(a)){o.push(a);continue}for(let[s,p]of Object.entries(a.components)){let c=rt(s,p),l=e.get(s);if(l&&l.module!==c.module){let u=n.find(d=>d.name===s);u?u.modules.push(c.module):n.push({name:s,modules:[l.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 p of o){let c=a.match(new RegExp(p.match.source,p.match.flags.replace("g","")));if(c){s={module:ve(p.module,c),export:p.export?ve(p.export,c):a};break}}return r.set(a,s),s},names:()=>[...e.keys()],conflicts:()=>n}}function ot(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 pn(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 it(ot(t),t.holes,e)}function it(t,e,n={}){let o=new Set,r=W(t,[...e],o,{svg:n.svg,...n.sourceFile?{sourceFile:n.sourceFile}:{},...n.positionAt?{positionAt:n.positionAt}:{}});ge(r),he(r,{resolver:j(n.resolve??[]),modules:{controlFlowModule:n.controlFlowModule??"@fluixi/dom",coreModule:n.coreModule??"@fluixi/core",routerModule:n.routerModule??"@fluixi/core/router"},strategyFor:p=>p.load}),Re(r);let{code:i,imports:a,templates:s}=v.emit(r,{templateClone:n.templateClone,partialTemplates:n.partialTemplates});for(let p of a)o.add(p);return n.hoistTemplates?{code:i,imports:[...o],ir:r,templates:s}:{code:P(i,s),imports:[...o],ir:r}}var st=new Set(["Text","Image","Option","Audio","Comment","Range","Selection","Notification","Event","Document","Element","Node","Screen","Window","Location","History","Storage","Response","Request","Headers"]);function at(t){if(t.startsWith(".")||t.startsWith("/")||/^[~@]\//.test(t))return;let e=t.split("/");return t.startsWith("@")?e.slice(0,2).join("/"):e[0]}function K(t){return`_${t.replace(/[^A-Za-z0-9]/g,"_")}`}function xe(t,e={}){let{available:n,builtins:o=!0,intrinsics:r=!0,generator:i="@fluixi/compiler",specifier:a=m=>m}=e,s=[...t];o&&s.push({components:C()});let p=j(s),c=n?new Set(n):void 0,l=[],u=[],d=[],f=new Map;for(let m of[...p.names()].sort()){let g=p.resolve(m);if(!g)continue;let N=at(g.module);st.has(m)?u.push(m):c&&N&&!c.has(N)?d.push(m):(l.push(m),f.set(m,g))}let h=r?[...new Set(Object.values(y).map(m=>m.module))]:[],q=[...new Set([...l.map(m=>f.get(m).module),...h])].sort().map(m=>`import * as ${K(m)} from '${a(m)}';`),Ee=l.map(m=>{let g=f.get(m),N=K(g.module);return g.export==="default"?` const ${m}: typeof ${N}.default;`:` export import ${m} = ${N}.${g.export};`}),Se=u.length>0?`//
|
|
2
|
-
|
|
1
|
+
/*! @fluixi/compiler v1.0.0-alpha.85 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
|
|
2
|
+
|
|
3
|
+
// src/analyze/reactive-expr.ts
|
|
4
|
+
var STABLE_MEMBER = "children";
|
|
5
|
+
function isReactiveShape(shape) {
|
|
6
|
+
switch (shape.kind) {
|
|
7
|
+
case "call":
|
|
8
|
+
return true;
|
|
9
|
+
case "member":
|
|
10
|
+
return shape.property !== STABLE_MEMBER;
|
|
11
|
+
case "compound":
|
|
12
|
+
return shape.parts.some(isReactiveShape);
|
|
13
|
+
case "opaque":
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// src/lower/template.ts
|
|
19
|
+
import {
|
|
20
|
+
parseTemplate
|
|
21
|
+
} from "@fluixi/template-parser";
|
|
22
|
+
|
|
23
|
+
// src/resolve/load-directive.ts
|
|
24
|
+
var EAGER = { kind: "eager" };
|
|
25
|
+
var DEFAULT_INTERACTION_EVENTS = ["pointerdown", "focusin", "keydown"];
|
|
26
|
+
var STRATEGIES = /* @__PURE__ */ new Set(["eager", "idle", "visible", "interaction", "media", "never"]);
|
|
27
|
+
var LoadDirectiveError = class extends Error {
|
|
28
|
+
};
|
|
29
|
+
function parseLoadDirective(name, value) {
|
|
30
|
+
const strategy = name.slice("load:".length);
|
|
31
|
+
if (!STRATEGIES.has(strategy)) {
|
|
32
|
+
throw new LoadDirectiveError(
|
|
33
|
+
`Unknown load strategy 'load:${strategy}'. Expected one of ${[...STRATEGIES].join(", ")}.`
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
switch (strategy) {
|
|
37
|
+
case "eager":
|
|
38
|
+
return EAGER;
|
|
39
|
+
case "idle":
|
|
40
|
+
return { kind: "idle" };
|
|
41
|
+
case "never":
|
|
42
|
+
return { kind: "never" };
|
|
43
|
+
case "visible":
|
|
44
|
+
return value ? { kind: "visible", rootMargin: value } : { kind: "visible" };
|
|
45
|
+
case "interaction":
|
|
46
|
+
return {
|
|
47
|
+
kind: "interaction",
|
|
48
|
+
events: value ? splitEvents(value) : [...DEFAULT_INTERACTION_EVENTS]
|
|
49
|
+
};
|
|
50
|
+
case "media":
|
|
51
|
+
if (!value) {
|
|
52
|
+
throw new LoadDirectiveError(
|
|
53
|
+
`'load:media' needs a query, e.g. load:media="(min-width: 1024px)".`
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
return { kind: "media", query: value };
|
|
57
|
+
default:
|
|
58
|
+
throw new LoadDirectiveError(`Unhandled load strategy '${strategy}'.`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function splitEvents(value) {
|
|
62
|
+
const events = value.split(/[\s,]+/).map((e) => e.trim()).filter(Boolean);
|
|
63
|
+
if (events.length === 0) {
|
|
64
|
+
throw new LoadDirectiveError(`'load:interaction' was given no event names.`);
|
|
65
|
+
}
|
|
66
|
+
return events;
|
|
67
|
+
}
|
|
68
|
+
function isDeferred(strategy) {
|
|
69
|
+
return strategy.kind !== "eager" && strategy.kind !== "never";
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// src/codegen/serialize-static.ts
|
|
73
|
+
var VOID_ELEMENTS = /* @__PURE__ */ new Set([
|
|
74
|
+
"area",
|
|
75
|
+
"base",
|
|
76
|
+
"br",
|
|
77
|
+
"col",
|
|
78
|
+
"embed",
|
|
79
|
+
"hr",
|
|
80
|
+
"img",
|
|
81
|
+
"input",
|
|
82
|
+
"link",
|
|
83
|
+
"meta",
|
|
84
|
+
"param",
|
|
85
|
+
"source",
|
|
86
|
+
"track",
|
|
87
|
+
"wbr"
|
|
88
|
+
]);
|
|
89
|
+
var HTML_NAME = {
|
|
90
|
+
className: "class",
|
|
91
|
+
htmlFor: "for"
|
|
92
|
+
};
|
|
93
|
+
var PROPERTY_ONLY = /* @__PURE__ */ new Set([
|
|
94
|
+
"innerHTML",
|
|
95
|
+
"outerHTML",
|
|
96
|
+
"innerText",
|
|
97
|
+
"textContent",
|
|
98
|
+
"value",
|
|
99
|
+
"checked",
|
|
100
|
+
"selected",
|
|
101
|
+
"muted",
|
|
102
|
+
"defaultValue",
|
|
103
|
+
"defaultChecked",
|
|
104
|
+
"children",
|
|
105
|
+
"ref"
|
|
106
|
+
]);
|
|
107
|
+
var RAW_TEXT_ELEMENTS = /* @__PURE__ */ new Set(["script", "style", "textarea", "title"]);
|
|
108
|
+
function isRawTextElement(tag) {
|
|
109
|
+
return RAW_TEXT_ELEMENTS.has(tag);
|
|
110
|
+
}
|
|
111
|
+
function htmlAttrName(name) {
|
|
112
|
+
return HTML_NAME[name] ?? name;
|
|
113
|
+
}
|
|
114
|
+
function isPlainLiteralAttr(prop) {
|
|
115
|
+
if (prop.kind !== "attr") return false;
|
|
116
|
+
if (prop.expr !== void 0) return false;
|
|
117
|
+
if (prop.name.includes(":")) return false;
|
|
118
|
+
return !PROPERTY_ONLY.has(prop.name);
|
|
119
|
+
}
|
|
120
|
+
function isSerializable(node) {
|
|
121
|
+
if (!node.static) return false;
|
|
122
|
+
if (isRawTextElement(node.tag)) return false;
|
|
123
|
+
for (const prop of node.props) {
|
|
124
|
+
if (!isPlainLiteralAttr(prop)) return false;
|
|
125
|
+
}
|
|
126
|
+
for (const child of node.children) {
|
|
127
|
+
if (child.kind === "text") continue;
|
|
128
|
+
if (child.kind === "element" && isSerializable(child)) continue;
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
function serializeStatic(node) {
|
|
134
|
+
const attrs = node.props.map(serializeProp).filter(Boolean).join("");
|
|
135
|
+
const open = `<${node.tag}${attrs}>`;
|
|
136
|
+
if (VOID_ELEMENTS.has(node.tag)) return open;
|
|
137
|
+
return `${open}${node.children.map(serializeChild).join("")}</${node.tag}>`;
|
|
138
|
+
}
|
|
139
|
+
function serializeChild(child) {
|
|
140
|
+
if (child.kind === "text") return escapeText(child.value);
|
|
141
|
+
if (child.kind !== "element") throw new Error(`serializeStatic: unexpected ${child.kind}`);
|
|
142
|
+
return serializeStatic(child);
|
|
143
|
+
}
|
|
144
|
+
function serializeProp(prop) {
|
|
145
|
+
const name = HTML_NAME[prop.name] ?? prop.name;
|
|
146
|
+
const value = prop.literal;
|
|
147
|
+
if (value === true || value === void 0) return ` ${name}`;
|
|
148
|
+
if (value === false || value === null) return "";
|
|
149
|
+
return ` ${name}="${escapeAttr(String(value))}"`;
|
|
150
|
+
}
|
|
151
|
+
function escapeAttr(value) {
|
|
152
|
+
return value.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
153
|
+
}
|
|
154
|
+
function escapeText(value) {
|
|
155
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
156
|
+
}
|
|
157
|
+
function templatePositions(root) {
|
|
158
|
+
const pos = [];
|
|
159
|
+
let file;
|
|
160
|
+
const walk2 = (el) => {
|
|
161
|
+
file ??= el.at?.file;
|
|
162
|
+
pos.push(el.at?.line ?? 0, el.at?.column ?? 0);
|
|
163
|
+
for (const child of el.children) if (child.kind === "element") walk2(child);
|
|
164
|
+
};
|
|
165
|
+
walk2(root);
|
|
166
|
+
return file && pos.some((n) => n !== 0) ? { file, pos } : void 0;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// src/codegen/partial-template.ts
|
|
170
|
+
var HOLE_START = "<!--fx-->";
|
|
171
|
+
var HOLE_END = "<!--fx/-->";
|
|
172
|
+
function isHole(node) {
|
|
173
|
+
return node.kind === "expr" || node.kind === "component" || node.kind === "control";
|
|
174
|
+
}
|
|
175
|
+
function isTemplateChild(node) {
|
|
176
|
+
return node.kind === "text" || node.kind === "element" && isTemplateElement(node);
|
|
177
|
+
}
|
|
178
|
+
function isTemplateElement(node) {
|
|
179
|
+
if (node.svg) return false;
|
|
180
|
+
if (isRawTextElement(node.tag)) return false;
|
|
181
|
+
if (!node.props.every((p) => isPlainLiteralAttr(p))) return false;
|
|
182
|
+
return node.children.every((c) => isTemplateChild(c) || isHole(c));
|
|
183
|
+
}
|
|
184
|
+
function containsHole(node) {
|
|
185
|
+
if (isHole(node)) return true;
|
|
186
|
+
const children = node.children;
|
|
187
|
+
return children ? children.some(containsHole) : false;
|
|
188
|
+
}
|
|
189
|
+
function addressable(el) {
|
|
190
|
+
for (let i = 0; i < el.children.length; i++) {
|
|
191
|
+
const child = el.children[i];
|
|
192
|
+
if (child.kind === "text") {
|
|
193
|
+
if (child.value === "") return false;
|
|
194
|
+
if (el.children[i + 1]?.kind === "text") return false;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return el.children.every((c) => c.kind !== "element" || addressable(c));
|
|
198
|
+
}
|
|
199
|
+
function planPartialTemplate(root) {
|
|
200
|
+
if (!isTemplateElement(root)) return null;
|
|
201
|
+
if (!addressable(root)) return null;
|
|
202
|
+
if (!root.children.some(containsHole)) return null;
|
|
203
|
+
const steps = [];
|
|
204
|
+
const holes = [];
|
|
205
|
+
let refCount = 0;
|
|
206
|
+
const declare = (expr) => {
|
|
207
|
+
const ref = `_n$${refCount++}`;
|
|
208
|
+
steps.push({ ref, expr });
|
|
209
|
+
return ref;
|
|
210
|
+
};
|
|
211
|
+
walk2(root, "_el$");
|
|
212
|
+
return { html: serializeWithMarkers(root), tag: root.tag, steps, holes };
|
|
213
|
+
function walk2(el, elementRef) {
|
|
214
|
+
let last = -1;
|
|
215
|
+
el.children.forEach((child, i) => {
|
|
216
|
+
if (containsHole(child)) last = i;
|
|
217
|
+
});
|
|
218
|
+
let previous = null;
|
|
219
|
+
for (let i = 0; i <= last; i++) {
|
|
220
|
+
const child = el.children[i];
|
|
221
|
+
const expr = previous ? `${previous}.nextSibling` : `${elementRef}.firstChild`;
|
|
222
|
+
if (isHole(child)) {
|
|
223
|
+
const startRef = declare(expr);
|
|
224
|
+
const endRef = declare(`holeEnd(${startRef})`);
|
|
225
|
+
holes.push({ parentRef: elementRef, startRef, endRef, node: child });
|
|
226
|
+
previous = endRef;
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
const ref = declare(expr);
|
|
230
|
+
if (child.kind === "element" && containsHole(child)) walk2(child, ref);
|
|
231
|
+
previous = ref;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
function serializeWithMarkers(el) {
|
|
236
|
+
return serializeStatic(withSentinels(el)).split(SENTINEL).join(HOLE_START + HOLE_END);
|
|
237
|
+
}
|
|
238
|
+
function withSentinels(el) {
|
|
239
|
+
return {
|
|
240
|
+
...el,
|
|
241
|
+
children: el.children.map(
|
|
242
|
+
(child) => isHole(child) ? { kind: "text", value: SENTINEL } : child.kind === "element" ? withSentinels(child) : child
|
|
243
|
+
)
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
var SENTINEL = "\0fx-hole\0";
|
|
247
|
+
|
|
248
|
+
// src/codegen/contract.ts
|
|
249
|
+
var RUNTIME_CONTRACT_V1 = {
|
|
250
|
+
version: 1,
|
|
251
|
+
module: "@fluixi/dom",
|
|
252
|
+
symbols: [
|
|
253
|
+
"createNativeElement",
|
|
254
|
+
"insert",
|
|
255
|
+
"spread",
|
|
256
|
+
"setAttribute",
|
|
257
|
+
"delegateEvents",
|
|
258
|
+
"createComponent",
|
|
259
|
+
"createMemo",
|
|
260
|
+
"untrack",
|
|
261
|
+
"Show",
|
|
262
|
+
"For",
|
|
263
|
+
"Index",
|
|
264
|
+
"Switch",
|
|
265
|
+
"Match",
|
|
266
|
+
"Dynamic",
|
|
267
|
+
"ErrorBoundary"
|
|
268
|
+
]
|
|
269
|
+
};
|
|
270
|
+
var RUNTIME_CONTRACT_V2 = {
|
|
271
|
+
version: 2,
|
|
272
|
+
module: "@fluixi/dom",
|
|
273
|
+
symbols: [...RUNTIME_CONTRACT_V1.symbols, "template", "cloneTemplate", "walk"]
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
// src/analyze/intrinsics.ts
|
|
277
|
+
var REACTIVE_MODULE = "@fluixi/reactive/signal";
|
|
278
|
+
var INTRINSICS = {
|
|
279
|
+
$signal: { export: "signal", module: REACTIVE_MODULE, returns: "signal-handle" },
|
|
280
|
+
$memo: { export: "memo", module: REACTIVE_MODULE, returns: "memo-handle" },
|
|
281
|
+
$effect: { export: "effect", module: REACTIVE_MODULE, returns: "effect" },
|
|
282
|
+
$store: { export: "store", module: "@fluixi/reactive/store", returns: "store-handle" },
|
|
283
|
+
$resource: { export: "resource", module: REACTIVE_MODULE, returns: "resource-handle" },
|
|
284
|
+
// No short runtime form for these yet, so the intrinsic stands for the `create*`
|
|
285
|
+
// name. Still worth having: no import to write, and when a shorter form is
|
|
286
|
+
// designed, the change is this line rather than a compiler pass.
|
|
287
|
+
$selector: { export: "createSelector", module: REACTIVE_MODULE, returns: "memo-accessor" },
|
|
288
|
+
$deferred: { export: "createDeferred", module: REACTIVE_MODULE, returns: "memo-accessor" },
|
|
289
|
+
// Not value constructors, but the compiler has to know them: they change what a read
|
|
290
|
+
// does. `untrack` suppresses dependency collection, which is exactly the kind of thing
|
|
291
|
+
// dependency analysis must not guess at.
|
|
292
|
+
$untrack: { export: "untrack", module: REACTIVE_MODULE, returns: "plain" },
|
|
293
|
+
$untrackStore: { export: "untrackStore", module: "@fluixi/reactive/store", returns: "plain" }
|
|
294
|
+
};
|
|
295
|
+
var INTRINSIC_CALL = new RegExp(`\\$(?:${Object.keys(INTRINSICS).map((n) => n.slice(1)).join("|")})\\s*\\(`);
|
|
296
|
+
|
|
297
|
+
// src/transform/source-locations.ts
|
|
298
|
+
var SOURCE_MARKER = "__fx_source";
|
|
299
|
+
var HOLE_MARKER = "__fx_hole";
|
|
300
|
+
var markHole = (accessor, line, column, name) => `(globalThis.${HOLE_MARKER} ?? ((f) => f))(${accessor},${line},${column}${name === void 0 ? "" : `,${JSON.stringify(name)}`})`;
|
|
301
|
+
var PROPS_MARKER = "__fx_props";
|
|
302
|
+
var markProps = (object, at) => `(globalThis.${PROPS_MARKER} ?? ((p) => p))(${object},${JSON.stringify(at)})`;
|
|
303
|
+
|
|
304
|
+
// src/analyze/paren.ts
|
|
305
|
+
function skipQuoted(src, start) {
|
|
306
|
+
const quote = src[start];
|
|
307
|
+
for (let i = start + 1; i < src.length; i++) {
|
|
308
|
+
const c = src[i];
|
|
309
|
+
if (c === "\\") i++;
|
|
310
|
+
else if (c === quote) return i + 1;
|
|
311
|
+
else if (c === "\n") return -1;
|
|
312
|
+
}
|
|
313
|
+
return -1;
|
|
314
|
+
}
|
|
315
|
+
function skipTemplate(src, start) {
|
|
316
|
+
let i = start + 1;
|
|
317
|
+
while (i < src.length) {
|
|
318
|
+
const c = src[i];
|
|
319
|
+
if (c === "\\") {
|
|
320
|
+
i += 2;
|
|
321
|
+
} else if (c === "`") {
|
|
322
|
+
return i + 1;
|
|
323
|
+
} else if (c === "$" && src[i + 1] === "{") {
|
|
324
|
+
let depth = 1;
|
|
325
|
+
i += 2;
|
|
326
|
+
while (i < src.length && depth > 0) {
|
|
327
|
+
const k = src[i];
|
|
328
|
+
if (k === "\\") i += 2;
|
|
329
|
+
else if (k === "`") {
|
|
330
|
+
const end = skipTemplate(src, i);
|
|
331
|
+
if (end < 0) return -1;
|
|
332
|
+
i = end;
|
|
333
|
+
} else if (k === '"' || k === "'") {
|
|
334
|
+
const end = skipQuoted(src, i);
|
|
335
|
+
if (end < 0) return -1;
|
|
336
|
+
i = end;
|
|
337
|
+
} else {
|
|
338
|
+
if (k === "{") depth++;
|
|
339
|
+
else if (k === "}") depth--;
|
|
340
|
+
i++;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
} else {
|
|
344
|
+
i++;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
return -1;
|
|
348
|
+
}
|
|
349
|
+
function skipRegex(src, start) {
|
|
350
|
+
let inClass = false;
|
|
351
|
+
for (let i = start + 1; i < src.length; i++) {
|
|
352
|
+
const c = src[i];
|
|
353
|
+
if (c === "\\") i++;
|
|
354
|
+
else if (c === "[") inClass = true;
|
|
355
|
+
else if (c === "]") inClass = false;
|
|
356
|
+
else if (c === "\n") return -1;
|
|
357
|
+
else if (c === "/" && !inClass) {
|
|
358
|
+
let end = i + 1;
|
|
359
|
+
while (end < src.length && /[a-z]/i.test(src[end])) end++;
|
|
360
|
+
return end;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return -1;
|
|
364
|
+
}
|
|
365
|
+
function divides(prev) {
|
|
366
|
+
return prev !== "" && /[A-Za-z0-9_$)\]]/.test(prev);
|
|
367
|
+
}
|
|
368
|
+
function topLevelComma(src) {
|
|
369
|
+
let depth = 0;
|
|
370
|
+
let prev = "";
|
|
371
|
+
let i = 0;
|
|
372
|
+
while (i < src.length) {
|
|
373
|
+
const c = src[i];
|
|
374
|
+
if (c === '"' || c === "'") {
|
|
375
|
+
const end = skipQuoted(src, i);
|
|
376
|
+
if (end < 0) return null;
|
|
377
|
+
i = end;
|
|
378
|
+
prev = c;
|
|
379
|
+
} else if (c === "`") {
|
|
380
|
+
const end = skipTemplate(src, i);
|
|
381
|
+
if (end < 0) return null;
|
|
382
|
+
i = end;
|
|
383
|
+
prev = c;
|
|
384
|
+
} else if (c === "/" && src[i + 1] === "/") {
|
|
385
|
+
return null;
|
|
386
|
+
} else if (c === "/" && src[i + 1] === "*") {
|
|
387
|
+
const end = src.indexOf("*/", i + 2);
|
|
388
|
+
if (end < 0) return null;
|
|
389
|
+
i = end + 2;
|
|
390
|
+
} else if (c === "/" && !divides(prev)) {
|
|
391
|
+
const end = skipRegex(src, i);
|
|
392
|
+
if (end < 0) return null;
|
|
393
|
+
i = end;
|
|
394
|
+
prev = "/";
|
|
395
|
+
} else {
|
|
396
|
+
if (c === "(" || c === "[" || c === "{") depth++;
|
|
397
|
+
else if (c === ")" || c === "]" || c === "}") {
|
|
398
|
+
depth--;
|
|
399
|
+
if (depth < 0) return null;
|
|
400
|
+
} else if (c === "," && depth === 0) return true;
|
|
401
|
+
if (!/\s/.test(c)) prev = c;
|
|
402
|
+
i++;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
return depth === 0 ? false : null;
|
|
406
|
+
}
|
|
407
|
+
function paren(code, position) {
|
|
408
|
+
const src = code.trim();
|
|
409
|
+
if (src === "") return `(${code})`;
|
|
410
|
+
if (position === "arrowBody" && src[0] === "{") return `(${code})`;
|
|
411
|
+
return topLevelComma(src) === false ? code : `(${code})`;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// src/codegen/backends/imperative.ts
|
|
415
|
+
var CONTROL_COMPONENT = {
|
|
416
|
+
show: "Show",
|
|
417
|
+
for: "For",
|
|
418
|
+
index: "Index",
|
|
419
|
+
switch: "Switch",
|
|
420
|
+
match: "Match",
|
|
421
|
+
dynamic: "Dynamic",
|
|
422
|
+
errorBoundary: "ErrorBoundary",
|
|
423
|
+
suspense: "Suspense"
|
|
424
|
+
};
|
|
425
|
+
function isIdent(name) {
|
|
426
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
|
|
427
|
+
}
|
|
428
|
+
function propKey(name) {
|
|
429
|
+
return isIdent(name) ? name : JSON.stringify(name);
|
|
430
|
+
}
|
|
431
|
+
function propValue(p) {
|
|
432
|
+
if (p.expr !== void 0) {
|
|
433
|
+
if (p.name === "ref" && p.at)
|
|
434
|
+
return markHole(paren(p.expr, "value"), p.at.line, p.at.column, p.expr);
|
|
435
|
+
if (!p.reactive) return paren(p.expr, "value");
|
|
436
|
+
const accessor = `() => ${paren(p.expr, "arrowBody")}`;
|
|
437
|
+
return p.at ? markHole(accessor, p.at.line, p.at.column, p.directive) : accessor;
|
|
438
|
+
}
|
|
439
|
+
return JSON.stringify(p.literal ?? true);
|
|
440
|
+
}
|
|
441
|
+
function componentPropValue(p) {
|
|
442
|
+
if (p.expr !== void 0) return paren(p.expr, "value");
|
|
443
|
+
return JSON.stringify(p.literal ?? true);
|
|
444
|
+
}
|
|
445
|
+
function emitProps(props, childrenExpr, used) {
|
|
446
|
+
const spreads = props.filter((p) => p.kind === "spread");
|
|
447
|
+
const normal = props.filter((p) => p.kind !== "spread");
|
|
448
|
+
const entries = normal.map((p) => `${propKey(p.name)}: ${propValue(p)}`);
|
|
449
|
+
if (childrenExpr != null) entries.push(`children: ${childrenExpr}`);
|
|
450
|
+
const obj = `{ ${entries.join(", ")} }`;
|
|
451
|
+
if (spreads.length > 0) {
|
|
452
|
+
used.add("mergeProps");
|
|
453
|
+
return `mergeProps(${spreads.map((s) => s.expr).join(", ")}, ${obj})`;
|
|
454
|
+
}
|
|
455
|
+
return obj;
|
|
456
|
+
}
|
|
457
|
+
function markBind(code, at) {
|
|
458
|
+
return at ? markHole(code, at.line, at.column) : code;
|
|
459
|
+
}
|
|
460
|
+
function emitChildren(children, used, templates) {
|
|
461
|
+
if (children.length === 1) return emitNode(children[0], used, templates);
|
|
462
|
+
return `[${children.map((c) => emitNode(c, used, templates)).join(", ")}]`;
|
|
463
|
+
}
|
|
464
|
+
function emitComponent(name, props, children, used, templates, at) {
|
|
465
|
+
used.add("createMemo");
|
|
466
|
+
used.add("createComponent");
|
|
467
|
+
const spreads = props.filter((p) => p.kind === "spread");
|
|
468
|
+
const normal = props.filter((p) => p.kind !== "spread");
|
|
469
|
+
const entries = normal.map(
|
|
470
|
+
(p) => `get ${propKey(p.name)}() { return ${componentPropValue(p)}; }`
|
|
471
|
+
);
|
|
472
|
+
if (children.length > 0) {
|
|
473
|
+
entries.push(`get children() { return ${emitChildren(children, used, templates)}; }`);
|
|
474
|
+
}
|
|
475
|
+
let obj = `{ ${entries.join(", ")} }`;
|
|
476
|
+
if (spreads.length > 0) {
|
|
477
|
+
used.add("mergeProps");
|
|
478
|
+
obj = `mergeProps(${spreads.map((s) => s.expr).join(", ")}, ${obj})`;
|
|
479
|
+
}
|
|
480
|
+
const propAt = {};
|
|
481
|
+
for (const p of normal) if (p.at) propAt[p.name] = [p.at.line, p.at.column];
|
|
482
|
+
if (Object.keys(propAt).length) obj = markProps(obj, propAt);
|
|
483
|
+
const mark = at ? `globalThis.${SOURCE_MARKER}?.(${JSON.stringify(at.file)},${at.line},${at.column},${JSON.stringify(name)}), ` : "";
|
|
484
|
+
return `createMemo(${paren(`${mark}() => createComponent(${name}, ${obj})`, "value")})`;
|
|
485
|
+
}
|
|
486
|
+
function staticAttrAssignment(el, prop) {
|
|
487
|
+
const name = htmlAttrName(prop.name);
|
|
488
|
+
const value = prop.literal;
|
|
489
|
+
if (value === true || value === void 0) return `${el}.setAttribute(${JSON.stringify(name)}, "");`;
|
|
490
|
+
if (value === false || value === null) return "";
|
|
491
|
+
return `${el}.setAttribute(${JSON.stringify(name)}, ${JSON.stringify(String(value))});`;
|
|
492
|
+
}
|
|
493
|
+
var partial = false;
|
|
494
|
+
function emitNode(node, used, templates) {
|
|
495
|
+
switch (node.kind) {
|
|
496
|
+
case "text":
|
|
497
|
+
return JSON.stringify(node.value);
|
|
498
|
+
case "expr": {
|
|
499
|
+
if (!node.reactive) return paren(node.code, "value");
|
|
500
|
+
const accessor = `() => ${paren(node.code, "arrowBody")}`;
|
|
501
|
+
return node.at ? markHole(accessor, node.at.line, node.at.column) : accessor;
|
|
502
|
+
}
|
|
503
|
+
case "fragment":
|
|
504
|
+
if (node.children.length === 0) return "null";
|
|
505
|
+
return emitChildren(node.children, used, templates);
|
|
506
|
+
case "component":
|
|
507
|
+
return markBind(emitComponent(node.name, node.props, node.children, used, templates, node.at), node.bindAt ?? node.at);
|
|
508
|
+
case "control": {
|
|
509
|
+
const name = CONTROL_COMPONENT[node.control] ?? node.control;
|
|
510
|
+
used.add(name);
|
|
511
|
+
return markBind(emitComponent(name, node.props, node.children, used, templates), node.bindAt);
|
|
512
|
+
}
|
|
513
|
+
case "element": {
|
|
514
|
+
if (templates && node.static && !node.svg && isSerializable(node)) {
|
|
515
|
+
used.add("templateNode");
|
|
516
|
+
const id = `_tmpl$${templates.length}`;
|
|
517
|
+
templates.push({ id, html: serializeStatic(node), tag: node.tag, svg: false });
|
|
518
|
+
const at = templatePositions(node);
|
|
519
|
+
return at ? `templateNode(${id}, ${JSON.stringify(node.tag)}, false, false, ${JSON.stringify(at)})` : `templateNode(${id}, ${JSON.stringify(node.tag)})`;
|
|
520
|
+
}
|
|
521
|
+
if (templates && partial) {
|
|
522
|
+
const plan = planPartialTemplate(node);
|
|
523
|
+
if (plan) {
|
|
524
|
+
used.add("templateNode");
|
|
525
|
+
used.add("insert");
|
|
526
|
+
used.add("holeEnd");
|
|
527
|
+
used.add("holeContent");
|
|
528
|
+
used.add("holeScope");
|
|
529
|
+
const id = `_tmpl$${templates.length}`;
|
|
530
|
+
templates.push({ id, html: plan.html, tag: plan.tag, svg: false });
|
|
531
|
+
const at = templatePositions(node);
|
|
532
|
+
const lines = [
|
|
533
|
+
`const _el$ = templateNode(${id}, ${JSON.stringify(plan.tag)}, true${at ? `, false, ${JSON.stringify(at)}` : ""});`
|
|
534
|
+
];
|
|
535
|
+
for (const step of plan.steps) lines.push(`const ${step.ref} = ${step.expr};`);
|
|
536
|
+
for (const hole of plan.holes) {
|
|
537
|
+
lines.push(
|
|
538
|
+
`insert(${hole.parentRef}, holeScope(${hole.startRef}, () => ${paren(emitNode(hole.node, used, templates), "arrowBody")}), ${hole.endRef}, holeContent(${hole.startRef}, ${hole.endRef}));`
|
|
539
|
+
);
|
|
540
|
+
}
|
|
541
|
+
lines.push("return _el$;");
|
|
542
|
+
return `(() => { ${lines.join(" ")} })()`;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
used.add("createNativeElement");
|
|
546
|
+
const tag = JSON.stringify(node.tag);
|
|
547
|
+
const el = "_el$";
|
|
548
|
+
const parts = [];
|
|
549
|
+
const createArgs = node.at ? `${tag}, ${node.svg}, ${JSON.stringify(node.at)}` : node.svg ? `${tag}, true` : tag;
|
|
550
|
+
parts.push(`const ${el} = createNativeElement(${createArgs});`);
|
|
551
|
+
if (node.props.length > 0) {
|
|
552
|
+
if (!node.svg && node.props.every(isPlainLiteralAttr)) {
|
|
553
|
+
for (const p of node.props) parts.push(staticAttrAssignment(el, p));
|
|
554
|
+
} else {
|
|
555
|
+
used.add("spread");
|
|
556
|
+
const svgFlag = node.svg ? ", isSVG: true" : "";
|
|
557
|
+
parts.push(`spread({ element: ${el}, props: ${emitProps(node.props, null, used)}${svgFlag} });`);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
for (const child of node.children) {
|
|
561
|
+
used.add("insert");
|
|
562
|
+
const childCode = emitNode(child, used, templates);
|
|
563
|
+
const isFn = child.kind === "expr" && child.reactive || child.kind === "component" || child.kind === "control";
|
|
564
|
+
parts.push(isFn ? `insert(${el}, ${childCode}, null);` : `insert(${el}, ${childCode});`);
|
|
565
|
+
}
|
|
566
|
+
parts.push(`return ${el};`);
|
|
567
|
+
return `(() => { ${parts.join(" ")} })()`;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
function inlineTemplates(code, templates) {
|
|
572
|
+
if (!templates || templates.length === 0) return code;
|
|
573
|
+
const byId = new Map(templates.map((t) => [t.id, JSON.stringify(t.html)]));
|
|
574
|
+
return code.replace(/_tmpl\$\d+/g, (id) => byId.get(id) ?? id);
|
|
575
|
+
}
|
|
576
|
+
var imperativeBackend = {
|
|
577
|
+
name: "imperative",
|
|
578
|
+
contract: RUNTIME_CONTRACT_V1,
|
|
579
|
+
emit(node, options) {
|
|
580
|
+
const used = /* @__PURE__ */ new Set();
|
|
581
|
+
const templates = options?.templateClone !== false ? [] : void 0;
|
|
582
|
+
partial = options?.partialTemplates === true;
|
|
583
|
+
const code = emitNode(node, used, templates);
|
|
584
|
+
return { code, imports: Array.from(used), templates };
|
|
585
|
+
}
|
|
586
|
+
};
|
|
587
|
+
|
|
588
|
+
// src/lower/text.ts
|
|
589
|
+
function cleanTemplateText(value) {
|
|
590
|
+
const lines = value.split(/\r\n|\n|\r/);
|
|
591
|
+
let lastNonEmpty = 0;
|
|
592
|
+
for (let i = 0; i < lines.length; i++) {
|
|
593
|
+
if (/[^ \t]/.test(lines[i])) lastNonEmpty = i;
|
|
594
|
+
}
|
|
595
|
+
let out = "";
|
|
596
|
+
for (let i = 0; i < lines.length; i++) {
|
|
597
|
+
let line = lines[i].replace(/\t/g, " ");
|
|
598
|
+
if (i !== 0) line = line.replace(/^ +/, "");
|
|
599
|
+
if (i !== lines.length - 1) line = line.replace(/ +$/, "");
|
|
600
|
+
if (!line) continue;
|
|
601
|
+
if (i !== lastNonEmpty) line += " ";
|
|
602
|
+
out += line;
|
|
603
|
+
}
|
|
604
|
+
return out;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
// src/lower/template.ts
|
|
608
|
+
var DELEGATED = /* @__PURE__ */ new Set([
|
|
609
|
+
"click",
|
|
610
|
+
"dblclick",
|
|
611
|
+
"input",
|
|
612
|
+
"change",
|
|
613
|
+
"submit",
|
|
614
|
+
"focus",
|
|
615
|
+
"blur",
|
|
616
|
+
"keydown",
|
|
617
|
+
"keyup",
|
|
618
|
+
"keypress",
|
|
619
|
+
"mousedown",
|
|
620
|
+
"mouseup"
|
|
621
|
+
]);
|
|
622
|
+
function escapeTemplate(s) {
|
|
623
|
+
return s.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$\{/g, "\\${");
|
|
624
|
+
}
|
|
625
|
+
function capitalize(s) {
|
|
626
|
+
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
627
|
+
}
|
|
628
|
+
var Lowerer = class {
|
|
629
|
+
constructor(holes, used, sourceFile, positionAt) {
|
|
630
|
+
this.holes = holes;
|
|
631
|
+
this.used = used;
|
|
632
|
+
this.sourceFile = sourceFile;
|
|
633
|
+
this.positionAt = positionAt;
|
|
634
|
+
}
|
|
635
|
+
/**
|
|
636
|
+
* Where a tag is written, for the mark a component node carries.
|
|
637
|
+
*
|
|
638
|
+
* The parser reports offsets, and they are offsets into the file: the pieces it was given
|
|
639
|
+
* carry the positions the front-end recorded. Only built when the host asked for source
|
|
640
|
+
* locations, which is the only time either of these is set.
|
|
641
|
+
*/
|
|
642
|
+
tagAt(node) {
|
|
643
|
+
const start = node.loc?.start;
|
|
644
|
+
if (!this.sourceFile || !this.positionAt || start == null) return void 0;
|
|
645
|
+
const { line, column } = this.positionAt(start);
|
|
646
|
+
return { file: this.sourceFile, line, column };
|
|
647
|
+
}
|
|
648
|
+
hole(i) {
|
|
649
|
+
return this.holes[i] ?? { code: "undefined", reactive: false };
|
|
650
|
+
}
|
|
651
|
+
/** Emit a nested IR subtree to code (for `Show` fallback / `For` children). */
|
|
652
|
+
emit(node) {
|
|
653
|
+
const { code, imports, templates } = imperativeBackend.emit(node, {});
|
|
654
|
+
for (const s of imports) this.used.add(s);
|
|
655
|
+
return inlineTemplates(code, templates);
|
|
656
|
+
}
|
|
657
|
+
// --- content ------------------------------------------------------------
|
|
658
|
+
lowerRoot(children) {
|
|
659
|
+
const nodes = this.lowerChildren(children);
|
|
660
|
+
if (nodes.length === 1) return nodes[0];
|
|
661
|
+
return { kind: "fragment", children: nodes };
|
|
662
|
+
}
|
|
663
|
+
/** Lower a sibling list, handling text-drop and if/else/each restructuring. */
|
|
664
|
+
lowerChildren(children) {
|
|
665
|
+
const out = [];
|
|
666
|
+
for (let i = 0; i < children.length; i++) {
|
|
667
|
+
const child = children[i];
|
|
668
|
+
if (child.kind === "Element" || child.kind === "Component") {
|
|
669
|
+
const ifDir = child.attributes.find((a) => a.kind === "IfDirective");
|
|
670
|
+
if (ifDir && ifDir.kind === "IfDirective") {
|
|
671
|
+
let elseIdx = i + 1;
|
|
672
|
+
if (elseIdx < children.length && this.isBlankText(children[elseIdx])) elseIdx++;
|
|
673
|
+
const elseNode = children[elseIdx];
|
|
674
|
+
const hasElse = elseNode && (elseNode.kind === "Element" || elseNode.kind === "Component") && elseNode.attributes.some((a) => a.kind === "ElseDirective");
|
|
675
|
+
out.push(this.lowerIf(child, ifDir.hole, hasElse ? elseNode : null));
|
|
676
|
+
if (hasElse) i = elseIdx;
|
|
677
|
+
continue;
|
|
678
|
+
}
|
|
679
|
+
if (child.attributes.some((a) => a.kind === "EachDirective")) {
|
|
680
|
+
out.push(this.lowerEach(child));
|
|
681
|
+
continue;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
const node = this.lowerNode(child);
|
|
685
|
+
if (node) out.push(node);
|
|
686
|
+
}
|
|
687
|
+
return out;
|
|
688
|
+
}
|
|
689
|
+
isBlankText(n) {
|
|
690
|
+
return n.kind === "Text" && !n.raw && cleanTemplateText(n.value) === "";
|
|
691
|
+
}
|
|
692
|
+
lowerNode(node) {
|
|
693
|
+
switch (node.kind) {
|
|
694
|
+
case "Text": {
|
|
695
|
+
if (node.raw) return { kind: "text", value: node.value };
|
|
696
|
+
const cleaned = cleanTemplateText(node.value);
|
|
697
|
+
return cleaned ? { kind: "text", value: cleaned } : null;
|
|
698
|
+
}
|
|
699
|
+
case "Comment":
|
|
700
|
+
return null;
|
|
701
|
+
// dropped, like JSX comments
|
|
702
|
+
case "Expression": {
|
|
703
|
+
const h = this.hole(node.hole);
|
|
704
|
+
return { kind: "expr", code: h.code, reactive: h.reactive, ...h.at ? { at: h.at } : {} };
|
|
705
|
+
}
|
|
706
|
+
case "Fragment":
|
|
707
|
+
return { kind: "fragment", children: this.lowerChildren(node.children) };
|
|
708
|
+
case "Element":
|
|
709
|
+
return this.lowerElement(node);
|
|
710
|
+
case "Component":
|
|
711
|
+
return this.lowerComponent(node);
|
|
712
|
+
default:
|
|
713
|
+
return null;
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
lowerElement(el) {
|
|
717
|
+
const isAttr = el.attributes.find((a) => a.kind === "Attribute" && a.name === "is");
|
|
718
|
+
if (el.tag === "component" && isAttr && isAttr.value && isAttr.value.kind === "hole") {
|
|
719
|
+
const rest = el.attributes.filter((a) => a !== isAttr);
|
|
720
|
+
const node = this.lowerComponent({ ...el, kind: "Component", tag: "Dynamic", tagHole: null, attributes: rest });
|
|
721
|
+
node.props.unshift({ name: "component", kind: "attr", expr: `() => ${paren(this.hole(isAttr.value.hole).code, "arrowBody")}` });
|
|
722
|
+
return node;
|
|
723
|
+
}
|
|
724
|
+
const at = this.tagAt(el);
|
|
725
|
+
return {
|
|
726
|
+
kind: "element",
|
|
727
|
+
tag: el.tag,
|
|
728
|
+
svg: el.namespace === "svg",
|
|
729
|
+
props: this.lowerAttributes(el.attributes),
|
|
730
|
+
children: this.lowerChildren(el.children),
|
|
731
|
+
static: false,
|
|
732
|
+
...at ? { at } : {}
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
lowerComponent(el) {
|
|
736
|
+
const name = el.tagHole != null ? this.hole(el.tagHole).code : el.tag;
|
|
737
|
+
const props = this.lowerAttributes(el.attributes);
|
|
738
|
+
const { slots, rest } = this.partitionSlots(el.children);
|
|
739
|
+
for (const [slotName, nodes] of slots) {
|
|
740
|
+
const ir = nodes.length === 1 ? nodes[0] : { kind: "fragment", children: nodes };
|
|
741
|
+
const slotProp = {
|
|
742
|
+
name: slotName,
|
|
743
|
+
kind: "attr",
|
|
744
|
+
expr: this.emit(ir),
|
|
745
|
+
jsxElement: true
|
|
746
|
+
};
|
|
747
|
+
const clash = props.findIndex((p) => p.name === slotName);
|
|
748
|
+
if (clash >= 0) props[clash] = slotProp;
|
|
749
|
+
else props.push(slotProp);
|
|
750
|
+
}
|
|
751
|
+
const loadAttr = el.attributes.find((a) => a.kind === "LoadDirective");
|
|
752
|
+
const load = loadAttr ? parseLoadDirective(`load:${loadAttr.strategy}`, loadAttr.modifier) : void 0;
|
|
753
|
+
const bindAt = props.find((p) => p.at && p.reactive)?.at;
|
|
754
|
+
const at = this.tagAt(el);
|
|
755
|
+
return {
|
|
756
|
+
kind: "component",
|
|
757
|
+
name,
|
|
758
|
+
props,
|
|
759
|
+
children: this.lowerChildren(rest),
|
|
760
|
+
...at ? { at } : {},
|
|
761
|
+
...bindAt ? { bindAt } : {},
|
|
762
|
+
...load ? { load } : {}
|
|
763
|
+
};
|
|
764
|
+
}
|
|
765
|
+
/** Split a component's children into named slots (`slot="x"`) and the rest. */
|
|
766
|
+
partitionSlots(children) {
|
|
767
|
+
const slots = /* @__PURE__ */ new Map();
|
|
768
|
+
const rest = [];
|
|
769
|
+
for (const child of children) {
|
|
770
|
+
if (child.kind === "Element" || child.kind === "Component") {
|
|
771
|
+
const slot = child.attributes.find(
|
|
772
|
+
(a) => a.kind === "Attribute" && a.name === "slot"
|
|
773
|
+
);
|
|
774
|
+
if (slot && slot.value && slot.value.kind === "static") {
|
|
775
|
+
const stripped = { ...child, attributes: child.attributes.filter((a) => a !== slot) };
|
|
776
|
+
const ir = stripped.kind === "Element" ? this.lowerElement(stripped) : this.lowerComponent(stripped);
|
|
777
|
+
const bucket = slots.get(slot.value.value) ?? [];
|
|
778
|
+
bucket.push(ir);
|
|
779
|
+
slots.set(slot.value.value, bucket);
|
|
780
|
+
continue;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
rest.push(child);
|
|
784
|
+
}
|
|
785
|
+
return { slots, rest };
|
|
786
|
+
}
|
|
787
|
+
// --- control-flow sugar -------------------------------------------------
|
|
788
|
+
/** `<el if=${cond}>…</el>` (+ optional `<el else>`) → `<Show when fallback>`. */
|
|
789
|
+
lowerIf(el, condHole, elseEl) {
|
|
790
|
+
const cond = this.hole(condHole);
|
|
791
|
+
const props = [
|
|
792
|
+
{ name: "when", kind: "attr", expr: cond.code, reactive: cond.reactive, ...cond.at ? { at: cond.at } : {} }
|
|
793
|
+
];
|
|
794
|
+
if (elseEl) {
|
|
795
|
+
const elseIR = this.stripAndLower(elseEl, (a) => a.kind === "ElseDirective");
|
|
796
|
+
props.push({ name: "fallback", kind: "attr", expr: this.emit(elseIR), jsxElement: true });
|
|
797
|
+
}
|
|
798
|
+
const child = this.stripAndLower(el, (a) => a.kind === "IfDirective");
|
|
799
|
+
const at = this.tagAt(el);
|
|
800
|
+
return { kind: "component", name: "Show", props, children: [child], ...at ? { at } : {}, ...cond.at ? { bindAt: cond.at } : {} };
|
|
801
|
+
}
|
|
802
|
+
/** `<el each=${items} key?>${item => …}</el>` → `<For each>{item => <el>…</el>}</For>`. */
|
|
803
|
+
lowerEach(el) {
|
|
804
|
+
const eachDir = el.attributes.find((a) => a.kind === "EachDirective");
|
|
805
|
+
if (!eachDir || eachDir.kind !== "EachDirective") return this.lowerNode(el);
|
|
806
|
+
const each = this.hole(eachDir.hole);
|
|
807
|
+
const props = [{ name: "each", kind: "attr", expr: each.code, reactive: each.reactive, ...each.at ? { at: each.at } : {} }];
|
|
808
|
+
if (eachDir.key) {
|
|
809
|
+
const by = "static" in eachDir.key ? `(item) => item[${JSON.stringify(eachDir.key.static)}]` : this.hole(eachDir.key.hole).code;
|
|
810
|
+
props.push({ name: "by", kind: "attr", expr: by });
|
|
811
|
+
}
|
|
812
|
+
const arrow = this.itemArrow(el);
|
|
813
|
+
let childrenCode;
|
|
814
|
+
if (arrow) {
|
|
815
|
+
const bodyIR = { kind: "expr", code: arrow.body, reactive: arrow.bodyReactive };
|
|
816
|
+
const repeated = this.rebuildWithChildren(el, [bodyIR]);
|
|
817
|
+
childrenCode = `(${arrow.params.join(", ")}) => ${paren(this.emit(repeated), "arrowBody")}`;
|
|
818
|
+
} else {
|
|
819
|
+
const repeated = this.stripAndLower(el, (a) => a.kind === "EachDirective");
|
|
820
|
+
childrenCode = `() => ${paren(this.emit(repeated), "arrowBody")}`;
|
|
821
|
+
}
|
|
822
|
+
const children = [{ kind: "expr", code: childrenCode, reactive: false }];
|
|
823
|
+
const at = this.tagAt(el);
|
|
824
|
+
return { kind: "component", name: "For", props, children, ...at ? { at } : {}, ...each.at ? { bindAt: each.at } : {} };
|
|
825
|
+
}
|
|
826
|
+
/** The single `${item => expr}` child arrow, if that's the element's content. */
|
|
827
|
+
itemArrow(el) {
|
|
828
|
+
const kids = el.children.filter((c) => !this.isBlankText(c));
|
|
829
|
+
if (kids.length !== 1 || kids[0].kind !== "Expression") return null;
|
|
830
|
+
return this.hole(kids[0].hole).arrow ?? null;
|
|
831
|
+
}
|
|
832
|
+
/** Lower an element to IR, dropping the directive attribute matched by `drop`. */
|
|
833
|
+
stripAndLower(el, drop) {
|
|
834
|
+
const clone = { ...el, attributes: el.attributes.filter((a) => !drop(a)) };
|
|
835
|
+
return clone.kind === "Element" ? this.lowerElement(clone) : this.lowerComponent(clone);
|
|
836
|
+
}
|
|
837
|
+
/** Same element (minus each/key) but with explicit IR children. */
|
|
838
|
+
rebuildWithChildren(el, children) {
|
|
839
|
+
const props = this.lowerAttributes(
|
|
840
|
+
el.attributes.filter((a) => a.kind !== "EachDirective")
|
|
841
|
+
);
|
|
842
|
+
if (el.kind === "Element") {
|
|
843
|
+
const at = this.tagAt(el);
|
|
844
|
+
return {
|
|
845
|
+
kind: "element",
|
|
846
|
+
tag: el.tag,
|
|
847
|
+
svg: el.namespace === "svg",
|
|
848
|
+
props,
|
|
849
|
+
children,
|
|
850
|
+
static: false,
|
|
851
|
+
...at ? { at } : {}
|
|
852
|
+
};
|
|
853
|
+
}
|
|
854
|
+
return { kind: "component", name: el.tag, props, children };
|
|
855
|
+
}
|
|
856
|
+
// --- attributes ---------------------------------------------------------
|
|
857
|
+
lowerAttributes(attrs) {
|
|
858
|
+
const props = [];
|
|
859
|
+
const classEntries = [];
|
|
860
|
+
let classReactive = false;
|
|
861
|
+
let classAt;
|
|
862
|
+
const styleEntries = [];
|
|
863
|
+
let styleReactive = false;
|
|
864
|
+
let styleAt;
|
|
865
|
+
const usePairs = [];
|
|
866
|
+
for (const a of attrs) {
|
|
867
|
+
switch (a.kind) {
|
|
868
|
+
case "IfDirective":
|
|
869
|
+
case "EachDirective":
|
|
870
|
+
case "ElseDirective":
|
|
871
|
+
break;
|
|
872
|
+
// handled as control-flow at the parent level
|
|
873
|
+
case "Attribute":
|
|
874
|
+
props.push(this.plainAttr(a));
|
|
875
|
+
break;
|
|
876
|
+
case "PropertyBinding":
|
|
877
|
+
props.push({
|
|
878
|
+
name: a.name,
|
|
879
|
+
kind: "prop",
|
|
880
|
+
expr: this.hole(a.hole).code,
|
|
881
|
+
reactive: this.hole(a.hole).reactive,
|
|
882
|
+
...this.hole(a.hole).at ? { at: this.hole(a.hole).at } : {}
|
|
883
|
+
});
|
|
884
|
+
break;
|
|
885
|
+
case "EventBinding":
|
|
886
|
+
props.push(this.eventProp(a));
|
|
887
|
+
break;
|
|
888
|
+
case "RefBinding":
|
|
889
|
+
props.push({
|
|
890
|
+
name: "ref",
|
|
891
|
+
kind: "ref",
|
|
892
|
+
expr: this.hole(a.hole).code,
|
|
893
|
+
reactive: this.hole(a.hole).reactive,
|
|
894
|
+
// Which variable ends up holding the element, and where it was written.
|
|
895
|
+
...this.hole(a.hole).at ? { at: this.hole(a.hole).at } : {}
|
|
896
|
+
});
|
|
897
|
+
break;
|
|
898
|
+
case "Spread":
|
|
899
|
+
props.push({ name: "", kind: "spread", expr: this.hole(a.hole).code });
|
|
900
|
+
break;
|
|
901
|
+
case "ClassDirective": {
|
|
902
|
+
const h = this.hole(a.hole);
|
|
903
|
+
classEntries.push(`${JSON.stringify(a.name)}: ${h.code}`);
|
|
904
|
+
classReactive = classReactive || h.reactive;
|
|
905
|
+
classAt ??= h.at;
|
|
906
|
+
break;
|
|
907
|
+
}
|
|
908
|
+
case "StyleDirective": {
|
|
909
|
+
const h = this.hole(a.hole);
|
|
910
|
+
styleEntries.push(`${JSON.stringify(a.name)}: ${h.code}`);
|
|
911
|
+
styleReactive = styleReactive || h.reactive;
|
|
912
|
+
styleAt ??= h.at;
|
|
913
|
+
break;
|
|
914
|
+
}
|
|
915
|
+
case "BindDirective":
|
|
916
|
+
props.push(...this.bindProps(a.name, this.hole(a.hole)));
|
|
917
|
+
break;
|
|
918
|
+
case "UseDirective": {
|
|
919
|
+
const dir = a.name ?? (a.hole != null ? this.hole(a.hole).code : null);
|
|
920
|
+
if (!dir) break;
|
|
921
|
+
usePairs.push(a.name != null && a.hole != null ? `[${dir}, () => ${paren(this.hole(a.hole).code, "arrowBody")}]` : `[${dir}]`);
|
|
922
|
+
break;
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
if (classEntries.length > 0) {
|
|
927
|
+
props.push({
|
|
928
|
+
name: "classList",
|
|
929
|
+
kind: "attr",
|
|
930
|
+
expr: `{ ${classEntries.join(", ")} }`,
|
|
931
|
+
reactive: classReactive,
|
|
932
|
+
...classAt ? { at: classAt } : {}
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
if (styleEntries.length > 0) {
|
|
936
|
+
props.push({
|
|
937
|
+
name: "style",
|
|
938
|
+
kind: "attr",
|
|
939
|
+
expr: `{ ${styleEntries.join(", ")} }`,
|
|
940
|
+
reactive: styleReactive,
|
|
941
|
+
...styleAt ? { at: styleAt } : {}
|
|
942
|
+
});
|
|
943
|
+
}
|
|
944
|
+
if (usePairs.length > 0) {
|
|
945
|
+
props.push({ name: "use", kind: "attr", expr: `[${usePairs.join(", ")}]` });
|
|
946
|
+
}
|
|
947
|
+
return props;
|
|
948
|
+
}
|
|
949
|
+
/**
|
|
950
|
+
* Lower an event binding. Plain `@click`/`onClick` (no modifiers) stays a
|
|
951
|
+
* DELEGATED handler (unchanged, byte-identical with JSX). `on:click` or any
|
|
952
|
+
* modifier (`.capture`/`.once`/`.passive`/`.prevent`/`.stop`/`.self`) emits a
|
|
953
|
+
* NATIVE `on:name` prop the runtime attaches via addEventListener.
|
|
954
|
+
*/
|
|
955
|
+
eventProp(a) {
|
|
956
|
+
const dom = a.name.toLowerCase();
|
|
957
|
+
const handler = this.hole(a.hole).code;
|
|
958
|
+
const native = a.syntax === "colon" || a.modifiers.length > 0;
|
|
959
|
+
if (!native) {
|
|
960
|
+
return {
|
|
961
|
+
name: "on" + capitalize(a.name),
|
|
962
|
+
kind: "event",
|
|
963
|
+
event: { name: dom, delegated: DELEGATED.has(dom) },
|
|
964
|
+
expr: handler
|
|
965
|
+
};
|
|
966
|
+
}
|
|
967
|
+
const wrapped = this.wrapHandler(handler, a.modifiers);
|
|
968
|
+
const options = this.eventOptions(a.modifiers);
|
|
969
|
+
const value = options ? `[${wrapped}, ${options}]` : wrapped;
|
|
970
|
+
return { name: "on:" + dom, kind: "attr", expr: value };
|
|
971
|
+
}
|
|
972
|
+
/** Wrap a handler for `.prevent`/`.stop`/`.self` (no wrap when none apply). */
|
|
973
|
+
wrapHandler(handler, mods) {
|
|
974
|
+
const guard = mods.includes("self") ? "if (e.target !== e.currentTarget) return; " : "";
|
|
975
|
+
const pre = [];
|
|
976
|
+
if (mods.includes("prevent")) pre.push("e.preventDefault();");
|
|
977
|
+
if (mods.includes("stop")) pre.push("e.stopPropagation();");
|
|
978
|
+
if (!guard && pre.length === 0) return handler;
|
|
979
|
+
return `(e) => { ${guard}${pre.join(" ")} return (${handler})(e); }`;
|
|
980
|
+
}
|
|
981
|
+
/** addEventListener options object from `.capture`/`.once`/`.passive`, or null. */
|
|
982
|
+
eventOptions(mods) {
|
|
983
|
+
const opts = [];
|
|
984
|
+
if (mods.includes("capture")) opts.push("capture: true");
|
|
985
|
+
if (mods.includes("once")) opts.push("once: true");
|
|
986
|
+
if (mods.includes("passive")) opts.push("passive: true");
|
|
987
|
+
return opts.length ? `{ ${opts.join(", ")} }` : null;
|
|
988
|
+
}
|
|
989
|
+
/** `bind:value=${sig}` → `value={read()}` + `onInput={e => write(e…value)}`. */
|
|
990
|
+
bindProps(prop, h) {
|
|
991
|
+
const sigCode = h.code;
|
|
992
|
+
const checked = prop === "checked";
|
|
993
|
+
const evt = checked ? "change" : "input";
|
|
994
|
+
const accessor = checked ? "checked" : "value";
|
|
995
|
+
this.used.add("bindPair");
|
|
996
|
+
const sig = `bindPair(${sigCode})`;
|
|
997
|
+
return [
|
|
998
|
+
{
|
|
999
|
+
name: prop,
|
|
1000
|
+
kind: "attr",
|
|
1001
|
+
expr: `${sig}[0]()`,
|
|
1002
|
+
reactive: true,
|
|
1003
|
+
directive: `bind:${prop}`,
|
|
1004
|
+
...h.at ? { at: h.at } : {}
|
|
1005
|
+
},
|
|
1006
|
+
{
|
|
1007
|
+
name: "on" + capitalize(evt),
|
|
1008
|
+
kind: "event",
|
|
1009
|
+
event: { name: evt, delegated: DELEGATED.has(evt) },
|
|
1010
|
+
// delegated handler: `target` is the bound element (currentTarget is the root)
|
|
1011
|
+
expr: `(e) => ${sig}[1](e.target.${accessor})`
|
|
1012
|
+
}
|
|
1013
|
+
];
|
|
1014
|
+
}
|
|
1015
|
+
/** A plain attribute (incl. lit `?bool`, mixed values), old `mkProp` behavior. */
|
|
1016
|
+
plainAttr(a) {
|
|
1017
|
+
const v = a.value;
|
|
1018
|
+
let name = a.name;
|
|
1019
|
+
if (a.name === "class") {
|
|
1020
|
+
name = v && v.kind === "hole" && this.hole(v.hole).object ? "classList" : "className";
|
|
1021
|
+
} else if (a.name === "html") {
|
|
1022
|
+
name = "innerHTML";
|
|
1023
|
+
}
|
|
1024
|
+
const kind = "attr";
|
|
1025
|
+
const base = { name, kind };
|
|
1026
|
+
if (v == null) {
|
|
1027
|
+
base.literal = true;
|
|
1028
|
+
return base;
|
|
1029
|
+
}
|
|
1030
|
+
if (v.kind === "static") {
|
|
1031
|
+
base.literal = v.value;
|
|
1032
|
+
return base;
|
|
1033
|
+
}
|
|
1034
|
+
if (v.kind === "hole") {
|
|
1035
|
+
const h = this.hole(v.hole);
|
|
1036
|
+
base.expr = h.code;
|
|
1037
|
+
base.reactive = h.reactive;
|
|
1038
|
+
if (h.at) base.at = h.at;
|
|
1039
|
+
return base;
|
|
1040
|
+
}
|
|
1041
|
+
let reactive = false;
|
|
1042
|
+
let at;
|
|
1043
|
+
const body = v.parts.map((p) => {
|
|
1044
|
+
if ("text" in p) return escapeTemplate(p.text);
|
|
1045
|
+
const h = this.hole(p.hole);
|
|
1046
|
+
reactive = reactive || h.reactive;
|
|
1047
|
+
at ??= h.at;
|
|
1048
|
+
return "${" + h.code + "}";
|
|
1049
|
+
}).join("");
|
|
1050
|
+
base.expr = "`" + body + "`";
|
|
1051
|
+
base.reactive = reactive;
|
|
1052
|
+
if (at) base.at = at;
|
|
1053
|
+
return base;
|
|
1054
|
+
}
|
|
1055
|
+
};
|
|
1056
|
+
function lowerTemplate(pieces, holes, used, opts = {}) {
|
|
1057
|
+
const { root } = parseTemplate(pieces, { svg: opts.svg });
|
|
1058
|
+
return new Lowerer(holes, used, opts.sourceFile, opts.positionAt).lowerRoot(root.children);
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
// src/optimize/analyze-static.ts
|
|
1062
|
+
function propReason(prop) {
|
|
1063
|
+
switch (prop.kind) {
|
|
1064
|
+
case "event":
|
|
1065
|
+
return "event";
|
|
1066
|
+
case "ref":
|
|
1067
|
+
return "ref";
|
|
1068
|
+
case "spread":
|
|
1069
|
+
return "spread";
|
|
1070
|
+
case "class":
|
|
1071
|
+
case "style":
|
|
1072
|
+
return "class-or-style-directive";
|
|
1073
|
+
case "attr":
|
|
1074
|
+
case "prop":
|
|
1075
|
+
if (prop.reactive) return "reactive-prop";
|
|
1076
|
+
if (prop.expr !== void 0) return "expression-prop";
|
|
1077
|
+
return void 0;
|
|
1078
|
+
/* c8 ignore next 2 */
|
|
1079
|
+
default:
|
|
1080
|
+
return "expression-prop";
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
function analyzeStatic(root) {
|
|
1084
|
+
const staticElements = [];
|
|
1085
|
+
const reasons = /* @__PURE__ */ new Map();
|
|
1086
|
+
const visit = (node) => {
|
|
1087
|
+
switch (node.kind) {
|
|
1088
|
+
case "text":
|
|
1089
|
+
return true;
|
|
1090
|
+
case "expr":
|
|
1091
|
+
return false;
|
|
1092
|
+
case "component":
|
|
1093
|
+
case "control":
|
|
1094
|
+
for (const child of node.children) visit(child);
|
|
1095
|
+
return false;
|
|
1096
|
+
case "fragment":
|
|
1097
|
+
for (const child of node.children) visit(child);
|
|
1098
|
+
return false;
|
|
1099
|
+
case "element": {
|
|
1100
|
+
let childrenStatic = true;
|
|
1101
|
+
for (const child of node.children) {
|
|
1102
|
+
if (!visit(child)) childrenStatic = false;
|
|
1103
|
+
}
|
|
1104
|
+
let reason;
|
|
1105
|
+
for (const prop of node.props) {
|
|
1106
|
+
reason = propReason(prop);
|
|
1107
|
+
if (reason) break;
|
|
1108
|
+
}
|
|
1109
|
+
if (!reason && !childrenStatic) {
|
|
1110
|
+
reason = childReason(node);
|
|
1111
|
+
}
|
|
1112
|
+
node.static = !reason;
|
|
1113
|
+
if (reason) reasons.set(node, reason);
|
|
1114
|
+
else staticElements.push(node);
|
|
1115
|
+
return node.static;
|
|
1116
|
+
}
|
|
1117
|
+
/* c8 ignore next 2 */
|
|
1118
|
+
default:
|
|
1119
|
+
return false;
|
|
1120
|
+
}
|
|
1121
|
+
};
|
|
1122
|
+
for (const node of Array.isArray(root) ? root : [root]) visit(node);
|
|
1123
|
+
return { staticElements, reasons };
|
|
1124
|
+
}
|
|
1125
|
+
function childReason(node) {
|
|
1126
|
+
for (const child of node.children) {
|
|
1127
|
+
if (child.kind === "expr") return "expression-child";
|
|
1128
|
+
if (child.kind === "component") return "component-child";
|
|
1129
|
+
if (child.kind === "control") return "control-child";
|
|
1130
|
+
if (child.kind === "element" && !child.static) return "expression-child";
|
|
1131
|
+
if (child.kind === "fragment") return "expression-child";
|
|
1132
|
+
}
|
|
1133
|
+
return "expression-child";
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
// src/resolve/builtins.ts
|
|
1137
|
+
var DOM_CONTROL_FLOW = [
|
|
1138
|
+
"Show",
|
|
1139
|
+
"For",
|
|
1140
|
+
"Index",
|
|
1141
|
+
"Switch",
|
|
1142
|
+
"Match",
|
|
1143
|
+
"Portal",
|
|
1144
|
+
"Dynamic",
|
|
1145
|
+
"ErrorBoundary"
|
|
1146
|
+
];
|
|
1147
|
+
var CORE_CONTROL_FLOW = ["Suspense", "SuspenseList", "Await"];
|
|
1148
|
+
var ROUTER_COMPONENTS = ["Router", "Outlet", "Redirect", "Link"];
|
|
1149
|
+
var BUILTIN_COMPONENTS = [
|
|
1150
|
+
...DOM_CONTROL_FLOW,
|
|
1151
|
+
...CORE_CONTROL_FLOW,
|
|
1152
|
+
...ROUTER_COMPONENTS
|
|
1153
|
+
];
|
|
1154
|
+
var RESERVED_COMPONENTS = /* @__PURE__ */ new Set([
|
|
1155
|
+
...DOM_CONTROL_FLOW,
|
|
1156
|
+
...CORE_CONTROL_FLOW
|
|
1157
|
+
]);
|
|
1158
|
+
function builtinComponentMap(modules = {}) {
|
|
1159
|
+
const {
|
|
1160
|
+
controlFlowModule = "@fluixi/dom",
|
|
1161
|
+
coreModule = "@fluixi/core",
|
|
1162
|
+
routerModule = "@fluixi/core/router"
|
|
1163
|
+
} = modules;
|
|
1164
|
+
const map = {};
|
|
1165
|
+
for (const name of DOM_CONTROL_FLOW) map[name] = controlFlowModule;
|
|
1166
|
+
for (const name of CORE_CONTROL_FLOW) map[name] = coreModule;
|
|
1167
|
+
for (const name of ROUTER_COMPONENTS) map[name] = routerModule;
|
|
1168
|
+
return map;
|
|
1169
|
+
}
|
|
1170
|
+
function builtinComponents(modules = {}) {
|
|
1171
|
+
return { components: builtinComponentMap(modules) };
|
|
1172
|
+
}
|
|
1173
|
+
function routerComponents(routerModule) {
|
|
1174
|
+
const module = routerModule ?? "@fluixi/core/router";
|
|
1175
|
+
return { components: Object.fromEntries(ROUTER_COMPONENTS.map((n) => [n, module])) };
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
// src/resolve/resolve-ir.ts
|
|
1179
|
+
function resolveComponentSources(root, options = {}) {
|
|
1180
|
+
const { resolver, isBound, modules, strategyFor } = options;
|
|
1181
|
+
const builtins = builtinComponentMap(modules);
|
|
1182
|
+
const resolved = /* @__PURE__ */ new Map();
|
|
1183
|
+
const unresolved = /* @__PURE__ */ new Set();
|
|
1184
|
+
const annotate = (node) => {
|
|
1185
|
+
const { name } = node;
|
|
1186
|
+
if (!name || name.includes(".")) return;
|
|
1187
|
+
if (isBound?.(name)) return;
|
|
1188
|
+
const source = sourceFor(name);
|
|
1189
|
+
if (!source) {
|
|
1190
|
+
unresolved.add(name);
|
|
1191
|
+
return;
|
|
1192
|
+
}
|
|
1193
|
+
const loading = source.origin === "builtin" && RESERVED_COMPONENTS.has(name) ? EAGER : strategyFor?.(node) ?? EAGER;
|
|
1194
|
+
node.source = { ...source, loading };
|
|
1195
|
+
resolved.set(name, node.source);
|
|
1196
|
+
};
|
|
1197
|
+
const sourceFor = (name) => {
|
|
1198
|
+
if (RESERVED_COMPONENTS.has(name)) {
|
|
1199
|
+
return { module: builtins[name], export: name, origin: "builtin" };
|
|
1200
|
+
}
|
|
1201
|
+
const rule = resolver?.resolve(name);
|
|
1202
|
+
if (rule) return { module: rule.module, export: rule.export, origin: "rule" };
|
|
1203
|
+
if (ROUTER_COMPONENTS.includes(name)) {
|
|
1204
|
+
return { module: builtins[name], export: name, origin: "builtin" };
|
|
1205
|
+
}
|
|
1206
|
+
return void 0;
|
|
1207
|
+
};
|
|
1208
|
+
walk(root, (node) => {
|
|
1209
|
+
if (node.kind === "component") annotate(node);
|
|
1210
|
+
});
|
|
1211
|
+
return { resolved, unresolved: [...unresolved] };
|
|
1212
|
+
}
|
|
1213
|
+
function walk(root, visit) {
|
|
1214
|
+
const nodes = Array.isArray(root) ? root : [root];
|
|
1215
|
+
for (const node of nodes) {
|
|
1216
|
+
visit(node);
|
|
1217
|
+
const children = node.children;
|
|
1218
|
+
if (children) walk(children, visit);
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
// src/optimize/collapse-strategies.ts
|
|
1223
|
+
function collapseStrategies(root) {
|
|
1224
|
+
const components = [];
|
|
1225
|
+
walk(root, (node) => {
|
|
1226
|
+
if (node.kind === "component" && node.source) components.push(node);
|
|
1227
|
+
});
|
|
1228
|
+
const eager = /* @__PURE__ */ new Set();
|
|
1229
|
+
const never = /* @__PURE__ */ new Set();
|
|
1230
|
+
for (const node of components) {
|
|
1231
|
+
const { module, loading } = node.source;
|
|
1232
|
+
if (loading.kind === "eager") eager.add(module);
|
|
1233
|
+
if (loading.kind === "never") never.add(module);
|
|
1234
|
+
}
|
|
1235
|
+
const collapsed = /* @__PURE__ */ new Map();
|
|
1236
|
+
for (const node of components) {
|
|
1237
|
+
const source = node.source;
|
|
1238
|
+
if (!isDeferred(source.loading)) continue;
|
|
1239
|
+
if (!eager.has(source.module)) continue;
|
|
1240
|
+
source.loading = { kind: "eager" };
|
|
1241
|
+
collapsed.set(source.module, (collapsed.get(source.module) ?? 0) + 1);
|
|
1242
|
+
}
|
|
1243
|
+
return {
|
|
1244
|
+
collapsed,
|
|
1245
|
+
conflicted: [...never].filter((m) => eager.has(m))
|
|
1246
|
+
};
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
// src/resolve/component-resolver.ts
|
|
1250
|
+
var isMapRule = (rule) => "components" in rule;
|
|
1251
|
+
function substitute(template, match) {
|
|
1252
|
+
return template.replace(/\$(\d+)/g, (whole, index) => {
|
|
1253
|
+
const group = match[Number(index)];
|
|
1254
|
+
return group ?? whole;
|
|
1255
|
+
});
|
|
1256
|
+
}
|
|
1257
|
+
function normalise(name, entry) {
|
|
1258
|
+
return typeof entry === "string" ? { module: entry, export: name } : entry;
|
|
1259
|
+
}
|
|
1260
|
+
function createComponentResolver(rules = []) {
|
|
1261
|
+
const exact = /* @__PURE__ */ new Map();
|
|
1262
|
+
const conflicts = [];
|
|
1263
|
+
const patterns = [];
|
|
1264
|
+
for (const rule of rules) {
|
|
1265
|
+
if (!isMapRule(rule)) {
|
|
1266
|
+
patterns.push(rule);
|
|
1267
|
+
continue;
|
|
1268
|
+
}
|
|
1269
|
+
for (const [name, entry] of Object.entries(rule.components)) {
|
|
1270
|
+
const resolved = normalise(name, entry);
|
|
1271
|
+
const existing = exact.get(name);
|
|
1272
|
+
if (existing && existing.module !== resolved.module) {
|
|
1273
|
+
const conflict = conflicts.find((c) => c.name === name);
|
|
1274
|
+
if (conflict) conflict.modules.push(resolved.module);
|
|
1275
|
+
else conflicts.push({ name, modules: [existing.module, resolved.module] });
|
|
1276
|
+
continue;
|
|
1277
|
+
}
|
|
1278
|
+
exact.set(name, resolved);
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
const cache = /* @__PURE__ */ new Map();
|
|
1282
|
+
const resolve = (name) => {
|
|
1283
|
+
if (cache.has(name)) return cache.get(name);
|
|
1284
|
+
let result = exact.get(name);
|
|
1285
|
+
if (!result) {
|
|
1286
|
+
for (const rule of patterns) {
|
|
1287
|
+
const match = name.match(new RegExp(rule.match.source, rule.match.flags.replace("g", "")));
|
|
1288
|
+
if (!match) continue;
|
|
1289
|
+
result = {
|
|
1290
|
+
module: substitute(rule.module, match),
|
|
1291
|
+
export: rule.export ? substitute(rule.export, match) : name
|
|
1292
|
+
};
|
|
1293
|
+
break;
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
cache.set(name, result);
|
|
1297
|
+
return result;
|
|
1298
|
+
};
|
|
1299
|
+
return {
|
|
1300
|
+
resolve,
|
|
1301
|
+
names: () => [...exact.keys()],
|
|
1302
|
+
conflicts: () => conflicts
|
|
1303
|
+
};
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1306
|
+
// src/lower/compile-template.ts
|
|
1307
|
+
function toPieces(source) {
|
|
1308
|
+
const pieces = [];
|
|
1309
|
+
let offset = 0;
|
|
1310
|
+
source.statics.forEach((text, i) => {
|
|
1311
|
+
pieces.push({ kind: "static", text, start: offset });
|
|
1312
|
+
offset += text.length;
|
|
1313
|
+
if (i < source.holes.length) {
|
|
1314
|
+
const end = offset + source.holes[i].code.length;
|
|
1315
|
+
pieces.push({ kind: "hole", index: i, start: offset, end });
|
|
1316
|
+
offset = end;
|
|
1317
|
+
}
|
|
1318
|
+
});
|
|
1319
|
+
return pieces;
|
|
1320
|
+
}
|
|
1321
|
+
function compileTemplate(source, options = {}) {
|
|
1322
|
+
if (source.statics.length !== source.holes.length + 1) {
|
|
1323
|
+
throw new Error(
|
|
1324
|
+
`[fluixi] compileTemplate: ${source.statics.length} static runs for ${source.holes.length} holes — a template always has exactly one more.`
|
|
1325
|
+
);
|
|
1326
|
+
}
|
|
1327
|
+
return compilePieces(toPieces(source), source.holes, options);
|
|
1328
|
+
}
|
|
1329
|
+
function compilePieces(pieces, holes, options = {}) {
|
|
1330
|
+
const used = /* @__PURE__ */ new Set();
|
|
1331
|
+
const ir = lowerTemplate(pieces, [...holes], used, {
|
|
1332
|
+
svg: options.svg,
|
|
1333
|
+
...options.sourceFile ? { sourceFile: options.sourceFile } : {},
|
|
1334
|
+
...options.positionAt ? { positionAt: options.positionAt } : {}
|
|
1335
|
+
});
|
|
1336
|
+
analyzeStatic(ir);
|
|
1337
|
+
resolveComponentSources(ir, {
|
|
1338
|
+
resolver: createComponentResolver(options.resolve ?? []),
|
|
1339
|
+
modules: {
|
|
1340
|
+
controlFlowModule: options.controlFlowModule ?? "@fluixi/dom",
|
|
1341
|
+
coreModule: options.coreModule ?? "@fluixi/core",
|
|
1342
|
+
routerModule: options.routerModule ?? "@fluixi/core/router"
|
|
1343
|
+
},
|
|
1344
|
+
strategyFor: (node) => node.load
|
|
1345
|
+
});
|
|
1346
|
+
collapseStrategies(ir);
|
|
1347
|
+
const { code, imports, templates } = imperativeBackend.emit(ir, {
|
|
1348
|
+
templateClone: options.templateClone,
|
|
1349
|
+
partialTemplates: options.partialTemplates
|
|
1350
|
+
});
|
|
1351
|
+
for (const symbol of imports) used.add(symbol);
|
|
1352
|
+
if (options.hoistTemplates) return { code, imports: [...used], ir, templates };
|
|
1353
|
+
return { code: inlineTemplates(code, templates), imports: [...used], ir };
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1356
|
+
// src/resolve/component-globals.ts
|
|
1357
|
+
var DOM_GLOBALS = /* @__PURE__ */ new Set([
|
|
1358
|
+
"Text",
|
|
1359
|
+
"Image",
|
|
1360
|
+
"Option",
|
|
1361
|
+
"Audio",
|
|
1362
|
+
"Comment",
|
|
1363
|
+
"Range",
|
|
1364
|
+
"Selection",
|
|
1365
|
+
"Notification",
|
|
1366
|
+
"Event",
|
|
1367
|
+
"Document",
|
|
1368
|
+
"Element",
|
|
1369
|
+
"Node",
|
|
1370
|
+
"Screen",
|
|
1371
|
+
"Window",
|
|
1372
|
+
"Location",
|
|
1373
|
+
"History",
|
|
1374
|
+
"Storage",
|
|
1375
|
+
"Response",
|
|
1376
|
+
"Request",
|
|
1377
|
+
"Headers"
|
|
1378
|
+
]);
|
|
1379
|
+
function packageOf(specifier) {
|
|
1380
|
+
if (specifier.startsWith(".") || specifier.startsWith("/")) return void 0;
|
|
1381
|
+
if (/^[~@]\//.test(specifier)) return void 0;
|
|
1382
|
+
const parts = specifier.split("/");
|
|
1383
|
+
return specifier.startsWith("@") ? parts.slice(0, 2).join("/") : parts[0];
|
|
1384
|
+
}
|
|
1385
|
+
function namespaceAlias(module) {
|
|
1386
|
+
return `_${module.replace(/[^A-Za-z0-9]/g, "_")}`;
|
|
1387
|
+
}
|
|
1388
|
+
function componentGlobals(rules, options = {}) {
|
|
1389
|
+
const {
|
|
1390
|
+
available: availableList,
|
|
1391
|
+
builtins = true,
|
|
1392
|
+
intrinsics = true,
|
|
1393
|
+
generator = "@fluixi/compiler",
|
|
1394
|
+
specifier = (module) => module
|
|
1395
|
+
} = options;
|
|
1396
|
+
const all = [...rules];
|
|
1397
|
+
if (builtins) {
|
|
1398
|
+
all.push({ components: builtinComponentMap() });
|
|
1399
|
+
}
|
|
1400
|
+
const resolver = createComponentResolver(all);
|
|
1401
|
+
const available = availableList ? new Set(availableList) : void 0;
|
|
1402
|
+
const declared = [];
|
|
1403
|
+
const shadowedByDom = [];
|
|
1404
|
+
const unavailable = [];
|
|
1405
|
+
const resolved = /* @__PURE__ */ new Map();
|
|
1406
|
+
for (const name of [...resolver.names()].sort()) {
|
|
1407
|
+
const target = resolver.resolve(name);
|
|
1408
|
+
if (!target) continue;
|
|
1409
|
+
const pkg = packageOf(target.module);
|
|
1410
|
+
if (DOM_GLOBALS.has(name)) shadowedByDom.push(name);
|
|
1411
|
+
else if (available && pkg && !available.has(pkg)) unavailable.push(name);
|
|
1412
|
+
else {
|
|
1413
|
+
declared.push(name);
|
|
1414
|
+
resolved.set(name, target);
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
const intrinsicModules = intrinsics ? [...new Set(Object.values(INTRINSICS).map((i) => i.module))] : [];
|
|
1418
|
+
const modules = [
|
|
1419
|
+
.../* @__PURE__ */ new Set([...declared.map((name) => resolved.get(name).module), ...intrinsicModules])
|
|
1420
|
+
].sort();
|
|
1421
|
+
const imports = modules.map(
|
|
1422
|
+
(m) => `import * as ${namespaceAlias(m)} from '${specifier(m)}';`
|
|
1423
|
+
);
|
|
1424
|
+
const lines = declared.map((name) => {
|
|
1425
|
+
const target = resolved.get(name);
|
|
1426
|
+
const ns = namespaceAlias(target.module);
|
|
1427
|
+
return target.export === "default" ? ` const ${name}: typeof ${ns}.default;` : ` export import ${name} = ${ns}.${target.export};`;
|
|
1428
|
+
});
|
|
1429
|
+
const note = shadowedByDom.length > 0 ? `//
|
|
1430
|
+
// Import these explicitly: lib.dom already declares the name, so an ambient
|
|
3
1431
|
// declaration cannot win:
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1432
|
+
` + shadowedByDom.map((n) => {
|
|
1433
|
+
const module = resolver.resolve(n)?.module;
|
|
1434
|
+
return `// import { ${n} } from '${module ? specifier(module) : ""}';
|
|
1435
|
+
`;
|
|
1436
|
+
}).join("") : "";
|
|
1437
|
+
const intrinsicLines = intrinsics ? Object.entries(INTRINSICS).map(
|
|
1438
|
+
([name, intrinsic]) => ` const ${name}: typeof ${namespaceAlias(intrinsic.module)}.${intrinsic.export};`
|
|
1439
|
+
) : [];
|
|
1440
|
+
const patternRules = rules.filter((rule) => "match" in rule).length;
|
|
1441
|
+
const patternNote = patternRules > 0 ? `//
|
|
1442
|
+
// ${patternRules} pattern rule${patternRules === 1 ? "" : "s"} cannot be declared:
|
|
7
1443
|
// a pattern matches unboundedly many names, so there is no list to emit. Those
|
|
8
1444
|
// tags still resolve at build time; declare them by hand if the editor needs them.
|
|
9
|
-
|
|
1445
|
+
` : "";
|
|
1446
|
+
const source = `// GENERATED by ${generator}, do not edit.
|
|
10
1447
|
//
|
|
11
1448
|
// The components \`resolve\` supplies without an import. This describes what the
|
|
12
1449
|
// compiler already does; it does not enable it.
|
|
13
|
-
|
|
14
|
-
${
|
|
15
|
-
`)
|
|
16
|
-
`:`
|
|
17
|
-
`)+`
|
|
1450
|
+
` + note + patternNote + (imports.length > 0 ? `
|
|
1451
|
+
${imports.join("\n")}
|
|
1452
|
+
` : "\n") + `
|
|
18
1453
|
declare global {
|
|
19
|
-
${[...
|
|
20
|
-
|
|
1454
|
+
${[...lines, ...intrinsicLines].join("\n")}
|
|
1455
|
+
}
|
|
1456
|
+
`;
|
|
1457
|
+
return { source, declared, shadowedByDom, unavailable, patternRules };
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1460
|
+
// src/resolve/write-globals.ts
|
|
1461
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
1462
|
+
import { dirname, relative, sep, resolve as resolvePath } from "node:path";
|
|
1463
|
+
var GENERATED_MARKER = "GENERATED by @fluixi/compiler";
|
|
1464
|
+
var DEFAULT_GLOBALS_PATH = "fluixi.d.ts";
|
|
1465
|
+
function coveredByInclude(root, target) {
|
|
1466
|
+
let config;
|
|
1467
|
+
try {
|
|
1468
|
+
const raw = readFileSync(resolvePath(root, "tsconfig.json"), "utf8");
|
|
1469
|
+
config = JSON.parse(raw.replace(/^\s*\/\/.*$/gm, ""));
|
|
1470
|
+
} catch {
|
|
1471
|
+
return void 0;
|
|
1472
|
+
}
|
|
1473
|
+
if (config.extends !== void 0) return void 0;
|
|
1474
|
+
if (!Array.isArray(config.include)) return true;
|
|
1475
|
+
const relative2 = relativePosix(root, target);
|
|
1476
|
+
return config.include.some((entry) => {
|
|
1477
|
+
if (typeof entry !== "string") return false;
|
|
1478
|
+
const pattern = entry.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*\*\//g, "(?:.*/)?").replace(/\*/g, "[^/]*");
|
|
1479
|
+
return new RegExp(`^${pattern}(?:/.*)?$`).test(relative2);
|
|
1480
|
+
});
|
|
1481
|
+
}
|
|
1482
|
+
function relativePosix(from, to) {
|
|
1483
|
+
return relative(from, to).split(sep).join("/");
|
|
1484
|
+
}
|
|
1485
|
+
function forTypeScript(module, outDir, root) {
|
|
1486
|
+
if (!module.startsWith("/")) return module;
|
|
1487
|
+
const absolute = resolvePath(root, module.slice(1));
|
|
1488
|
+
let rel = relative(outDir, absolute).split(sep).join("/");
|
|
1489
|
+
if (!rel.startsWith(".")) rel = `./${rel}`;
|
|
1490
|
+
return rel.replace(/\.(tsx|ts|jsx|mts|cts)$/, ".js");
|
|
1491
|
+
}
|
|
1492
|
+
function projectDependencies(root) {
|
|
1493
|
+
try {
|
|
1494
|
+
const manifest = JSON.parse(readFileSync(resolvePath(root, "package.json"), "utf8"));
|
|
1495
|
+
return [
|
|
1496
|
+
...Object.keys(manifest.dependencies ?? {}),
|
|
1497
|
+
...Object.keys(manifest.devDependencies ?? {}),
|
|
1498
|
+
...Object.keys(manifest.peerDependencies ?? {})
|
|
1499
|
+
];
|
|
1500
|
+
} catch {
|
|
1501
|
+
return void 0;
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1504
|
+
function writeComponentGlobals(options, root) {
|
|
1505
|
+
if (options.globals === false) return { status: "skipped", reason: "disabled" };
|
|
1506
|
+
const rules = options.resolve ?? [];
|
|
1507
|
+
const target = resolvePath(root, options.globals ?? DEFAULT_GLOBALS_PATH);
|
|
1508
|
+
let existing;
|
|
1509
|
+
try {
|
|
1510
|
+
existing = readFileSync(target, "utf8");
|
|
1511
|
+
} catch {
|
|
1512
|
+
existing = void 0;
|
|
1513
|
+
}
|
|
1514
|
+
if (existing !== void 0 && !existing.includes(GENERATED_MARKER)) {
|
|
1515
|
+
return { status: "skipped", reason: "foreign-file", path: target };
|
|
1516
|
+
}
|
|
1517
|
+
const { source, declared } = componentGlobals(rules, {
|
|
1518
|
+
available: projectDependencies(root),
|
|
1519
|
+
specifier: (module) => forTypeScript(module, dirname(target), root)
|
|
1520
|
+
});
|
|
1521
|
+
if (existing === source) {
|
|
1522
|
+
return {
|
|
1523
|
+
status: "unchanged",
|
|
1524
|
+
path: target,
|
|
1525
|
+
declared: declared.length,
|
|
1526
|
+
warning: warnIfUnreachable(root, target)
|
|
1527
|
+
};
|
|
1528
|
+
}
|
|
1529
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
1530
|
+
writeFileSync(target, source);
|
|
1531
|
+
return {
|
|
1532
|
+
status: "written",
|
|
1533
|
+
path: target,
|
|
1534
|
+
declared: declared.length,
|
|
1535
|
+
warning: warnIfUnreachable(root, target)
|
|
1536
|
+
};
|
|
1537
|
+
}
|
|
1538
|
+
function warnIfUnreachable(root, target) {
|
|
1539
|
+
if (coveredByInclude(root, target) !== false) return void 0;
|
|
1540
|
+
const relative2 = relativePosix(root, target);
|
|
1541
|
+
return `[fluixi] ${relative2} is not covered by "include" in tsconfig.json, so TypeScript will ignore it and every resolved component stays "Cannot find name". Add "${relative2}" to "include".`;
|
|
21
1542
|
}
|
|
22
|
-
|
|
1543
|
+
|
|
1544
|
+
// src/options.ts
|
|
1545
|
+
var defaultOptions = {
|
|
1546
|
+
backend: "imperative",
|
|
1547
|
+
moduleName: "@fluixi/dom",
|
|
1548
|
+
delegateEvents: true,
|
|
1549
|
+
hydratable: false,
|
|
1550
|
+
sourceMaps: true,
|
|
1551
|
+
dev: false
|
|
1552
|
+
};
|
|
1553
|
+
|
|
1554
|
+
// src/index.ts
|
|
1555
|
+
var backends = {
|
|
1556
|
+
imperative: imperativeBackend
|
|
1557
|
+
};
|
|
1558
|
+
export {
|
|
1559
|
+
BUILTIN_COMPONENTS,
|
|
1560
|
+
CORE_CONTROL_FLOW,
|
|
1561
|
+
DEFAULT_GLOBALS_PATH,
|
|
1562
|
+
DOM_CONTROL_FLOW,
|
|
1563
|
+
RESERVED_COMPONENTS,
|
|
1564
|
+
ROUTER_COMPONENTS,
|
|
1565
|
+
RUNTIME_CONTRACT_V1,
|
|
1566
|
+
RUNTIME_CONTRACT_V2,
|
|
1567
|
+
backends,
|
|
1568
|
+
builtinComponentMap,
|
|
1569
|
+
builtinComponents,
|
|
1570
|
+
cleanTemplateText,
|
|
1571
|
+
compilePieces,
|
|
1572
|
+
compileTemplate,
|
|
1573
|
+
componentGlobals,
|
|
1574
|
+
createComponentResolver,
|
|
1575
|
+
defaultOptions,
|
|
1576
|
+
imperativeBackend,
|
|
1577
|
+
isReactiveShape,
|
|
1578
|
+
lowerTemplate,
|
|
1579
|
+
resolveComponentSources,
|
|
1580
|
+
routerComponents,
|
|
1581
|
+
walk,
|
|
1582
|
+
writeComponentGlobals
|
|
1583
|
+
};
|