@fluixi/compiler 1.0.0-alpha.82 → 1.0.0-alpha.84
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.mjs +41 -1
- package/dist/analyze/intrinsics.cjs +80 -1
- package/dist/analyze/intrinsics.d.cts +66 -0
- 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.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.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.mjs +17 -1
- package/dist/analyze/static-graph.cjs +811 -0
- package/dist/analyze/static-graph.d.cts +102 -0
- package/dist/analyze/static-graph.d.ts +103 -0
- package/dist/analyze/static-graph.d.ts.map +1 -0
- package/dist/analyze/static-graph.js +869 -0
- package/dist/analyze/static-graph.mjs +793 -0
- package/dist/babel-NRW4EFTG.mjs +636 -0
- package/dist/chunk-3YKZ4GNH.mjs +11 -0
- package/dist/chunk-EZR7NZOY.mjs +1718 -0
- package/dist/chunk-FSSWUXUR.mjs +16 -0
- package/dist/chunk-MJV3UXIL.mjs +158 -0
- package/dist/chunk-R2VHAZAX.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.map +1 -1
- package/dist/codegen/backends/imperative.js +68 -13
- 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.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.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 +16 -0
- package/dist/codegen/serialize-static.d.ts.map +1 -1
- package/dist/codegen/serialize-static.js +25 -0
- 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.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.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.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.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.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 +1594 -13
- package/dist/index.d.cts +27 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.mjs +1574 -13
- package/dist/integrations.cjs +4307 -25
- package/dist/integrations.d.cts +215 -0
- package/dist/integrations.d.ts +16 -0
- package/dist/integrations.d.ts.map +1 -1
- package/dist/integrations.js +21 -2
- package/dist/integrations.mjs +531 -17
- package/dist/ir/nodes.cjs +18 -1
- package/dist/ir/nodes.d.cts +168 -0
- package/dist/ir/nodes.d.ts +46 -0
- package/dist/ir/nodes.d.ts.map +1 -1
- package/dist/lower/compile-template.cjs +1356 -1
- package/dist/lower/compile-template.d.cts +69 -0
- package/dist/lower/compile-template.d.ts +12 -0
- package/dist/lower/compile-template.d.ts.map +1 -1
- package/dist/lower/compile-template.js +5 -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 +2 -0
- package/dist/lower/jsx.d.ts.map +1 -1
- package/dist/lower/jsx.js +27 -4
- 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 +14 -1
- package/dist/lower/template.d.ts.map +1 -1
- package/dist/lower/template.js +106 -19
- 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.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.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.mjs +67 -1
- package/dist/options.cjs +33 -1
- package/dist/options.d.cts +39 -0
- 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.mjs +53 -1
- package/dist/resolve/component-globals.cjs +233 -13
- package/dist/resolve/component-globals.d.cts +80 -0
- package/dist/resolve/component-globals.mjs +210 -13
- package/dist/resolve/component-resolver.cjs +80 -1
- package/dist/resolve/component-resolver.d.cts +68 -0
- 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.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.mjs +85 -1
- package/dist/resolve/write-globals.cjs +318 -13
- package/dist/resolve/write-globals.d.cts +30 -0
- package/dist/resolve/write-globals.mjs +297 -13
- package/dist/server-fns-4ZXN6DYU.mjs +78 -0
- package/dist/server-functions-V6D6DRIA.mjs +82 -0
- package/dist/transform/bindings.cjs +71 -1
- package/dist/transform/bindings.d.cts +16 -0
- 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.mjs +193 -3
- package/dist/transform/props.cjs +441 -3
- package/dist/transform/props.d.cts +45 -0
- 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.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.mjs +1155 -11
- package/dist/transform/source-locations.cjs +330 -0
- package/dist/transform/source-locations.d.cts +43 -0
- package/dist/transform/source-locations.d.ts +44 -0
- package/dist/transform/source-locations.d.ts.map +1 -0
- package/dist/transform/source-locations.js +238 -0
- package/dist/transform/source-locations.mjs +307 -0
- package/dist/transform/templates.cjs +2701 -7
- package/dist/transform/templates.d.cts +69 -0
- package/dist/transform/templates.d.ts +12 -4
- package/dist/transform/templates.d.ts.map +1 -1
- package/dist/transform/templates.js +72 -7
- package/dist/transform/templates.mjs +3743 -16
- package/dist/transform-BT4MMASR.mjs +893 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/package.json +6 -5
- package/dist/babel-4JUDAR2G.mjs +0 -1
- package/dist/chunk-3SAEGOMQ.mjs +0 -1
- package/dist/chunk-IBRM4W7I.mjs +0 -10
- package/dist/chunk-OHHZCYIQ.mjs +0 -1
- package/dist/chunk-PVR2SN3B.mjs +0 -1
- package/dist/chunk-QUIYULS2.mjs +0 -1
- package/dist/chunk-YR3SAEO7.mjs +0 -1
- package/dist/server-fns-6QM243HC.mjs +0 -2
- package/dist/server-functions-E3LKCZ4R.mjs +0 -1
- package/dist/transform-5WC4FWJE.mjs +0 -8
package/dist/transform/index.cjs
CHANGED
|
@@ -1,8 +1,2732 @@
|
|
|
1
|
-
"use strict";var xt=Object.create;var G=Object.defineProperty;var Rt=Object.getOwnPropertyDescriptor;var kt=Object.getOwnPropertyNames;var bt=Object.getPrototypeOf,St=Object.prototype.hasOwnProperty;var Et=(e,t)=>{for(var n in t)G(e,n,{get:t[n],enumerable:!0})},Ce=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of kt(t))!St.call(e,o)&&o!==n&&G(e,o,{get:()=>t[o],enumerable:!(r=Rt(t,o))||r.enumerable});return e};var $e=(e,t,n)=>(n=e!=null?xt(bt(e)):{},Ce(t||!e||!e.__esModule?G(n,"default",{value:e,enumerable:!0}):n,e)),It=e=>Ce(G({},"__esModule",{value:!0}),e);var Mn={};Et(Mn,{explainBail:()=>le,moduleBindings:()=>L,planPropsDestructure:()=>ae,shapeOf:()=>w,transformProps:()=>st,transformTemplates:()=>Nt});module.exports=It(Mn);var mt=require("@babel/parser"),gt=$e(require("magic-string"),1);function wt(e){switch(e.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 e.reactive?"reactive-prop":e.expr!==void 0?"expression-prop":void 0;default:return"expression-prop"}}function M(e){let t=[],n=new Map,r=o=>{switch(o.kind){case"text":return!0;case"expr":return!1;case"component":case"control":for(let s of o.children)r(s);return!1;case"fragment":for(let s of o.children)r(s);return!1;case"element":{let s=!0;for(let i of o.children)r(i)||(s=!1);let a;for(let i of o.props)if(a=wt(i),a)break;return!a&&!s&&(a=Ct(o)),o.static=!a,a?n.set(o,a):t.push(o),o.static}default:return!1}};for(let o of Array.isArray(e)?e:[e])r(o);return{staticElements:t,reasons:n}}function Ct(e){for(let t of e.children){if(t.kind==="expr")return"expression-child";if(t.kind==="component")return"component-child";if(t.kind==="control")return"control-child";if(t.kind==="element"&&!t.static||t.kind==="fragment")return"expression-child"}return"expression-child"}var Z={kind:"eager"},$t=["pointerdown","focusin","keydown"],Te=new Set(["eager","idle","visible","interaction","media","never"]),I=class extends Error{};function de(e){return e.startsWith("load:")}function Y(e,t){let n=e.slice(5);if(!Te.has(n))throw new I(`Unknown load strategy 'load:${n}'. Expected one of ${[...Te].join(", ")}.`);switch(n){case"eager":return Z;case"idle":return{kind:"idle"};case"never":return{kind:"never"};case"visible":return t?{kind:"visible",rootMargin:t}:{kind:"visible"};case"interaction":return{kind:"interaction",events:t?Tt(t):[...$t]};case"media":if(!t)throw new I(`'load:media' needs a query, e.g. load:media="(min-width: 1024px)".`);return{kind:"media",query:t};default:throw new I(`Unhandled load strategy '${n}'.`)}}function Tt(e){let t=e.split(/[\s,]+/).map(n=>n.trim()).filter(Boolean);if(t.length===0)throw new I("'load:interaction' was given no event names.");return t}function j(e){return e.kind!=="eager"&&e.kind!=="never"}var J=["Show","For","Index","Switch","Match","Portal","Dynamic","ErrorBoundary"],B=["Suspense","SuspenseList","Await"],O=["Router","Outlet","Redirect","Link"],Ln=[...J,...B,...O],ue=new Set([...J,...B]);function Pe(e={}){let{controlFlowModule:t="@fluixi/dom",coreModule:n="@fluixi/core",routerModule:r="@fluixi/core/router"}=e,o={};for(let s of J)o[s]=t;for(let s of B)o[s]=n;for(let s of O)o[s]=r;return o}function Q(e,t={}){let{resolver:n,isBound:r,modules:o,strategyFor:s}=t,a=Pe(o),i=new Map,l=new Set,c=f=>{let{name:d}=f;if(!d||d.includes(".")||r?.(d))return;let g=p(d);if(!g){l.add(d);return}let m=g.origin==="builtin"&&ue.has(d)?Z:s?.(f)??Z;f.source={...g,loading:m},i.set(d,f.source)},p=f=>{if(ue.has(f))return{module:a[f],export:f,origin:"builtin"};let d=n?.resolve(f);if(d)return{module:d.module,export:d.export,origin:"rule"};if(O.includes(f))return{module:a[f],export:f,origin:"builtin"}};return ee(e,f=>{f.kind==="component"&&c(f)}),{resolved:i,unresolved:[...l]}}function ee(e,t){let n=Array.isArray(e)?e:[e];for(let r of n){t(r);let o=r.children;o&&ee(o,t)}}function te(e){let t=[];ee(e,s=>{s.kind==="component"&&s.source&&t.push(s)});let n=new Set,r=new Set;for(let s of t){let{module:a,loading:i}=s.source;i.kind==="eager"&&n.add(a),i.kind==="never"&&r.add(a)}let o=new Map;for(let s of t){let a=s.source;j(a.loading)&&n.has(a.module)&&(a.loading={kind:"eager"},o.set(a.module,(o.get(a.module)??0)+1))}return{collapsed:o,conflicted:[...r].filter(s=>n.has(s))}}var Pt=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]),Me={className:"class",htmlFor:"for"},Mt=new Set(["innerHTML","outerHTML","innerText","textContent","value","checked","selected","muted","defaultValue","defaultChecked","children","ref"]),Ot=new Set(["script","style","textarea","title"]);function fe(e){return Ot.has(e)}function Oe(e){return Me[e]??e}function _(e){return e.kind!=="attr"||e.expr!==void 0||e.name.includes(":")?!1:!Mt.has(e.name)}function me(e){if(!e.static||fe(e.tag))return!1;for(let t of e.props)if(!_(t))return!1;for(let t of e.children)if(t.kind!=="text"&&!(t.kind==="element"&&me(t)))return!1;return!0}function F(e){let t=e.props.map(At).filter(Boolean).join(""),n=`<${e.tag}${t}>`;return Pt.has(e.tag)?n:`${n}${e.children.map(Dt).join("")}</${e.tag}>`}function Dt(e){if(e.kind==="text")return jt(e.value);if(e.kind!=="element")throw new Error(`serializeStatic: unexpected ${e.kind}`);return F(e)}function At(e){let t=Me[e.name]??e.name,n=e.literal;return n===!0||n===void 0?` ${t}`:n===!1||n===null?"":` ${t}="${Lt(String(n))}"`}function Lt(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function jt(e){return e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}var Jt="<!--fx-->",Bt="<!--fx/-->";function re(e){return e.kind==="expr"||e.kind==="component"||e.kind==="control"}function _t(e){return e.kind==="text"||e.kind==="element"&&De(e)}function De(e){return e.svg||fe(e.tag)||!e.props.every(t=>_(t))?!1:e.children.every(t=>_t(t)||re(t))}function ne(e){if(re(e))return!0;let t=e.children;return t?t.some(ne):!1}function Ae(e){for(let t=0;t<e.children.length;t++){let n=e.children[t];if(n.kind==="text"&&(n.value===""||e.children[t+1]?.kind==="text"))return!1}return e.children.every(t=>t.kind!=="element"||Ae(t))}function Le(e){if(!De(e)||!Ae(e)||!e.children.some(ne))return null;let t=[],n=[],r=0,o=a=>{let i=`_n$${r++}`;return t.push({ref:i,expr:a}),i};return s(e,"_el$"),{html:Ft(e),tag:e.tag,steps:t,holes:n};function s(a,i){let l=-1;a.children.forEach((p,f)=>{ne(p)&&(l=f)});let c=null;for(let p=0;p<=l;p++){let f=a.children[p],d=c?`${c}.nextSibling`:`${i}.firstChild`;if(re(f)){let m=o(d),h=o(`holeEnd(${m})`);n.push({parentRef:i,startRef:m,endRef:h,node:f}),c=h;continue}let g=o(d);f.kind==="element"&&ne(f)&&s(f,g),c=g}}}function Ft(e){return F(je(e)).split(Je).join(Jt+Bt)}function je(e){return{...e,children:e.children.map(t=>re(t)?{kind:"text",value:Je}:t.kind==="element"?je(t):t)}}var Je="\0fx-hole\0";var ge={version:1,module:"@fluixi/dom",symbols:["createNativeElement","insert","spread","setAttribute","delegateEvents","createComponent","createMemo","untrack","Show","For","Index","Switch","Match","Dynamic","ErrorBoundary"]},Wn={version:2,module:"@fluixi/dom",symbols:[...ge.symbols,"template","cloneTemplate","walk"]};var Ht={show:"Show",for:"For",index:"Index",switch:"Switch",match:"Match",dynamic:"Dynamic",errorBoundary:"ErrorBoundary",suspense:"Suspense"};function Vt(e){return/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(e)}function _e(e){return Vt(e)?e:JSON.stringify(e)}function Xt(e){return e.expr!==void 0?e.reactive?`() => (${e.expr})`:`(${e.expr})`:JSON.stringify(e.literal??!0)}function zt(e){return e.expr!==void 0?`(${e.expr})`:JSON.stringify(e.literal??!0)}function Ut(e,t,n){let r=e.filter(i=>i.kind==="spread"),s=e.filter(i=>i.kind!=="spread").map(i=>`${_e(i.name)}: ${Xt(i)}`);t!=null&&s.push(`children: ${t}`);let a=`{ ${s.join(", ")} }`;return r.length>0?(n.add("mergeProps"),`mergeProps(${r.map(i=>i.expr).join(", ")}, ${a})`):a}function Fe(e,t,n){return e.length===1?H(e[0],t,n):`[${e.map(r=>H(r,t,n)).join(", ")}]`}function Be(e,t,n,r,o){r.add("createMemo"),r.add("createComponent");let s=t.filter(c=>c.kind==="spread"),i=t.filter(c=>c.kind!=="spread").map(c=>`get ${_e(c.name)}() { return ${zt(c)}; }`);n.length>0&&i.push(`get children() { return ${Fe(n,r,o)}; }`);let l=`{ ${i.join(", ")} }`;return s.length>0&&(r.add("mergeProps"),l=`mergeProps(${s.map(c=>c.expr).join(", ")}, ${l})`),`createMemo(() => createComponent(${e}, ${l}))`}function Wt(e,t){let n=Oe(t.name),r=t.literal;return r===!0||r===void 0?`${e}.setAttribute(${JSON.stringify(n)}, "");`:r===!1||r===null?"":`${e}.setAttribute(${JSON.stringify(n)}, ${JSON.stringify(String(r))});`}var He=!1;function H(e,t,n){switch(e.kind){case"text":return JSON.stringify(e.value);case"expr":return e.reactive?`() => (${e.code})`:`(${e.code})`;case"fragment":return e.children.length===0?"null":Fe(e.children,t,n);case"component":return Be(e.name,e.props,e.children,t,n);case"control":{let r=Ht[e.control]??e.control;return t.add(r),Be(r,e.props,e.children,t,n)}case"element":{if(n&&e.static&&!e.svg&&me(e)){t.add("templateNode");let i=`_tmpl$${n.length}`;return n.push({id:i,html:F(e),tag:e.tag,svg:!1}),`templateNode(${i}, ${JSON.stringify(e.tag)})`}if(n&&He){let i=Le(e);if(i){t.add("templateNode"),t.add("insert"),t.add("holeEnd"),t.add("holeContent"),t.add("holeScope");let l=`_tmpl$${n.length}`;n.push({id:l,html:i.html,tag:i.tag,svg:!1});let c=[`const _el$ = templateNode(${l}, ${JSON.stringify(i.tag)}, true);`];for(let p of i.steps)c.push(`const ${p.ref} = ${p.expr};`);for(let p of i.holes)c.push(`insert(${p.parentRef}, holeScope(${p.startRef}, () => (${H(p.node,t,n)})), ${p.endRef}, holeContent(${p.startRef}, ${p.endRef}));`);return c.push("return _el$;"),`(() => { ${c.join(" ")} })()`}}t.add("createNativeElement");let r=JSON.stringify(e.tag),o="_el$",s=[],a=e.svg?`${r}, true`:r;if(s.push(`const ${o} = createNativeElement(${a});`),e.props.length>0)if(!e.svg&&e.props.every(_))for(let i of e.props)s.push(Wt(o,i));else{t.add("spread");let i=e.svg?", isSVG: true":"";s.push(`spread({ element: ${o}, props: ${Ut(e.props,null,t)}${i} });`)}for(let i of e.children){t.add("insert");let l=H(i,t,n),c=i.kind==="expr"&&i.reactive||i.kind==="component"||i.kind==="control";s.push(c?`insert(${o}, ${l}, null);`:`insert(${o}, ${l});`)}return s.push(`return ${o};`),`(() => { ${s.join(" ")} })()`}}}function oe(e,t){if(!t||t.length===0)return e;let n=new Map(t.map(r=>[r.id,JSON.stringify(r.html)]));return e.replace(/_tmpl\$\d+/g,r=>n.get(r)??r)}var D={name:"imperative",contract:ge,emit(e,t){let n=new Set,r=t?.templateClone!==!1?[]:void 0;return He=t?.partialTemplates===!0,{code:H(e,n,r),imports:Array.from(n),templates:r}}};var qt=e=>"components"in e;function Ve(e,t){return e.replace(/\$(\d+)/g,(n,r)=>t[Number(r)]??n)}function Kt(e,t){return typeof t=="string"?{module:t,export:e}:t}function se(e=[]){let t=new Map,n=[],r=[];for(let a of e){if(!qt(a)){r.push(a);continue}for(let[i,l]of Object.entries(a.components)){let c=Kt(i,l),p=t.get(i);if(p&&p.module!==c.module){let f=n.find(d=>d.name===i);f?f.modules.push(c.module):n.push({name:i,modules:[p.module,c.module]});continue}t.set(i,c)}}let o=new Map;return{resolve:a=>{if(o.has(a))return o.get(a);let i=t.get(a);if(!i)for(let l of r){let c=a.match(new RegExp(l.match.source,l.match.flags.replace("g","")));if(c){i={module:Ve(l.module,c),export:l.export?Ve(l.export,c):a};break}}return o.set(a,i),i},names:()=>[...t.keys()],conflicts:()=>n}}var Ue=require("@fluixi/template-parser");function V(e){let t=e.split(/\r\n|\n|\r/),n=0;for(let o=0;o<t.length;o++)/[^ \t]/.test(t[o])&&(n=o);let r="";for(let o=0;o<t.length;o++){let s=t[o].replace(/\t/g," ");o!==0&&(s=s.replace(/^ +/,"")),o!==t.length-1&&(s=s.replace(/ +$/,"")),s&&(o!==n&&(s+=" "),r+=s)}return r}var Xe=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function Gt(e){return e.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$\{/g,"\\${")}function ze(e){return e.charAt(0).toUpperCase()+e.slice(1)}var he=class{constructor(t,n){this.holes=t;this.used=n}hole(t){return this.holes[t]??{code:"undefined",reactive:!1}}emit(t){let{code:n,imports:r,templates:o}=D.emit(t,{});for(let s of r)this.used.add(s);return oe(n,o)}lowerRoot(t){let n=this.lowerChildren(t);return n.length===1?n[0]:{kind:"fragment",children:n}}lowerChildren(t){let n=[];for(let r=0;r<t.length;r++){let o=t[r];if(o.kind==="Element"||o.kind==="Component"){let a=o.attributes.find(i=>i.kind==="IfDirective");if(a&&a.kind==="IfDirective"){let i=r+1;i<t.length&&this.isBlankText(t[i])&&i++;let l=t[i],c=l&&(l.kind==="Element"||l.kind==="Component")&&l.attributes.some(p=>p.kind==="ElseDirective");n.push(this.lowerIf(o,a.hole,c?l:null)),c&&(r=i);continue}if(o.attributes.some(i=>i.kind==="EachDirective")){n.push(this.lowerEach(o));continue}}let s=this.lowerNode(o);s&&n.push(s)}return n}isBlankText(t){return t.kind==="Text"&&!t.raw&&V(t.value)===""}lowerNode(t){switch(t.kind){case"Text":{if(t.raw)return{kind:"text",value:t.value};let n=V(t.value);return n?{kind:"text",value:n}:null}case"Comment":return null;case"Expression":{let n=this.hole(t.hole);return{kind:"expr",code:n.code,reactive:n.reactive}}case"Fragment":return{kind:"fragment",children:this.lowerChildren(t.children)};case"Element":return this.lowerElement(t);case"Component":return this.lowerComponent(t);default:return null}}lowerElement(t){let n=t.attributes.find(r=>r.kind==="Attribute"&&r.name==="is");if(t.tag==="component"&&n&&n.value&&n.value.kind==="hole"){let r=t.attributes.filter(s=>s!==n),o=this.lowerComponent({...t,kind:"Component",tag:"Dynamic",tagHole:null,attributes:r});return o.props.unshift({name:"component",kind:"attr",expr:`() => (${this.hole(n.value.hole).code})`}),o}return{kind:"element",tag:t.tag,svg:t.namespace==="svg",props:this.lowerAttributes(t.attributes),children:this.lowerChildren(t.children),static:!1}}lowerComponent(t){let n=t.tagHole!=null?this.hole(t.tagHole).code:t.tag,r=this.lowerAttributes(t.attributes),{slots:o,rest:s}=this.partitionSlots(t.children);for(let[l,c]of o){let p=c.length===1?c[0]:{kind:"fragment",children:c},f={name:l,kind:"attr",expr:this.emit(p),jsxElement:!0},d=r.findIndex(g=>g.name===l);d>=0?r[d]=f:r.push(f)}let a=t.attributes.find(l=>l.kind==="LoadDirective"),i=a?Y(`load:${a.strategy}`,a.modifier):void 0;return{kind:"component",name:n,props:r,children:this.lowerChildren(s),...i?{load:i}:{}}}partitionSlots(t){let n=new Map,r=[];for(let o of t){if(o.kind==="Element"||o.kind==="Component"){let s=o.attributes.find(a=>a.kind==="Attribute"&&a.name==="slot");if(s&&s.value&&s.value.kind==="static"){let a={...o,attributes:o.attributes.filter(c=>c!==s)},i=a.kind==="Element"?this.lowerElement(a):this.lowerComponent(a),l=n.get(s.value.value)??[];l.push(i),n.set(s.value.value,l);continue}}r.push(o)}return{slots:n,rest:r}}lowerIf(t,n,r){let o=this.hole(n),s=[{name:"when",kind:"attr",expr:o.code,reactive:o.reactive}];if(r){let i=this.stripAndLower(r,l=>l.kind==="ElseDirective");s.push({name:"fallback",kind:"attr",expr:this.emit(i),jsxElement:!0})}let a=this.stripAndLower(t,i=>i.kind==="IfDirective");return{kind:"component",name:"Show",props:s,children:[a]}}lowerEach(t){let n=t.attributes.find(l=>l.kind==="EachDirective");if(!n||n.kind!=="EachDirective")return this.lowerNode(t);let r=this.hole(n.hole),o=[{name:"each",kind:"attr",expr:r.code,reactive:r.reactive}];if(n.key){let l="static"in n.key?`(item) => item[${JSON.stringify(n.key.static)}]`:this.hole(n.key.hole).code;o.push({name:"by",kind:"attr",expr:l})}let s=this.itemArrow(t),a;if(s){let l={kind:"expr",code:s.body,reactive:s.bodyReactive},c=this.rebuildWithChildren(t,[l]);a=`(${s.params.join(", ")}) => (${this.emit(c)})`}else{let l=this.stripAndLower(t,c=>c.kind==="EachDirective");a=`() => (${this.emit(l)})`}return{kind:"component",name:"For",props:o,children:[{kind:"expr",code:a,reactive:!1}]}}itemArrow(t){let n=t.children.filter(r=>!this.isBlankText(r));return n.length!==1||n[0].kind!=="Expression"?null:this.hole(n[0].hole).arrow??null}stripAndLower(t,n){let r={...t,attributes:t.attributes.filter(o=>!n(o))};return r.kind==="Element"?this.lowerElement(r):this.lowerComponent(r)}rebuildWithChildren(t,n){let r=this.lowerAttributes(t.attributes.filter(o=>o.kind!=="EachDirective"));return t.kind==="Element"?{kind:"element",tag:t.tag,svg:t.namespace==="svg",props:r,children:n,static:!1}:{kind:"component",name:t.tag,props:r,children:n}}lowerAttributes(t){let n=[],r=[],o=!1,s=[],a=!1,i=[];for(let l of t)switch(l.kind){case"IfDirective":case"EachDirective":case"ElseDirective":break;case"Attribute":n.push(this.plainAttr(l));break;case"PropertyBinding":n.push({name:l.name,kind:"prop",expr:this.hole(l.hole).code,reactive:this.hole(l.hole).reactive});break;case"EventBinding":n.push(this.eventProp(l));break;case"RefBinding":n.push({name:"ref",kind:"ref",expr:this.hole(l.hole).code,reactive:this.hole(l.hole).reactive});break;case"Spread":n.push({name:"",kind:"spread",expr:this.hole(l.hole).code});break;case"ClassDirective":{let c=this.hole(l.hole);r.push(`${JSON.stringify(l.name)}: ${c.code}`),o=o||c.reactive;break}case"StyleDirective":{let c=this.hole(l.hole);s.push(`${JSON.stringify(l.name)}: ${c.code}`),a=a||c.reactive;break}case"BindDirective":n.push(...this.bindProps(l.name,this.hole(l.hole).code));break;case"UseDirective":{let c=l.name??(l.hole!=null?this.hole(l.hole).code:null);if(!c)break;i.push(l.name!=null&&l.hole!=null?`[${c}, () => (${this.hole(l.hole).code})]`:`[${c}]`);break}}return r.length>0&&n.push({name:"classList",kind:"attr",expr:`{ ${r.join(", ")} }`,reactive:o}),s.length>0&&n.push({name:"style",kind:"attr",expr:`{ ${s.join(", ")} }`,reactive:a}),i.length>0&&n.push({name:"use",kind:"attr",expr:`[${i.join(", ")}]`}),n}eventProp(t){let n=t.name.toLowerCase(),r=this.hole(t.hole).code;if(!(t.syntax==="colon"||t.modifiers.length>0))return{name:"on"+ze(t.name),kind:"event",event:{name:n,delegated:Xe.has(n)},expr:r};let s=this.wrapHandler(r,t.modifiers),a=this.eventOptions(t.modifiers),i=a?`[${s}, ${a}]`:s;return{name:"on:"+n,kind:"attr",expr:i}}wrapHandler(t,n){let r=n.includes("self")?"if (e.target !== e.currentTarget) return; ":"",o=[];return n.includes("prevent")&&o.push("e.preventDefault();"),n.includes("stop")&&o.push("e.stopPropagation();"),!r&&o.length===0?t:`(e) => { ${r}${o.join(" ")} return (${t})(e); }`}eventOptions(t){let n=[];return t.includes("capture")&&n.push("capture: true"),t.includes("once")&&n.push("once: true"),t.includes("passive")&&n.push("passive: true"),n.length?`{ ${n.join(", ")} }`:null}bindProps(t,n){let r=t==="checked",o=r?"change":"input",s=r?"checked":"value";this.used.add("bindPair");let a=`bindPair(${n})`;return[{name:t,kind:"attr",expr:`${a}[0]()`,reactive:!0},{name:"on"+ze(o),kind:"event",event:{name:o,delegated:Xe.has(o)},expr:`(e) => ${a}[1](e.target.${s})`}]}plainAttr(t){let n=t.value,r=t.name;t.name==="class"?r=n&&n.kind==="hole"&&this.hole(n.hole).object?"classList":"className":t.name==="html"&&(r="innerHTML");let s={name:r,kind:"attr"};if(n==null)return s.literal=!0,s;if(n.kind==="static")return s.literal=n.value,s;if(n.kind==="hole"){let l=this.hole(n.hole);return s.expr=l.code,s.reactive=l.reactive,s}let a=!1,i=n.parts.map(l=>{if("text"in l)return Gt(l.text);let c=this.hole(l.hole);return a=a||c.reactive,"${"+c.code+"}"}).join("");return s.expr="`"+i+"`",s.reactive=a,s}};function We(e,t,n,r={}){let{root:o}=(0,Ue.parseTemplate)(e,{svg:r.svg});return new he(t,n).lowerRoot(o.children)}function qe(e,t,n={}){let r=new Set,o=We(e,[...t],r,{svg:n.svg});M(o),Q(o,{resolver:se(n.resolve??[]),modules:{controlFlowModule:n.controlFlowModule??"@fluixi/dom",coreModule:n.coreModule??"@fluixi/core",routerModule:n.routerModule??"@fluixi/core/router"},strategyFor:l=>l.load}),te(o);let{code:s,imports:a,templates:i}=D.emit(o,{templateClone:n.templateClone,partialTemplates:n.partialTemplates});for(let l of a)r.add(l);return n.hoistTemplates?{code:s,imports:[...r],ir:o,templates:i}:{code:oe(s,i),imports:[...r],ir:o}}var Zt="children";function A(e){switch(e.kind){case"call":return!0;case"member":return e.property!==Zt;case"compound":return e.parts.some(A);case"opaque":return!1}}var Yt=new Set(["CallExpression","OptionalCallExpression"]),Qt=new Set(["MemberExpression","OptionalMemberExpression"]);function w(e){if(!e)return{kind:"opaque"};if(Yt.has(e.type))return{kind:"call"};if(Qt.has(e.type)){let t=e.property;return{kind:"member",property:!(e.computed===!0)&&t?.type==="Identifier"?t.name:null}}return e.type==="ConditionalExpression"?{kind:"compound",parts:[e.test,e.consequent,e.alternate].map(X)}:e.type==="LogicalExpression"||e.type==="BinaryExpression"?{kind:"compound",parts:[e.left,e.right].map(X)}:e.type==="TemplateLiteral"?{kind:"compound",parts:e.expressions.map(X)}:e.type==="ObjectExpression"?{kind:"compound",parts:e.properties.filter(n=>(n.type==="ObjectProperty"||n.type==="Property")&&n.computed!==!0).map(n=>X(n.value))}:e.type==="ArrayExpression"?{kind:"compound",parts:e.elements.filter(n=>n!=null&&n.type!=="SpreadElement").map(X)}:{kind:"opaque"}}var X=e=>w(e);var en=new Set(["svg","path","circle","rect","line","polygon","polyline","ellipse","g","defs","clipPath","text"]),tn=/^on[A-Z]/,nn=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]),Ze=e=>A(w(e));function z(e){if(e.type==="StringLiteral"||e.type==="NumericLiteral"||e.type==="BooleanLiteral"||e.type==="Literal"&&(typeof e.value=="string"||typeof e.value=="number"||typeof e.value=="boolean"))return e.value}var ye=e=>!!e&&typeof z(e)=="string";function rn(e,t){return e.type==="JSXIdentifier"?{tag:e.name,component:e.name[0]!==e.name[0].toLowerCase()}:e.type==="JSXMemberExpression"?{tag:t.code(e),component:!0}:e.type==="JSXNamespacedName"?{tag:`${e.namespace.name}:${e.name.name}`,component:!1}:{tag:"div",component:!1}}function Ne(e){return e.type==="JSXIdentifier"?e.name==="class"?"className":e.name:e.type==="JSXNamespacedName"?`${e.namespace.name}:${e.name.name}`:"unknown"}function on(e,t){let n=Ne(e.name),r=tn.test(n),o=r||n.startsWith("on:")||n==="ref",a={name:n,kind:r?"event":"attr"};if(r){let l=n.slice(2).toLowerCase();a.event={name:l,delegated:nn.has(l)}}let i=e.value;if(i==null)return a.literal=!0,a;if(ye(i))return a.literal=z(i),a;if(i.type==="JSXExpressionContainer"&&i.expression?.type!=="JSXEmptyExpression"){let l=i.expression,c=z(l);return c!==void 0?(a.literal=c,a):(a.expr=t.code(l),a.reactive=o?!1:Ze(l),(l.type==="JSXElement"||l.type==="JSXFragment")&&(a.jsxElement=!0),a)}return a.literal=!0,a}function Ke(e,t){let n=e.value;return n==null?null:ye(n)?JSON.stringify(z(n)):n.type==="JSXExpressionContainer"&&n.expression?.type!=="JSXEmptyExpression"?t.code(n.expression):null}function sn(e,t){let n=[],r=[];for(let o of e){if(o.type==="JSXSpreadAttribute"){n.push({name:"",kind:"spread",expr:t.code(o.argument)});continue}if(o.type!=="JSXAttribute"||de(Ne(o.name)))continue;let s=o.name.type==="JSXNamespacedName"?o.name.namespace.name:null;if(s==="use"){let a=o.name.name.name;t.used.add(a);let i=Ke(o,t);r.push(i!=null?`[${a}, () => (${i})]`:`[${a}]`);continue}if(s==="oncapture"){let a=o.name.name.name.toLowerCase(),i=Ke(o,t)??"undefined";n.push({name:"on:"+a,kind:"attr",expr:`[${i}, { capture: true }]`});continue}n.push(on(o,t))}return r.length>0&&n.push({name:"use",kind:"attr",expr:`[${r.join(", ")}]`}),n}function Ge(e,t){let n=[];for(let r of e)if(r.type==="JSXText"){let o=V(r.value);o&&n.push({kind:"text",value:o})}else if(r.type==="JSXExpressionContainer"){if(r.expression?.type!=="JSXEmptyExpression"){let o=r.expression;n.push({kind:"expr",code:t.code(o),reactive:Ze(o)})}}else r.type==="JSXElement"||r.type==="JSXFragment"?n.push(Ye(r,t)):r.type==="JSXSpreadChild"&&n.push({kind:"expr",code:t.code(r.expression),reactive:!1});return n}function an(e){for(let t of e){if(t.type!=="JSXAttribute")continue;let n=Ne(t.name);if(de(n)){if(t.value&&!ye(t.value))throw new I(`'${n}' needs a literal value, not an expression — the strategy is compile-time.`);return Y(n,t.value?z(t.value):null)}}}function Ye(e,t){if(e.type==="JSXFragment")return{kind:"fragment",children:Ge(e.children,t)};let{tag:n,component:r}=rn(e.openingElement.name,t),o=sn(e.openingElement.attributes,t),s=Ge(e.children,t);if(r){let a=an(e.openingElement.attributes);return{kind:"component",name:n,props:o,children:s,...a?{load:a}:{}}}return{kind:"element",tag:n,svg:en.has(n),props:o,children:s,static:!1}}function Qe(e,t){let n=Ye(e,t);return M(n),n}function C(e,t){if(e)switch(e.type){case"Identifier":t.add(e.name);return;case"ObjectPattern":for(let n of e.properties)n.type==="RestElement"?C(n.argument,t):C(n.value,t);return;case"ArrayPattern":for(let n of e.elements)C(n,t);return;case"AssignmentPattern":C(e.left,t);return;case"RestElement":C(e.argument,t);return}}function et(e,t){switch(e.type){case"ImportDeclaration":for(let n of e.specifiers)C(n.local,t);return;case"VariableDeclaration":for(let n of e.declarations)C(n.id,t);return;case"FunctionDeclaration":case"ClassDeclaration":C(e.id,t);return;case"ExportNamedDeclaration":case"ExportDefaultDeclaration":e.declaration&&et(e.declaration,t);return}}function L(e){let t=new Set;for(let n of e.body)et(n,t);return t}var rt=require("@babel/parser"),ot=$e(require("magic-string"),1);function k(e,t){if(e)switch(e.type){case"Identifier":t.add(e.name);return;case"ObjectPattern":for(let n of e.properties)n.type==="RestElement"?k(n.argument,t):k(n.value,t);return;case"ArrayPattern":for(let n of e.elements)k(n,t);return;case"AssignmentPattern":k(e.left,t);return;case"RestElement":k(e.argument,t);return}}function ve(e){switch(e.type){case"StringLiteral":case"NumericLiteral":case"BooleanLiteral":case"NullLiteral":case"BigIntLiteral":case"RegExpLiteral":case"Identifier":return!0;case"Literal":return!0;case"TemplateLiteral":return e.expressions.every(ve);case"UnaryExpression":return ve(e.argument);case"MemberExpression":return!1;default:return!1}}function ie(e,t){if(!(!e||typeof e!="object")){t(e);for(let n of Object.keys(e)){if(n==="loc"||n==="range"||n==="leadingComments"||n==="trailingComments")continue;let r=e[n];if(Array.isArray(r))for(let o of r)o&&typeof o=="object"&&ie(o,t);else r&&typeof r=="object"&&typeof r.type=="string"&&ie(r,t)}}}function ln(e){let t=new Set;return ie(e,n=>{if(n.type==="AssignmentExpression")k(n.left,t);else if(n.type==="UpdateExpression"){let r=n.argument;r?.type==="Identifier"&&t.add(r.name)}}),t}function cn(e,t){let n=new Set,r=o=>{let s=new Set;k(o,s);for(let a of s)t.has(a)&&n.add(a)};return ie(e,o=>{switch(o.type){case"VariableDeclarator":r(o.id);return;case"FunctionDeclaration":case"FunctionExpression":case"ArrowFunctionExpression":r(o.id);for(let s of o.params??[])r(s);return;case"CatchClause":r(o.param);return;case"ClassDeclaration":case"ClassExpression":r(o.id);return}}),n}function tt(e,t,n){for(let r of e.properties??[]){if(r.type==="RestElement"){let p=r.argument;if(p?.type!=="Identifier"||t.length>0){let f=new Set;k(p,f);for(let d of f)n.bails.push({name:d,reason:"rest"});continue}n.rest={name:p.name,keys:[]};continue}if(r.computed){let p=new Set;k(r.value,p);for(let f of p)n.bails.push({name:f,reason:"computed"});continue}let o=r.key,s=o.type==="Identifier"?o.name:o.value,a=r.value,i;a.type==="AssignmentPattern"&&(i=a.right,a=a.left);let l=[...t,s];if(a.type==="Identifier"){if(i&&!ve(i)){n.bails.push({name:a.name,reason:"unsafe-default"});continue}n.reads.push({name:a.name,path:l,...i?{fallback:i}:{}});continue}if(a.type==="ObjectPattern"){if(i){let p=new Set;k(a,p);for(let f of p)n.bails.push({name:f,reason:"unsafe-default"});continue}tt(a,l,n);continue}let c=new Set;k(a,c);for(let p of c)n.bails.push({name:p,reason:"computed"})}}function ae(e,t){let n={reads:[],bails:[]};if(e?.type!=="ObjectPattern"||(tt(e,[],n),n.rest&&(t?.type!=="BlockStatement"?(n.bails.push({name:n.rest.name,reason:"rest"}),delete n.rest):n.rest.keys=(e.properties??[]).filter(i=>i.type!=="RestElement"&&!i.computed).map(i=>{let l=i.key;return l.type==="Identifier"?l.name:l.value})),n.reads.length===0&&!n.rest))return n;let r=new Set(n.reads.map(i=>i.name));n.rest&&r.add(n.rest.name);let o=ln(t),s=cn(t,r),a=[];for(let i of n.reads)o.has(i.name)?n.bails.push({name:i.name,reason:"reassigned"}):s.has(i.name)?n.bails.push({name:i.name,reason:"shadowed"}):a.push(i);return n.reads=a,n.rest&&o.has(n.rest.name)?(n.bails.push({name:n.rest.name,reason:"reassigned"}),delete n.rest):n.rest&&s.has(n.rest.name)&&(n.bails.push({name:n.rest.name,reason:"shadowed"}),delete n.rest),n}function le(e){switch(e){case"rest":return"this rest element cannot be served by splitProps, and copying the props into a plain object would lose the getters";case"computed":return"the property is not known until it runs";case"reassigned":return"the binding is assigned to, and props are read-only";case"shadowed":return"an inner scope binds the same name";case"unsafe-default":return"the default would have to run again on every read"}}function U(e,t,n=null){if(!(!e||typeof e!="object"||typeof e.type!="string")&&!e.type.startsWith("TS")&&t(e,n)!==!1)for(let r of Object.keys(e)){if(r==="loc"||r==="leadingComments"||r==="trailingComments")continue;let o=e[r];if(Array.isArray(o))for(let s of o)U(s,t,e);else o&&typeof o=="object"&&U(o,t,e)}}function nt(e){return!!e&&e[0]>="A"&&e[0]<="Z"}function pn(e){let t=[];return U(e,n=>{if(n.type==="FunctionDeclaration"&&nt(n.id?.name)){t.push(n);return}if(n.type==="VariableDeclarator"&&nt(n.id?.name)){let r=n.init;r&&(r.type==="ArrowFunctionExpression"||r.type==="FunctionExpression")&&t.push(r)}}),t}function dn(e){let t=new Set;return U(e,n=>{n.type==="Identifier"&&t.add(n.name)}),t}function un(e,t){if(!t)return!0;switch(t.type){case"MemberExpression":case"OptionalMemberExpression":return!(t.property===e&&!t.computed);case"ObjectProperty":case"ObjectMethod":case"ClassProperty":case"ClassMethod":return!(t.key===e&&!t.computed);case"LabeledStatement":case"BreakStatement":case"ContinueStatement":return t.label!==e;case"ImportSpecifier":case"ExportSpecifier":return!1;default:return!0}}function xe(e,t,n={}){let r=[],o=[],s=new Set;for(let a of pn(e)){let i=a.params?.[0];if(i?.type!=="ObjectPattern")continue;let l=ae(i,a.body);for(let m of l.bails)o.push({name:m.name,reason:m.reason,message:le(m.reason),start:i.start});if(l.bails.length>0||l.reads.length===0&&!l.rest)continue;let c=dn(a),p=n.parameterName??"props";for(;c.has(p);)p=`_${p}`;let f=new Map;for(let m of l.reads){let h=`${p}.${m.path.join(".")}`,v=m.fallback;f.set(m.name,v?`(${h} === undefined ? ${t.slice(v.start,v.end)} : ${h})`:h)}let d=i.typeAnnotation;if(r.push({start:i.start,end:d?.start??i.end,code:p}),l.rest){let m=l.rest.keys.map(v=>JSON.stringify(v)).join(", "),h=a.body;r.push({start:h.start+1,end:h.start+1,code:`
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
/*! @fluixi/compiler v1.0.0-alpha.84 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/transform/index.ts
|
|
32
|
+
var index_exports = {};
|
|
33
|
+
__export(index_exports, {
|
|
34
|
+
explainBail: () => explainBail,
|
|
35
|
+
moduleBindings: () => moduleBindings,
|
|
36
|
+
planPropsDestructure: () => planPropsDestructure,
|
|
37
|
+
shapeOf: () => shapeOf,
|
|
38
|
+
transformProps: () => transformProps,
|
|
39
|
+
transformTemplates: () => transformTemplates
|
|
40
|
+
});
|
|
41
|
+
module.exports = __toCommonJS(index_exports);
|
|
42
|
+
|
|
43
|
+
// src/transform/templates.ts
|
|
44
|
+
var import_parser2 = require("@babel/parser");
|
|
45
|
+
var import_magic_string2 = __toESM(require("magic-string"), 1);
|
|
46
|
+
|
|
47
|
+
// src/optimize/analyze-static.ts
|
|
48
|
+
function propReason(prop) {
|
|
49
|
+
switch (prop.kind) {
|
|
50
|
+
case "event":
|
|
51
|
+
return "event";
|
|
52
|
+
case "ref":
|
|
53
|
+
return "ref";
|
|
54
|
+
case "spread":
|
|
55
|
+
return "spread";
|
|
56
|
+
case "class":
|
|
57
|
+
case "style":
|
|
58
|
+
return "class-or-style-directive";
|
|
59
|
+
case "attr":
|
|
60
|
+
case "prop":
|
|
61
|
+
if (prop.reactive) return "reactive-prop";
|
|
62
|
+
if (prop.expr !== void 0) return "expression-prop";
|
|
63
|
+
return void 0;
|
|
64
|
+
/* c8 ignore next 2 */
|
|
65
|
+
default:
|
|
66
|
+
return "expression-prop";
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function analyzeStatic(root) {
|
|
70
|
+
const staticElements = [];
|
|
71
|
+
const reasons = /* @__PURE__ */ new Map();
|
|
72
|
+
const visit = (node) => {
|
|
73
|
+
switch (node.kind) {
|
|
74
|
+
case "text":
|
|
75
|
+
return true;
|
|
76
|
+
case "expr":
|
|
77
|
+
return false;
|
|
78
|
+
case "component":
|
|
79
|
+
case "control":
|
|
80
|
+
for (const child of node.children) visit(child);
|
|
81
|
+
return false;
|
|
82
|
+
case "fragment":
|
|
83
|
+
for (const child of node.children) visit(child);
|
|
84
|
+
return false;
|
|
85
|
+
case "element": {
|
|
86
|
+
let childrenStatic = true;
|
|
87
|
+
for (const child of node.children) {
|
|
88
|
+
if (!visit(child)) childrenStatic = false;
|
|
89
|
+
}
|
|
90
|
+
let reason;
|
|
91
|
+
for (const prop of node.props) {
|
|
92
|
+
reason = propReason(prop);
|
|
93
|
+
if (reason) break;
|
|
94
|
+
}
|
|
95
|
+
if (!reason && !childrenStatic) {
|
|
96
|
+
reason = childReason(node);
|
|
97
|
+
}
|
|
98
|
+
node.static = !reason;
|
|
99
|
+
if (reason) reasons.set(node, reason);
|
|
100
|
+
else staticElements.push(node);
|
|
101
|
+
return node.static;
|
|
102
|
+
}
|
|
103
|
+
/* c8 ignore next 2 */
|
|
104
|
+
default:
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
for (const node of Array.isArray(root) ? root : [root]) visit(node);
|
|
109
|
+
return { staticElements, reasons };
|
|
110
|
+
}
|
|
111
|
+
function childReason(node) {
|
|
112
|
+
for (const child of node.children) {
|
|
113
|
+
if (child.kind === "expr") return "expression-child";
|
|
114
|
+
if (child.kind === "component") return "component-child";
|
|
115
|
+
if (child.kind === "control") return "control-child";
|
|
116
|
+
if (child.kind === "element" && !child.static) return "expression-child";
|
|
117
|
+
if (child.kind === "fragment") return "expression-child";
|
|
118
|
+
}
|
|
119
|
+
return "expression-child";
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// src/resolve/load-directive.ts
|
|
123
|
+
var EAGER = { kind: "eager" };
|
|
124
|
+
var DEFAULT_INTERACTION_EVENTS = ["pointerdown", "focusin", "keydown"];
|
|
125
|
+
var STRATEGIES = /* @__PURE__ */ new Set(["eager", "idle", "visible", "interaction", "media", "never"]);
|
|
126
|
+
var LoadDirectiveError = class extends Error {
|
|
127
|
+
};
|
|
128
|
+
function isLoadDirective(name) {
|
|
129
|
+
return name.startsWith("load:");
|
|
130
|
+
}
|
|
131
|
+
function parseLoadDirective(name, value) {
|
|
132
|
+
const strategy = name.slice("load:".length);
|
|
133
|
+
if (!STRATEGIES.has(strategy)) {
|
|
134
|
+
throw new LoadDirectiveError(
|
|
135
|
+
`Unknown load strategy 'load:${strategy}'. Expected one of ${[...STRATEGIES].join(", ")}.`
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
switch (strategy) {
|
|
139
|
+
case "eager":
|
|
140
|
+
return EAGER;
|
|
141
|
+
case "idle":
|
|
142
|
+
return { kind: "idle" };
|
|
143
|
+
case "never":
|
|
144
|
+
return { kind: "never" };
|
|
145
|
+
case "visible":
|
|
146
|
+
return value ? { kind: "visible", rootMargin: value } : { kind: "visible" };
|
|
147
|
+
case "interaction":
|
|
148
|
+
return {
|
|
149
|
+
kind: "interaction",
|
|
150
|
+
events: value ? splitEvents(value) : [...DEFAULT_INTERACTION_EVENTS]
|
|
151
|
+
};
|
|
152
|
+
case "media":
|
|
153
|
+
if (!value) {
|
|
154
|
+
throw new LoadDirectiveError(
|
|
155
|
+
`'load:media' needs a query, e.g. load:media="(min-width: 1024px)".`
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
return { kind: "media", query: value };
|
|
159
|
+
default:
|
|
160
|
+
throw new LoadDirectiveError(`Unhandled load strategy '${strategy}'.`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
function splitEvents(value) {
|
|
164
|
+
const events = value.split(/[\s,]+/).map((e) => e.trim()).filter(Boolean);
|
|
165
|
+
if (events.length === 0) {
|
|
166
|
+
throw new LoadDirectiveError(`'load:interaction' was given no event names.`);
|
|
167
|
+
}
|
|
168
|
+
return events;
|
|
169
|
+
}
|
|
170
|
+
function isDeferred(strategy) {
|
|
171
|
+
return strategy.kind !== "eager" && strategy.kind !== "never";
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// src/resolve/builtins.ts
|
|
175
|
+
var DOM_CONTROL_FLOW = [
|
|
176
|
+
"Show",
|
|
177
|
+
"For",
|
|
178
|
+
"Index",
|
|
179
|
+
"Switch",
|
|
180
|
+
"Match",
|
|
181
|
+
"Portal",
|
|
182
|
+
"Dynamic",
|
|
183
|
+
"ErrorBoundary"
|
|
184
|
+
];
|
|
185
|
+
var CORE_CONTROL_FLOW = ["Suspense", "SuspenseList", "Await"];
|
|
186
|
+
var ROUTER_COMPONENTS = ["Router", "Outlet", "Redirect", "Link"];
|
|
187
|
+
var BUILTIN_COMPONENTS = [
|
|
188
|
+
...DOM_CONTROL_FLOW,
|
|
189
|
+
...CORE_CONTROL_FLOW,
|
|
190
|
+
...ROUTER_COMPONENTS
|
|
191
|
+
];
|
|
192
|
+
var RESERVED_COMPONENTS = /* @__PURE__ */ new Set([
|
|
193
|
+
...DOM_CONTROL_FLOW,
|
|
194
|
+
...CORE_CONTROL_FLOW
|
|
195
|
+
]);
|
|
196
|
+
function builtinComponentMap(modules = {}) {
|
|
197
|
+
const {
|
|
198
|
+
controlFlowModule = "@fluixi/dom",
|
|
199
|
+
coreModule = "@fluixi/core",
|
|
200
|
+
routerModule = "@fluixi/core/router"
|
|
201
|
+
} = modules;
|
|
202
|
+
const map = {};
|
|
203
|
+
for (const name of DOM_CONTROL_FLOW) map[name] = controlFlowModule;
|
|
204
|
+
for (const name of CORE_CONTROL_FLOW) map[name] = coreModule;
|
|
205
|
+
for (const name of ROUTER_COMPONENTS) map[name] = routerModule;
|
|
206
|
+
return map;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// src/resolve/resolve-ir.ts
|
|
210
|
+
function resolveComponentSources(root, options = {}) {
|
|
211
|
+
const { resolver, isBound, modules, strategyFor } = options;
|
|
212
|
+
const builtins = builtinComponentMap(modules);
|
|
213
|
+
const resolved = /* @__PURE__ */ new Map();
|
|
214
|
+
const unresolved = /* @__PURE__ */ new Set();
|
|
215
|
+
const annotate = (node) => {
|
|
216
|
+
const { name } = node;
|
|
217
|
+
if (!name || name.includes(".")) return;
|
|
218
|
+
if (isBound?.(name)) return;
|
|
219
|
+
const source = sourceFor(name);
|
|
220
|
+
if (!source) {
|
|
221
|
+
unresolved.add(name);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
const loading = source.origin === "builtin" && RESERVED_COMPONENTS.has(name) ? EAGER : strategyFor?.(node) ?? EAGER;
|
|
225
|
+
node.source = { ...source, loading };
|
|
226
|
+
resolved.set(name, node.source);
|
|
227
|
+
};
|
|
228
|
+
const sourceFor = (name) => {
|
|
229
|
+
if (RESERVED_COMPONENTS.has(name)) {
|
|
230
|
+
return { module: builtins[name], export: name, origin: "builtin" };
|
|
231
|
+
}
|
|
232
|
+
const rule = resolver?.resolve(name);
|
|
233
|
+
if (rule) return { module: rule.module, export: rule.export, origin: "rule" };
|
|
234
|
+
if (ROUTER_COMPONENTS.includes(name)) {
|
|
235
|
+
return { module: builtins[name], export: name, origin: "builtin" };
|
|
236
|
+
}
|
|
237
|
+
return void 0;
|
|
238
|
+
};
|
|
239
|
+
walk(root, (node) => {
|
|
240
|
+
if (node.kind === "component") annotate(node);
|
|
241
|
+
});
|
|
242
|
+
return { resolved, unresolved: [...unresolved] };
|
|
243
|
+
}
|
|
244
|
+
function walk(root, visit) {
|
|
245
|
+
const nodes = Array.isArray(root) ? root : [root];
|
|
246
|
+
for (const node of nodes) {
|
|
247
|
+
visit(node);
|
|
248
|
+
const children = node.children;
|
|
249
|
+
if (children) walk(children, visit);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// src/optimize/collapse-strategies.ts
|
|
254
|
+
function collapseStrategies(root) {
|
|
255
|
+
const components = [];
|
|
256
|
+
walk(root, (node) => {
|
|
257
|
+
if (node.kind === "component" && node.source) components.push(node);
|
|
258
|
+
});
|
|
259
|
+
const eager = /* @__PURE__ */ new Set();
|
|
260
|
+
const never = /* @__PURE__ */ new Set();
|
|
261
|
+
for (const node of components) {
|
|
262
|
+
const { module: module2, loading } = node.source;
|
|
263
|
+
if (loading.kind === "eager") eager.add(module2);
|
|
264
|
+
if (loading.kind === "never") never.add(module2);
|
|
265
|
+
}
|
|
266
|
+
const collapsed = /* @__PURE__ */ new Map();
|
|
267
|
+
for (const node of components) {
|
|
268
|
+
const source = node.source;
|
|
269
|
+
if (!isDeferred(source.loading)) continue;
|
|
270
|
+
if (!eager.has(source.module)) continue;
|
|
271
|
+
source.loading = { kind: "eager" };
|
|
272
|
+
collapsed.set(source.module, (collapsed.get(source.module) ?? 0) + 1);
|
|
273
|
+
}
|
|
274
|
+
return {
|
|
275
|
+
collapsed,
|
|
276
|
+
conflicted: [...never].filter((m) => eager.has(m))
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// src/codegen/serialize-static.ts
|
|
281
|
+
var VOID_ELEMENTS = /* @__PURE__ */ new Set([
|
|
282
|
+
"area",
|
|
283
|
+
"base",
|
|
284
|
+
"br",
|
|
285
|
+
"col",
|
|
286
|
+
"embed",
|
|
287
|
+
"hr",
|
|
288
|
+
"img",
|
|
289
|
+
"input",
|
|
290
|
+
"link",
|
|
291
|
+
"meta",
|
|
292
|
+
"param",
|
|
293
|
+
"source",
|
|
294
|
+
"track",
|
|
295
|
+
"wbr"
|
|
296
|
+
]);
|
|
297
|
+
var HTML_NAME = {
|
|
298
|
+
className: "class",
|
|
299
|
+
htmlFor: "for"
|
|
300
|
+
};
|
|
301
|
+
var PROPERTY_ONLY = /* @__PURE__ */ new Set([
|
|
302
|
+
"innerHTML",
|
|
303
|
+
"outerHTML",
|
|
304
|
+
"innerText",
|
|
305
|
+
"textContent",
|
|
306
|
+
"value",
|
|
307
|
+
"checked",
|
|
308
|
+
"selected",
|
|
309
|
+
"muted",
|
|
310
|
+
"defaultValue",
|
|
311
|
+
"defaultChecked",
|
|
312
|
+
"children",
|
|
313
|
+
"ref"
|
|
314
|
+
]);
|
|
315
|
+
var RAW_TEXT_ELEMENTS = /* @__PURE__ */ new Set(["script", "style", "textarea", "title"]);
|
|
316
|
+
function isRawTextElement(tag) {
|
|
317
|
+
return RAW_TEXT_ELEMENTS.has(tag);
|
|
318
|
+
}
|
|
319
|
+
function htmlAttrName(name) {
|
|
320
|
+
return HTML_NAME[name] ?? name;
|
|
321
|
+
}
|
|
322
|
+
function isPlainLiteralAttr(prop) {
|
|
323
|
+
if (prop.kind !== "attr") return false;
|
|
324
|
+
if (prop.expr !== void 0) return false;
|
|
325
|
+
if (prop.name.includes(":")) return false;
|
|
326
|
+
return !PROPERTY_ONLY.has(prop.name);
|
|
327
|
+
}
|
|
328
|
+
function isSerializable(node) {
|
|
329
|
+
if (!node.static) return false;
|
|
330
|
+
if (isRawTextElement(node.tag)) return false;
|
|
331
|
+
for (const prop of node.props) {
|
|
332
|
+
if (!isPlainLiteralAttr(prop)) return false;
|
|
333
|
+
}
|
|
334
|
+
for (const child of node.children) {
|
|
335
|
+
if (child.kind === "text") continue;
|
|
336
|
+
if (child.kind === "element" && isSerializable(child)) continue;
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
return true;
|
|
340
|
+
}
|
|
341
|
+
function serializeStatic(node) {
|
|
342
|
+
const attrs = node.props.map(serializeProp).filter(Boolean).join("");
|
|
343
|
+
const open = `<${node.tag}${attrs}>`;
|
|
344
|
+
if (VOID_ELEMENTS.has(node.tag)) return open;
|
|
345
|
+
return `${open}${node.children.map(serializeChild).join("")}</${node.tag}>`;
|
|
346
|
+
}
|
|
347
|
+
function serializeChild(child) {
|
|
348
|
+
if (child.kind === "text") return escapeText(child.value);
|
|
349
|
+
if (child.kind !== "element") throw new Error(`serializeStatic: unexpected ${child.kind}`);
|
|
350
|
+
return serializeStatic(child);
|
|
351
|
+
}
|
|
352
|
+
function serializeProp(prop) {
|
|
353
|
+
const name = HTML_NAME[prop.name] ?? prop.name;
|
|
354
|
+
const value = prop.literal;
|
|
355
|
+
if (value === true || value === void 0) return ` ${name}`;
|
|
356
|
+
if (value === false || value === null) return "";
|
|
357
|
+
return ` ${name}="${escapeAttr(String(value))}"`;
|
|
358
|
+
}
|
|
359
|
+
function escapeAttr(value) {
|
|
360
|
+
return value.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
361
|
+
}
|
|
362
|
+
function escapeText(value) {
|
|
363
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
364
|
+
}
|
|
365
|
+
function templatePositions(root) {
|
|
366
|
+
const pos = [];
|
|
367
|
+
let file;
|
|
368
|
+
const walk8 = (el) => {
|
|
369
|
+
file ??= el.at?.file;
|
|
370
|
+
pos.push(el.at?.line ?? 0, el.at?.column ?? 0);
|
|
371
|
+
for (const child of el.children) if (child.kind === "element") walk8(child);
|
|
372
|
+
};
|
|
373
|
+
walk8(root);
|
|
374
|
+
return file && pos.some((n) => n !== 0) ? { file, pos } : void 0;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// src/codegen/partial-template.ts
|
|
378
|
+
var HOLE_START = "<!--fx-->";
|
|
379
|
+
var HOLE_END = "<!--fx/-->";
|
|
380
|
+
function isHole(node) {
|
|
381
|
+
return node.kind === "expr" || node.kind === "component" || node.kind === "control";
|
|
382
|
+
}
|
|
383
|
+
function isTemplateChild(node) {
|
|
384
|
+
return node.kind === "text" || node.kind === "element" && isTemplateElement(node);
|
|
385
|
+
}
|
|
386
|
+
function isTemplateElement(node) {
|
|
387
|
+
if (node.svg) return false;
|
|
388
|
+
if (isRawTextElement(node.tag)) return false;
|
|
389
|
+
if (!node.props.every((p) => isPlainLiteralAttr(p))) return false;
|
|
390
|
+
return node.children.every((c) => isTemplateChild(c) || isHole(c));
|
|
391
|
+
}
|
|
392
|
+
function containsHole(node) {
|
|
393
|
+
if (isHole(node)) return true;
|
|
394
|
+
const children = node.children;
|
|
395
|
+
return children ? children.some(containsHole) : false;
|
|
396
|
+
}
|
|
397
|
+
function addressable(el) {
|
|
398
|
+
for (let i = 0; i < el.children.length; i++) {
|
|
399
|
+
const child = el.children[i];
|
|
400
|
+
if (child.kind === "text") {
|
|
401
|
+
if (child.value === "") return false;
|
|
402
|
+
if (el.children[i + 1]?.kind === "text") return false;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
return el.children.every((c) => c.kind !== "element" || addressable(c));
|
|
406
|
+
}
|
|
407
|
+
function planPartialTemplate(root) {
|
|
408
|
+
if (!isTemplateElement(root)) return null;
|
|
409
|
+
if (!addressable(root)) return null;
|
|
410
|
+
if (!root.children.some(containsHole)) return null;
|
|
411
|
+
const steps = [];
|
|
412
|
+
const holes = [];
|
|
413
|
+
let refCount = 0;
|
|
414
|
+
const declare = (expr) => {
|
|
415
|
+
const ref = `_n$${refCount++}`;
|
|
416
|
+
steps.push({ ref, expr });
|
|
417
|
+
return ref;
|
|
418
|
+
};
|
|
419
|
+
walk8(root, "_el$");
|
|
420
|
+
return { html: serializeWithMarkers(root), tag: root.tag, steps, holes };
|
|
421
|
+
function walk8(el, elementRef) {
|
|
422
|
+
let last = -1;
|
|
423
|
+
el.children.forEach((child, i) => {
|
|
424
|
+
if (containsHole(child)) last = i;
|
|
425
|
+
});
|
|
426
|
+
let previous = null;
|
|
427
|
+
for (let i = 0; i <= last; i++) {
|
|
428
|
+
const child = el.children[i];
|
|
429
|
+
const expr = previous ? `${previous}.nextSibling` : `${elementRef}.firstChild`;
|
|
430
|
+
if (isHole(child)) {
|
|
431
|
+
const startRef = declare(expr);
|
|
432
|
+
const endRef = declare(`holeEnd(${startRef})`);
|
|
433
|
+
holes.push({ parentRef: elementRef, startRef, endRef, node: child });
|
|
434
|
+
previous = endRef;
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
const ref = declare(expr);
|
|
438
|
+
if (child.kind === "element" && containsHole(child)) walk8(child, ref);
|
|
439
|
+
previous = ref;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
function serializeWithMarkers(el) {
|
|
444
|
+
return serializeStatic(withSentinels(el)).split(SENTINEL).join(HOLE_START + HOLE_END);
|
|
445
|
+
}
|
|
446
|
+
function withSentinels(el) {
|
|
447
|
+
return {
|
|
448
|
+
...el,
|
|
449
|
+
children: el.children.map(
|
|
450
|
+
(child) => isHole(child) ? { kind: "text", value: SENTINEL } : child.kind === "element" ? withSentinels(child) : child
|
|
451
|
+
)
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
var SENTINEL = "\0fx-hole\0";
|
|
455
|
+
|
|
456
|
+
// src/codegen/contract.ts
|
|
457
|
+
var RUNTIME_CONTRACT_V1 = {
|
|
458
|
+
version: 1,
|
|
459
|
+
module: "@fluixi/dom",
|
|
460
|
+
symbols: [
|
|
461
|
+
"createNativeElement",
|
|
462
|
+
"insert",
|
|
463
|
+
"spread",
|
|
464
|
+
"setAttribute",
|
|
465
|
+
"delegateEvents",
|
|
466
|
+
"createComponent",
|
|
467
|
+
"createMemo",
|
|
468
|
+
"untrack",
|
|
469
|
+
"Show",
|
|
470
|
+
"For",
|
|
471
|
+
"Index",
|
|
472
|
+
"Switch",
|
|
473
|
+
"Match",
|
|
474
|
+
"Dynamic",
|
|
475
|
+
"ErrorBoundary"
|
|
476
|
+
]
|
|
477
|
+
};
|
|
478
|
+
var RUNTIME_CONTRACT_V2 = {
|
|
479
|
+
version: 2,
|
|
480
|
+
module: "@fluixi/dom",
|
|
481
|
+
symbols: [...RUNTIME_CONTRACT_V1.symbols, "template", "cloneTemplate", "walk"]
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
// src/analyze/intrinsics.ts
|
|
485
|
+
var REACTIVE_MODULE = "@fluixi/reactive/signal";
|
|
486
|
+
var INTRINSICS = {
|
|
487
|
+
$signal: { export: "signal", module: REACTIVE_MODULE, returns: "signal-handle" },
|
|
488
|
+
$memo: { export: "memo", module: REACTIVE_MODULE, returns: "memo-handle" },
|
|
489
|
+
$effect: { export: "effect", module: REACTIVE_MODULE, returns: "effect" },
|
|
490
|
+
$store: { export: "store", module: "@fluixi/reactive/store", returns: "store-handle" },
|
|
491
|
+
$resource: { export: "resource", module: REACTIVE_MODULE, returns: "resource-handle" },
|
|
492
|
+
// No short runtime form for these yet, so the intrinsic stands for the `create*`
|
|
493
|
+
// name. Still worth having — no import to write — and when a shorter form is
|
|
494
|
+
// designed, the change is this line rather than a compiler pass.
|
|
495
|
+
$selector: { export: "createSelector", module: REACTIVE_MODULE, returns: "memo-accessor" },
|
|
496
|
+
$deferred: { export: "createDeferred", module: REACTIVE_MODULE, returns: "memo-accessor" },
|
|
497
|
+
// Not value constructors, but the compiler has to know them: they change what a read
|
|
498
|
+
// does. `untrack` suppresses dependency collection, which is exactly the kind of thing
|
|
499
|
+
// dependency analysis must not guess at.
|
|
500
|
+
$untrack: { export: "untrack", module: REACTIVE_MODULE, returns: "plain" },
|
|
501
|
+
$untrackStore: { export: "untrackStore", module: "@fluixi/reactive/store", returns: "plain" }
|
|
502
|
+
};
|
|
503
|
+
var RUNTIME_PRIMITIVES = {
|
|
504
|
+
signal: "signal-handle",
|
|
505
|
+
memo: "memo-handle",
|
|
506
|
+
effect: "effect",
|
|
507
|
+
store: "store-handle",
|
|
508
|
+
createSignal: ["signal-accessor", "signal-setter"],
|
|
509
|
+
createMemo: "memo-accessor",
|
|
510
|
+
createEffect: "effect",
|
|
511
|
+
createRenderEffect: "effect",
|
|
512
|
+
createStore: "reactive-object",
|
|
513
|
+
resource: "resource-handle",
|
|
514
|
+
createResource: "reactive-object",
|
|
515
|
+
createSelector: "memo-accessor",
|
|
516
|
+
createDeferred: "memo-accessor"
|
|
517
|
+
};
|
|
518
|
+
var PRIMITIVE_MODULES = [
|
|
519
|
+
"@fluixi/reactive",
|
|
520
|
+
"@fluixi/reactive/signal",
|
|
521
|
+
"@fluixi/reactive/store",
|
|
522
|
+
"@fluixi/core"
|
|
523
|
+
];
|
|
524
|
+
var INTRINSIC_CALL = new RegExp(`\\$(?:${Object.keys(INTRINSICS).map((n) => n.slice(1)).join("|")})\\s*\\(`);
|
|
525
|
+
function mayHaveIntrinsic(code) {
|
|
526
|
+
return INTRINSIC_CALL.test(code);
|
|
527
|
+
}
|
|
528
|
+
function isIntrinsicName(name) {
|
|
529
|
+
return Object.prototype.hasOwnProperty.call(INTRINSICS, name);
|
|
530
|
+
}
|
|
531
|
+
function isPrimitiveModule(source) {
|
|
532
|
+
return PRIMITIVE_MODULES.includes(source);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
// src/analyze/reactive-bindings.ts
|
|
536
|
+
function patternNames(node, out) {
|
|
537
|
+
if (!node) return;
|
|
538
|
+
switch (node.type) {
|
|
539
|
+
case "Identifier":
|
|
540
|
+
out.add(node.name);
|
|
541
|
+
return;
|
|
542
|
+
case "ObjectPattern":
|
|
543
|
+
for (const p of node.properties ?? []) {
|
|
544
|
+
if (p.type === "RestElement") patternNames(p.argument, out);
|
|
545
|
+
else patternNames(p.value, out);
|
|
546
|
+
}
|
|
547
|
+
return;
|
|
548
|
+
case "ArrayPattern":
|
|
549
|
+
for (const e of node.elements ?? []) patternNames(e, out);
|
|
550
|
+
return;
|
|
551
|
+
case "AssignmentPattern":
|
|
552
|
+
patternNames(node.left, out);
|
|
553
|
+
return;
|
|
554
|
+
case "RestElement":
|
|
555
|
+
patternNames(node.argument, out);
|
|
556
|
+
return;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
function walk2(root, visit) {
|
|
560
|
+
if (!root || typeof root !== "object" || typeof root.type !== "string") return;
|
|
561
|
+
visit(root);
|
|
562
|
+
for (const key of Object.keys(root)) {
|
|
563
|
+
if (key === "loc" || key === "leadingComments" || key === "trailingComments") continue;
|
|
564
|
+
const value = root[key];
|
|
565
|
+
if (Array.isArray(value)) for (const item of value) walk2(item, visit);
|
|
566
|
+
else if (value && typeof value === "object") walk2(value, visit);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
function calledPrimitive(node, bindings) {
|
|
570
|
+
if (node.type !== "CallExpression") return void 0;
|
|
571
|
+
const callee = node.callee;
|
|
572
|
+
if (callee?.type !== "Identifier") return void 0;
|
|
573
|
+
const name = callee.name;
|
|
574
|
+
const imported = bindings.primitives.get(name);
|
|
575
|
+
if (imported) return imported;
|
|
576
|
+
const intrinsic = INTRINSICS[name];
|
|
577
|
+
if (intrinsic && !bindings.shadowed.has(name)) return intrinsic.returns;
|
|
578
|
+
return void 0;
|
|
579
|
+
}
|
|
580
|
+
function classify(id, returns, kinds) {
|
|
581
|
+
if (Array.isArray(returns)) {
|
|
582
|
+
if (id.type === "ArrayPattern") {
|
|
583
|
+
id.elements.forEach((element, index) => {
|
|
584
|
+
const kind = returns[index];
|
|
585
|
+
if (element?.type === "Identifier" && kind) kinds.set(element.name, kind);
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
if (id.type === "Identifier") kinds.set(id.name, returns);
|
|
591
|
+
}
|
|
592
|
+
function declaredNames(program) {
|
|
593
|
+
const names = /* @__PURE__ */ new Set();
|
|
594
|
+
walk2(program, (node) => {
|
|
595
|
+
switch (node.type) {
|
|
596
|
+
case "VariableDeclarator":
|
|
597
|
+
patternNames(node.id, names);
|
|
598
|
+
return;
|
|
599
|
+
case "FunctionDeclaration":
|
|
600
|
+
case "FunctionExpression":
|
|
601
|
+
case "ArrowFunctionExpression":
|
|
602
|
+
patternNames(node.id, names);
|
|
603
|
+
for (const p of node.params ?? []) patternNames(p, names);
|
|
604
|
+
return;
|
|
605
|
+
case "ClassDeclaration":
|
|
606
|
+
case "ClassExpression":
|
|
607
|
+
patternNames(node.id, names);
|
|
608
|
+
return;
|
|
609
|
+
case "ImportSpecifier":
|
|
610
|
+
case "ImportDefaultSpecifier":
|
|
611
|
+
case "ImportNamespaceSpecifier":
|
|
612
|
+
patternNames(node.local, names);
|
|
613
|
+
return;
|
|
614
|
+
case "CatchClause":
|
|
615
|
+
patternNames(node.param, names);
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
});
|
|
619
|
+
return names;
|
|
620
|
+
}
|
|
621
|
+
function resolveReactiveBindings(program) {
|
|
622
|
+
const bindings = { kinds: /* @__PURE__ */ new Map(), primitives: /* @__PURE__ */ new Map(), shadowed: /* @__PURE__ */ new Set() };
|
|
623
|
+
const root = program;
|
|
624
|
+
walk2(root, (node) => {
|
|
625
|
+
const names = /* @__PURE__ */ new Set();
|
|
626
|
+
if (node.type === "VariableDeclarator") patternNames(node.id, names);
|
|
627
|
+
else if (node.type === "FunctionDeclaration" || node.type === "ClassDeclaration") patternNames(node.id, names);
|
|
628
|
+
else if (node.type === "ImportDefaultSpecifier" || node.type === "ImportNamespaceSpecifier") patternNames(node.local, names);
|
|
629
|
+
else if (node.type === "ImportSpecifier") patternNames(node.local, names);
|
|
630
|
+
else if (node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression" || node.type === "FunctionDeclaration") {
|
|
631
|
+
for (const p of node.params ?? []) patternNames(p, names);
|
|
632
|
+
}
|
|
633
|
+
for (const name of names) if (INTRINSICS[name]) bindings.shadowed.add(name);
|
|
634
|
+
});
|
|
635
|
+
for (const statement of root.body ?? []) {
|
|
636
|
+
if (statement.type !== "ImportDeclaration") continue;
|
|
637
|
+
if (!isPrimitiveModule(statement.source?.value)) continue;
|
|
638
|
+
for (const specifier of statement.specifiers ?? []) {
|
|
639
|
+
if (specifier.type !== "ImportSpecifier") continue;
|
|
640
|
+
const importedNode = specifier.imported;
|
|
641
|
+
const imported = importedNode.type === "Identifier" ? importedNode.name : importedNode.value;
|
|
642
|
+
const returns = RUNTIME_PRIMITIVES[imported];
|
|
643
|
+
if (returns) bindings.primitives.set(specifier.local.name, returns);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
walk2(root, (node) => {
|
|
647
|
+
if (node.type !== "VariableDeclarator" || !node.init) return;
|
|
648
|
+
const returns = calledPrimitive(node.init, bindings);
|
|
649
|
+
if (returns) classify(node.id, returns, bindings.kinds);
|
|
650
|
+
});
|
|
651
|
+
return bindings;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
// src/transform/source-locations.ts
|
|
655
|
+
var SOURCE_MARKER = "__fx_source";
|
|
656
|
+
var COMPONENT_MARKER = "__fx_component";
|
|
657
|
+
var HOLE_MARKER = "__fx_hole";
|
|
658
|
+
var markHole = (accessor, line, column, name) => `(globalThis.${HOLE_MARKER} ?? ((f) => f))(${accessor},${line},${column}${name === void 0 ? "" : `,${JSON.stringify(name)}`})`;
|
|
659
|
+
var PROPS_MARKER = "__fx_props";
|
|
660
|
+
var markProps = (object, at) => `(globalThis.${PROPS_MARKER} ?? ((p) => p))(${object},${JSON.stringify(at)})`;
|
|
661
|
+
var RUNTIME_PRIMITIVES2 = /* @__PURE__ */ new Set([
|
|
662
|
+
"signal",
|
|
663
|
+
"memo",
|
|
664
|
+
"effect",
|
|
665
|
+
"store",
|
|
666
|
+
"resource",
|
|
667
|
+
"watch",
|
|
668
|
+
"createSignal",
|
|
669
|
+
"createMemo",
|
|
670
|
+
"createEffect",
|
|
671
|
+
"createStore",
|
|
672
|
+
"createResource"
|
|
673
|
+
]);
|
|
674
|
+
function mayHaveReactiveCall(code) {
|
|
675
|
+
for (const name of RUNTIME_PRIMITIVES2) {
|
|
676
|
+
if (code.includes(name)) return true;
|
|
677
|
+
}
|
|
678
|
+
return code.includes("$signal") || code.includes("$memo") || code.includes("$effect") || code.includes("$store");
|
|
679
|
+
}
|
|
680
|
+
var SINGLE_STATEMENT_PARENTS = /* @__PURE__ */ new Set([
|
|
681
|
+
"IfStatement",
|
|
682
|
+
"ForStatement",
|
|
683
|
+
"ForInStatement",
|
|
684
|
+
"ForOfStatement",
|
|
685
|
+
"WhileStatement",
|
|
686
|
+
"DoWhileStatement",
|
|
687
|
+
"LabeledStatement",
|
|
688
|
+
"WithStatement"
|
|
689
|
+
]);
|
|
690
|
+
function isLoneBranch(statement, parent) {
|
|
691
|
+
if (!parent || statement.type === "BlockStatement") return false;
|
|
692
|
+
return SINGLE_STATEMENT_PARENTS.has(parent.type);
|
|
693
|
+
}
|
|
694
|
+
function isExport(node) {
|
|
695
|
+
return node.type === "ExportNamedDeclaration" || node.type === "ExportDefaultDeclaration";
|
|
696
|
+
}
|
|
697
|
+
function isStatement(node) {
|
|
698
|
+
return node.type.endsWith("Statement") || node.type === "VariableDeclaration";
|
|
699
|
+
}
|
|
700
|
+
function walk3(node, visit, stack = []) {
|
|
701
|
+
if (!node || typeof node.type !== "string") return;
|
|
702
|
+
visit(node, stack);
|
|
703
|
+
stack.push(node);
|
|
704
|
+
for (const key of Object.keys(node)) {
|
|
705
|
+
if (key === "loc" || key === "parent") continue;
|
|
706
|
+
const value = node[key];
|
|
707
|
+
if (Array.isArray(value)) {
|
|
708
|
+
for (const child of value) walk3(child, visit, stack);
|
|
709
|
+
} else if (value && typeof value.type === "string") {
|
|
710
|
+
walk3(value, visit, stack);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
stack.pop();
|
|
714
|
+
}
|
|
715
|
+
function nameFor(stack) {
|
|
716
|
+
const declarator = stack[stack.length - 1];
|
|
717
|
+
if (declarator?.type !== "VariableDeclarator") return void 0;
|
|
718
|
+
const id = declarator.id;
|
|
719
|
+
if (id?.type === "Identifier") return id.name;
|
|
720
|
+
if (id?.type === "ArrayPattern") {
|
|
721
|
+
const first = id.elements?.[0];
|
|
722
|
+
if (first?.type === "Identifier") return first.name;
|
|
723
|
+
}
|
|
724
|
+
return void 0;
|
|
725
|
+
}
|
|
726
|
+
function componentName(node) {
|
|
727
|
+
if (node.type === "FunctionDeclaration") {
|
|
728
|
+
const name = node.id?.name;
|
|
729
|
+
return name && name[0] === name[0]?.toUpperCase() ? name : void 0;
|
|
730
|
+
}
|
|
731
|
+
if (node.type === "VariableDeclarator") {
|
|
732
|
+
const id = node.id;
|
|
733
|
+
const init = node.init;
|
|
734
|
+
if (id?.type !== "Identifier" || !init) return void 0;
|
|
735
|
+
if (init.type !== "ArrowFunctionExpression" && init.type !== "FunctionExpression") return void 0;
|
|
736
|
+
const name = id.name;
|
|
737
|
+
return name[0] === name[0]?.toUpperCase() ? name : void 0;
|
|
738
|
+
}
|
|
739
|
+
return void 0;
|
|
740
|
+
}
|
|
741
|
+
function collectSourceEdits(program, filename) {
|
|
742
|
+
const edits = [];
|
|
743
|
+
const claimed = /* @__PURE__ */ new Set();
|
|
744
|
+
const { primitives, shadowed } = resolveReactiveBindings(program);
|
|
745
|
+
walk3(program, (node, stack) => {
|
|
746
|
+
const component = componentName(node);
|
|
747
|
+
if (component) {
|
|
748
|
+
const at = node.loc?.start;
|
|
749
|
+
let end2 = node.end;
|
|
750
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
|
751
|
+
const up = stack[i];
|
|
752
|
+
if (up.type !== "VariableDeclaration" && up.type !== "ExportNamedDeclaration" && up.type !== "ExportDefaultDeclaration") {
|
|
753
|
+
break;
|
|
754
|
+
}
|
|
755
|
+
end2 = up.end;
|
|
756
|
+
}
|
|
757
|
+
if (at) {
|
|
758
|
+
edits.push({
|
|
759
|
+
start: end2,
|
|
760
|
+
end: end2,
|
|
761
|
+
code: `;globalThis.${COMPONENT_MARKER}?.(${JSON.stringify(component)},${JSON.stringify(filename)},${at.line},${at.column});`
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
if (node.type !== "CallExpression") return;
|
|
766
|
+
const callee = node.callee;
|
|
767
|
+
if (callee?.type !== "Identifier") return;
|
|
768
|
+
const name = callee.name;
|
|
769
|
+
const intrinsic = isIntrinsicName(name) && !shadowed.has(name) ? INTRINSICS[name] : void 0;
|
|
770
|
+
const imported = primitives.has(name) || RUNTIME_PRIMITIVES2.has(name);
|
|
771
|
+
if (!intrinsic && !imported) return;
|
|
772
|
+
const position = node.loc?.start;
|
|
773
|
+
if (!position) return;
|
|
774
|
+
let statement;
|
|
775
|
+
let parent;
|
|
776
|
+
let index = -1;
|
|
777
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
|
778
|
+
if (isStatement(stack[i]) || isExport(stack[i])) {
|
|
779
|
+
statement = stack[i];
|
|
780
|
+
parent = stack[i - 1];
|
|
781
|
+
index = i;
|
|
782
|
+
break;
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
if (!statement) return;
|
|
786
|
+
let start = statement.start;
|
|
787
|
+
let end = statement.end;
|
|
788
|
+
for (let i = index - 1; i >= 0; i--) {
|
|
789
|
+
if (!isExport(stack[i])) break;
|
|
790
|
+
start = stack[i].start;
|
|
791
|
+
end = stack[i].end;
|
|
792
|
+
parent = stack[i - 1];
|
|
793
|
+
}
|
|
794
|
+
if (claimed.has(start)) return;
|
|
795
|
+
claimed.add(start);
|
|
796
|
+
const label = nameFor(stack);
|
|
797
|
+
const args = [
|
|
798
|
+
JSON.stringify(filename),
|
|
799
|
+
String(position.line),
|
|
800
|
+
String(position.column),
|
|
801
|
+
label ? JSON.stringify(label) : void 0
|
|
802
|
+
].filter((a) => a !== void 0).join(",");
|
|
803
|
+
const mark = `globalThis.${SOURCE_MARKER}?.(${args});`;
|
|
804
|
+
if (isLoneBranch(statement, parent)) {
|
|
805
|
+
edits.push({ start, end: start, code: `{${mark}` });
|
|
806
|
+
edits.push({ start: end, end, code: "}" });
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
edits.push({ start, end: start, code: mark });
|
|
810
|
+
});
|
|
811
|
+
return { edits, marked: edits.length };
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
// src/analyze/paren.ts
|
|
815
|
+
function skipQuoted(src, start) {
|
|
816
|
+
const quote = src[start];
|
|
817
|
+
for (let i = start + 1; i < src.length; i++) {
|
|
818
|
+
const c = src[i];
|
|
819
|
+
if (c === "\\") i++;
|
|
820
|
+
else if (c === quote) return i + 1;
|
|
821
|
+
else if (c === "\n") return -1;
|
|
822
|
+
}
|
|
823
|
+
return -1;
|
|
824
|
+
}
|
|
825
|
+
function skipTemplate(src, start) {
|
|
826
|
+
let i = start + 1;
|
|
827
|
+
while (i < src.length) {
|
|
828
|
+
const c = src[i];
|
|
829
|
+
if (c === "\\") {
|
|
830
|
+
i += 2;
|
|
831
|
+
} else if (c === "`") {
|
|
832
|
+
return i + 1;
|
|
833
|
+
} else if (c === "$" && src[i + 1] === "{") {
|
|
834
|
+
let depth = 1;
|
|
835
|
+
i += 2;
|
|
836
|
+
while (i < src.length && depth > 0) {
|
|
837
|
+
const k = src[i];
|
|
838
|
+
if (k === "\\") i += 2;
|
|
839
|
+
else if (k === "`") {
|
|
840
|
+
const end = skipTemplate(src, i);
|
|
841
|
+
if (end < 0) return -1;
|
|
842
|
+
i = end;
|
|
843
|
+
} else if (k === '"' || k === "'") {
|
|
844
|
+
const end = skipQuoted(src, i);
|
|
845
|
+
if (end < 0) return -1;
|
|
846
|
+
i = end;
|
|
847
|
+
} else {
|
|
848
|
+
if (k === "{") depth++;
|
|
849
|
+
else if (k === "}") depth--;
|
|
850
|
+
i++;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
} else {
|
|
854
|
+
i++;
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
return -1;
|
|
858
|
+
}
|
|
859
|
+
function skipRegex(src, start) {
|
|
860
|
+
let inClass = false;
|
|
861
|
+
for (let i = start + 1; i < src.length; i++) {
|
|
862
|
+
const c = src[i];
|
|
863
|
+
if (c === "\\") i++;
|
|
864
|
+
else if (c === "[") inClass = true;
|
|
865
|
+
else if (c === "]") inClass = false;
|
|
866
|
+
else if (c === "\n") return -1;
|
|
867
|
+
else if (c === "/" && !inClass) {
|
|
868
|
+
let end = i + 1;
|
|
869
|
+
while (end < src.length && /[a-z]/i.test(src[end])) end++;
|
|
870
|
+
return end;
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
return -1;
|
|
874
|
+
}
|
|
875
|
+
function divides(prev) {
|
|
876
|
+
return prev !== "" && /[A-Za-z0-9_$)\]]/.test(prev);
|
|
877
|
+
}
|
|
878
|
+
function topLevelComma(src) {
|
|
879
|
+
let depth = 0;
|
|
880
|
+
let prev = "";
|
|
881
|
+
let i = 0;
|
|
882
|
+
while (i < src.length) {
|
|
883
|
+
const c = src[i];
|
|
884
|
+
if (c === '"' || c === "'") {
|
|
885
|
+
const end = skipQuoted(src, i);
|
|
886
|
+
if (end < 0) return null;
|
|
887
|
+
i = end;
|
|
888
|
+
prev = c;
|
|
889
|
+
} else if (c === "`") {
|
|
890
|
+
const end = skipTemplate(src, i);
|
|
891
|
+
if (end < 0) return null;
|
|
892
|
+
i = end;
|
|
893
|
+
prev = c;
|
|
894
|
+
} else if (c === "/" && src[i + 1] === "/") {
|
|
895
|
+
return null;
|
|
896
|
+
} else if (c === "/" && src[i + 1] === "*") {
|
|
897
|
+
const end = src.indexOf("*/", i + 2);
|
|
898
|
+
if (end < 0) return null;
|
|
899
|
+
i = end + 2;
|
|
900
|
+
} else if (c === "/" && !divides(prev)) {
|
|
901
|
+
const end = skipRegex(src, i);
|
|
902
|
+
if (end < 0) return null;
|
|
903
|
+
i = end;
|
|
904
|
+
prev = "/";
|
|
905
|
+
} else {
|
|
906
|
+
if (c === "(" || c === "[" || c === "{") depth++;
|
|
907
|
+
else if (c === ")" || c === "]" || c === "}") {
|
|
908
|
+
depth--;
|
|
909
|
+
if (depth < 0) return null;
|
|
910
|
+
} else if (c === "," && depth === 0) return true;
|
|
911
|
+
if (!/\s/.test(c)) prev = c;
|
|
912
|
+
i++;
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
return depth === 0 ? false : null;
|
|
916
|
+
}
|
|
917
|
+
function paren(code, position) {
|
|
918
|
+
const src = code.trim();
|
|
919
|
+
if (src === "") return `(${code})`;
|
|
920
|
+
if (position === "arrowBody" && src[0] === "{") return `(${code})`;
|
|
921
|
+
return topLevelComma(src) === false ? code : `(${code})`;
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
// src/codegen/backends/imperative.ts
|
|
925
|
+
var CONTROL_COMPONENT = {
|
|
926
|
+
show: "Show",
|
|
927
|
+
for: "For",
|
|
928
|
+
index: "Index",
|
|
929
|
+
switch: "Switch",
|
|
930
|
+
match: "Match",
|
|
931
|
+
dynamic: "Dynamic",
|
|
932
|
+
errorBoundary: "ErrorBoundary",
|
|
933
|
+
suspense: "Suspense"
|
|
934
|
+
};
|
|
935
|
+
function isIdent(name) {
|
|
936
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
|
|
937
|
+
}
|
|
938
|
+
function propKey(name) {
|
|
939
|
+
return isIdent(name) ? name : JSON.stringify(name);
|
|
940
|
+
}
|
|
941
|
+
function propValue(p) {
|
|
942
|
+
if (p.expr !== void 0) {
|
|
943
|
+
if (p.name === "ref" && p.at)
|
|
944
|
+
return markHole(paren(p.expr, "value"), p.at.line, p.at.column, p.expr);
|
|
945
|
+
if (!p.reactive) return paren(p.expr, "value");
|
|
946
|
+
const accessor = `() => ${paren(p.expr, "arrowBody")}`;
|
|
947
|
+
return p.at ? markHole(accessor, p.at.line, p.at.column, p.directive) : accessor;
|
|
948
|
+
}
|
|
949
|
+
return JSON.stringify(p.literal ?? true);
|
|
950
|
+
}
|
|
951
|
+
function componentPropValue(p) {
|
|
952
|
+
if (p.expr !== void 0) return paren(p.expr, "value");
|
|
953
|
+
return JSON.stringify(p.literal ?? true);
|
|
954
|
+
}
|
|
955
|
+
function emitProps(props, childrenExpr, used) {
|
|
956
|
+
const spreads = props.filter((p) => p.kind === "spread");
|
|
957
|
+
const normal = props.filter((p) => p.kind !== "spread");
|
|
958
|
+
const entries = normal.map((p) => `${propKey(p.name)}: ${propValue(p)}`);
|
|
959
|
+
if (childrenExpr != null) entries.push(`children: ${childrenExpr}`);
|
|
960
|
+
const obj = `{ ${entries.join(", ")} }`;
|
|
961
|
+
if (spreads.length > 0) {
|
|
962
|
+
used.add("mergeProps");
|
|
963
|
+
return `mergeProps(${spreads.map((s) => s.expr).join(", ")}, ${obj})`;
|
|
964
|
+
}
|
|
965
|
+
return obj;
|
|
966
|
+
}
|
|
967
|
+
function markBind(code, at) {
|
|
968
|
+
return at ? markHole(code, at.line, at.column) : code;
|
|
969
|
+
}
|
|
970
|
+
function emitChildren(children, used, templates) {
|
|
971
|
+
if (children.length === 1) return emitNode(children[0], used, templates);
|
|
972
|
+
return `[${children.map((c) => emitNode(c, used, templates)).join(", ")}]`;
|
|
973
|
+
}
|
|
974
|
+
function emitComponent(name, props, children, used, templates, at) {
|
|
975
|
+
used.add("createMemo");
|
|
976
|
+
used.add("createComponent");
|
|
977
|
+
const spreads = props.filter((p) => p.kind === "spread");
|
|
978
|
+
const normal = props.filter((p) => p.kind !== "spread");
|
|
979
|
+
const entries = normal.map(
|
|
980
|
+
(p) => `get ${propKey(p.name)}() { return ${componentPropValue(p)}; }`
|
|
981
|
+
);
|
|
982
|
+
if (children.length > 0) {
|
|
983
|
+
entries.push(`get children() { return ${emitChildren(children, used, templates)}; }`);
|
|
984
|
+
}
|
|
985
|
+
let obj = `{ ${entries.join(", ")} }`;
|
|
986
|
+
if (spreads.length > 0) {
|
|
987
|
+
used.add("mergeProps");
|
|
988
|
+
obj = `mergeProps(${spreads.map((s) => s.expr).join(", ")}, ${obj})`;
|
|
989
|
+
}
|
|
990
|
+
const propAt = {};
|
|
991
|
+
for (const p of normal) if (p.at) propAt[p.name] = [p.at.line, p.at.column];
|
|
992
|
+
if (Object.keys(propAt).length) obj = markProps(obj, propAt);
|
|
993
|
+
const mark = at ? `globalThis.${SOURCE_MARKER}?.(${JSON.stringify(at.file)},${at.line},${at.column},${JSON.stringify(name)}), ` : "";
|
|
994
|
+
return `createMemo(${paren(`${mark}() => createComponent(${name}, ${obj})`, "value")})`;
|
|
995
|
+
}
|
|
996
|
+
function staticAttrAssignment(el, prop) {
|
|
997
|
+
const name = htmlAttrName(prop.name);
|
|
998
|
+
const value = prop.literal;
|
|
999
|
+
if (value === true || value === void 0) return `${el}.setAttribute(${JSON.stringify(name)}, "");`;
|
|
1000
|
+
if (value === false || value === null) return "";
|
|
1001
|
+
return `${el}.setAttribute(${JSON.stringify(name)}, ${JSON.stringify(String(value))});`;
|
|
1002
|
+
}
|
|
1003
|
+
var partial = false;
|
|
1004
|
+
function emitNode(node, used, templates) {
|
|
1005
|
+
switch (node.kind) {
|
|
1006
|
+
case "text":
|
|
1007
|
+
return JSON.stringify(node.value);
|
|
1008
|
+
case "expr": {
|
|
1009
|
+
if (!node.reactive) return paren(node.code, "value");
|
|
1010
|
+
const accessor = `() => ${paren(node.code, "arrowBody")}`;
|
|
1011
|
+
return node.at ? markHole(accessor, node.at.line, node.at.column) : accessor;
|
|
1012
|
+
}
|
|
1013
|
+
case "fragment":
|
|
1014
|
+
if (node.children.length === 0) return "null";
|
|
1015
|
+
return emitChildren(node.children, used, templates);
|
|
1016
|
+
case "component":
|
|
1017
|
+
return markBind(emitComponent(node.name, node.props, node.children, used, templates, node.at), node.bindAt ?? node.at);
|
|
1018
|
+
case "control": {
|
|
1019
|
+
const name = CONTROL_COMPONENT[node.control] ?? node.control;
|
|
1020
|
+
used.add(name);
|
|
1021
|
+
return markBind(emitComponent(name, node.props, node.children, used, templates), node.bindAt);
|
|
1022
|
+
}
|
|
1023
|
+
case "element": {
|
|
1024
|
+
if (templates && node.static && !node.svg && isSerializable(node)) {
|
|
1025
|
+
used.add("templateNode");
|
|
1026
|
+
const id = `_tmpl$${templates.length}`;
|
|
1027
|
+
templates.push({ id, html: serializeStatic(node), tag: node.tag, svg: false });
|
|
1028
|
+
const at = templatePositions(node);
|
|
1029
|
+
return at ? `templateNode(${id}, ${JSON.stringify(node.tag)}, false, false, ${JSON.stringify(at)})` : `templateNode(${id}, ${JSON.stringify(node.tag)})`;
|
|
1030
|
+
}
|
|
1031
|
+
if (templates && partial) {
|
|
1032
|
+
const plan = planPartialTemplate(node);
|
|
1033
|
+
if (plan) {
|
|
1034
|
+
used.add("templateNode");
|
|
1035
|
+
used.add("insert");
|
|
1036
|
+
used.add("holeEnd");
|
|
1037
|
+
used.add("holeContent");
|
|
1038
|
+
used.add("holeScope");
|
|
1039
|
+
const id = `_tmpl$${templates.length}`;
|
|
1040
|
+
templates.push({ id, html: plan.html, tag: plan.tag, svg: false });
|
|
1041
|
+
const at = templatePositions(node);
|
|
1042
|
+
const lines = [
|
|
1043
|
+
`const _el$ = templateNode(${id}, ${JSON.stringify(plan.tag)}, true${at ? `, false, ${JSON.stringify(at)}` : ""});`
|
|
1044
|
+
];
|
|
1045
|
+
for (const step of plan.steps) lines.push(`const ${step.ref} = ${step.expr};`);
|
|
1046
|
+
for (const hole of plan.holes) {
|
|
1047
|
+
lines.push(
|
|
1048
|
+
`insert(${hole.parentRef}, holeScope(${hole.startRef}, () => ${paren(emitNode(hole.node, used, templates), "arrowBody")}), ${hole.endRef}, holeContent(${hole.startRef}, ${hole.endRef}));`
|
|
1049
|
+
);
|
|
1050
|
+
}
|
|
1051
|
+
lines.push("return _el$;");
|
|
1052
|
+
return `(() => { ${lines.join(" ")} })()`;
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
used.add("createNativeElement");
|
|
1056
|
+
const tag = JSON.stringify(node.tag);
|
|
1057
|
+
const el = "_el$";
|
|
1058
|
+
const parts = [];
|
|
1059
|
+
const createArgs = node.at ? `${tag}, ${node.svg}, ${JSON.stringify(node.at)}` : node.svg ? `${tag}, true` : tag;
|
|
1060
|
+
parts.push(`const ${el} = createNativeElement(${createArgs});`);
|
|
1061
|
+
if (node.props.length > 0) {
|
|
1062
|
+
if (!node.svg && node.props.every(isPlainLiteralAttr)) {
|
|
1063
|
+
for (const p of node.props) parts.push(staticAttrAssignment(el, p));
|
|
1064
|
+
} else {
|
|
1065
|
+
used.add("spread");
|
|
1066
|
+
const svgFlag = node.svg ? ", isSVG: true" : "";
|
|
1067
|
+
parts.push(`spread({ element: ${el}, props: ${emitProps(node.props, null, used)}${svgFlag} });`);
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
for (const child of node.children) {
|
|
1071
|
+
used.add("insert");
|
|
1072
|
+
const childCode = emitNode(child, used, templates);
|
|
1073
|
+
const isFn = child.kind === "expr" && child.reactive || child.kind === "component" || child.kind === "control";
|
|
1074
|
+
parts.push(isFn ? `insert(${el}, ${childCode}, null);` : `insert(${el}, ${childCode});`);
|
|
1075
|
+
}
|
|
1076
|
+
parts.push(`return ${el};`);
|
|
1077
|
+
return `(() => { ${parts.join(" ")} })()`;
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
function inlineTemplates(code, templates) {
|
|
1082
|
+
if (!templates || templates.length === 0) return code;
|
|
1083
|
+
const byId = new Map(templates.map((t) => [t.id, JSON.stringify(t.html)]));
|
|
1084
|
+
return code.replace(/_tmpl\$\d+/g, (id) => byId.get(id) ?? id);
|
|
1085
|
+
}
|
|
1086
|
+
var imperativeBackend = {
|
|
1087
|
+
name: "imperative",
|
|
1088
|
+
contract: RUNTIME_CONTRACT_V1,
|
|
1089
|
+
emit(node, options) {
|
|
1090
|
+
const used = /* @__PURE__ */ new Set();
|
|
1091
|
+
const templates = options?.templateClone !== false ? [] : void 0;
|
|
1092
|
+
partial = options?.partialTemplates === true;
|
|
1093
|
+
const code = emitNode(node, used, templates);
|
|
1094
|
+
return { code, imports: Array.from(used), templates };
|
|
1095
|
+
}
|
|
1096
|
+
};
|
|
1097
|
+
|
|
1098
|
+
// src/resolve/component-resolver.ts
|
|
1099
|
+
var isMapRule = (rule) => "components" in rule;
|
|
1100
|
+
function substitute(template, match) {
|
|
1101
|
+
return template.replace(/\$(\d+)/g, (whole, index) => {
|
|
1102
|
+
const group = match[Number(index)];
|
|
1103
|
+
return group ?? whole;
|
|
1104
|
+
});
|
|
1105
|
+
}
|
|
1106
|
+
function normalise(name, entry) {
|
|
1107
|
+
return typeof entry === "string" ? { module: entry, export: name } : entry;
|
|
1108
|
+
}
|
|
1109
|
+
function createComponentResolver(rules = []) {
|
|
1110
|
+
const exact = /* @__PURE__ */ new Map();
|
|
1111
|
+
const conflicts = [];
|
|
1112
|
+
const patterns = [];
|
|
1113
|
+
for (const rule of rules) {
|
|
1114
|
+
if (!isMapRule(rule)) {
|
|
1115
|
+
patterns.push(rule);
|
|
1116
|
+
continue;
|
|
1117
|
+
}
|
|
1118
|
+
for (const [name, entry] of Object.entries(rule.components)) {
|
|
1119
|
+
const resolved = normalise(name, entry);
|
|
1120
|
+
const existing = exact.get(name);
|
|
1121
|
+
if (existing && existing.module !== resolved.module) {
|
|
1122
|
+
const conflict = conflicts.find((c) => c.name === name);
|
|
1123
|
+
if (conflict) conflict.modules.push(resolved.module);
|
|
1124
|
+
else conflicts.push({ name, modules: [existing.module, resolved.module] });
|
|
1125
|
+
continue;
|
|
1126
|
+
}
|
|
1127
|
+
exact.set(name, resolved);
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
const cache = /* @__PURE__ */ new Map();
|
|
1131
|
+
const resolve = (name) => {
|
|
1132
|
+
if (cache.has(name)) return cache.get(name);
|
|
1133
|
+
let result = exact.get(name);
|
|
1134
|
+
if (!result) {
|
|
1135
|
+
for (const rule of patterns) {
|
|
1136
|
+
const match = name.match(new RegExp(rule.match.source, rule.match.flags.replace("g", "")));
|
|
1137
|
+
if (!match) continue;
|
|
1138
|
+
result = {
|
|
1139
|
+
module: substitute(rule.module, match),
|
|
1140
|
+
export: rule.export ? substitute(rule.export, match) : name
|
|
1141
|
+
};
|
|
1142
|
+
break;
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
cache.set(name, result);
|
|
1146
|
+
return result;
|
|
1147
|
+
};
|
|
1148
|
+
return {
|
|
1149
|
+
resolve,
|
|
1150
|
+
names: () => [...exact.keys()],
|
|
1151
|
+
conflicts: () => conflicts
|
|
1152
|
+
};
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
// src/lower/template.ts
|
|
1156
|
+
var import_template_parser = require("@fluixi/template-parser");
|
|
1157
|
+
|
|
1158
|
+
// src/lower/text.ts
|
|
1159
|
+
function cleanTemplateText(value) {
|
|
1160
|
+
const lines = value.split(/\r\n|\n|\r/);
|
|
1161
|
+
let lastNonEmpty = 0;
|
|
1162
|
+
for (let i = 0; i < lines.length; i++) {
|
|
1163
|
+
if (/[^ \t]/.test(lines[i])) lastNonEmpty = i;
|
|
1164
|
+
}
|
|
1165
|
+
let out = "";
|
|
1166
|
+
for (let i = 0; i < lines.length; i++) {
|
|
1167
|
+
let line = lines[i].replace(/\t/g, " ");
|
|
1168
|
+
if (i !== 0) line = line.replace(/^ +/, "");
|
|
1169
|
+
if (i !== lines.length - 1) line = line.replace(/ +$/, "");
|
|
1170
|
+
if (!line) continue;
|
|
1171
|
+
if (i !== lastNonEmpty) line += " ";
|
|
1172
|
+
out += line;
|
|
1173
|
+
}
|
|
1174
|
+
return out;
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
// src/lower/template.ts
|
|
1178
|
+
var DELEGATED = /* @__PURE__ */ new Set([
|
|
1179
|
+
"click",
|
|
1180
|
+
"dblclick",
|
|
1181
|
+
"input",
|
|
1182
|
+
"change",
|
|
1183
|
+
"submit",
|
|
1184
|
+
"focus",
|
|
1185
|
+
"blur",
|
|
1186
|
+
"keydown",
|
|
1187
|
+
"keyup",
|
|
1188
|
+
"keypress",
|
|
1189
|
+
"mousedown",
|
|
1190
|
+
"mouseup"
|
|
1191
|
+
]);
|
|
1192
|
+
function escapeTemplate(s) {
|
|
1193
|
+
return s.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$\{/g, "\\${");
|
|
1194
|
+
}
|
|
1195
|
+
function capitalize(s) {
|
|
1196
|
+
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
1197
|
+
}
|
|
1198
|
+
var Lowerer = class {
|
|
1199
|
+
constructor(holes, used, sourceFile, positionAt) {
|
|
1200
|
+
this.holes = holes;
|
|
1201
|
+
this.used = used;
|
|
1202
|
+
this.sourceFile = sourceFile;
|
|
1203
|
+
this.positionAt = positionAt;
|
|
1204
|
+
}
|
|
1205
|
+
/**
|
|
1206
|
+
* Where a tag is written, for the mark a component node carries.
|
|
1207
|
+
*
|
|
1208
|
+
* The parser reports offsets, and they are offsets into the file: the pieces it was given
|
|
1209
|
+
* carry the positions the front-end recorded. Only built when the host asked for source
|
|
1210
|
+
* locations, which is the only time either of these is set.
|
|
1211
|
+
*/
|
|
1212
|
+
tagAt(node) {
|
|
1213
|
+
const start = node.loc?.start;
|
|
1214
|
+
if (!this.sourceFile || !this.positionAt || start == null) return void 0;
|
|
1215
|
+
const { line, column } = this.positionAt(start);
|
|
1216
|
+
return { file: this.sourceFile, line, column };
|
|
1217
|
+
}
|
|
1218
|
+
hole(i) {
|
|
1219
|
+
return this.holes[i] ?? { code: "undefined", reactive: false };
|
|
1220
|
+
}
|
|
1221
|
+
/** Emit a nested IR subtree to code (for `Show` fallback / `For` children). */
|
|
1222
|
+
emit(node) {
|
|
1223
|
+
const { code, imports, templates } = imperativeBackend.emit(node, {});
|
|
1224
|
+
for (const s of imports) this.used.add(s);
|
|
1225
|
+
return inlineTemplates(code, templates);
|
|
1226
|
+
}
|
|
1227
|
+
// --- content ------------------------------------------------------------
|
|
1228
|
+
lowerRoot(children) {
|
|
1229
|
+
const nodes = this.lowerChildren(children);
|
|
1230
|
+
if (nodes.length === 1) return nodes[0];
|
|
1231
|
+
return { kind: "fragment", children: nodes };
|
|
1232
|
+
}
|
|
1233
|
+
/** Lower a sibling list, handling text-drop and if/else/each restructuring. */
|
|
1234
|
+
lowerChildren(children) {
|
|
1235
|
+
const out = [];
|
|
1236
|
+
for (let i = 0; i < children.length; i++) {
|
|
1237
|
+
const child = children[i];
|
|
1238
|
+
if (child.kind === "Element" || child.kind === "Component") {
|
|
1239
|
+
const ifDir = child.attributes.find((a) => a.kind === "IfDirective");
|
|
1240
|
+
if (ifDir && ifDir.kind === "IfDirective") {
|
|
1241
|
+
let elseIdx = i + 1;
|
|
1242
|
+
if (elseIdx < children.length && this.isBlankText(children[elseIdx])) elseIdx++;
|
|
1243
|
+
const elseNode = children[elseIdx];
|
|
1244
|
+
const hasElse = elseNode && (elseNode.kind === "Element" || elseNode.kind === "Component") && elseNode.attributes.some((a) => a.kind === "ElseDirective");
|
|
1245
|
+
out.push(this.lowerIf(child, ifDir.hole, hasElse ? elseNode : null));
|
|
1246
|
+
if (hasElse) i = elseIdx;
|
|
1247
|
+
continue;
|
|
1248
|
+
}
|
|
1249
|
+
if (child.attributes.some((a) => a.kind === "EachDirective")) {
|
|
1250
|
+
out.push(this.lowerEach(child));
|
|
1251
|
+
continue;
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
const node = this.lowerNode(child);
|
|
1255
|
+
if (node) out.push(node);
|
|
1256
|
+
}
|
|
1257
|
+
return out;
|
|
1258
|
+
}
|
|
1259
|
+
isBlankText(n) {
|
|
1260
|
+
return n.kind === "Text" && !n.raw && cleanTemplateText(n.value) === "";
|
|
1261
|
+
}
|
|
1262
|
+
lowerNode(node) {
|
|
1263
|
+
switch (node.kind) {
|
|
1264
|
+
case "Text": {
|
|
1265
|
+
if (node.raw) return { kind: "text", value: node.value };
|
|
1266
|
+
const cleaned = cleanTemplateText(node.value);
|
|
1267
|
+
return cleaned ? { kind: "text", value: cleaned } : null;
|
|
1268
|
+
}
|
|
1269
|
+
case "Comment":
|
|
1270
|
+
return null;
|
|
1271
|
+
// dropped, like JSX comments
|
|
1272
|
+
case "Expression": {
|
|
1273
|
+
const h = this.hole(node.hole);
|
|
1274
|
+
return { kind: "expr", code: h.code, reactive: h.reactive, ...h.at ? { at: h.at } : {} };
|
|
1275
|
+
}
|
|
1276
|
+
case "Fragment":
|
|
1277
|
+
return { kind: "fragment", children: this.lowerChildren(node.children) };
|
|
1278
|
+
case "Element":
|
|
1279
|
+
return this.lowerElement(node);
|
|
1280
|
+
case "Component":
|
|
1281
|
+
return this.lowerComponent(node);
|
|
1282
|
+
default:
|
|
1283
|
+
return null;
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
lowerElement(el) {
|
|
1287
|
+
const isAttr = el.attributes.find((a) => a.kind === "Attribute" && a.name === "is");
|
|
1288
|
+
if (el.tag === "component" && isAttr && isAttr.value && isAttr.value.kind === "hole") {
|
|
1289
|
+
const rest = el.attributes.filter((a) => a !== isAttr);
|
|
1290
|
+
const node = this.lowerComponent({ ...el, kind: "Component", tag: "Dynamic", tagHole: null, attributes: rest });
|
|
1291
|
+
node.props.unshift({ name: "component", kind: "attr", expr: `() => ${paren(this.hole(isAttr.value.hole).code, "arrowBody")}` });
|
|
1292
|
+
return node;
|
|
1293
|
+
}
|
|
1294
|
+
const at = this.tagAt(el);
|
|
1295
|
+
return {
|
|
1296
|
+
kind: "element",
|
|
1297
|
+
tag: el.tag,
|
|
1298
|
+
svg: el.namespace === "svg",
|
|
1299
|
+
props: this.lowerAttributes(el.attributes),
|
|
1300
|
+
children: this.lowerChildren(el.children),
|
|
1301
|
+
static: false,
|
|
1302
|
+
...at ? { at } : {}
|
|
1303
|
+
};
|
|
1304
|
+
}
|
|
1305
|
+
lowerComponent(el) {
|
|
1306
|
+
const name = el.tagHole != null ? this.hole(el.tagHole).code : el.tag;
|
|
1307
|
+
const props = this.lowerAttributes(el.attributes);
|
|
1308
|
+
const { slots, rest } = this.partitionSlots(el.children);
|
|
1309
|
+
for (const [slotName, nodes] of slots) {
|
|
1310
|
+
const ir = nodes.length === 1 ? nodes[0] : { kind: "fragment", children: nodes };
|
|
1311
|
+
const slotProp = {
|
|
1312
|
+
name: slotName,
|
|
1313
|
+
kind: "attr",
|
|
1314
|
+
expr: this.emit(ir),
|
|
1315
|
+
jsxElement: true
|
|
1316
|
+
};
|
|
1317
|
+
const clash = props.findIndex((p) => p.name === slotName);
|
|
1318
|
+
if (clash >= 0) props[clash] = slotProp;
|
|
1319
|
+
else props.push(slotProp);
|
|
1320
|
+
}
|
|
1321
|
+
const loadAttr = el.attributes.find((a) => a.kind === "LoadDirective");
|
|
1322
|
+
const load = loadAttr ? parseLoadDirective(`load:${loadAttr.strategy}`, loadAttr.modifier) : void 0;
|
|
1323
|
+
const bindAt = props.find((p) => p.at && p.reactive)?.at;
|
|
1324
|
+
const at = this.tagAt(el);
|
|
1325
|
+
return {
|
|
1326
|
+
kind: "component",
|
|
1327
|
+
name,
|
|
1328
|
+
props,
|
|
1329
|
+
children: this.lowerChildren(rest),
|
|
1330
|
+
...at ? { at } : {},
|
|
1331
|
+
...bindAt ? { bindAt } : {},
|
|
1332
|
+
...load ? { load } : {}
|
|
1333
|
+
};
|
|
1334
|
+
}
|
|
1335
|
+
/** Split a component's children into named slots (`slot="x"`) and the rest. */
|
|
1336
|
+
partitionSlots(children) {
|
|
1337
|
+
const slots = /* @__PURE__ */ new Map();
|
|
1338
|
+
const rest = [];
|
|
1339
|
+
for (const child of children) {
|
|
1340
|
+
if (child.kind === "Element" || child.kind === "Component") {
|
|
1341
|
+
const slot = child.attributes.find(
|
|
1342
|
+
(a) => a.kind === "Attribute" && a.name === "slot"
|
|
1343
|
+
);
|
|
1344
|
+
if (slot && slot.value && slot.value.kind === "static") {
|
|
1345
|
+
const stripped = { ...child, attributes: child.attributes.filter((a) => a !== slot) };
|
|
1346
|
+
const ir = stripped.kind === "Element" ? this.lowerElement(stripped) : this.lowerComponent(stripped);
|
|
1347
|
+
const bucket = slots.get(slot.value.value) ?? [];
|
|
1348
|
+
bucket.push(ir);
|
|
1349
|
+
slots.set(slot.value.value, bucket);
|
|
1350
|
+
continue;
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
rest.push(child);
|
|
1354
|
+
}
|
|
1355
|
+
return { slots, rest };
|
|
1356
|
+
}
|
|
1357
|
+
// --- control-flow sugar -------------------------------------------------
|
|
1358
|
+
/** `<el if=${cond}>…</el>` (+ optional `<el else>`) → `<Show when fallback>`. */
|
|
1359
|
+
lowerIf(el, condHole, elseEl) {
|
|
1360
|
+
const cond = this.hole(condHole);
|
|
1361
|
+
const props = [
|
|
1362
|
+
{ name: "when", kind: "attr", expr: cond.code, reactive: cond.reactive, ...cond.at ? { at: cond.at } : {} }
|
|
1363
|
+
];
|
|
1364
|
+
if (elseEl) {
|
|
1365
|
+
const elseIR = this.stripAndLower(elseEl, (a) => a.kind === "ElseDirective");
|
|
1366
|
+
props.push({ name: "fallback", kind: "attr", expr: this.emit(elseIR), jsxElement: true });
|
|
1367
|
+
}
|
|
1368
|
+
const child = this.stripAndLower(el, (a) => a.kind === "IfDirective");
|
|
1369
|
+
const at = this.tagAt(el);
|
|
1370
|
+
return { kind: "component", name: "Show", props, children: [child], ...at ? { at } : {}, ...cond.at ? { bindAt: cond.at } : {} };
|
|
1371
|
+
}
|
|
1372
|
+
/** `<el each=${items} key?>${item => …}</el>` → `<For each>{item => <el>…</el>}</For>`. */
|
|
1373
|
+
lowerEach(el) {
|
|
1374
|
+
const eachDir = el.attributes.find((a) => a.kind === "EachDirective");
|
|
1375
|
+
if (!eachDir || eachDir.kind !== "EachDirective") return this.lowerNode(el);
|
|
1376
|
+
const each = this.hole(eachDir.hole);
|
|
1377
|
+
const props = [{ name: "each", kind: "attr", expr: each.code, reactive: each.reactive, ...each.at ? { at: each.at } : {} }];
|
|
1378
|
+
if (eachDir.key) {
|
|
1379
|
+
const by = "static" in eachDir.key ? `(item) => item[${JSON.stringify(eachDir.key.static)}]` : this.hole(eachDir.key.hole).code;
|
|
1380
|
+
props.push({ name: "by", kind: "attr", expr: by });
|
|
1381
|
+
}
|
|
1382
|
+
const arrow = this.itemArrow(el);
|
|
1383
|
+
let childrenCode;
|
|
1384
|
+
if (arrow) {
|
|
1385
|
+
const bodyIR = { kind: "expr", code: arrow.body, reactive: arrow.bodyReactive };
|
|
1386
|
+
const repeated = this.rebuildWithChildren(el, [bodyIR]);
|
|
1387
|
+
childrenCode = `(${arrow.params.join(", ")}) => ${paren(this.emit(repeated), "arrowBody")}`;
|
|
1388
|
+
} else {
|
|
1389
|
+
const repeated = this.stripAndLower(el, (a) => a.kind === "EachDirective");
|
|
1390
|
+
childrenCode = `() => ${paren(this.emit(repeated), "arrowBody")}`;
|
|
1391
|
+
}
|
|
1392
|
+
const children = [{ kind: "expr", code: childrenCode, reactive: false }];
|
|
1393
|
+
const at = this.tagAt(el);
|
|
1394
|
+
return { kind: "component", name: "For", props, children, ...at ? { at } : {}, ...each.at ? { bindAt: each.at } : {} };
|
|
1395
|
+
}
|
|
1396
|
+
/** The single `${item => expr}` child arrow, if that's the element's content. */
|
|
1397
|
+
itemArrow(el) {
|
|
1398
|
+
const kids = el.children.filter((c) => !this.isBlankText(c));
|
|
1399
|
+
if (kids.length !== 1 || kids[0].kind !== "Expression") return null;
|
|
1400
|
+
return this.hole(kids[0].hole).arrow ?? null;
|
|
1401
|
+
}
|
|
1402
|
+
/** Lower an element to IR, dropping the directive attribute matched by `drop`. */
|
|
1403
|
+
stripAndLower(el, drop) {
|
|
1404
|
+
const clone = { ...el, attributes: el.attributes.filter((a) => !drop(a)) };
|
|
1405
|
+
return clone.kind === "Element" ? this.lowerElement(clone) : this.lowerComponent(clone);
|
|
1406
|
+
}
|
|
1407
|
+
/** Same element (minus each/key) but with explicit IR children. */
|
|
1408
|
+
rebuildWithChildren(el, children) {
|
|
1409
|
+
const props = this.lowerAttributes(
|
|
1410
|
+
el.attributes.filter((a) => a.kind !== "EachDirective")
|
|
1411
|
+
);
|
|
1412
|
+
if (el.kind === "Element") {
|
|
1413
|
+
const at = this.tagAt(el);
|
|
1414
|
+
return {
|
|
1415
|
+
kind: "element",
|
|
1416
|
+
tag: el.tag,
|
|
1417
|
+
svg: el.namespace === "svg",
|
|
1418
|
+
props,
|
|
1419
|
+
children,
|
|
1420
|
+
static: false,
|
|
1421
|
+
...at ? { at } : {}
|
|
1422
|
+
};
|
|
1423
|
+
}
|
|
1424
|
+
return { kind: "component", name: el.tag, props, children };
|
|
1425
|
+
}
|
|
1426
|
+
// --- attributes ---------------------------------------------------------
|
|
1427
|
+
lowerAttributes(attrs) {
|
|
1428
|
+
const props = [];
|
|
1429
|
+
const classEntries = [];
|
|
1430
|
+
let classReactive = false;
|
|
1431
|
+
let classAt;
|
|
1432
|
+
const styleEntries = [];
|
|
1433
|
+
let styleReactive = false;
|
|
1434
|
+
let styleAt;
|
|
1435
|
+
const usePairs = [];
|
|
1436
|
+
for (const a of attrs) {
|
|
1437
|
+
switch (a.kind) {
|
|
1438
|
+
case "IfDirective":
|
|
1439
|
+
case "EachDirective":
|
|
1440
|
+
case "ElseDirective":
|
|
1441
|
+
break;
|
|
1442
|
+
// handled as control-flow at the parent level
|
|
1443
|
+
case "Attribute":
|
|
1444
|
+
props.push(this.plainAttr(a));
|
|
1445
|
+
break;
|
|
1446
|
+
case "PropertyBinding":
|
|
1447
|
+
props.push({
|
|
1448
|
+
name: a.name,
|
|
1449
|
+
kind: "prop",
|
|
1450
|
+
expr: this.hole(a.hole).code,
|
|
1451
|
+
reactive: this.hole(a.hole).reactive,
|
|
1452
|
+
...this.hole(a.hole).at ? { at: this.hole(a.hole).at } : {}
|
|
1453
|
+
});
|
|
1454
|
+
break;
|
|
1455
|
+
case "EventBinding":
|
|
1456
|
+
props.push(this.eventProp(a));
|
|
1457
|
+
break;
|
|
1458
|
+
case "RefBinding":
|
|
1459
|
+
props.push({
|
|
1460
|
+
name: "ref",
|
|
1461
|
+
kind: "ref",
|
|
1462
|
+
expr: this.hole(a.hole).code,
|
|
1463
|
+
reactive: this.hole(a.hole).reactive,
|
|
1464
|
+
// Which variable ends up holding the element, and where it was written.
|
|
1465
|
+
...this.hole(a.hole).at ? { at: this.hole(a.hole).at } : {}
|
|
1466
|
+
});
|
|
1467
|
+
break;
|
|
1468
|
+
case "Spread":
|
|
1469
|
+
props.push({ name: "", kind: "spread", expr: this.hole(a.hole).code });
|
|
1470
|
+
break;
|
|
1471
|
+
case "ClassDirective": {
|
|
1472
|
+
const h = this.hole(a.hole);
|
|
1473
|
+
classEntries.push(`${JSON.stringify(a.name)}: ${h.code}`);
|
|
1474
|
+
classReactive = classReactive || h.reactive;
|
|
1475
|
+
classAt ??= h.at;
|
|
1476
|
+
break;
|
|
1477
|
+
}
|
|
1478
|
+
case "StyleDirective": {
|
|
1479
|
+
const h = this.hole(a.hole);
|
|
1480
|
+
styleEntries.push(`${JSON.stringify(a.name)}: ${h.code}`);
|
|
1481
|
+
styleReactive = styleReactive || h.reactive;
|
|
1482
|
+
styleAt ??= h.at;
|
|
1483
|
+
break;
|
|
1484
|
+
}
|
|
1485
|
+
case "BindDirective":
|
|
1486
|
+
props.push(...this.bindProps(a.name, this.hole(a.hole)));
|
|
1487
|
+
break;
|
|
1488
|
+
case "UseDirective": {
|
|
1489
|
+
const dir = a.name ?? (a.hole != null ? this.hole(a.hole).code : null);
|
|
1490
|
+
if (!dir) break;
|
|
1491
|
+
usePairs.push(a.name != null && a.hole != null ? `[${dir}, () => ${paren(this.hole(a.hole).code, "arrowBody")}]` : `[${dir}]`);
|
|
1492
|
+
break;
|
|
1493
|
+
}
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
if (classEntries.length > 0) {
|
|
1497
|
+
props.push({
|
|
1498
|
+
name: "classList",
|
|
1499
|
+
kind: "attr",
|
|
1500
|
+
expr: `{ ${classEntries.join(", ")} }`,
|
|
1501
|
+
reactive: classReactive,
|
|
1502
|
+
...classAt ? { at: classAt } : {}
|
|
1503
|
+
});
|
|
1504
|
+
}
|
|
1505
|
+
if (styleEntries.length > 0) {
|
|
1506
|
+
props.push({
|
|
1507
|
+
name: "style",
|
|
1508
|
+
kind: "attr",
|
|
1509
|
+
expr: `{ ${styleEntries.join(", ")} }`,
|
|
1510
|
+
reactive: styleReactive,
|
|
1511
|
+
...styleAt ? { at: styleAt } : {}
|
|
1512
|
+
});
|
|
1513
|
+
}
|
|
1514
|
+
if (usePairs.length > 0) {
|
|
1515
|
+
props.push({ name: "use", kind: "attr", expr: `[${usePairs.join(", ")}]` });
|
|
1516
|
+
}
|
|
1517
|
+
return props;
|
|
1518
|
+
}
|
|
1519
|
+
/**
|
|
1520
|
+
* Lower an event binding. Plain `@click`/`onClick` (no modifiers) stays a
|
|
1521
|
+
* DELEGATED handler (unchanged, byte-identical with JSX). `on:click` or any
|
|
1522
|
+
* modifier (`.capture`/`.once`/`.passive`/`.prevent`/`.stop`/`.self`) emits a
|
|
1523
|
+
* NATIVE `on:name` prop the runtime attaches via addEventListener.
|
|
1524
|
+
*/
|
|
1525
|
+
eventProp(a) {
|
|
1526
|
+
const dom = a.name.toLowerCase();
|
|
1527
|
+
const handler = this.hole(a.hole).code;
|
|
1528
|
+
const native = a.syntax === "colon" || a.modifiers.length > 0;
|
|
1529
|
+
if (!native) {
|
|
1530
|
+
return {
|
|
1531
|
+
name: "on" + capitalize(a.name),
|
|
1532
|
+
kind: "event",
|
|
1533
|
+
event: { name: dom, delegated: DELEGATED.has(dom) },
|
|
1534
|
+
expr: handler
|
|
1535
|
+
};
|
|
1536
|
+
}
|
|
1537
|
+
const wrapped = this.wrapHandler(handler, a.modifiers);
|
|
1538
|
+
const options = this.eventOptions(a.modifiers);
|
|
1539
|
+
const value = options ? `[${wrapped}, ${options}]` : wrapped;
|
|
1540
|
+
return { name: "on:" + dom, kind: "attr", expr: value };
|
|
1541
|
+
}
|
|
1542
|
+
/** Wrap a handler for `.prevent`/`.stop`/`.self` (no wrap when none apply). */
|
|
1543
|
+
wrapHandler(handler, mods) {
|
|
1544
|
+
const guard = mods.includes("self") ? "if (e.target !== e.currentTarget) return; " : "";
|
|
1545
|
+
const pre = [];
|
|
1546
|
+
if (mods.includes("prevent")) pre.push("e.preventDefault();");
|
|
1547
|
+
if (mods.includes("stop")) pre.push("e.stopPropagation();");
|
|
1548
|
+
if (!guard && pre.length === 0) return handler;
|
|
1549
|
+
return `(e) => { ${guard}${pre.join(" ")} return (${handler})(e); }`;
|
|
1550
|
+
}
|
|
1551
|
+
/** addEventListener options object from `.capture`/`.once`/`.passive`, or null. */
|
|
1552
|
+
eventOptions(mods) {
|
|
1553
|
+
const opts = [];
|
|
1554
|
+
if (mods.includes("capture")) opts.push("capture: true");
|
|
1555
|
+
if (mods.includes("once")) opts.push("once: true");
|
|
1556
|
+
if (mods.includes("passive")) opts.push("passive: true");
|
|
1557
|
+
return opts.length ? `{ ${opts.join(", ")} }` : null;
|
|
1558
|
+
}
|
|
1559
|
+
/** `bind:value=${sig}` → `value={read()}` + `onInput={e => write(e…value)}`. */
|
|
1560
|
+
bindProps(prop, h) {
|
|
1561
|
+
const sigCode = h.code;
|
|
1562
|
+
const checked = prop === "checked";
|
|
1563
|
+
const evt = checked ? "change" : "input";
|
|
1564
|
+
const accessor = checked ? "checked" : "value";
|
|
1565
|
+
this.used.add("bindPair");
|
|
1566
|
+
const sig = `bindPair(${sigCode})`;
|
|
1567
|
+
return [
|
|
1568
|
+
{
|
|
1569
|
+
name: prop,
|
|
1570
|
+
kind: "attr",
|
|
1571
|
+
expr: `${sig}[0]()`,
|
|
1572
|
+
reactive: true,
|
|
1573
|
+
directive: `bind:${prop}`,
|
|
1574
|
+
...h.at ? { at: h.at } : {}
|
|
1575
|
+
},
|
|
1576
|
+
{
|
|
1577
|
+
name: "on" + capitalize(evt),
|
|
1578
|
+
kind: "event",
|
|
1579
|
+
event: { name: evt, delegated: DELEGATED.has(evt) },
|
|
1580
|
+
// delegated handler: `target` is the bound element (currentTarget is the root)
|
|
1581
|
+
expr: `(e) => ${sig}[1](e.target.${accessor})`
|
|
1582
|
+
}
|
|
1583
|
+
];
|
|
1584
|
+
}
|
|
1585
|
+
/** A plain attribute (incl. lit `?bool`, mixed values) — old `mkProp` behavior. */
|
|
1586
|
+
plainAttr(a) {
|
|
1587
|
+
const v = a.value;
|
|
1588
|
+
let name = a.name;
|
|
1589
|
+
if (a.name === "class") {
|
|
1590
|
+
name = v && v.kind === "hole" && this.hole(v.hole).object ? "classList" : "className";
|
|
1591
|
+
} else if (a.name === "html") {
|
|
1592
|
+
name = "innerHTML";
|
|
1593
|
+
}
|
|
1594
|
+
const kind = "attr";
|
|
1595
|
+
const base = { name, kind };
|
|
1596
|
+
if (v == null) {
|
|
1597
|
+
base.literal = true;
|
|
1598
|
+
return base;
|
|
1599
|
+
}
|
|
1600
|
+
if (v.kind === "static") {
|
|
1601
|
+
base.literal = v.value;
|
|
1602
|
+
return base;
|
|
1603
|
+
}
|
|
1604
|
+
if (v.kind === "hole") {
|
|
1605
|
+
const h = this.hole(v.hole);
|
|
1606
|
+
base.expr = h.code;
|
|
1607
|
+
base.reactive = h.reactive;
|
|
1608
|
+
if (h.at) base.at = h.at;
|
|
1609
|
+
return base;
|
|
1610
|
+
}
|
|
1611
|
+
let reactive = false;
|
|
1612
|
+
let at;
|
|
1613
|
+
const body = v.parts.map((p) => {
|
|
1614
|
+
if ("text" in p) return escapeTemplate(p.text);
|
|
1615
|
+
const h = this.hole(p.hole);
|
|
1616
|
+
reactive = reactive || h.reactive;
|
|
1617
|
+
at ??= h.at;
|
|
1618
|
+
return "${" + h.code + "}";
|
|
1619
|
+
}).join("");
|
|
1620
|
+
base.expr = "`" + body + "`";
|
|
1621
|
+
base.reactive = reactive;
|
|
1622
|
+
if (at) base.at = at;
|
|
1623
|
+
return base;
|
|
1624
|
+
}
|
|
1625
|
+
};
|
|
1626
|
+
function lowerTemplate(pieces, holes, used, opts = {}) {
|
|
1627
|
+
const { root } = (0, import_template_parser.parseTemplate)(pieces, { svg: opts.svg });
|
|
1628
|
+
return new Lowerer(holes, used, opts.sourceFile, opts.positionAt).lowerRoot(root.children);
|
|
1629
|
+
}
|
|
1630
|
+
|
|
1631
|
+
// src/lower/compile-template.ts
|
|
1632
|
+
function compilePieces(pieces, holes, options = {}) {
|
|
1633
|
+
const used = /* @__PURE__ */ new Set();
|
|
1634
|
+
const ir = lowerTemplate(pieces, [...holes], used, {
|
|
1635
|
+
svg: options.svg,
|
|
1636
|
+
...options.sourceFile ? { sourceFile: options.sourceFile } : {},
|
|
1637
|
+
...options.positionAt ? { positionAt: options.positionAt } : {}
|
|
1638
|
+
});
|
|
1639
|
+
analyzeStatic(ir);
|
|
1640
|
+
resolveComponentSources(ir, {
|
|
1641
|
+
resolver: createComponentResolver(options.resolve ?? []),
|
|
1642
|
+
modules: {
|
|
1643
|
+
controlFlowModule: options.controlFlowModule ?? "@fluixi/dom",
|
|
1644
|
+
coreModule: options.coreModule ?? "@fluixi/core",
|
|
1645
|
+
routerModule: options.routerModule ?? "@fluixi/core/router"
|
|
1646
|
+
},
|
|
1647
|
+
strategyFor: (node) => node.load
|
|
1648
|
+
});
|
|
1649
|
+
collapseStrategies(ir);
|
|
1650
|
+
const { code, imports, templates } = imperativeBackend.emit(ir, {
|
|
1651
|
+
templateClone: options.templateClone,
|
|
1652
|
+
partialTemplates: options.partialTemplates
|
|
1653
|
+
});
|
|
1654
|
+
for (const symbol of imports) used.add(symbol);
|
|
1655
|
+
if (options.hoistTemplates) return { code, imports: [...used], ir, templates };
|
|
1656
|
+
return { code: inlineTemplates(code, templates), imports: [...used], ir };
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1659
|
+
// src/analyze/reactive-expr.ts
|
|
1660
|
+
var STABLE_MEMBER = "children";
|
|
1661
|
+
function isReactiveShape(shape) {
|
|
1662
|
+
switch (shape.kind) {
|
|
1663
|
+
case "call":
|
|
1664
|
+
return true;
|
|
1665
|
+
case "member":
|
|
1666
|
+
return shape.property !== STABLE_MEMBER;
|
|
1667
|
+
case "compound":
|
|
1668
|
+
return shape.parts.some(isReactiveShape);
|
|
1669
|
+
case "opaque":
|
|
1670
|
+
return false;
|
|
1671
|
+
}
|
|
1672
|
+
}
|
|
1673
|
+
|
|
1674
|
+
// src/analyze/expr-shape.ts
|
|
1675
|
+
var CALL = /* @__PURE__ */ new Set(["CallExpression", "OptionalCallExpression"]);
|
|
1676
|
+
var MEMBER = /* @__PURE__ */ new Set(["MemberExpression", "OptionalMemberExpression"]);
|
|
1677
|
+
function shapeOf(node) {
|
|
1678
|
+
if (!node) return { kind: "opaque" };
|
|
1679
|
+
if (CALL.has(node.type)) return { kind: "call" };
|
|
1680
|
+
if (MEMBER.has(node.type)) {
|
|
1681
|
+
const property = node.property;
|
|
1682
|
+
const computed = node.computed === true;
|
|
1683
|
+
return {
|
|
1684
|
+
kind: "member",
|
|
1685
|
+
property: !computed && property?.type === "Identifier" ? property.name : null
|
|
1686
|
+
};
|
|
1687
|
+
}
|
|
1688
|
+
if (node.type === "ConditionalExpression") {
|
|
1689
|
+
return { kind: "compound", parts: [node.test, node.consequent, node.alternate].map(part) };
|
|
1690
|
+
}
|
|
1691
|
+
if (node.type === "LogicalExpression" || node.type === "BinaryExpression") {
|
|
1692
|
+
return { kind: "compound", parts: [node.left, node.right].map(part) };
|
|
1693
|
+
}
|
|
1694
|
+
if (node.type === "TemplateLiteral") {
|
|
1695
|
+
return { kind: "compound", parts: node.expressions.map(part) };
|
|
1696
|
+
}
|
|
1697
|
+
if (node.type === "ObjectExpression") {
|
|
1698
|
+
const props = node.properties.filter(
|
|
1699
|
+
(p) => (p.type === "ObjectProperty" || p.type === "Property") && p.computed !== true
|
|
1700
|
+
);
|
|
1701
|
+
return { kind: "compound", parts: props.map((p) => part(p.value)) };
|
|
1702
|
+
}
|
|
1703
|
+
if (node.type === "ArrayExpression") {
|
|
1704
|
+
const items = node.elements.filter(
|
|
1705
|
+
(e) => e != null && e.type !== "SpreadElement"
|
|
1706
|
+
);
|
|
1707
|
+
return { kind: "compound", parts: items.map(part) };
|
|
1708
|
+
}
|
|
1709
|
+
return { kind: "opaque" };
|
|
1710
|
+
}
|
|
1711
|
+
var part = (n) => shapeOf(n);
|
|
1712
|
+
|
|
1713
|
+
// src/lower/jsx.ts
|
|
1714
|
+
var SVG_TAGS = /* @__PURE__ */ new Set([
|
|
1715
|
+
"svg",
|
|
1716
|
+
"path",
|
|
1717
|
+
"circle",
|
|
1718
|
+
"rect",
|
|
1719
|
+
"line",
|
|
1720
|
+
"polygon",
|
|
1721
|
+
"polyline",
|
|
1722
|
+
"ellipse",
|
|
1723
|
+
"g",
|
|
1724
|
+
"defs",
|
|
1725
|
+
"clipPath",
|
|
1726
|
+
"text"
|
|
1727
|
+
]);
|
|
1728
|
+
var EVENT_RE = /^on[A-Z]/;
|
|
1729
|
+
var DELEGATED2 = /* @__PURE__ */ new Set([
|
|
1730
|
+
"click",
|
|
1731
|
+
"dblclick",
|
|
1732
|
+
"input",
|
|
1733
|
+
"change",
|
|
1734
|
+
"submit",
|
|
1735
|
+
"focus",
|
|
1736
|
+
"blur",
|
|
1737
|
+
"keydown",
|
|
1738
|
+
"keyup",
|
|
1739
|
+
"keypress",
|
|
1740
|
+
"mousedown",
|
|
1741
|
+
"mouseup"
|
|
1742
|
+
]);
|
|
1743
|
+
var isReactive = (node) => isReactiveShape(shapeOf(node));
|
|
1744
|
+
var posOf = (node, ctx) => {
|
|
1745
|
+
if (!ctx.sourceFile) return void 0;
|
|
1746
|
+
const at = node.loc?.start;
|
|
1747
|
+
return at ? { line: at.line, column: at.column } : void 0;
|
|
1748
|
+
};
|
|
1749
|
+
function literalOf(node) {
|
|
1750
|
+
if (node.type === "StringLiteral" || node.type === "NumericLiteral" || node.type === "BooleanLiteral") {
|
|
1751
|
+
return node.value;
|
|
1752
|
+
}
|
|
1753
|
+
if (node.type === "Literal" && (typeof node.value === "string" || typeof node.value === "number" || typeof node.value === "boolean")) {
|
|
1754
|
+
return node.value;
|
|
1755
|
+
}
|
|
1756
|
+
return void 0;
|
|
1757
|
+
}
|
|
1758
|
+
var isString = (node) => !!node && typeof literalOf(node) === "string";
|
|
1759
|
+
function tagInfo(name, ctx) {
|
|
1760
|
+
if (name.type === "JSXIdentifier") {
|
|
1761
|
+
return { tag: name.name, component: name.name[0] !== name.name[0].toLowerCase() };
|
|
1762
|
+
}
|
|
1763
|
+
if (name.type === "JSXMemberExpression") return { tag: ctx.code(name), component: true };
|
|
1764
|
+
if (name.type === "JSXNamespacedName") {
|
|
1765
|
+
return { tag: `${name.namespace.name}:${name.name.name}`, component: false };
|
|
1766
|
+
}
|
|
1767
|
+
return { tag: "div", component: false };
|
|
1768
|
+
}
|
|
1769
|
+
function attrName(name) {
|
|
1770
|
+
if (name.type === "JSXIdentifier") return name.name === "class" ? "className" : name.name;
|
|
1771
|
+
if (name.type === "JSXNamespacedName") return `${name.namespace.name}:${name.name.name}`;
|
|
1772
|
+
return "unknown";
|
|
1773
|
+
}
|
|
1774
|
+
function buildAttr(attr, ctx) {
|
|
1775
|
+
const name = attrName(attr.name);
|
|
1776
|
+
const isEvent = EVENT_RE.test(name);
|
|
1777
|
+
const isHandler = isEvent || name.startsWith("on:") || name === "ref";
|
|
1778
|
+
const kind = isEvent ? "event" : "attr";
|
|
1779
|
+
const base = { name, kind };
|
|
1780
|
+
if (isEvent) {
|
|
1781
|
+
const evt = name.slice(2).toLowerCase();
|
|
1782
|
+
base.event = { name: evt, delegated: DELEGATED2.has(evt) };
|
|
1783
|
+
}
|
|
1784
|
+
const v = attr.value;
|
|
1785
|
+
if (v == null) {
|
|
1786
|
+
base.literal = true;
|
|
1787
|
+
base.at = posOf(attr, ctx);
|
|
1788
|
+
return base;
|
|
1789
|
+
}
|
|
1790
|
+
if (isString(v)) {
|
|
1791
|
+
base.literal = literalOf(v);
|
|
1792
|
+
base.at = posOf(v, ctx);
|
|
1793
|
+
return base;
|
|
1794
|
+
}
|
|
1795
|
+
if (v.type === "JSXExpressionContainer" && v.expression?.type !== "JSXEmptyExpression") {
|
|
1796
|
+
const expr = v.expression;
|
|
1797
|
+
const literal = literalOf(expr);
|
|
1798
|
+
if (literal !== void 0) {
|
|
1799
|
+
base.literal = literal;
|
|
1800
|
+
base.at = posOf(expr, ctx);
|
|
1801
|
+
return base;
|
|
1802
|
+
}
|
|
1803
|
+
base.expr = ctx.code(expr);
|
|
1804
|
+
base.reactive = isHandler ? false : isReactive(expr);
|
|
1805
|
+
base.at = posOf(expr, ctx);
|
|
1806
|
+
if (expr.type === "JSXElement" || expr.type === "JSXFragment") base.jsxElement = true;
|
|
1807
|
+
return base;
|
|
1808
|
+
}
|
|
1809
|
+
base.literal = true;
|
|
1810
|
+
return base;
|
|
1811
|
+
}
|
|
1812
|
+
function attrExprCode(attr, ctx) {
|
|
1813
|
+
const v = attr.value;
|
|
1814
|
+
if (v == null) return null;
|
|
1815
|
+
if (isString(v)) return JSON.stringify(literalOf(v));
|
|
1816
|
+
if (v.type === "JSXExpressionContainer" && v.expression?.type !== "JSXEmptyExpression") {
|
|
1817
|
+
return ctx.code(v.expression);
|
|
1818
|
+
}
|
|
1819
|
+
return null;
|
|
1820
|
+
}
|
|
1821
|
+
function buildProps(attrs, ctx) {
|
|
1822
|
+
const props = [];
|
|
1823
|
+
const usePairs = [];
|
|
1824
|
+
for (const a of attrs) {
|
|
1825
|
+
if (a.type === "JSXSpreadAttribute") {
|
|
1826
|
+
props.push({ name: "", kind: "spread", expr: ctx.code(a.argument) });
|
|
1827
|
+
continue;
|
|
1828
|
+
}
|
|
1829
|
+
if (a.type !== "JSXAttribute") continue;
|
|
1830
|
+
if (isLoadDirective(attrName(a.name))) continue;
|
|
1831
|
+
const ns = a.name.type === "JSXNamespacedName" ? a.name.namespace.name : null;
|
|
1832
|
+
if (ns === "use") {
|
|
1833
|
+
const dir = a.name.name.name;
|
|
1834
|
+
ctx.used.add(dir);
|
|
1835
|
+
const opts = attrExprCode(a, ctx);
|
|
1836
|
+
usePairs.push(opts != null ? `[${dir}, () => ${paren(opts, "arrowBody")}]` : `[${dir}]`);
|
|
1837
|
+
continue;
|
|
1838
|
+
}
|
|
1839
|
+
if (ns === "oncapture") {
|
|
1840
|
+
const evt = a.name.name.name.toLowerCase();
|
|
1841
|
+
const h = attrExprCode(a, ctx) ?? "undefined";
|
|
1842
|
+
props.push({ name: "on:" + evt, kind: "attr", expr: `[${h}, { capture: true }]` });
|
|
1843
|
+
continue;
|
|
1844
|
+
}
|
|
1845
|
+
props.push(buildAttr(a, ctx));
|
|
1846
|
+
}
|
|
1847
|
+
if (usePairs.length > 0) props.push({ name: "use", kind: "attr", expr: `[${usePairs.join(", ")}]` });
|
|
1848
|
+
return props;
|
|
1849
|
+
}
|
|
1850
|
+
function buildChildren(children, ctx) {
|
|
1851
|
+
const out = [];
|
|
1852
|
+
for (const child of children) {
|
|
1853
|
+
if (child.type === "JSXText") {
|
|
1854
|
+
const text = cleanTemplateText(child.value);
|
|
1855
|
+
if (text) out.push({ kind: "text", value: text });
|
|
1856
|
+
} else if (child.type === "JSXExpressionContainer") {
|
|
1857
|
+
if (child.expression?.type !== "JSXEmptyExpression") {
|
|
1858
|
+
const expr = child.expression;
|
|
1859
|
+
const reactive = isReactive(expr);
|
|
1860
|
+
out.push({
|
|
1861
|
+
kind: "expr",
|
|
1862
|
+
code: ctx.code(expr),
|
|
1863
|
+
reactive,
|
|
1864
|
+
...reactive ? { at: posOf(expr, ctx) } : {}
|
|
1865
|
+
});
|
|
1866
|
+
}
|
|
1867
|
+
} else if (child.type === "JSXElement" || child.type === "JSXFragment") {
|
|
1868
|
+
out.push(buildNode(child, ctx));
|
|
1869
|
+
} else if (child.type === "JSXSpreadChild") {
|
|
1870
|
+
out.push({ kind: "expr", code: ctx.code(child.expression), reactive: false });
|
|
1871
|
+
}
|
|
1872
|
+
}
|
|
1873
|
+
return out;
|
|
1874
|
+
}
|
|
1875
|
+
function readLoad(attrs) {
|
|
1876
|
+
for (const a of attrs) {
|
|
1877
|
+
if (a.type !== "JSXAttribute") continue;
|
|
1878
|
+
const name = attrName(a.name);
|
|
1879
|
+
if (!isLoadDirective(name)) continue;
|
|
1880
|
+
if (a.value && !isString(a.value)) {
|
|
1881
|
+
throw new LoadDirectiveError(
|
|
1882
|
+
`'${name}' needs a literal value, not an expression — the strategy is compile-time.`
|
|
1883
|
+
);
|
|
1884
|
+
}
|
|
1885
|
+
return parseLoadDirective(name, a.value ? literalOf(a.value) : null);
|
|
1886
|
+
}
|
|
1887
|
+
return void 0;
|
|
1888
|
+
}
|
|
1889
|
+
function buildNode(node, ctx) {
|
|
1890
|
+
if (node.type === "JSXFragment") {
|
|
1891
|
+
return { kind: "fragment", children: buildChildren(node.children, ctx) };
|
|
1892
|
+
}
|
|
1893
|
+
const { tag, component } = tagInfo(node.openingElement.name, ctx);
|
|
1894
|
+
const props = buildProps(node.openingElement.attributes, ctx);
|
|
1895
|
+
const children = buildChildren(node.children, ctx);
|
|
1896
|
+
const at = ctx.sourceFile && node.loc?.start ? { file: ctx.sourceFile, line: node.loc.start.line, column: node.loc.start.column } : void 0;
|
|
1897
|
+
if (component) {
|
|
1898
|
+
const load = readLoad(node.openingElement.attributes);
|
|
1899
|
+
return { kind: "component", name: tag, props, children, ...load ? { load } : {}, ...at ? { at } : {} };
|
|
1900
|
+
}
|
|
1901
|
+
return { kind: "element", tag, svg: SVG_TAGS.has(tag), props, children, static: false, ...at ? { at } : {} };
|
|
1902
|
+
}
|
|
1903
|
+
function buildJsxIR(node, ctx) {
|
|
1904
|
+
const ir = buildNode(node, ctx);
|
|
1905
|
+
analyzeStatic(ir);
|
|
1906
|
+
return ir;
|
|
1907
|
+
}
|
|
1908
|
+
|
|
1909
|
+
// src/transform/bindings.ts
|
|
1910
|
+
function patternNames2(node, out) {
|
|
1911
|
+
if (!node) return;
|
|
1912
|
+
switch (node.type) {
|
|
1913
|
+
case "Identifier":
|
|
1914
|
+
out.add(node.name);
|
|
1915
|
+
return;
|
|
1916
|
+
case "ObjectPattern":
|
|
1917
|
+
for (const p of node.properties) {
|
|
1918
|
+
if (p.type === "RestElement") patternNames2(p.argument, out);
|
|
1919
|
+
else patternNames2(p.value, out);
|
|
1920
|
+
}
|
|
1921
|
+
return;
|
|
1922
|
+
case "ArrayPattern":
|
|
1923
|
+
for (const e of node.elements) patternNames2(e, out);
|
|
1924
|
+
return;
|
|
1925
|
+
case "AssignmentPattern":
|
|
1926
|
+
patternNames2(node.left, out);
|
|
1927
|
+
return;
|
|
1928
|
+
case "RestElement":
|
|
1929
|
+
patternNames2(node.argument, out);
|
|
1930
|
+
return;
|
|
1931
|
+
}
|
|
1932
|
+
}
|
|
1933
|
+
function declared(node, out) {
|
|
1934
|
+
switch (node.type) {
|
|
1935
|
+
case "ImportDeclaration":
|
|
1936
|
+
for (const s of node.specifiers) patternNames2(s.local, out);
|
|
1937
|
+
return;
|
|
1938
|
+
case "VariableDeclaration":
|
|
1939
|
+
for (const d of node.declarations) patternNames2(d.id, out);
|
|
1940
|
+
return;
|
|
1941
|
+
case "FunctionDeclaration":
|
|
1942
|
+
case "ClassDeclaration":
|
|
1943
|
+
patternNames2(node.id, out);
|
|
1944
|
+
return;
|
|
1945
|
+
case "ExportNamedDeclaration":
|
|
1946
|
+
case "ExportDefaultDeclaration":
|
|
1947
|
+
if (node.declaration) declared(node.declaration, out);
|
|
1948
|
+
return;
|
|
1949
|
+
}
|
|
1950
|
+
}
|
|
1951
|
+
function moduleBindings(program) {
|
|
1952
|
+
const names = /* @__PURE__ */ new Set();
|
|
1953
|
+
for (const statement of program.body) declared(statement, names);
|
|
1954
|
+
return names;
|
|
1955
|
+
}
|
|
1956
|
+
|
|
1957
|
+
// src/transform/props.ts
|
|
1958
|
+
var import_parser = require("@babel/parser");
|
|
1959
|
+
var import_magic_string = __toESM(require("magic-string"), 1);
|
|
1960
|
+
|
|
1961
|
+
// src/analyze/props-destructure.ts
|
|
1962
|
+
function patternNames3(node, out) {
|
|
1963
|
+
if (!node) return;
|
|
1964
|
+
switch (node.type) {
|
|
1965
|
+
case "Identifier":
|
|
1966
|
+
out.add(node.name);
|
|
1967
|
+
return;
|
|
1968
|
+
case "ObjectPattern":
|
|
1969
|
+
for (const p of node.properties) {
|
|
1970
|
+
if (p.type === "RestElement") patternNames3(p.argument, out);
|
|
1971
|
+
else patternNames3(p.value, out);
|
|
1972
|
+
}
|
|
1973
|
+
return;
|
|
1974
|
+
case "ArrayPattern":
|
|
1975
|
+
for (const e of node.elements) patternNames3(e, out);
|
|
1976
|
+
return;
|
|
1977
|
+
case "AssignmentPattern":
|
|
1978
|
+
patternNames3(node.left, out);
|
|
1979
|
+
return;
|
|
1980
|
+
case "RestElement":
|
|
1981
|
+
patternNames3(node.argument, out);
|
|
1982
|
+
return;
|
|
1983
|
+
}
|
|
1984
|
+
}
|
|
1985
|
+
function isSafeDefault(node) {
|
|
1986
|
+
switch (node.type) {
|
|
1987
|
+
case "StringLiteral":
|
|
1988
|
+
case "NumericLiteral":
|
|
1989
|
+
case "BooleanLiteral":
|
|
1990
|
+
case "NullLiteral":
|
|
1991
|
+
case "BigIntLiteral":
|
|
1992
|
+
case "RegExpLiteral":
|
|
1993
|
+
case "Identifier":
|
|
1994
|
+
return true;
|
|
1995
|
+
// ESTree spells every literal as `Literal`; both shapes reach this analysis.
|
|
1996
|
+
case "Literal":
|
|
1997
|
+
return true;
|
|
1998
|
+
case "TemplateLiteral":
|
|
1999
|
+
return node.expressions.every(isSafeDefault);
|
|
2000
|
+
case "UnaryExpression":
|
|
2001
|
+
return isSafeDefault(node.argument);
|
|
2002
|
+
case "MemberExpression":
|
|
2003
|
+
return false;
|
|
2004
|
+
default:
|
|
2005
|
+
return false;
|
|
2006
|
+
}
|
|
2007
|
+
}
|
|
2008
|
+
function walk4(root, visit) {
|
|
2009
|
+
if (!root || typeof root !== "object") return;
|
|
2010
|
+
visit(root);
|
|
2011
|
+
for (const key of Object.keys(root)) {
|
|
2012
|
+
if (key === "loc" || key === "range" || key === "leadingComments" || key === "trailingComments") continue;
|
|
2013
|
+
const value = root[key];
|
|
2014
|
+
if (Array.isArray(value)) {
|
|
2015
|
+
for (const item of value) if (item && typeof item === "object") walk4(item, visit);
|
|
2016
|
+
} else if (value && typeof value === "object" && typeof value.type === "string") {
|
|
2017
|
+
walk4(value, visit);
|
|
2018
|
+
}
|
|
2019
|
+
}
|
|
2020
|
+
}
|
|
2021
|
+
function reassignedNames(body) {
|
|
2022
|
+
const written = /* @__PURE__ */ new Set();
|
|
2023
|
+
walk4(body, (node) => {
|
|
2024
|
+
if (node.type === "AssignmentExpression") patternNames3(node.left, written);
|
|
2025
|
+
else if (node.type === "UpdateExpression") {
|
|
2026
|
+
const arg = node.argument;
|
|
2027
|
+
if (arg?.type === "Identifier") written.add(arg.name);
|
|
2028
|
+
}
|
|
2029
|
+
});
|
|
2030
|
+
return written;
|
|
2031
|
+
}
|
|
2032
|
+
function shadowedNames(body, candidates) {
|
|
2033
|
+
const shadowed = /* @__PURE__ */ new Set();
|
|
2034
|
+
const note = (node) => {
|
|
2035
|
+
const names = /* @__PURE__ */ new Set();
|
|
2036
|
+
patternNames3(node, names);
|
|
2037
|
+
for (const n of names) if (candidates.has(n)) shadowed.add(n);
|
|
2038
|
+
};
|
|
2039
|
+
walk4(body, (node) => {
|
|
2040
|
+
switch (node.type) {
|
|
2041
|
+
case "VariableDeclarator":
|
|
2042
|
+
note(node.id);
|
|
2043
|
+
return;
|
|
2044
|
+
case "FunctionDeclaration":
|
|
2045
|
+
case "FunctionExpression":
|
|
2046
|
+
case "ArrowFunctionExpression":
|
|
2047
|
+
note(node.id);
|
|
2048
|
+
for (const p of node.params ?? []) note(p);
|
|
2049
|
+
return;
|
|
2050
|
+
case "CatchClause":
|
|
2051
|
+
note(node.param);
|
|
2052
|
+
return;
|
|
2053
|
+
case "ClassDeclaration":
|
|
2054
|
+
case "ClassExpression":
|
|
2055
|
+
note(node.id);
|
|
2056
|
+
return;
|
|
2057
|
+
}
|
|
2058
|
+
});
|
|
2059
|
+
return shadowed;
|
|
2060
|
+
}
|
|
2061
|
+
function collect(pattern, path, plan) {
|
|
2062
|
+
for (const property of pattern.properties ?? []) {
|
|
2063
|
+
if (property.type === "RestElement") {
|
|
2064
|
+
const argument = property.argument;
|
|
2065
|
+
if (argument?.type !== "Identifier" || path.length > 0) {
|
|
2066
|
+
const names2 = /* @__PURE__ */ new Set();
|
|
2067
|
+
patternNames3(argument, names2);
|
|
2068
|
+
for (const name of names2) plan.bails.push({ name, reason: "rest" });
|
|
2069
|
+
continue;
|
|
2070
|
+
}
|
|
2071
|
+
plan.rest = { name: argument.name, keys: [] };
|
|
2072
|
+
continue;
|
|
2073
|
+
}
|
|
2074
|
+
if (property.computed) {
|
|
2075
|
+
const names2 = /* @__PURE__ */ new Set();
|
|
2076
|
+
patternNames3(property.value, names2);
|
|
2077
|
+
for (const name of names2) plan.bails.push({ name, reason: "computed" });
|
|
2078
|
+
continue;
|
|
2079
|
+
}
|
|
2080
|
+
const key = property.key;
|
|
2081
|
+
const propertyName = key.type === "Identifier" ? key.name : key.value;
|
|
2082
|
+
let value = property.value;
|
|
2083
|
+
let fallback;
|
|
2084
|
+
if (value.type === "AssignmentPattern") {
|
|
2085
|
+
fallback = value.right;
|
|
2086
|
+
value = value.left;
|
|
2087
|
+
}
|
|
2088
|
+
const here = [...path, propertyName];
|
|
2089
|
+
if (value.type === "Identifier") {
|
|
2090
|
+
if (fallback && !isSafeDefault(fallback)) {
|
|
2091
|
+
plan.bails.push({ name: value.name, reason: "unsafe-default" });
|
|
2092
|
+
continue;
|
|
2093
|
+
}
|
|
2094
|
+
plan.reads.push({ name: value.name, path: here, ...fallback ? { fallback } : {} });
|
|
2095
|
+
continue;
|
|
2096
|
+
}
|
|
2097
|
+
if (value.type === "ObjectPattern") {
|
|
2098
|
+
if (fallback) {
|
|
2099
|
+
const names2 = /* @__PURE__ */ new Set();
|
|
2100
|
+
patternNames3(value, names2);
|
|
2101
|
+
for (const name of names2) plan.bails.push({ name, reason: "unsafe-default" });
|
|
2102
|
+
continue;
|
|
2103
|
+
}
|
|
2104
|
+
collect(value, here, plan);
|
|
2105
|
+
continue;
|
|
2106
|
+
}
|
|
2107
|
+
const names = /* @__PURE__ */ new Set();
|
|
2108
|
+
patternNames3(value, names);
|
|
2109
|
+
for (const name of names) plan.bails.push({ name, reason: "computed" });
|
|
2110
|
+
}
|
|
2111
|
+
}
|
|
2112
|
+
function planPropsDestructure(pattern, body) {
|
|
2113
|
+
const plan = { reads: [], bails: [] };
|
|
2114
|
+
if (pattern?.type !== "ObjectPattern") return plan;
|
|
2115
|
+
collect(pattern, [], plan);
|
|
2116
|
+
if (plan.rest) {
|
|
2117
|
+
if (body?.type !== "BlockStatement") {
|
|
2118
|
+
plan.bails.push({ name: plan.rest.name, reason: "rest" });
|
|
2119
|
+
delete plan.rest;
|
|
2120
|
+
} else {
|
|
2121
|
+
plan.rest.keys = (pattern.properties ?? []).filter((p) => p.type !== "RestElement" && !p.computed).map((p) => {
|
|
2122
|
+
const key = p.key;
|
|
2123
|
+
return key.type === "Identifier" ? key.name : key.value;
|
|
2124
|
+
});
|
|
2125
|
+
}
|
|
2126
|
+
}
|
|
2127
|
+
if (plan.reads.length === 0 && !plan.rest) return plan;
|
|
2128
|
+
const candidates = new Set(plan.reads.map((r) => r.name));
|
|
2129
|
+
if (plan.rest) candidates.add(plan.rest.name);
|
|
2130
|
+
const written = reassignedNames(body);
|
|
2131
|
+
const shadowed = shadowedNames(body, candidates);
|
|
2132
|
+
const kept = [];
|
|
2133
|
+
for (const read of plan.reads) {
|
|
2134
|
+
if (written.has(read.name)) plan.bails.push({ name: read.name, reason: "reassigned" });
|
|
2135
|
+
else if (shadowed.has(read.name)) plan.bails.push({ name: read.name, reason: "shadowed" });
|
|
2136
|
+
else kept.push(read);
|
|
2137
|
+
}
|
|
2138
|
+
plan.reads = kept;
|
|
2139
|
+
if (plan.rest && written.has(plan.rest.name)) {
|
|
2140
|
+
plan.bails.push({ name: plan.rest.name, reason: "reassigned" });
|
|
2141
|
+
delete plan.rest;
|
|
2142
|
+
} else if (plan.rest && shadowed.has(plan.rest.name)) {
|
|
2143
|
+
plan.bails.push({ name: plan.rest.name, reason: "shadowed" });
|
|
2144
|
+
delete plan.rest;
|
|
2145
|
+
}
|
|
2146
|
+
return plan;
|
|
2147
|
+
}
|
|
2148
|
+
function explainBail(reason) {
|
|
2149
|
+
switch (reason) {
|
|
2150
|
+
case "rest":
|
|
2151
|
+
return "this rest element cannot be served by splitProps, and copying the props into a plain object would lose the getters";
|
|
2152
|
+
case "computed":
|
|
2153
|
+
return "the property is not known until it runs";
|
|
2154
|
+
case "reassigned":
|
|
2155
|
+
return "the binding is assigned to, and props are read-only";
|
|
2156
|
+
case "shadowed":
|
|
2157
|
+
return "an inner scope binds the same name";
|
|
2158
|
+
case "unsafe-default":
|
|
2159
|
+
return "the default would have to run again on every read";
|
|
2160
|
+
}
|
|
2161
|
+
}
|
|
2162
|
+
|
|
2163
|
+
// src/transform/props.ts
|
|
2164
|
+
function walk5(root, visit, parent = null) {
|
|
2165
|
+
if (!root || typeof root !== "object" || typeof root.type !== "string") return;
|
|
2166
|
+
if (root.type.startsWith("TS")) return;
|
|
2167
|
+
if (visit(root, parent) === false) return;
|
|
2168
|
+
for (const key of Object.keys(root)) {
|
|
2169
|
+
if (key === "loc" || key === "leadingComments" || key === "trailingComments") continue;
|
|
2170
|
+
const value = root[key];
|
|
2171
|
+
if (Array.isArray(value)) {
|
|
2172
|
+
for (const item of value) walk5(item, visit, root);
|
|
2173
|
+
} else if (value && typeof value === "object") {
|
|
2174
|
+
walk5(value, visit, root);
|
|
2175
|
+
}
|
|
2176
|
+
}
|
|
2177
|
+
}
|
|
2178
|
+
function isComponentName(name) {
|
|
2179
|
+
return !!name && name[0] >= "A" && name[0] <= "Z";
|
|
2180
|
+
}
|
|
2181
|
+
function componentFunctions(program) {
|
|
2182
|
+
const found = [];
|
|
2183
|
+
walk5(program, (node) => {
|
|
2184
|
+
if (node.type === "FunctionDeclaration" && isComponentName(node.id?.name)) {
|
|
2185
|
+
found.push(node);
|
|
2186
|
+
return;
|
|
2187
|
+
}
|
|
2188
|
+
if (node.type === "VariableDeclarator" && isComponentName(node.id?.name)) {
|
|
2189
|
+
const init = node.init;
|
|
2190
|
+
if (init && (init.type === "ArrowFunctionExpression" || init.type === "FunctionExpression")) found.push(init);
|
|
2191
|
+
}
|
|
2192
|
+
});
|
|
2193
|
+
return found;
|
|
2194
|
+
}
|
|
2195
|
+
function usedNames(fn) {
|
|
2196
|
+
const names = /* @__PURE__ */ new Set();
|
|
2197
|
+
walk5(fn, (node) => {
|
|
2198
|
+
if (node.type === "Identifier") names.add(node.name);
|
|
2199
|
+
});
|
|
2200
|
+
return names;
|
|
2201
|
+
}
|
|
2202
|
+
function isReferencePosition(node, parent) {
|
|
2203
|
+
if (!parent) return true;
|
|
2204
|
+
switch (parent.type) {
|
|
2205
|
+
case "MemberExpression":
|
|
2206
|
+
case "OptionalMemberExpression":
|
|
2207
|
+
return !(parent.property === node && !parent.computed);
|
|
2208
|
+
case "ObjectProperty":
|
|
2209
|
+
case "ObjectMethod":
|
|
2210
|
+
case "ClassProperty":
|
|
2211
|
+
case "ClassMethod":
|
|
2212
|
+
return !(parent.key === node && !parent.computed);
|
|
2213
|
+
case "LabeledStatement":
|
|
2214
|
+
case "BreakStatement":
|
|
2215
|
+
case "ContinueStatement":
|
|
2216
|
+
return parent.label !== node;
|
|
2217
|
+
case "ImportSpecifier":
|
|
2218
|
+
case "ExportSpecifier":
|
|
2219
|
+
return false;
|
|
2220
|
+
default:
|
|
2221
|
+
return true;
|
|
2222
|
+
}
|
|
2223
|
+
}
|
|
2224
|
+
function collectPropsEdits(program, code, options = {}) {
|
|
2225
|
+
const edits = [];
|
|
2226
|
+
const diagnostics = [];
|
|
2227
|
+
const used = /* @__PURE__ */ new Set();
|
|
2228
|
+
for (const fn of componentFunctions(program)) {
|
|
2229
|
+
const pattern = fn.params?.[0];
|
|
2230
|
+
if (pattern?.type !== "ObjectPattern") continue;
|
|
2231
|
+
const plan = planPropsDestructure(pattern, fn.body);
|
|
2232
|
+
for (const b of plan.bails) {
|
|
2233
|
+
diagnostics.push({ name: b.name, reason: b.reason, message: explainBail(b.reason), start: pattern.start });
|
|
2234
|
+
}
|
|
2235
|
+
if (plan.bails.length > 0) continue;
|
|
2236
|
+
if (plan.reads.length === 0 && !plan.rest) continue;
|
|
2237
|
+
const taken = usedNames(fn);
|
|
2238
|
+
let parameter = options.parameterName ?? "props";
|
|
2239
|
+
while (taken.has(parameter)) parameter = `_${parameter}`;
|
|
2240
|
+
const replacement = /* @__PURE__ */ new Map();
|
|
2241
|
+
for (const read of plan.reads) {
|
|
2242
|
+
const access = `${parameter}.${read.path.join(".")}`;
|
|
2243
|
+
const fallback = read.fallback;
|
|
2244
|
+
replacement.set(
|
|
2245
|
+
read.name,
|
|
2246
|
+
fallback ? `(${access} === undefined ? ${code.slice(fallback.start, fallback.end)} : ${access})` : access
|
|
2247
|
+
);
|
|
2248
|
+
}
|
|
2249
|
+
const annotation = pattern.typeAnnotation;
|
|
2250
|
+
edits.push({ start: pattern.start, end: annotation?.start ?? pattern.end, code: parameter });
|
|
2251
|
+
if (plan.rest) {
|
|
2252
|
+
const keys = plan.rest.keys.map((k) => JSON.stringify(k)).join(", ");
|
|
2253
|
+
const body = fn.body;
|
|
2254
|
+
edits.push({
|
|
2255
|
+
start: body.start + 1,
|
|
2256
|
+
end: body.start + 1,
|
|
2257
|
+
code: `
|
|
2258
|
+
const [, ${plan.rest.name}] = splitProps(${parameter}, [${keys}]);`
|
|
2259
|
+
});
|
|
2260
|
+
used.add("splitProps");
|
|
2261
|
+
}
|
|
2262
|
+
const matches = [];
|
|
2263
|
+
walk5(fn.body, (node, parent) => {
|
|
2264
|
+
if (node.type !== "Identifier") return void 0;
|
|
2265
|
+
const text = replacement.get(node.name);
|
|
2266
|
+
if (!text) return void 0;
|
|
2267
|
+
const shorthand = parent?.type === "ObjectProperty" && parent.shorthand && parent.value === node;
|
|
2268
|
+
if (!shorthand && !isReferencePosition(node, parent)) return void 0;
|
|
2269
|
+
const path = plan.reads.find((r) => r.name === node.name).path;
|
|
2270
|
+
matches.push({ node, parent: shorthand ? parent : null, text, property: path[path.length - 1] });
|
|
2271
|
+
return void 0;
|
|
2272
|
+
});
|
|
2273
|
+
for (const match of matches) {
|
|
2274
|
+
if (match.parent) {
|
|
2275
|
+
edits.push({ start: match.parent.start, end: match.parent.end, code: `${match.node.name}: ${match.text}` });
|
|
2276
|
+
} else {
|
|
2277
|
+
edits.push({ start: match.node.start, end: match.node.end, code: match.text });
|
|
2278
|
+
}
|
|
2279
|
+
asMemberRead(match.node, parameter, match.property);
|
|
2280
|
+
}
|
|
2281
|
+
}
|
|
2282
|
+
return { edits, diagnostics, used };
|
|
2283
|
+
}
|
|
2284
|
+
function asMemberRead(node, parameter, property) {
|
|
2285
|
+
node.type = "MemberExpression";
|
|
2286
|
+
node.computed = false;
|
|
2287
|
+
node.optional = false;
|
|
2288
|
+
node.object = { type: "Identifier", name: parameter, start: node.start, end: node.start };
|
|
2289
|
+
node.property = { type: "Identifier", name: property, start: node.end, end: node.end };
|
|
2290
|
+
delete node["name"];
|
|
2291
|
+
}
|
|
2292
|
+
function transformProps(code, filename = "module.tsx", options = {}) {
|
|
2293
|
+
const ast = (0, import_parser.parse)(code, {
|
|
2294
|
+
sourceType: "module",
|
|
2295
|
+
plugins: ["typescript", "jsx"],
|
|
2296
|
+
errorRecovery: true
|
|
2297
|
+
});
|
|
2298
|
+
const { edits, diagnostics, used } = collectPropsEdits(ast.program, code, options);
|
|
2299
|
+
const source = new import_magic_string.default(code);
|
|
2300
|
+
for (const e of edits) {
|
|
2301
|
+
if (e.start === e.end) source.appendLeft(e.start, e.code);
|
|
2302
|
+
else source.overwrite(e.start, e.end, e.code);
|
|
2303
|
+
}
|
|
2304
|
+
if (used.has("splitProps") && !moduleBindings(ast.program).has("splitProps")) {
|
|
2305
|
+
source.prepend(`import { splitProps } from ${JSON.stringify(options.runtimeModule ?? "@fluixi/dom")};
|
|
2306
|
+
`);
|
|
2307
|
+
}
|
|
2308
|
+
return {
|
|
2309
|
+
code: source.toString(),
|
|
2310
|
+
map: source.generateMap({ source: filename, includeContent: true, hires: true }),
|
|
2311
|
+
diagnostics,
|
|
2312
|
+
rewritten: edits.filter((e) => e.code !== (options.parameterName ?? "props")).length
|
|
2313
|
+
};
|
|
2314
|
+
}
|
|
2315
|
+
|
|
2316
|
+
// src/transform/intrinsics.ts
|
|
2317
|
+
function walk6(root, visit) {
|
|
2318
|
+
if (!root || typeof root !== "object" || typeof root.type !== "string") return;
|
|
2319
|
+
visit(root);
|
|
2320
|
+
for (const key of Object.keys(root)) {
|
|
2321
|
+
if (key === "loc" || key === "leadingComments" || key === "trailingComments") continue;
|
|
2322
|
+
const value = root[key];
|
|
2323
|
+
if (Array.isArray(value)) for (const item of value) walk6(item, visit);
|
|
2324
|
+
else if (value && typeof value === "object") walk6(value, visit);
|
|
2325
|
+
}
|
|
2326
|
+
}
|
|
2327
|
+
function collectIntrinsicEdits(program) {
|
|
2328
|
+
const edits = [];
|
|
2329
|
+
const diagnostics = [];
|
|
2330
|
+
const imports = /* @__PURE__ */ new Map();
|
|
2331
|
+
const reported = /* @__PURE__ */ new Set();
|
|
2332
|
+
const { shadowed } = resolveReactiveBindings(program);
|
|
2333
|
+
walk6(program, (node) => {
|
|
2334
|
+
if (node.type !== "CallExpression") return;
|
|
2335
|
+
const callee = node.callee;
|
|
2336
|
+
if (callee?.type !== "Identifier") return;
|
|
2337
|
+
const name = callee.name;
|
|
2338
|
+
if (!isIntrinsicName(name)) return;
|
|
2339
|
+
if (shadowed.has(name)) {
|
|
2340
|
+
if (!reported.has(name)) {
|
|
2341
|
+
reported.add(name);
|
|
2342
|
+
diagnostics.push({
|
|
2343
|
+
name,
|
|
2344
|
+
message: `${name} is reserved for the compiler, and this module binds it. The call keeps your binding's meaning; rename it to use the intrinsic.`,
|
|
2345
|
+
start: callee.start
|
|
2346
|
+
});
|
|
2347
|
+
}
|
|
2348
|
+
return;
|
|
2349
|
+
}
|
|
2350
|
+
const intrinsic = INTRINSICS[name];
|
|
2351
|
+
edits.push({ start: callee.start, end: callee.end, code: intrinsic.export });
|
|
2352
|
+
const forModule = imports.get(intrinsic.module) ?? /* @__PURE__ */ new Set();
|
|
2353
|
+
forModule.add(intrinsic.export);
|
|
2354
|
+
imports.set(intrinsic.module, forModule);
|
|
2355
|
+
});
|
|
2356
|
+
return { edits, diagnostics, imports };
|
|
2357
|
+
}
|
|
2358
|
+
function intrinsicImports(result, bound) {
|
|
2359
|
+
const lines = [];
|
|
2360
|
+
for (const [module2, names] of [...result.imports].sort(([a], [b]) => a.localeCompare(b))) {
|
|
2361
|
+
const wanted = [...names].filter((name) => !bound.has(name)).sort();
|
|
2362
|
+
if (wanted.length) lines.push(`import { ${wanted.join(", ")} } from ${JSON.stringify(module2)};`);
|
|
2363
|
+
}
|
|
2364
|
+
return lines.length ? `${lines.join("\n")}
|
|
2365
|
+
` : "";
|
|
2366
|
+
}
|
|
2367
|
+
|
|
2368
|
+
// src/transform/templates.ts
|
|
2369
|
+
var CONTROL_FLOW = new Set(DOM_CONTROL_FLOW);
|
|
2370
|
+
var CORE_ONLY = new Set(CORE_CONTROL_FLOW);
|
|
2371
|
+
var ROUTER = new Set(ROUTER_COMPONENTS);
|
|
2372
|
+
var REACTIVE = /* @__PURE__ */ new Set([
|
|
2373
|
+
"createMemo",
|
|
2374
|
+
"createEffect",
|
|
2375
|
+
"createRenderEffect",
|
|
2376
|
+
"createRoot",
|
|
2377
|
+
"createSignal",
|
|
2378
|
+
"onCleanup",
|
|
2379
|
+
"batch",
|
|
2380
|
+
"untrack"
|
|
2381
|
+
]);
|
|
2382
|
+
function walk7(node, visit, parent = null, key = "") {
|
|
2383
|
+
if (!node || typeof node !== "object") return;
|
|
2384
|
+
if (Array.isArray(node)) {
|
|
2385
|
+
for (const child of node) walk7(child, visit, parent, key);
|
|
2386
|
+
return;
|
|
2387
|
+
}
|
|
2388
|
+
if (typeof node.type === "string") visit(node, parent, key);
|
|
2389
|
+
for (const k of Object.keys(node)) {
|
|
2390
|
+
if (k === "loc" || k === "leadingComments" || k === "trailingComments") continue;
|
|
2391
|
+
walk7(node[k], visit, typeof node.type === "string" ? node : parent, k);
|
|
2392
|
+
}
|
|
2393
|
+
}
|
|
2394
|
+
var isJsx = (node) => node.type === "JSXElement" || node.type === "JSXFragment";
|
|
2395
|
+
function isJsxRoot(node, parent, key) {
|
|
2396
|
+
if (!isJsx(node)) return false;
|
|
2397
|
+
return !(parent && isJsx(parent) && key === "children");
|
|
2398
|
+
}
|
|
2399
|
+
function isTemplateTag(node, litTag) {
|
|
2400
|
+
return node.type === "TaggedTemplateExpression" && node.tag?.type === "Identifier" && (node.tag.name === litTag || node.tag.name === "svg");
|
|
2401
|
+
}
|
|
2402
|
+
function outermost(list) {
|
|
2403
|
+
return list.filter(
|
|
2404
|
+
(e) => !list.some(
|
|
2405
|
+
(o) => o !== e && o.start <= e.start && o.end >= e.end && o.end - o.start > e.end - e.start && // An insertion at either edge of the container sits beside it, not inside it, so
|
|
2406
|
+
// it is not something the container's code already folded in. A statement can
|
|
2407
|
+
// start where its callee starts (`$effect(…)`) and a declarator can end where its
|
|
2408
|
+
// JSX ends (`const Row = () => <b/>`).
|
|
2409
|
+
!(e.start === e.end && (e.start === o.start || e.start === o.end))
|
|
2410
|
+
)
|
|
2411
|
+
);
|
|
2412
|
+
}
|
|
2413
|
+
function slice(code, start, end, edits) {
|
|
2414
|
+
const inside = outermost(edits.filter((e) => e.start >= start && e.end <= end)).sort((a, b) => b.start - a.start);
|
|
2415
|
+
let out = code.slice(start, end);
|
|
2416
|
+
for (const e of inside) out = out.slice(0, e.start - start) + e.code + out.slice(e.end - start);
|
|
2417
|
+
return out;
|
|
2418
|
+
}
|
|
2419
|
+
function bindHole(code, expr, edits, keepPosition = false) {
|
|
2420
|
+
const at = keepPosition ? expr.loc?.start : void 0;
|
|
2421
|
+
const hole = {
|
|
2422
|
+
code: slice(code, expr.start, expr.end, edits),
|
|
2423
|
+
reactive: isReactiveShape(shapeOf(expr)),
|
|
2424
|
+
// Where the `${…}` is, so a binding made from it can say so. The source map cannot:
|
|
2425
|
+
// the whole template is one overwrite. Only when source locations were asked for.
|
|
2426
|
+
...at ? { at: { line: at.line, column: at.column } } : {}
|
|
2427
|
+
};
|
|
2428
|
+
if (expr.type === "ArrowFunctionExpression" && expr.body?.type !== "BlockStatement") {
|
|
2429
|
+
hole.arrow = {
|
|
2430
|
+
params: expr.params.map((p) => slice(code, p.start, p.end, edits)),
|
|
2431
|
+
body: slice(code, expr.body.start, expr.body.end, edits),
|
|
2432
|
+
bodyReactive: isReactiveShape(shapeOf(expr.body))
|
|
2433
|
+
};
|
|
2434
|
+
}
|
|
2435
|
+
if (expr.type === "ObjectExpression") hole.object = true;
|
|
2436
|
+
return hole;
|
|
2437
|
+
}
|
|
2438
|
+
function adoptTemplates(code, templates, hoisted) {
|
|
2439
|
+
if (!templates || templates.length === 0) return code;
|
|
2440
|
+
const rename = /* @__PURE__ */ new Map();
|
|
2441
|
+
for (const tpl of templates) {
|
|
2442
|
+
const key = `${tpl.svg ? "svg:" : ""}${tpl.html}`;
|
|
2443
|
+
let name = hoisted.get(key);
|
|
2444
|
+
if (!name) {
|
|
2445
|
+
name = `_fxTmpl$${hoisted.size}`;
|
|
2446
|
+
hoisted.set(key, name);
|
|
2447
|
+
}
|
|
2448
|
+
rename.set(tpl.id, name);
|
|
2449
|
+
}
|
|
2450
|
+
return code.replace(/_tmpl\$\d+/g, (id) => rename.get(id) ?? id);
|
|
2451
|
+
}
|
|
2452
|
+
function collectNeeds(node, out) {
|
|
2453
|
+
if (node.kind === "component" && node.load && !node.source && isDeferred(node.load)) {
|
|
2454
|
+
if (!out.ignoredLoad.has(node.name)) out.ignoredLoad.set(node.name, node.load);
|
|
2455
|
+
}
|
|
2456
|
+
if (node.kind === "component" && node.source) {
|
|
2457
|
+
const src = node.source;
|
|
2458
|
+
if (src.origin === "builtin") out.builtins.add(node.name);
|
|
2459
|
+
else if (isDeferred(src.loading) && !out.resolved.has(node.name)) out.deferred.set(node.name, src);
|
|
2460
|
+
else {
|
|
2461
|
+
out.deferred.delete(node.name);
|
|
2462
|
+
out.resolved.set(node.name, src);
|
|
2463
|
+
}
|
|
2464
|
+
}
|
|
2465
|
+
if ("props" in node && node.props) {
|
|
2466
|
+
for (const p of node.props) {
|
|
2467
|
+
if (p.kind === "event" && p.event?.delegated) out.delegatedEvents.add(p.event.name);
|
|
2468
|
+
}
|
|
2469
|
+
}
|
|
2470
|
+
if ("children" in node && node.children) for (const c of node.children) collectNeeds(c, out);
|
|
2471
|
+
}
|
|
2472
|
+
function deferrableImport(program, name) {
|
|
2473
|
+
for (const statement of program.body ?? []) {
|
|
2474
|
+
if (statement.type !== "ImportDeclaration") continue;
|
|
2475
|
+
for (const specifier of statement.specifiers ?? []) {
|
|
2476
|
+
if (specifier.local?.name !== name) continue;
|
|
2477
|
+
if (specifier.type === "ImportNamespaceSpecifier") return void 0;
|
|
2478
|
+
const exported = specifier.type === "ImportDefaultSpecifier" ? "default" : specifier.imported?.name ?? specifier.imported?.value;
|
|
2479
|
+
return { module: statement.source.value, export: exported, declaration: statement, specifier };
|
|
2480
|
+
}
|
|
2481
|
+
}
|
|
2482
|
+
return void 0;
|
|
2483
|
+
}
|
|
2484
|
+
function hasNamespaceImport(program, name) {
|
|
2485
|
+
for (const statement of program.body ?? []) {
|
|
2486
|
+
if (statement.type !== "ImportDeclaration") continue;
|
|
2487
|
+
for (const specifier of statement.specifiers ?? []) {
|
|
2488
|
+
if (specifier.type === "ImportNamespaceSpecifier" && specifier.local?.name === name) return true;
|
|
2489
|
+
}
|
|
2490
|
+
}
|
|
2491
|
+
return false;
|
|
2492
|
+
}
|
|
2493
|
+
function usedAsValue(program, name, specifier) {
|
|
2494
|
+
let seen = false;
|
|
2495
|
+
walk7(program, (node) => {
|
|
2496
|
+
if (seen || node.type !== "Identifier" || node.name !== name) return;
|
|
2497
|
+
if (node.start === specifier.local?.start && node.end === specifier.local?.end) return;
|
|
2498
|
+
seen = true;
|
|
2499
|
+
});
|
|
2500
|
+
return seen;
|
|
2501
|
+
}
|
|
2502
|
+
function strategyLiteral(s) {
|
|
2503
|
+
const parts = [`kind: ${JSON.stringify(s.kind)}`];
|
|
2504
|
+
if (s.kind === "visible" && s.rootMargin) parts.push(`rootMargin: ${JSON.stringify(s.rootMargin)}`);
|
|
2505
|
+
if (s.kind === "interaction") parts.push(`events: ${JSON.stringify(s.events)}`);
|
|
2506
|
+
if (s.kind === "media") parts.push(`query: ${JSON.stringify(s.query)}`);
|
|
2507
|
+
return `{ ${parts.join(", ")} }`;
|
|
2508
|
+
}
|
|
2509
|
+
function positionsIn(code) {
|
|
2510
|
+
const starts = [0];
|
|
2511
|
+
for (let i = 0; i < code.length; i++) if (code.charCodeAt(i) === 10) starts.push(i + 1);
|
|
2512
|
+
return (offset) => {
|
|
2513
|
+
let low = 0;
|
|
2514
|
+
let high = starts.length - 1;
|
|
2515
|
+
while (low < high) {
|
|
2516
|
+
const mid = low + high + 1 >> 1;
|
|
2517
|
+
if (starts[mid] <= offset) low = mid;
|
|
2518
|
+
else high = mid - 1;
|
|
2519
|
+
}
|
|
2520
|
+
return { line: low + 1, column: offset - starts[low] };
|
|
2521
|
+
};
|
|
2522
|
+
}
|
|
2523
|
+
function toPieces(node) {
|
|
2524
|
+
const pieces = [];
|
|
2525
|
+
node.quasi.quasis.forEach((q, i) => {
|
|
2526
|
+
pieces.push({ kind: "static", text: q.value.cooked ?? q.value.raw, start: q.start });
|
|
2527
|
+
if (i < node.quasi.expressions.length) {
|
|
2528
|
+
const e = node.quasi.expressions[i];
|
|
2529
|
+
pieces.push({ kind: "hole", index: i, start: e.start, end: e.end });
|
|
2530
|
+
}
|
|
2531
|
+
});
|
|
2532
|
+
return pieces;
|
|
2533
|
+
}
|
|
2534
|
+
function relativeToRoot(id, root) {
|
|
2535
|
+
if (!root) return id;
|
|
2536
|
+
const base = root.endsWith("/") ? root : `${root}/`;
|
|
2537
|
+
return id.startsWith(base) ? id.slice(base.length) : id;
|
|
2538
|
+
}
|
|
2539
|
+
function transformTemplates(code, id, options = {}) {
|
|
2540
|
+
const litTag = options.litTag ?? "html";
|
|
2541
|
+
const format = options.format ?? "both";
|
|
2542
|
+
const wantsLit = format !== "jsx";
|
|
2543
|
+
const wantsJsx = format !== "lit";
|
|
2544
|
+
const mayHaveTemplate = wantsLit && (code.includes(`${litTag}\``) || code.includes("svg`"));
|
|
2545
|
+
const wantsIntrinsics = options.intrinsics !== false && mayHaveIntrinsic(code);
|
|
2546
|
+
const wantsSources = options.sourceLocations === true && mayHaveReactiveCall(code);
|
|
2547
|
+
const sourceName = relativeToRoot(id, options.sourceRoot);
|
|
2548
|
+
if (!mayHaveTemplate && !(wantsJsx && code.includes("<")) && !wantsIntrinsics && !wantsSources) return null;
|
|
2549
|
+
const ast = (0, import_parser2.parse)(code, {
|
|
2550
|
+
sourceType: "module",
|
|
2551
|
+
plugins: ["jsx", "typescript"],
|
|
2552
|
+
errorRecovery: true
|
|
2553
|
+
});
|
|
2554
|
+
const found = [];
|
|
2555
|
+
walk7(ast.program, (n, parent, key) => {
|
|
2556
|
+
if (wantsLit && isTemplateTag(n, litTag)) found.push(n);
|
|
2557
|
+
else if (wantsJsx && isJsxRoot(n, parent, key)) found.push(n);
|
|
2558
|
+
});
|
|
2559
|
+
const props = options.propsDestructure === false ? { edits: [], diagnostics: [], used: /* @__PURE__ */ new Set() } : collectPropsEdits(ast.program, code, { runtimeModule: options.runtimeModule });
|
|
2560
|
+
const intrinsics = options.intrinsics === false ? { edits: [], diagnostics: [], imports: /* @__PURE__ */ new Map() } : collectIntrinsicEdits(ast.program);
|
|
2561
|
+
const sources = options.sourceLocations === true ? collectSourceEdits(ast.program, sourceName) : { edits: [], marked: 0 };
|
|
2562
|
+
const silent = found.length === 0 && props.edits.length === 0 && sources.edits.length === 0 && intrinsics.edits.length === 0 && intrinsics.diagnostics.length === 0 && props.diagnostics.length === 0;
|
|
2563
|
+
if (silent) return null;
|
|
2564
|
+
found.sort((a, b) => b.start - a.start || a.end - b.end);
|
|
2565
|
+
const used = new Set(props.used);
|
|
2566
|
+
const needs = {
|
|
2567
|
+
builtins: /* @__PURE__ */ new Set(),
|
|
2568
|
+
resolved: /* @__PURE__ */ new Map(),
|
|
2569
|
+
deferred: /* @__PURE__ */ new Map(),
|
|
2570
|
+
delegatedEvents: /* @__PURE__ */ new Set(),
|
|
2571
|
+
ignoredLoad: /* @__PURE__ */ new Map(),
|
|
2572
|
+
unresolved: /* @__PURE__ */ new Set(),
|
|
2573
|
+
declared: declaredNames(ast.program)
|
|
2574
|
+
};
|
|
2575
|
+
const hoisted = /* @__PURE__ */ new Map();
|
|
2576
|
+
const edits = [...props.edits, ...intrinsics.edits, ...sources.edits];
|
|
2577
|
+
for (const node of found) {
|
|
2578
|
+
const nested = found.some(
|
|
2579
|
+
(o) => o !== node && o.start <= node.start && o.end >= node.end && o.end - o.start > node.end - node.start
|
|
2580
|
+
);
|
|
2581
|
+
if (isJsx(node)) {
|
|
2582
|
+
edits.push({ start: node.start, end: node.end, code: compileJsx(code, node, edits, nested, used, needs, hoisted, options, sourceName) });
|
|
2583
|
+
continue;
|
|
2584
|
+
}
|
|
2585
|
+
const holes = node.quasi.expressions.map(
|
|
2586
|
+
(e) => bindHole(code, e, edits, options.sourceLocations === true)
|
|
2587
|
+
);
|
|
2588
|
+
const result = compilePieces(toPieces(node), holes, {
|
|
2589
|
+
...options,
|
|
2590
|
+
hoistTemplates: true,
|
|
2591
|
+
// A tag inside a template gets the same mark a JSX tag does. Without one, the nodes it
|
|
2592
|
+
// makes are the only ones in the module carrying no mark, and they take whichever was
|
|
2593
|
+
// left behind by the last declaration that did.
|
|
2594
|
+
...options.sourceLocations === true ? { sourceFile: sourceName, positionAt: positionsIn(code) } : {},
|
|
2595
|
+
// Same defaults as the plugin, so moving a build over is not a rendering change.
|
|
2596
|
+
templateClone: options.templateClone ?? true,
|
|
2597
|
+
partialTemplates: nested ? false : options.partialTemplates ?? true,
|
|
2598
|
+
svg: node.tag.name === "svg"
|
|
2599
|
+
});
|
|
2600
|
+
for (const symbol of result.imports) used.add(symbol);
|
|
2601
|
+
collectNeeds(result.ir, needs);
|
|
2602
|
+
edits.push({ start: node.start, end: node.end, code: adoptTemplates(result.code, result.templates, hoisted) });
|
|
2603
|
+
}
|
|
2604
|
+
const out = new import_magic_string2.default(code);
|
|
2605
|
+
for (const e of outermost(edits)) {
|
|
2606
|
+
if (e.start === e.end) out.appendLeft(e.start, e.code);
|
|
2607
|
+
else out.overwrite(e.start, e.end, e.code);
|
|
2608
|
+
}
|
|
2609
|
+
const bound = moduleBindings(ast.program);
|
|
2610
|
+
const why = /* @__PURE__ */ new Map();
|
|
2611
|
+
for (const [name] of needs.ignoredLoad) {
|
|
2612
|
+
if (!bound.has(name)) why.set(name, "unsupplied");
|
|
2613
|
+
}
|
|
2614
|
+
for (const [name, strategy] of [...needs.ignoredLoad]) {
|
|
2615
|
+
if (!bound.has(name)) continue;
|
|
2616
|
+
const found2 = deferrableImport(ast.program, name);
|
|
2617
|
+
if (!found2) {
|
|
2618
|
+
why.set(name, hasNamespaceImport(ast.program, name) ? "namespace" : "local");
|
|
2619
|
+
continue;
|
|
2620
|
+
}
|
|
2621
|
+
if (usedAsValue(ast.program, name, found2.specifier)) {
|
|
2622
|
+
why.set(name, "value");
|
|
2623
|
+
continue;
|
|
2624
|
+
}
|
|
2625
|
+
const rest = found2.declaration.specifiers.filter((sp) => sp !== found2.specifier);
|
|
2626
|
+
const end = rest.length === 0 && code[found2.declaration.end] === "\n" ? found2.declaration.end + 1 : found2.declaration.end;
|
|
2627
|
+
out.overwrite(
|
|
2628
|
+
found2.declaration.start,
|
|
2629
|
+
end,
|
|
2630
|
+
rest.length === 0 ? "" : `import { ${rest.map((sp) => code.slice(sp.start, sp.end)).join(", ")} } from ${JSON.stringify(found2.declaration.source.value)};`
|
|
2631
|
+
);
|
|
2632
|
+
needs.deferred.set(name, { module: found2.module, export: found2.export, origin: "rule", loading: strategy });
|
|
2633
|
+
bound.delete(name);
|
|
2634
|
+
needs.ignoredLoad.delete(name);
|
|
2635
|
+
}
|
|
2636
|
+
const prologue = buildPrologue(used, needs, hoisted, bound, options);
|
|
2637
|
+
if (prologue) out.prepend(prologue);
|
|
2638
|
+
const runtimeImports = intrinsicImports(intrinsics, bound);
|
|
2639
|
+
if (runtimeImports) out.prepend(runtimeImports);
|
|
2640
|
+
const reasons = {
|
|
2641
|
+
value: (name) => `${name} is used as a value here, not only as a tag, so its import has to stay — deferring would hand those references a lazy wrapper instead of the component. Defer it at the point it is rendered, or keep it eager.`,
|
|
2642
|
+
namespace: (name) => `${name} came in through a namespace import, which has no single export to defer. Import ${name} by name.`,
|
|
2643
|
+
local: (name) => `${name} is declared in this file, so there is no module to load separately. Move it to its own file and import it.`,
|
|
2644
|
+
unsupplied: (name) => `nothing supplies ${name}, so there is no import to defer. Import it, or add a \`resolve\` rule for it.`
|
|
2645
|
+
};
|
|
2646
|
+
const loadDiagnostics = [...needs.ignoredLoad].map(([name, strategy]) => ({
|
|
2647
|
+
name,
|
|
2648
|
+
strategy,
|
|
2649
|
+
message: `load:${strategy.kind} has no effect on <${name}>: ${reasons[why.get(name) ?? "unsupplied"](name)}`
|
|
2650
|
+
}));
|
|
2651
|
+
const unresolvedDiagnostics = [...needs.unresolved].map((name) => ({
|
|
2652
|
+
name,
|
|
2653
|
+
message: `<${name}> is not defined: nothing imports it and no \`resolve\` rule supplies it. This compiles to a reference to a name that does not exist, so the component renders nothing. Import ${name}, or add a \`resolve\` rule for it.`
|
|
2654
|
+
}));
|
|
2655
|
+
return {
|
|
2656
|
+
code: out.toString(),
|
|
2657
|
+
map: out.generateMap({ source: id, includeContent: true, hires: true }),
|
|
2658
|
+
...loadDiagnostics.length ? { loadDiagnostics } : {},
|
|
2659
|
+
...unresolvedDiagnostics.length ? { unresolvedDiagnostics } : {},
|
|
2660
|
+
...props.diagnostics.length ? { propsDiagnostics: props.diagnostics } : {},
|
|
2661
|
+
...intrinsics.diagnostics.length ? { intrinsicDiagnostics: intrinsics.diagnostics } : {}
|
|
2662
|
+
};
|
|
2663
|
+
}
|
|
2664
|
+
function compileJsx(code, node, edits, nested, used, needs, hoisted, options, sourceFile) {
|
|
2665
|
+
const ir = buildJsxIR(node, {
|
|
2666
|
+
code: (n) => slice(code, n.start, n.end, edits),
|
|
2667
|
+
used,
|
|
2668
|
+
...options.sourceLocations === true && sourceFile ? { sourceFile } : {}
|
|
2669
|
+
});
|
|
2670
|
+
analyzeStatic(ir);
|
|
2671
|
+
const report = resolveComponentSources(ir, {
|
|
2672
|
+
resolver: createComponentResolver(options.resolve ?? []),
|
|
2673
|
+
// Without this the pass calls every author-imported component unresolved, and the
|
|
2674
|
+
// report is unusable for diagnostics.
|
|
2675
|
+
isBound: (name) => needs.declared.has(name),
|
|
2676
|
+
modules: {
|
|
2677
|
+
controlFlowModule: options.controlFlowModule ?? options.runtimeModule ?? "@fluixi/dom",
|
|
2678
|
+
coreModule: options.coreModule ?? "@fluixi/core",
|
|
2679
|
+
routerModule: options.routerModule ?? "@fluixi/core/router"
|
|
2680
|
+
},
|
|
2681
|
+
strategyFor: (n) => n.load
|
|
2682
|
+
});
|
|
2683
|
+
for (const name of report.unresolved) needs.unresolved.add(name);
|
|
2684
|
+
collapseStrategies(ir);
|
|
2685
|
+
const emitted = imperativeBackend.emit(ir, {
|
|
2686
|
+
templateClone: options.templateClone ?? true,
|
|
2687
|
+
partialTemplates: nested ? false : options.partialTemplates ?? true
|
|
2688
|
+
});
|
|
2689
|
+
for (const symbol of emitted.imports) used.add(symbol);
|
|
2690
|
+
collectNeeds(ir, needs);
|
|
2691
|
+
return adoptTemplates(emitted.code, emitted.templates, hoisted);
|
|
2692
|
+
}
|
|
2693
|
+
function buildPrologue(used, needs, hoisted, bound, options) {
|
|
2694
|
+
const runtime = options.runtimeModule ?? "@fluixi/dom";
|
|
2695
|
+
const core = options.coreModule ?? "@fluixi/core";
|
|
2696
|
+
const groups = /* @__PURE__ */ new Map();
|
|
2697
|
+
const add = (mod, name) => groups.set(mod, [...groups.get(mod) ?? [], name]);
|
|
2698
|
+
const wanted = new Set(used);
|
|
2699
|
+
for (const name of needs.builtins) wanted.add(name);
|
|
2700
|
+
if (needs.delegatedEvents.size > 0) wanted.add("delegateEvents");
|
|
2701
|
+
for (const name of [...wanted].sort()) {
|
|
2702
|
+
if (bound.has(name)) continue;
|
|
2703
|
+
if (REACTIVE.has(name)) add(options.reactiveModule ?? "@fluixi/reactive/signal", name);
|
|
2704
|
+
else if (ROUTER.has(name)) add(options.routerModule ?? "@fluixi/core/router", name);
|
|
2705
|
+
else if (CORE_ONLY.has(name)) add(core, name);
|
|
2706
|
+
else if (CONTROL_FLOW.has(name)) add(options.controlFlowModule ?? runtime, name);
|
|
2707
|
+
else add(runtime, name);
|
|
2708
|
+
}
|
|
2709
|
+
for (const [name, src] of needs.resolved) {
|
|
2710
|
+
if (!bound.has(name)) add(src.module, src.export === "default" ? `default as ${name}` : name === src.export ? name : `${src.export} as ${name}`);
|
|
2711
|
+
}
|
|
2712
|
+
const lines = [];
|
|
2713
|
+
for (const [mod, names] of groups) {
|
|
2714
|
+
lines.push(`import { ${names.join(", ")} } from ${JSON.stringify(mod)};`);
|
|
2715
|
+
}
|
|
2716
|
+
const lazy = [...needs.deferred].filter(([name]) => !bound.has(name));
|
|
2717
|
+
if (lazy.length > 0) {
|
|
2718
|
+
if (!bound.has("deferred")) lines.push(`import { deferred } from ${JSON.stringify(core)};`);
|
|
2719
|
+
for (const [name, src] of lazy) {
|
|
2720
|
+
lines.push(
|
|
2721
|
+
`const ${name} = deferred(() => import(${JSON.stringify(src.module)}), { strategy: ${strategyLiteral(src.loading)}, export: ${JSON.stringify(src.export)} });`
|
|
2722
|
+
);
|
|
2723
|
+
}
|
|
2724
|
+
}
|
|
2725
|
+
for (const [key, name] of hoisted) {
|
|
2726
|
+
lines.push(`const ${name} = ${JSON.stringify(key.startsWith("svg:") ? key.slice(4) : key)};`);
|
|
2727
|
+
}
|
|
2728
|
+
if (needs.delegatedEvents.size > 0) {
|
|
2729
|
+
lines.push(`delegateEvents(${JSON.stringify([...needs.delegatedEvents])});`);
|
|
2730
|
+
}
|
|
2731
|
+
return lines.length > 0 ? lines.join("\n") + "\n" : null;
|
|
2732
|
+
}
|