what-compiler 0.6.1 → 0.7.0

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.
@@ -1,4 +1,4 @@
1
- import Z from"path";import{transformSync as _e}from"@babel/core";var me=new Set(["preventDefault","stopPropagation","once","capture","passive","self"]),he=new Set(["once","capture","passive"]),ge=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]),xe=new Set(["click","input","change","keydown","keyup","submit","focusin","focusout","mousedown","mouseup"]),R=new Set(["Math","Number","String","Boolean","parseInt","parseFloat","isNaN","isFinite","encodeURIComponent","decodeURIComponent","encodeURI","decodeURI","JSON","Date","Array","Object","console","RegExp"]),ie=new Set(["useSignal","signal","computed","useComputed","useState","useReducer","createResource","useSWR","useQuery","useInfiniteQuery"]);function G({types:e}){function f(n){let i=n.split("|"),r=i[0],s=i.slice(1).filter(a=>me.has(a));return{eventName:r,modifiers:s}}function b(n){return n.startsWith("bind:")}function I(n){return n.slice(5)}function w(n){return/^[A-Z]/.test(n)}function T(n){return ge.has(String(n).toLowerCase())}function v(n){return n?e.isJSXExpressionContainer(n)?n.expression:e.isStringLiteral(n)?n:e.stringLiteral(n.value||""):e.booleanLiteral(!0)}function C(n){return n==="className"?"class":n==="htmlFor"?"for":n}function L(n){return e.isJSXNamespacedName(n.name)?`${n.name.namespace.name}:${n.name.name.name}`:typeof n.name.name=="string"?n.name.name:String(n.name.name)}function $(n,i){if(i.length===0)return n;let r=n;for(let s of i)switch(s){case"preventDefault":r=e.arrowFunctionExpression([e.identifier("e")],e.blockStatement([e.expressionStatement(e.callExpression(e.memberExpression(e.identifier("e"),e.identifier("preventDefault")),[])),e.expressionStatement(e.callExpression(r,[e.identifier("e")]))]));break;case"stopPropagation":r=e.arrowFunctionExpression([e.identifier("e")],e.blockStatement([e.expressionStatement(e.callExpression(e.memberExpression(e.identifier("e"),e.identifier("stopPropagation")),[])),e.expressionStatement(e.callExpression(r,[e.identifier("e")]))]));break;case"self":r=e.arrowFunctionExpression([e.identifier("e")],e.blockStatement([e.ifStatement(e.binaryExpression("===",e.memberExpression(e.identifier("e"),e.identifier("target")),e.memberExpression(e.identifier("e"),e.identifier("currentTarget"))),e.expressionStatement(e.callExpression(r,[e.identifier("e")])))]));break;case"once":case"capture":case"passive":break}return r}function d(n,i){return i.has(n)}function m(n){let i=new Set;function r(a){let t=a.init;if(!t||!e.isCallExpression(t))return;let o=t.callee,c="";if(e.isIdentifier(o)?c=o.name:e.isMemberExpression(o)&&e.isIdentifier(o.property)&&(c=o.property.name),ie.has(c)){let u=a.id;if(e.isIdentifier(u))i.add(u.name);else if(e.isArrayPattern(u))for(let S of u.elements)e.isIdentifier(S)&&i.add(S.name);else if(e.isObjectPattern(u))for(let S of u.properties)e.isObjectProperty(S)&&e.isIdentifier(S.value)&&i.add(S.value.name)}}let s=n.scope;for(;s;){for(let[a,t]of Object.entries(s.bindings))if(t.path.isVariableDeclarator()&&r(t.path.node),t.path.isIdentifier()||t.kind==="param"){let o=t.scope.path;if(o&&o.node&&o.node.params){for(let c of o.node.params)if(e.isObjectPattern(c))for(let u of c.properties)e.isObjectProperty(u)&&e.isIdentifier(u.value)?i.add(u.value.name):e.isRestElement(u)&&e.isIdentifier(u.argument)&&i.add(u.argument.name)}}s=s.parent}return i}function x(n){return m(n)}function g(n){if(!e.isCallExpression(n))return!1;let i=n.callee;return e.isMemberExpression(i)&&e.isIdentifier(i.object)?R.has(i.object.name):e.isIdentifier(i)?R.has(i.name):!1}function P(n,i,r){if(!i)return!1;if(e.isCallExpression(n)){if(e.isIdentifier(n.callee)&&d(n.callee.name,i)||r&&e.isIdentifier(n.callee)&&r.has(n.callee.name)&&!R.has(n.callee.name)||e.isMemberExpression(n.callee)&&e.isIdentifier(n.callee.object)&&d(n.callee.object.name,i)||g(n))return!1;if(n.arguments.some(s=>k(s,i,r)))return!0}return!1}function k(n,i,r){return i||(i=new Set),e.isCallExpression(n)?e.isIdentifier(n.callee)&&d(n.callee.name,i)||r&&e.isIdentifier(n.callee)&&r.has(n.callee.name)&&!R.has(n.callee.name)||e.isMemberExpression(n.callee)&&e.isIdentifier(n.callee.object)&&d(n.callee.object.name,i)?!0:g(n)?n.arguments.some(s=>k(s,i,r)):e.isIdentifier(n.callee)?n.arguments.some(s=>k(s,i,r)):k(n.callee,i,r)||n.arguments.some(s=>k(s,i,r)):e.isIdentifier(n)?d(n.name,i):e.isMemberExpression(n)?k(n.object,i,r):e.isConditionalExpression(n)?k(n.test,i,r)||k(n.consequent,i,r)||k(n.alternate,i,r):e.isBinaryExpression(n)||e.isLogicalExpression(n)?k(n.left,i,r)||k(n.right,i,r):e.isUnaryExpression(n)?k(n.argument,i,r):e.isTemplateLiteral(n)?n.expressions.some(s=>k(s,i,r)):e.isObjectExpression(n)?n.properties.some(s=>e.isObjectProperty(s)&&k(s.value,i,r)):e.isArrayExpression(n)?n.elements.some(s=>s&&k(s,i,r)):(e.isArrowFunctionExpression(n)||e.isFunctionExpression(n),!1)}function J(n){if(e.isJSXText(n))return!0;if(e.isJSXExpressionContainer(n))return!1;if(e.isJSXElement(n)){let i=n.openingElement,r=i.name.name;if(w(r))return!1;for(let s of i.attributes){if(e.isJSXSpreadAttribute(s))return!1;let a=s.value;if(e.isJSXExpressionContainer(a))return!1}return n.children.every(J)}return!1}function j(n){return e.isJSXSpreadAttribute(n)?!0:n.value?e.isJSXExpressionContainer(n.value):!1}function B(n){if(e.isJSXText(n)){let t=n.value.replace(/\n\s+/g," ").trim();return t?Y(t):""}if(e.isJSXExpressionContainer(n))return e.isJSXEmptyExpression(n.expression)?"":"<!--$-->";if(!e.isJSXElement(n))return"";let i=n.openingElement,r=i.name.name;if(w(r))return"";let s=`<${r}`;for(let t of i.attributes){if(e.isJSXSpreadAttribute(t))continue;let o=L(t);if(o.startsWith("on")||o.startsWith("bind:")||o.includes("|"))continue;let c=o;if(o==="className"&&(c="class"),o==="htmlFor"&&(c="for"),!t.value)s+=` ${c}`;else if(e.isStringLiteral(t.value))s+=` ${c}="${ce(t.value.value)}"`;else if(e.isJSXExpressionContainer(t.value))continue}let a=n.openingElement.selfClosing;if(a&&T(r))return s+=">",s;if(a)return s+=`></${r}>`,s;s+=">";for(let t of n.children)if(e.isJSXText(t)){let o=t.value.replace(/\n\s+/g," ").trim();o&&(s+=Y(o))}else e.isJSXExpressionContainer(t)?e.isJSXEmptyExpression(t.expression)||(s+="<!--$-->"):e.isJSXElement(t)&&(w(t.openingElement.name.name)?s+="<!--$-->":s+=B(t));return s+=`</${r}>`,s}function Y(n){return n.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function ce(n){return n.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function X(n,i){let{node:r}=n,s=r.openingElement,a=s.name.name;if(w(a))return ee(n,i);if(a==="For")return fe(n,i);if(a==="Show")return pe(n,i);let t=s.attributes,o=r.children,c=o.every(J),u=t.every(E=>!j(E)),S=t.every(E=>{if(e.isJSXSpreadAttribute(E))return!1;let p=L(E);return!p?.startsWith("on")&&!p?.startsWith("bind:")});if(c&&u&&S){let E=B(r);if(E){let p=ne(i,E);return i.needsTemplate=!0,e.callExpression(e.identifier(p),[])}}let y=B(r);if(!y){let E=r.loc,p=i.filename||i.file?.opts?.filename||"<unknown>",_=E?`:${E.start.line}:${E.start.column}`:"";return console.warn(`[what-compiler] Could not extract template for <${a}> at ${p}${_}. Falling back to h() for this element. This element could not be statically analyzed. Consider simplifying the JSX.`),i.needsH=!0,z(n,i)}let M=ne(i,y);i.needsTemplate=!0;let l=i.nextVarId(),h=[e.variableDeclaration("const",[e.variableDeclarator(e.identifier(l),e.callExpression(e.identifier(M),[]))])];return Q(h,l,t,i),K(h,l,o,r,i),i._pendingSetup||(i._pendingSetup=[]),i._pendingSetup.push(...h),e.identifier(l)}function z(n,i){let{node:r}=n,s=r.openingElement,a=s.name.name,t=s.attributes,o=r.children,c=[];for(let y of t){if(e.isJSXSpreadAttribute(y))continue;let M=L(y),l=v(y.value),h=C(M);c.push(e.objectProperty(/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(h)?e.identifier(h):e.stringLiteral(h),l))}let u=[];for(let y of o)if(e.isJSXText(y)){let M=y.value.replace(/\n\s+/g," ").trim();M&&u.push(e.stringLiteral(M))}else e.isJSXExpressionContainer(y)?e.isJSXEmptyExpression(y.expression)||u.push(y.expression):e.isJSXElement(y)?u.push(X({node:y},i)):e.isJSXFragment(y)&&u.push(H({node:y},i));let S=c.length>0?e.objectExpression(c):e.nullLiteral();return e.callExpression(e.identifier("h"),[e.stringLiteral(a),S,...u])}function Q(n,i,r,s){function a(t,o){return s.needsSetProp=!0,e.callExpression(e.identifier("_$setProp"),[e.identifier(i),e.stringLiteral(t),o])}for(let t of r){if(e.isJSXSpreadAttribute(t)){s.needsSpread=!0,n.push(e.expressionStatement(e.callExpression(e.identifier("_$spread"),[e.identifier(i),t.argument])));continue}let o=L(t);if(o==="ref"){let c=v(t.value);n.push(e.expressionStatement(e.conditionalExpression(e.binaryExpression("===",e.unaryExpression("typeof",c),e.stringLiteral("function")),e.callExpression(e.cloneNode(c),[e.identifier(i)]),e.assignmentExpression("=",e.memberExpression(e.cloneNode(c),e.identifier("current")),e.identifier(i)))));continue}if(o.startsWith("on")&&!o.includes("|")){let c=o.slice(2).toLowerCase(),u=v(t.value);xe.has(c)?(s.needsDelegation=!0,s.delegatedEvents||(s.delegatedEvents=new Set),s.delegatedEvents.add(c),n.push(e.expressionStatement(e.assignmentExpression("=",e.memberExpression(e.identifier(i),e.identifier(`__${c}`)),u)))):n.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(i),e.identifier("addEventListener")),[e.stringLiteral(c),u])));continue}if(o.startsWith("on")&&o.includes("|")){let{eventName:c,modifiers:u}=f(o),S=v(t.value),y=$(S,u),M=c.slice(2).toLowerCase(),l=u.filter(E=>he.has(E)),h=[e.stringLiteral(M),y];if(l.length>0){let E=l.map(p=>e.objectProperty(e.identifier(p),e.booleanLiteral(!0)));h.push(e.objectExpression(E))}n.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(i),e.identifier("addEventListener")),h)));continue}if(b(o)){let c=I(o),u=t.value.expression;s.needsEffect=!0,c==="value"?(n.push(e.expressionStatement(e.callExpression(e.identifier("_$effect"),[e.arrowFunctionExpression([],e.assignmentExpression("=",e.memberExpression(e.identifier(i),e.identifier("value")),e.callExpression(e.cloneNode(u),[])))]))),n.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(i),e.identifier("addEventListener")),[e.stringLiteral("input"),e.arrowFunctionExpression([e.identifier("e")],e.callExpression(e.memberExpression(e.cloneNode(u),e.identifier("set")),[e.memberExpression(e.memberExpression(e.identifier("e"),e.identifier("target")),e.identifier("value"))]))])))):c==="checked"&&(s.needsEffect=!0,n.push(e.expressionStatement(e.callExpression(e.identifier("_$effect"),[e.arrowFunctionExpression([],e.assignmentExpression("=",e.memberExpression(e.identifier(i),e.identifier("checked")),e.callExpression(e.cloneNode(u),[])))]))),n.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(i),e.identifier("addEventListener")),[e.stringLiteral("change"),e.arrowFunctionExpression([e.identifier("e")],e.callExpression(e.memberExpression(e.cloneNode(u),e.identifier("set")),[e.memberExpression(e.memberExpression(e.identifier("e"),e.identifier("target")),e.identifier("checked"))]))]))));continue}if(e.isJSXExpressionContainer(t.value)){let c=t.value.expression,u=C(o);if(k(c,s.signalNames,s.importedIdentifiers)){s.needsEffect=!0;let S=e.callExpression(e.identifier("_$effect"),[e.arrowFunctionExpression([],a(u,c))]);P(c,s.signalNames,s.importedIdentifiers)&&e.addComment(S,"leading"," @what-dev: effect wrapping may be unnecessary \u2014 expression contains a non-signal function call with reactive args ",!1),n.push(e.expressionStatement(S))}else n.push(e.expressionStatement(a(u,c)))}}}function K(n,i,r,s,a){let t=[],o=0;for(let l of r){if(e.isJSXText(l)){l.value.replace(/\n\s+/g," ").trim()&&o++;continue}if(e.isJSXExpressionContainer(l)){if(e.isJSXEmptyExpression(l.expression))continue;t.push({type:"expression",child:l,childIndex:o}),o++;continue}if(e.isJSXElement(l)){let h=l.openingElement.name.name;if(w(h)||h==="For"||h==="Show")t.push({type:"component",child:l,childIndex:o}),o++;else{let E=l.openingElement.attributes.some(j)||l.openingElement.attributes.some(p=>!e.isJSXSpreadAttribute(p)&&L(p)?.startsWith("on"))||!l.children.every(J);t.push({type:"static",child:l,childIndex:o,hasAnythingDynamic:E}),o++}continue}e.isJSXFragment(l)&&t.push({type:"fragment",child:l})}let c=t.filter(l=>l.type==="expression"||l.type==="component"||l.type==="static"&&l.hasAnythingDynamic),u=t.some(l=>l.type==="expression"||l.type==="component"),S=c.length>=2&&u,y=new Map;if(S)for(let l of c){let h=`_m$${l.childIndex}`,E=a.nextVarId();y.set(l.childIndex,E),n.push(e.variableDeclaration("const",[e.variableDeclarator(e.identifier(E),U(i,l.childIndex))]))}function M(l){return y.has(l)?e.identifier(y.get(l)):U(i,l)}for(let l of t){if(l.type==="expression"){let h=l.child.expression,E=M(l.childIndex);if(a.needsInsert=!0,k(h,a.signalNames,a.importedIdentifiers)){let p=e.callExpression(e.identifier("_$insert"),[e.identifier(i),e.arrowFunctionExpression([],h),E]);P(h,a.signalNames,a.importedIdentifiers)&&e.addComment(p,"leading"," @what-dev: reactive wrapping may be unnecessary \u2014 expression contains a non-signal function call with reactive args ",!1),n.push(e.expressionStatement(p))}else n.push(e.expressionStatement(e.callExpression(e.identifier("_$insert"),[e.identifier(i),h,E])));continue}if(l.type==="component"){let h=X({node:l.child},a),E=M(l.childIndex);a.needsInsert=!0,n.push(e.expressionStatement(e.callExpression(e.identifier("_$insert"),[e.identifier(i),h,E])));continue}if(l.type==="static"&&l.hasAnythingDynamic){let h;y.has(l.childIndex)?h=y.get(l.childIndex):(h=a.nextVarId(),n.push(e.variableDeclaration("const",[e.variableDeclarator(e.identifier(h),U(i,l.childIndex))]))),Q(n,h,l.child.openingElement.attributes,a),K(n,h,l.child.children,l.child,a);continue}if(l.type==="fragment"){for(let h of l.child.children)if(e.isJSXExpressionContainer(h)&&!e.isJSXEmptyExpression(h.expression)){a.needsInsert=!0;let E=h.expression;k(E,a.signalNames,a.importedIdentifiers)?n.push(e.expressionStatement(e.callExpression(e.identifier("_$insert"),[e.identifier(i),e.arrowFunctionExpression([],E)]))):n.push(e.expressionStatement(e.callExpression(e.identifier("_$insert"),[e.identifier(i),E])))}}}}function U(n,i){if(i===0)return e.memberExpression(e.identifier(n),e.identifier("firstChild"));let r=e.memberExpression(e.identifier(n),e.identifier("firstChild"));for(let s=0;s<i;s++)r=e.memberExpression(r,e.identifier("nextSibling"));return r}function ee(n,i){let{node:r}=n,s=r.openingElement,a=s.name.name,t=s.attributes,o=r.children,c=null,u=[];for(let p of t){if(e.isJSXAttribute(p)){let _;if(e.isJSXNamespacedName(p.name)?_=`${p.name.namespace.name}:${p.name.name.name}`:_=p.name.name,_&&typeof _=="string"&&_.startsWith("client:")){let A=_.slice(7);p.value?c={type:A,value:p.value.value}:c={type:A};continue}}u.push(p)}if(c){i.needsCreateComponent=!0,i.needsIsland=!0;let p=[e.objectProperty(e.identifier("component"),e.identifier(a)),e.objectProperty(e.identifier("mode"),e.stringLiteral(c.type))];c.value&&p.push(e.objectProperty(e.identifier("mediaQuery"),e.stringLiteral(c.value)));for(let _ of u){if(e.isJSXSpreadAttribute(_))continue;let A=L(_),F=v(_.value);p.push(e.objectProperty(e.identifier(A),F))}return e.callExpression(e.identifier("_$createComponent"),[e.identifier("Island"),e.objectExpression(p),e.arrayExpression([])])}i.needsCreateComponent=!0;let S=[],y=!1,M=null;for(let p of u){if(e.isJSXSpreadAttribute(p)){y=!0,M=p.argument;continue}let _=L(p);if(b(_)){let F=I(_),N=p.value.expression;F==="value"?(S.push(e.objectProperty(e.identifier("value"),e.callExpression(e.cloneNode(N),[]))),S.push(e.objectProperty(e.identifier("onInput"),e.arrowFunctionExpression([e.identifier("e")],e.callExpression(e.memberExpression(e.cloneNode(N),e.identifier("set")),[e.memberExpression(e.memberExpression(e.identifier("e"),e.identifier("target")),e.identifier("value"))]))))):F==="checked"&&(S.push(e.objectProperty(e.identifier("checked"),e.callExpression(e.cloneNode(N),[]))),S.push(e.objectProperty(e.identifier("onChange"),e.arrowFunctionExpression([e.identifier("e")],e.callExpression(e.memberExpression(e.cloneNode(N),e.identifier("set")),[e.memberExpression(e.memberExpression(e.identifier("e"),e.identifier("target")),e.identifier("checked"))])))));continue}if(_.startsWith("on")&&_.includes("|")){let{eventName:F,modifiers:N}=f(_),ue=v(p.value),de=$(ue,N);S.push(e.objectProperty(e.identifier(F),de));continue}let A=v(p.value);S.push(e.objectProperty(/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(_)?e.identifier(_):e.stringLiteral(_),A))}let l=[];for(let p of o)if(e.isJSXText(p)){let _=p.value.replace(/\n\s+/g," ").trim();_&&l.push(e.stringLiteral(_))}else e.isJSXExpressionContainer(p)?e.isJSXEmptyExpression(p.expression)||l.push(p.expression):e.isJSXElement(p)?l.push(X({node:p},i)):e.isJSXFragment(p)&&l.push(H({node:p},i));let h;y?S.length>0?h=e.callExpression(e.memberExpression(e.identifier("Object"),e.identifier("assign")),[e.objectExpression([]),M,e.objectExpression(S)]):h=M:S.length>0?h=e.objectExpression(S):h=e.nullLiteral();let E=l.length>0?e.arrayExpression(l):e.arrayExpression([]);return e.callExpression(e.identifier("_$createComponent"),[e.identifier(a),h,E])}function fe(n,i){let{node:r}=n,s=r.openingElement.attributes,a=r.children,t=null;for(let c of s)e.isJSXAttribute(c)&&L(c)==="each"&&(t=v(c.value));if(!t)return console.warn('[what-compiler] <For> element missing "each" attribute.'),i.needsH=!0,z(n,i);let o=null;for(let c of a)if(e.isJSXExpressionContainer(c)&&!e.isJSXEmptyExpression(c.expression)){o=c.expression;break}return o?(i.needsMapArray=!0,e.callExpression(e.identifier("_$mapArray"),[t,o])):(console.warn("[what-compiler] <For> element missing render function child."),i.needsH=!0,z(n,i))}function pe(n,i){return i.needsCreateComponent=!0,ee(n,i)}function H(n,i){let{node:r}=n,s=r.children,a=[];for(let t of s)if(e.isJSXText(t)){let o=t.value.replace(/\n\s+/g," ").trim();o&&a.push(e.stringLiteral(o))}else e.isJSXExpressionContainer(t)?e.isJSXEmptyExpression(t.expression)||a.push(t.expression):e.isJSXElement(t)?a.push(X({node:t},i)):e.isJSXFragment(t)&&a.push(H({node:t},i));return a.length===1?a[0]:e.arrayExpression(a)}function ne(n,i){if(n.templateMap.has(i))return n.templateMap.get(i);let r=`_tmpl$${n.templateCount++}`;return n.templateMap.set(i,r),n.templates.push({id:r,html:i}),r}return{name:"what-jsx-transform",visitor:{Program:{enter(n,i){i.needsTemplate=!1,i.needsInsert=!1,i.needsEffect=!1,i.needsMapArray=!1,i.needsSpread=!1,i.needsSetProp=!1,i.needsH=!1,i.needsCreateComponent=!1,i.needsFragment=!1,i.needsIsland=!1,i.needsDelegation=!1,i.delegatedEvents=new Set,i.templates=[],i.templateMap=new Map,i.templateCount=0,i._varCounter=0,i._pendingSetup=[],i.nextVarId=()=>`_el$${i._varCounter++}`,i.signalNames=new Set,i.importedIdentifiers=new Set;for(let r of n.node.body)if(e.isImportDeclaration(r)){let s=r.source.value,a=s==="what-framework"||s.startsWith("what-framework/")||s==="what-core"||s.startsWith("what-core/")||s.startsWith("./")||s.startsWith("../");for(let t of r.specifiers){let o=null;(e.isImportSpecifier(t)&&e.isIdentifier(t.local)||e.isImportDefaultSpecifier(t)&&e.isIdentifier(t.local)||e.isImportNamespaceSpecifier(t)&&e.isIdentifier(t.local))&&(o=t.local.name),o&&(a||/^(use|create)[A-Z]/.test(o))&&i.importedIdentifiers.add(o)}}n.traverse({VariableDeclarator(r){let s=r.node.init;if(!s||!e.isCallExpression(s))return;let a=s.callee,t="";if(e.isIdentifier(a)?t=a.name:e.isMemberExpression(a)&&e.isIdentifier(a.property)&&(t=a.property.name),ie.has(t)){let o=r.node.id;if(e.isIdentifier(o))i.signalNames.add(o.name);else if(e.isArrayPattern(o))for(let c of o.elements)e.isIdentifier(c)&&i.signalNames.add(c.name);else if(e.isObjectPattern(o))for(let c of o.properties)e.isObjectProperty(c)&&e.isIdentifier(c.value)&&i.signalNames.add(c.value.name)}}})},exit(n,i){for(let a of i.templates.reverse())n.unshiftContainer("body",e.variableDeclaration("const",[e.variableDeclarator(e.identifier(a.id),e.callExpression(e.identifier("_$template"),[e.stringLiteral(a.html)]))]));let r=[];i.needsTemplate&&r.push(e.importSpecifier(e.identifier("_$template"),e.identifier("template"))),i.needsInsert&&r.push(e.importSpecifier(e.identifier("_$insert"),e.identifier("insert"))),i.needsEffect&&r.push(e.importSpecifier(e.identifier("_$effect"),e.identifier("effect"))),i.needsMapArray&&r.push(e.importSpecifier(e.identifier("_$mapArray"),e.identifier("mapArray"))),i.needsSpread&&r.push(e.importSpecifier(e.identifier("_$spread"),e.identifier("spread"))),i.needsSetProp&&r.push(e.importSpecifier(e.identifier("_$setProp"),e.identifier("setProp"))),i.needsCreateComponent&&r.push(e.importSpecifier(e.identifier("_$createComponent"),e.identifier("_$createComponent"))),i.needsDelegation&&r.push(e.importSpecifier(e.identifier("_$delegateEvents"),e.identifier("delegateEvents")));let s=[];if(i.needsH&&s.push(e.importSpecifier(e.identifier("h"),e.identifier("h"))),i.needsFragment&&s.push(e.importSpecifier(e.identifier("Fragment"),e.identifier("Fragment"))),i.needsIsland&&s.push(e.importSpecifier(e.identifier("Island"),e.identifier("Island"))),r.length>0){let a=null;for(let t of n.node.body)if(e.isImportDeclaration(t)&&(t.source.value==="what-framework/render"||t.source.value==="what-core/render")){a=t;break}if(a){let t=new Set(a.specifiers.filter(o=>e.isImportSpecifier(o)).map(o=>o.imported.name));for(let o of r)t.has(o.imported.name)||a.specifiers.push(o)}else n.unshiftContainer("body",e.importDeclaration(r,e.stringLiteral("what-framework/render")))}if(s.length>0&&Ee(n,e,s),i.needsDelegation&&i.delegatedEvents&&i.delegatedEvents.size>0){let a=e.arrayExpression([...i.delegatedEvents].map(t=>e.stringLiteral(t)));n.pushContainer("body",e.expressionStatement(e.callExpression(e.identifier("_$delegateEvents"),[a])))}}},JSXElement(n,i){i.signalNames=m(n),i._pendingSetup=[];let r=X(n,i),s=i._pendingSetup;if(i._pendingSetup=[],s.length>0){let a=n;for(;a&&!a.isStatement();)a=a.parentPath;if(a&&a.isStatement()){for(let t of s)a.insertBefore(t);n.replaceWith(r)}else s.push(e.returnStatement(r)),n.replaceWith(e.callExpression(e.arrowFunctionExpression([],e.blockStatement(s)),[]))}else n.replaceWith(r)},JSXFragment(n,i){let r=H(n,i);n.replaceWith(r)}}}}function Ee(e,f,b){let I=null;for(let w of e.node.body)if(f.isImportDeclaration(w)&&(w.source.value==="what-core"||w.source.value==="what-framework")){I=w;break}if(I){let w=new Set(I.specifiers.filter(T=>f.isImportSpecifier(T)).map(T=>T.imported.name));for(let T of b)w.has(T.imported.name)||I.specifiers.push(T)}else{let w=f.importDeclaration(b,f.stringLiteral("what-framework"));e.unshiftContainer("body",w)}}import D from"fs";import O from"path";var re=new Set([".jsx",".tsx",".js",".ts"]),be=new Set(["_layout","_error","_loading","_404"]);function Se(e){let f=[],b=[],I=[];function w(v,C=""){if(!D.existsSync(v))return;let L=D.readdirSync(v,{withFileTypes:!0});for(let $ of L){let d=O.join(v,$.name);if($.isDirectory()){if($.name.match(/^\((.+)\)$/)){w(d,C);continue}if($.name==="api"&&C===""){T(d,"/api");continue}w(d,C+"/"+W($.name));continue}let m=O.extname($.name);if(!re.has(m))continue;let x=O.basename($.name,m);if(x==="_layout"){b.push({filePath:d,urlPrefix:C||"/"});continue}if(be.has(x))continue;let g=W(x),P=x==="index"?C||"/":C+"/"+g;f.push({filePath:d,routePath:se(P),isDynamic:P.includes(":")||P.includes("*")})}}function T(v,C){if(!D.existsSync(v))return;let L=D.readdirSync(v,{withFileTypes:!0});for(let $ of L){let d=O.join(v,$.name);if($.isDirectory()){T(d,C+"/"+W($.name));continue}let m=O.extname($.name);if(!re.has(m))continue;let x=O.basename($.name,m),g=W(x),P=x==="index"?C:C+"/"+g;I.push({filePath:d,routePath:se(P)})}}return w(e),f.sort((v,C)=>{let L=te(v.routePath),$=te(C.routePath);return L-$}),{pages:f,layouts:b,apiRoutes:I}}function W(e){let f=e.match(/^\[\.\.\.(\w+)\]$/);if(f)return"*"+f[1];let b=e.match(/^\[(\w+)\]$/);return b?":"+b[1]:e.toLowerCase()}function se(e){let f=e.replace(/\/+/g,"/");return f.length>1&&f.endsWith("/")&&(f=f.slice(0,-1)),f||"/"}function te(e){return e.includes("*")?100:e.includes(":")?10:0}function ye(e){let f=e.match(/export\s+const\s+page\s*=\s*(\{[^}]*\})/s);if(!f)return{mode:"client"};try{let b=f[1].replace(/'/g,'"').replace(/(\w+)\s*:/g,'"$1":').replace(/,\s*}/g,"}").replace(/\/\/[^\n]*/g,"");return{mode:"client",...JSON.parse(b)}}catch{return{mode:"client"}}}function oe(e,f){let{pages:b,layouts:I,apiRoutes:w}=Se(e),T=[],v=[],C=new Map;I.forEach((d,m)=>{let x=`_layout${m}`,g=q(d.filePath,f);T.push(`import ${x} from '${g}';`),C.set(d.urlPrefix,x)}),b.forEach((d,m)=>{let x=`_page${m}`,g=q(d.filePath,f);T.push(`import ${x} from '${g}';`);let P={mode:"client"};try{let j=D.readFileSync(d.filePath,"utf-8");P=ye(j)}catch{}let k=ve(d.routePath,C),J={path:d.routePath,component:x,mode:P.mode||"client",layout:k||null};v.push(J)});let L=[];return w.forEach((d,m)=>{let x=`_api${m}`,g=q(d.filePath,f);T.push(`import * as ${x} from '${g}';`),L.push({path:d.routePath,handlers:x})}),["// Auto-generated by What Framework file router","// Do not edit \u2014 changes will be overwritten","",...T,"","export const routes = [",...v.map(d=>` { path: '${d.path}', component: ${d.component}, mode: '${d.mode}'${d.layout?`, layout: ${d.layout}`:""} },`),"];","","export const apiRoutes = [",...L.map(d=>` { path: '${d.path}', handlers: ${d.handlers} },`),"];","","export const pageModes = {",...v.map(d=>` '${d.path}': '${d.mode}',`),"};"].join(`
1
+ import Z from"path";import{transformSync as _e}from"@babel/core";var me=new Set(["preventDefault","stopPropagation","once","capture","passive","self"]),he=new Set(["once","capture","passive"]),ge=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]),xe=new Set(["click","input","change","keydown","keyup","submit","focusin","focusout","mousedown","mouseup"]),R=new Set(["Math","Number","String","Boolean","parseInt","parseFloat","isNaN","isFinite","encodeURIComponent","decodeURIComponent","encodeURI","decodeURI","JSON","Date","Array","Object","console","RegExp"]),ie=new Set(["useSignal","signal","computed","useComputed","useState","useReducer","createResource","useSWR","useQuery","useInfiniteQuery"]);function G({types:e}){function f(n){let i=n.split("|"),r=i[0],t=i.slice(1).filter(a=>me.has(a));return{eventName:r,modifiers:t}}function b(n){return n.startsWith("bind:")}function I(n){return n.slice(5)}function _(n){return/^[A-Z]/.test(n)}function T(n){return ge.has(String(n).toLowerCase())}function v(n){return n?e.isJSXExpressionContainer(n)?n.expression:e.isStringLiteral(n)?n:e.stringLiteral(n.value||""):e.booleanLiteral(!0)}function C(n){return n==="className"?"class":n==="htmlFor"?"for":n}function k(n){return e.isJSXNamespacedName(n.name)?`${n.name.namespace.name}:${n.name.name.name}`:typeof n.name.name=="string"?n.name.name:String(n.name.name)}function $(n,i){if(i.length===0)return n;let r=n;for(let t of i)switch(t){case"preventDefault":r=e.arrowFunctionExpression([e.identifier("e")],e.blockStatement([e.expressionStatement(e.callExpression(e.memberExpression(e.identifier("e"),e.identifier("preventDefault")),[])),e.expressionStatement(e.callExpression(r,[e.identifier("e")]))]));break;case"stopPropagation":r=e.arrowFunctionExpression([e.identifier("e")],e.blockStatement([e.expressionStatement(e.callExpression(e.memberExpression(e.identifier("e"),e.identifier("stopPropagation")),[])),e.expressionStatement(e.callExpression(r,[e.identifier("e")]))]));break;case"self":r=e.arrowFunctionExpression([e.identifier("e")],e.blockStatement([e.ifStatement(e.binaryExpression("===",e.memberExpression(e.identifier("e"),e.identifier("target")),e.memberExpression(e.identifier("e"),e.identifier("currentTarget"))),e.expressionStatement(e.callExpression(r,[e.identifier("e")])))]));break;case"once":case"capture":case"passive":break}return r}function d(n,i){return i.has(n)}function m(n){let i=new Set;function r(a){let s=a.init;if(!s||!e.isCallExpression(s))return;let o=s.callee,c="";if(e.isIdentifier(o)?c=o.name:e.isMemberExpression(o)&&e.isIdentifier(o.property)&&(c=o.property.name),ie.has(c)){let u=a.id;if(e.isIdentifier(u))i.add(u.name);else if(e.isArrayPattern(u))for(let S of u.elements)e.isIdentifier(S)&&i.add(S.name);else if(e.isObjectPattern(u))for(let S of u.properties)e.isObjectProperty(S)&&e.isIdentifier(S.value)&&i.add(S.value.name)}}let t=n.scope;for(;t;){for(let[a,s]of Object.entries(t.bindings))if(s.path.isVariableDeclarator()&&r(s.path.node),s.path.isIdentifier()||s.kind==="param"){let o=s.scope.path;if(o&&o.node&&o.node.params){for(let c of o.node.params)if(e.isObjectPattern(c))for(let u of c.properties)e.isObjectProperty(u)&&e.isIdentifier(u.value)?i.add(u.value.name):e.isRestElement(u)&&e.isIdentifier(u.argument)&&i.add(u.argument.name)}}t=t.parent}return i}function x(n){return m(n)}function g(n){if(!e.isCallExpression(n))return!1;let i=n.callee;return e.isMemberExpression(i)&&e.isIdentifier(i.object)?R.has(i.object.name):e.isIdentifier(i)?R.has(i.name):!1}function P(n,i,r){if(!i)return!1;if(e.isCallExpression(n)){if(e.isIdentifier(n.callee)&&d(n.callee.name,i)||r&&e.isIdentifier(n.callee)&&r.has(n.callee.name)&&!R.has(n.callee.name)||e.isMemberExpression(n.callee)&&e.isIdentifier(n.callee.object)&&d(n.callee.object.name,i)||g(n))return!1;if(n.arguments.some(t=>L(t,i,r)))return!0}return!1}function L(n,i,r){return i||(i=new Set),e.isCallExpression(n)?e.isIdentifier(n.callee)&&d(n.callee.name,i)||r&&e.isIdentifier(n.callee)&&r.has(n.callee.name)&&!R.has(n.callee.name)||e.isMemberExpression(n.callee)&&e.isIdentifier(n.callee.object)&&d(n.callee.object.name,i)?!0:g(n)?n.arguments.some(t=>L(t,i,r)):e.isIdentifier(n.callee)?n.arguments.some(t=>L(t,i,r)):L(n.callee,i,r)||n.arguments.some(t=>L(t,i,r)):e.isIdentifier(n)?d(n.name,i):e.isMemberExpression(n)?L(n.object,i,r):e.isConditionalExpression(n)?L(n.test,i,r)||L(n.consequent,i,r)||L(n.alternate,i,r):e.isBinaryExpression(n)||e.isLogicalExpression(n)?L(n.left,i,r)||L(n.right,i,r):e.isUnaryExpression(n)?L(n.argument,i,r):e.isTemplateLiteral(n)?n.expressions.some(t=>L(t,i,r)):e.isObjectExpression(n)?n.properties.some(t=>e.isObjectProperty(t)&&L(t.value,i,r)):e.isArrayExpression(n)?n.elements.some(t=>t&&L(t,i,r)):(e.isArrowFunctionExpression(n)||e.isFunctionExpression(n),!1)}function J(n){if(e.isJSXText(n))return!0;if(e.isJSXExpressionContainer(n))return!1;if(e.isJSXElement(n)){let i=n.openingElement,r=i.name.name;if(_(r))return!1;for(let t of i.attributes){if(e.isJSXSpreadAttribute(t))return!1;let a=t.value;if(e.isJSXExpressionContainer(a))return!1}return n.children.every(J)}return!1}function j(n){return e.isJSXSpreadAttribute(n)?!0:n.value?e.isJSXExpressionContainer(n.value):!1}function B(n){if(e.isJSXText(n)){let s=n.value.replace(/\n\s+/g," ").trim();return s?Y(s):""}if(e.isJSXExpressionContainer(n))return e.isJSXEmptyExpression(n.expression)?"":"<!--$-->";if(!e.isJSXElement(n))return"";let i=n.openingElement,r=i.name.name;if(_(r))return"";let t=`<${r}`;for(let s of i.attributes){if(e.isJSXSpreadAttribute(s))continue;let o=k(s);if(o==="key"||o.startsWith("on")||o.startsWith("bind:")||o.includes("|"))continue;let c=o;if(o==="className"&&(c="class"),o==="htmlFor"&&(c="for"),!s.value)t+=` ${c}`;else if(e.isStringLiteral(s.value))t+=` ${c}="${ce(s.value.value)}"`;else if(e.isJSXExpressionContainer(s.value))continue}let a=n.openingElement.selfClosing;if(a&&T(r))return t+=">",t;if(a)return t+=`></${r}>`,t;t+=">";for(let s of n.children)if(e.isJSXText(s)){let o=s.value.replace(/\n\s+/g," ").trim();o&&(t+=Y(o))}else e.isJSXExpressionContainer(s)?e.isJSXEmptyExpression(s.expression)||(t+="<!--$-->"):e.isJSXElement(s)&&(_(s.openingElement.name.name)?t+="<!--$-->":t+=B(s));return t+=`</${r}>`,t}function Y(n){return n.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function ce(n){return n.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function X(n,i){let{node:r}=n,t=r.openingElement,a=t.name.name;if(_(a))return ee(n,i);if(a==="For")return fe(n,i);if(a==="Show")return pe(n,i);let s=t.attributes,o=r.children,c=o.every(J),u=s.every(E=>!j(E)),S=s.every(E=>{if(e.isJSXSpreadAttribute(E))return!1;let p=k(E);return!p?.startsWith("on")&&!p?.startsWith("bind:")});if(c&&u&&S){let E=B(r);if(E){let p=ne(i,E);return i.needsTemplate=!0,e.callExpression(e.identifier(p),[])}}let y=B(r);if(!y){let E=r.loc,p=i.filename||i.file?.opts?.filename||"<unknown>",w=E?`:${E.start.line}:${E.start.column}`:"";return console.warn(`[what-compiler] Could not extract template for <${a}> at ${p}${w}. Falling back to h() for this element. This element could not be statically analyzed. Consider simplifying the JSX.`),i.needsH=!0,z(n,i)}let M=ne(i,y);i.needsTemplate=!0;let l=i.nextVarId(),h=[e.variableDeclaration("const",[e.variableDeclarator(e.identifier(l),e.callExpression(e.identifier(M),[]))])];return Q(h,l,s,i),K(h,l,o,r,i),i._pendingSetup||(i._pendingSetup=[]),i._pendingSetup.push(...h),e.identifier(l)}function z(n,i){let{node:r}=n,t=r.openingElement,a=t.name.name,s=t.attributes,o=r.children,c=[];for(let y of s){if(e.isJSXSpreadAttribute(y))continue;let M=k(y),l=v(y.value),h=C(M);c.push(e.objectProperty(/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(h)?e.identifier(h):e.stringLiteral(h),l))}let u=[];for(let y of o)if(e.isJSXText(y)){let M=y.value.replace(/\n\s+/g," ").trim();M&&u.push(e.stringLiteral(M))}else e.isJSXExpressionContainer(y)?e.isJSXEmptyExpression(y.expression)||u.push(y.expression):e.isJSXElement(y)?u.push(X({node:y},i)):e.isJSXFragment(y)&&u.push(H({node:y},i));let S=c.length>0?e.objectExpression(c):e.nullLiteral();return e.callExpression(e.identifier("h"),[e.stringLiteral(a),S,...u])}function Q(n,i,r,t){function a(s,o){return t.needsSetProp=!0,e.callExpression(e.identifier("_$setProp"),[e.identifier(i),e.stringLiteral(s),o])}for(let s of r){if(e.isJSXSpreadAttribute(s)){t.needsSpread=!0,n.push(e.expressionStatement(e.callExpression(e.identifier("_$spread"),[e.identifier(i),s.argument])));continue}let o=k(s);if(o!=="key"){if(o==="ref"){let c=v(s.value);n.push(e.expressionStatement(e.conditionalExpression(e.binaryExpression("===",e.unaryExpression("typeof",c),e.stringLiteral("function")),e.callExpression(e.cloneNode(c),[e.identifier(i)]),e.assignmentExpression("=",e.memberExpression(e.cloneNode(c),e.identifier("current")),e.identifier(i)))));continue}if(o.startsWith("on")&&!o.includes("|")){let c=o.slice(2).toLowerCase(),u=v(s.value);xe.has(c)?(t.needsDelegation=!0,t.delegatedEvents||(t.delegatedEvents=new Set),t.delegatedEvents.add(c),n.push(e.expressionStatement(e.assignmentExpression("=",e.memberExpression(e.identifier(i),e.identifier(`__${c}`)),u)))):n.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(i),e.identifier("addEventListener")),[e.stringLiteral(c),u])));continue}if(o.startsWith("on")&&o.includes("|")){let{eventName:c,modifiers:u}=f(o),S=v(s.value),y=$(S,u),M=c.slice(2).toLowerCase(),l=u.filter(E=>he.has(E)),h=[e.stringLiteral(M),y];if(l.length>0){let E=l.map(p=>e.objectProperty(e.identifier(p),e.booleanLiteral(!0)));h.push(e.objectExpression(E))}n.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(i),e.identifier("addEventListener")),h)));continue}if(b(o)){let c=I(o),u=s.value.expression;t.needsEffect=!0,c==="value"?(n.push(e.expressionStatement(e.callExpression(e.identifier("_$effect"),[e.arrowFunctionExpression([],e.assignmentExpression("=",e.memberExpression(e.identifier(i),e.identifier("value")),e.callExpression(e.cloneNode(u),[])))]))),n.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(i),e.identifier("addEventListener")),[e.stringLiteral("input"),e.arrowFunctionExpression([e.identifier("e")],e.callExpression(e.memberExpression(e.cloneNode(u),e.identifier("set")),[e.memberExpression(e.memberExpression(e.identifier("e"),e.identifier("target")),e.identifier("value"))]))])))):c==="checked"&&(t.needsEffect=!0,n.push(e.expressionStatement(e.callExpression(e.identifier("_$effect"),[e.arrowFunctionExpression([],e.assignmentExpression("=",e.memberExpression(e.identifier(i),e.identifier("checked")),e.callExpression(e.cloneNode(u),[])))]))),n.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(i),e.identifier("addEventListener")),[e.stringLiteral("change"),e.arrowFunctionExpression([e.identifier("e")],e.callExpression(e.memberExpression(e.cloneNode(u),e.identifier("set")),[e.memberExpression(e.memberExpression(e.identifier("e"),e.identifier("target")),e.identifier("checked"))]))]))));continue}if(e.isJSXExpressionContainer(s.value)){let c=s.value.expression,u=C(o);if(L(c,t.signalNames,t.importedIdentifiers)){t.needsEffect=!0;let S=e.callExpression(e.identifier("_$effect"),[e.arrowFunctionExpression([],a(u,c))]);P(c,t.signalNames,t.importedIdentifiers)&&e.addComment(S,"leading"," @what-dev: effect wrapping may be unnecessary \u2014 expression contains a non-signal function call with reactive args ",!1),n.push(e.expressionStatement(S))}else n.push(e.expressionStatement(a(u,c)))}}}}function K(n,i,r,t,a){let s=[],o=0;for(let l of r){if(e.isJSXText(l)){l.value.replace(/\n\s+/g," ").trim()&&o++;continue}if(e.isJSXExpressionContainer(l)){if(e.isJSXEmptyExpression(l.expression))continue;s.push({type:"expression",child:l,childIndex:o}),o++;continue}if(e.isJSXElement(l)){let h=l.openingElement.name.name;if(_(h)||h==="For"||h==="Show")s.push({type:"component",child:l,childIndex:o}),o++;else{let E=l.openingElement.attributes.some(j)||l.openingElement.attributes.some(p=>!e.isJSXSpreadAttribute(p)&&k(p)?.startsWith("on"))||!l.children.every(J);s.push({type:"static",child:l,childIndex:o,hasAnythingDynamic:E}),o++}continue}e.isJSXFragment(l)&&s.push({type:"fragment",child:l})}let c=s.filter(l=>l.type==="expression"||l.type==="component"||l.type==="static"&&l.hasAnythingDynamic),u=s.some(l=>l.type==="expression"||l.type==="component"),S=c.length>=2&&u,y=new Map;if(S)for(let l of c){let h=`_m$${l.childIndex}`,E=a.nextVarId();y.set(l.childIndex,E),n.push(e.variableDeclaration("const",[e.variableDeclarator(e.identifier(E),U(i,l.childIndex))]))}function M(l){return y.has(l)?e.identifier(y.get(l)):U(i,l)}for(let l of s){if(l.type==="expression"){let h=l.child.expression,E=M(l.childIndex);if(a.needsInsert=!0,L(h,a.signalNames,a.importedIdentifiers)){let p=e.callExpression(e.identifier("_$insert"),[e.identifier(i),e.arrowFunctionExpression([],h),E]);P(h,a.signalNames,a.importedIdentifiers)&&e.addComment(p,"leading"," @what-dev: reactive wrapping may be unnecessary \u2014 expression contains a non-signal function call with reactive args ",!1),n.push(e.expressionStatement(p))}else n.push(e.expressionStatement(e.callExpression(e.identifier("_$insert"),[e.identifier(i),h,E])));continue}if(l.type==="component"){let h=X({node:l.child},a),E=M(l.childIndex);a.needsInsert=!0,n.push(e.expressionStatement(e.callExpression(e.identifier("_$insert"),[e.identifier(i),h,E])));continue}if(l.type==="static"&&l.hasAnythingDynamic){let h;y.has(l.childIndex)?h=y.get(l.childIndex):(h=a.nextVarId(),n.push(e.variableDeclaration("const",[e.variableDeclarator(e.identifier(h),U(i,l.childIndex))]))),Q(n,h,l.child.openingElement.attributes,a),K(n,h,l.child.children,l.child,a);continue}if(l.type==="fragment"){for(let h of l.child.children)if(e.isJSXExpressionContainer(h)&&!e.isJSXEmptyExpression(h.expression)){a.needsInsert=!0;let E=h.expression;L(E,a.signalNames,a.importedIdentifiers)?n.push(e.expressionStatement(e.callExpression(e.identifier("_$insert"),[e.identifier(i),e.arrowFunctionExpression([],E)]))):n.push(e.expressionStatement(e.callExpression(e.identifier("_$insert"),[e.identifier(i),E])))}}}}function U(n,i){if(i===0)return e.memberExpression(e.identifier(n),e.identifier("firstChild"));let r=e.memberExpression(e.identifier(n),e.identifier("firstChild"));for(let t=0;t<i;t++)r=e.memberExpression(r,e.identifier("nextSibling"));return r}function ee(n,i){let{node:r}=n,t=r.openingElement,a=t.name.name,s=t.attributes,o=r.children,c=null,u=[];for(let p of s){if(e.isJSXAttribute(p)){let w;if(e.isJSXNamespacedName(p.name)?w=`${p.name.namespace.name}:${p.name.name.name}`:w=p.name.name,w&&typeof w=="string"&&w.startsWith("client:")){let A=w.slice(7);p.value?c={type:A,value:p.value.value}:c={type:A};continue}}u.push(p)}if(c){i.needsCreateComponent=!0,i.needsIsland=!0;let p=[e.objectProperty(e.identifier("component"),e.identifier(a)),e.objectProperty(e.identifier("mode"),e.stringLiteral(c.type))];c.value&&p.push(e.objectProperty(e.identifier("mediaQuery"),e.stringLiteral(c.value)));for(let w of u){if(e.isJSXSpreadAttribute(w))continue;let A=k(w),F=v(w.value);p.push(e.objectProperty(e.identifier(A),F))}return e.callExpression(e.identifier("_$createComponent"),[e.identifier("Island"),e.objectExpression(p),e.arrayExpression([])])}i.needsCreateComponent=!0;let S=[],y=!1,M=null;for(let p of u){if(e.isJSXSpreadAttribute(p)){y=!0,M=p.argument;continue}let w=k(p);if(w==="key")continue;if(b(w)){let F=I(w),N=p.value.expression;F==="value"?(S.push(e.objectProperty(e.identifier("value"),e.callExpression(e.cloneNode(N),[]))),S.push(e.objectProperty(e.identifier("onInput"),e.arrowFunctionExpression([e.identifier("e")],e.callExpression(e.memberExpression(e.cloneNode(N),e.identifier("set")),[e.memberExpression(e.memberExpression(e.identifier("e"),e.identifier("target")),e.identifier("value"))]))))):F==="checked"&&(S.push(e.objectProperty(e.identifier("checked"),e.callExpression(e.cloneNode(N),[]))),S.push(e.objectProperty(e.identifier("onChange"),e.arrowFunctionExpression([e.identifier("e")],e.callExpression(e.memberExpression(e.cloneNode(N),e.identifier("set")),[e.memberExpression(e.memberExpression(e.identifier("e"),e.identifier("target")),e.identifier("checked"))])))));continue}if(w.startsWith("on")&&w.includes("|")){let{eventName:F,modifiers:N}=f(w),ue=v(p.value),de=$(ue,N);S.push(e.objectProperty(e.identifier(F),de));continue}let A=v(p.value);S.push(e.objectProperty(/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(w)?e.identifier(w):e.stringLiteral(w),A))}let l=[];for(let p of o)if(e.isJSXText(p)){let w=p.value.replace(/\n\s+/g," ").trim();w&&l.push(e.stringLiteral(w))}else e.isJSXExpressionContainer(p)?e.isJSXEmptyExpression(p.expression)||l.push(p.expression):e.isJSXElement(p)?l.push(X({node:p},i)):e.isJSXFragment(p)&&l.push(H({node:p},i));let h;y?S.length>0?h=e.callExpression(e.memberExpression(e.identifier("Object"),e.identifier("assign")),[e.objectExpression([]),M,e.objectExpression(S)]):h=M:S.length>0?h=e.objectExpression(S):h=e.nullLiteral();let E=l.length>0?e.arrayExpression(l):e.arrayExpression([]);return e.callExpression(e.identifier("_$createComponent"),[e.identifier(a),h,E])}function fe(n,i){let{node:r}=n,t=r.openingElement.attributes,a=r.children,s=null;for(let c of t)e.isJSXAttribute(c)&&k(c)==="each"&&(s=v(c.value));if(!s)return console.warn('[what-compiler] <For> element missing "each" attribute.'),i.needsH=!0,z(n,i);let o=null;for(let c of a)if(e.isJSXExpressionContainer(c)&&!e.isJSXEmptyExpression(c.expression)){o=c.expression;break}return o?(i.needsMapArray=!0,e.callExpression(e.identifier("_$mapArray"),[s,o])):(console.warn("[what-compiler] <For> element missing render function child."),i.needsH=!0,z(n,i))}function pe(n,i){return i.needsCreateComponent=!0,ee(n,i)}function H(n,i){let{node:r}=n,t=r.children,a=[];for(let s of t)if(e.isJSXText(s)){let o=s.value.replace(/\n\s+/g," ").trim();o&&a.push(e.stringLiteral(o))}else e.isJSXExpressionContainer(s)?e.isJSXEmptyExpression(s.expression)||a.push(s.expression):e.isJSXElement(s)?a.push(X({node:s},i)):e.isJSXFragment(s)&&a.push(H({node:s},i));return a.length===1?a[0]:e.arrayExpression(a)}function ne(n,i){if(n.templateMap.has(i))return n.templateMap.get(i);let r=`_tmpl$${n.templateCount++}`;return n.templateMap.set(i,r),n.templates.push({id:r,html:i}),r}return{name:"what-jsx-transform",visitor:{Program:{enter(n,i){i.needsTemplate=!1,i.needsInsert=!1,i.needsEffect=!1,i.needsMapArray=!1,i.needsSpread=!1,i.needsSetProp=!1,i.needsH=!1,i.needsCreateComponent=!1,i.needsFragment=!1,i.needsIsland=!1,i.needsDelegation=!1,i.delegatedEvents=new Set,i.templates=[],i.templateMap=new Map,i.templateCount=0,i._varCounter=0,i._pendingSetup=[],i.nextVarId=()=>`_el$${i._varCounter++}`,i.signalNames=new Set,i.importedIdentifiers=new Set;for(let r of n.node.body)if(e.isImportDeclaration(r)){let t=r.source.value,a=t==="what-framework"||t.startsWith("what-framework/")||t==="what-core"||t.startsWith("what-core/")||t.startsWith("./")||t.startsWith("../");for(let s of r.specifiers){let o=null;(e.isImportSpecifier(s)&&e.isIdentifier(s.local)||e.isImportDefaultSpecifier(s)&&e.isIdentifier(s.local)||e.isImportNamespaceSpecifier(s)&&e.isIdentifier(s.local))&&(o=s.local.name),o&&(a||/^(use|create)[A-Z]/.test(o))&&i.importedIdentifiers.add(o)}}n.traverse({VariableDeclarator(r){let t=r.node.init;if(!t||!e.isCallExpression(t))return;let a=t.callee,s="";if(e.isIdentifier(a)?s=a.name:e.isMemberExpression(a)&&e.isIdentifier(a.property)&&(s=a.property.name),ie.has(s)){let o=r.node.id;if(e.isIdentifier(o))i.signalNames.add(o.name);else if(e.isArrayPattern(o))for(let c of o.elements)e.isIdentifier(c)&&i.signalNames.add(c.name);else if(e.isObjectPattern(o))for(let c of o.properties)e.isObjectProperty(c)&&e.isIdentifier(c.value)&&i.signalNames.add(c.value.name)}}})},exit(n,i){for(let a of i.templates.reverse())n.unshiftContainer("body",e.variableDeclaration("const",[e.variableDeclarator(e.identifier(a.id),e.callExpression(e.identifier("_$template"),[e.stringLiteral(a.html)]))]));let r=[];i.needsTemplate&&r.push(e.importSpecifier(e.identifier("_$template"),e.identifier("template"))),i.needsInsert&&r.push(e.importSpecifier(e.identifier("_$insert"),e.identifier("insert"))),i.needsEffect&&r.push(e.importSpecifier(e.identifier("_$effect"),e.identifier("effect"))),i.needsMapArray&&r.push(e.importSpecifier(e.identifier("_$mapArray"),e.identifier("mapArray"))),i.needsSpread&&r.push(e.importSpecifier(e.identifier("_$spread"),e.identifier("spread"))),i.needsSetProp&&r.push(e.importSpecifier(e.identifier("_$setProp"),e.identifier("setProp"))),i.needsCreateComponent&&r.push(e.importSpecifier(e.identifier("_$createComponent"),e.identifier("_$createComponent"))),i.needsDelegation&&r.push(e.importSpecifier(e.identifier("_$delegateEvents"),e.identifier("delegateEvents")));let t=[];if(i.needsH&&t.push(e.importSpecifier(e.identifier("h"),e.identifier("h"))),i.needsFragment&&t.push(e.importSpecifier(e.identifier("Fragment"),e.identifier("Fragment"))),i.needsIsland&&t.push(e.importSpecifier(e.identifier("Island"),e.identifier("Island"))),r.length>0){let a=null;for(let s of n.node.body)if(e.isImportDeclaration(s)&&(s.source.value==="what-framework/render"||s.source.value==="what-core/render")){a=s;break}if(a){let s=new Set(a.specifiers.filter(o=>e.isImportSpecifier(o)).map(o=>o.imported.name));for(let o of r)s.has(o.imported.name)||a.specifiers.push(o)}else n.unshiftContainer("body",e.importDeclaration(r,e.stringLiteral("what-framework/render")))}if(t.length>0&&Ee(n,e,t),i.needsDelegation&&i.delegatedEvents&&i.delegatedEvents.size>0){let a=e.arrayExpression([...i.delegatedEvents].map(s=>e.stringLiteral(s)));n.pushContainer("body",e.expressionStatement(e.callExpression(e.identifier("_$delegateEvents"),[a])))}}},JSXElement(n,i){i.signalNames=m(n),i._pendingSetup=[];let r=X(n,i),t=i._pendingSetup;if(i._pendingSetup=[],t.length>0){let a=n;for(;a&&!a.isStatement();)a=a.parentPath;if(a&&a.isStatement()){for(let s of t)a.insertBefore(s);n.replaceWith(r)}else t.push(e.returnStatement(r)),n.replaceWith(e.callExpression(e.arrowFunctionExpression([],e.blockStatement(t)),[]))}else n.replaceWith(r)},JSXFragment(n,i){let r=H(n,i);n.replaceWith(r)}}}}function Ee(e,f,b){let I=null;for(let _ of e.node.body)if(f.isImportDeclaration(_)&&(_.source.value==="what-core"||_.source.value==="what-framework")){I=_;break}if(I){let _=new Set(I.specifiers.filter(T=>f.isImportSpecifier(T)).map(T=>T.imported.name));for(let T of b)_.has(T.imported.name)||I.specifiers.push(T)}else{let _=f.importDeclaration(b,f.stringLiteral("what-framework"));e.unshiftContainer("body",_)}}import D from"fs";import O from"path";var re=new Set([".jsx",".tsx",".js",".ts"]),be=new Set(["_layout","_error","_loading","_404"]);function Se(e){let f=[],b=[],I=[];function _(v,C=""){if(!D.existsSync(v))return;let k=D.readdirSync(v,{withFileTypes:!0});for(let $ of k){let d=O.join(v,$.name);if($.isDirectory()){if($.name.match(/^\((.+)\)$/)){_(d,C);continue}if($.name==="api"&&C===""){T(d,"/api");continue}_(d,C+"/"+W($.name));continue}let m=O.extname($.name);if(!re.has(m))continue;let x=O.basename($.name,m);if(x==="_layout"){b.push({filePath:d,urlPrefix:C||"/"});continue}if(be.has(x))continue;let g=W(x),P=x==="index"?C||"/":C+"/"+g;f.push({filePath:d,routePath:te(P),isDynamic:P.includes(":")||P.includes("*")})}}function T(v,C){if(!D.existsSync(v))return;let k=D.readdirSync(v,{withFileTypes:!0});for(let $ of k){let d=O.join(v,$.name);if($.isDirectory()){T(d,C+"/"+W($.name));continue}let m=O.extname($.name);if(!re.has(m))continue;let x=O.basename($.name,m),g=W(x),P=x==="index"?C:C+"/"+g;I.push({filePath:d,routePath:te(P)})}}return _(e),f.sort((v,C)=>{let k=se(v.routePath),$=se(C.routePath);return k-$}),{pages:f,layouts:b,apiRoutes:I}}function W(e){let f=e.match(/^\[\.\.\.(\w+)\]$/);if(f)return"*"+f[1];let b=e.match(/^\[(\w+)\]$/);return b?":"+b[1]:e.toLowerCase()}function te(e){let f=e.replace(/\/+/g,"/");return f.length>1&&f.endsWith("/")&&(f=f.slice(0,-1)),f||"/"}function se(e){return e.includes("*")?100:e.includes(":")?10:0}function ye(e){let f=e.match(/export\s+const\s+page\s*=\s*(\{[^}]*\})/s);if(!f)return{mode:"client"};try{let b=f[1].replace(/'/g,'"').replace(/(\w+)\s*:/g,'"$1":').replace(/,\s*}/g,"}").replace(/\/\/[^\n]*/g,"");return{mode:"client",...JSON.parse(b)}}catch{return{mode:"client"}}}function oe(e,f){let{pages:b,layouts:I,apiRoutes:_}=Se(e),T=[],v=[],C=new Map;I.forEach((d,m)=>{let x=`_layout${m}`,g=q(d.filePath,f);T.push(`import ${x} from '${g}';`),C.set(d.urlPrefix,x)}),b.forEach((d,m)=>{let x=`_page${m}`,g=q(d.filePath,f);T.push(`import ${x} from '${g}';`);let P={mode:"client"};try{let j=D.readFileSync(d.filePath,"utf-8");P=ye(j)}catch{}let L=ve(d.routePath,C),J={path:d.routePath,component:x,mode:P.mode||"client",layout:L||null};v.push(J)});let k=[];return _.forEach((d,m)=>{let x=`_api${m}`,g=q(d.filePath,f);T.push(`import * as ${x} from '${g}';`),k.push({path:d.routePath,handlers:x})}),["// Auto-generated by What Framework file router","// Do not edit \u2014 changes will be overwritten","",...T,"","export const routes = [",...v.map(d=>` { path: '${d.path}', component: ${d.component}, mode: '${d.mode}'${d.layout?`, layout: ${d.layout}`:""} },`),"];","","export const apiRoutes = [",...k.map(d=>` { path: '${d.path}', handlers: ${d.handlers} },`),"];","","export const pageModes = {",...v.map(d=>` '${d.path}': '${d.mode}',`),"};"].join(`
2
2
  `)}function q(e,f){return"/"+O.relative(f,e).split(O.sep).join("/")}function ve(e,f){let b=e.split("/").filter(Boolean);for(;b.length>0;){let I="/"+b.join("/");if(f.has(I))return f.get(I);b.pop()}return f.has("/")?f.get("/"):null}var we=`
3
3
  :host {
4
4
  position: fixed;
@@ -380,7 +380,7 @@ class WhatErrorOverlay extends HTMLElement {
380
380
  if (!customElements.get('what-error-overlay')) {
381
381
  customElements.define('what-error-overlay', WhatErrorOverlay);
382
382
  }
383
- `;function ae(e){let f=e.ws.send.bind(e.ws);e.ws.send=function(b){return b?.type==="error"&&b.err?.plugin==="vite-plugin-what"&&(b.err._isCompilerError=!0),f(b)}}var le="virtual:what-routes",V="\0"+le,Ce=/export\s+(?:default\s+)?function\s+([A-Z]\w*)/,Le=/(?:store|signal|state|context|util|helper|lib|config)\b/i;function ke(e={}){let{include:f=/\.[jt]sx$/,exclude:b=/node_modules/,sourceMaps:I=!0,production:w=!0,pages:T="src/pages",hot:v=!w}=e,C="",L="",$=null,d=!1;return{name:"vite-plugin-what",configResolved(m){C=m.root,L=Z.resolve(C,T),d=m.command==="serve"},configureServer(m){$=m,ae(m),m.watcher.on("add",x=>{if(x.startsWith(L)){let g=m.moduleGraph.getModuleById(V);g&&(m.moduleGraph.invalidateModule(g),m.ws.send({type:"full-reload"}))}}),m.watcher.on("unlink",x=>{if(x.startsWith(L)){let g=m.moduleGraph.getModuleById(V);g&&(m.moduleGraph.invalidateModule(g),m.ws.send({type:"full-reload"}))}})},resolveId(m){if(m===le)return V},load(m){if(m===V)return oe(L,C)},transform(m,x){if(!f.test(x)||b&&b.test(x))return null;try{let g=_e(m,{filename:x,sourceMaps:I,plugins:[[G,{production:w}]],parserOpts:{plugins:["jsx","typescript"]}});if(!g||!g.code)return null;let P=g.code;return v&&d&&!w&&$e(m,x)&&(P+=Ie(x)),{code:P,map:g.map}}catch(g){throw g.plugin="vite-plugin-what",g.id||(g.id=x),g.loc===void 0&&g._loc&&(g.loc={file:x,line:g._loc.line,column:g._loc.column}),console.error(`[what] Error transforming ${x}:`,g.message),g}},handleHotUpdate({file:m,server:x,modules:g}){if(v&&f.test(m)&&!(b&&b.test(m))&&Te(m))return x.ws.send({type:"full-reload"}),[]},config(m,{mode:x}){return{esbuild:{jsx:"preserve"},optimizeDeps:{include:["what-framework"]}}}}}function $e(e,f){return!!(Ce.test(e)||f.includes("/pages/")||f.includes("\\pages\\"))}function Te(e){let f=Z.basename(e,Z.extname(e));return Le.test(f)}function Ie(e){return`
383
+ `;function ae(e){let f=e.ws.send.bind(e.ws);e.ws.send=function(b){return b?.type==="error"&&b.err?.plugin==="vite-plugin-what"&&(b.err._isCompilerError=!0),f(b)}}var le="virtual:what-routes",V="\0"+le,Ce=/export\s+(?:default\s+)?function\s+([A-Z]\w*)/,ke=/(?:store|signal|state|context|util|helper|lib|config)\b/i;function Le(e={}){let{include:f=/\.[jt]sx$/,exclude:b=/node_modules/,sourceMaps:I=!0,production:_=!0,pages:T="src/pages",hot:v=!_}=e,C="",k="",$=null,d=!1;return{name:"vite-plugin-what",configResolved(m){C=m.root,k=Z.resolve(C,T),d=m.command==="serve"},configureServer(m){$=m,ae(m),m.watcher.on("add",x=>{if(x.startsWith(k)){let g=m.moduleGraph.getModuleById(V);g&&(m.moduleGraph.invalidateModule(g),m.ws.send({type:"full-reload"}))}}),m.watcher.on("unlink",x=>{if(x.startsWith(k)){let g=m.moduleGraph.getModuleById(V);g&&(m.moduleGraph.invalidateModule(g),m.ws.send({type:"full-reload"}))}})},resolveId(m){if(m===le)return V},load(m){if(m===V)return oe(k,C)},transform(m,x){if(!f.test(x)||b&&b.test(x))return null;try{let g=_e(m,{filename:x,sourceMaps:I,plugins:[[G,{production:_}]],parserOpts:{plugins:["jsx","typescript"]}});if(!g||!g.code)return null;let P=g.code;return v&&d&&!_&&$e(m,x)&&(P+=Ie(x)),{code:P,map:g.map}}catch(g){throw g.plugin="vite-plugin-what",g.id||(g.id=x),g.loc===void 0&&g._loc&&(g.loc={file:x,line:g._loc.line,column:g._loc.column}),console.error(`[what] Error transforming ${x}:`,g.message),g}},handleHotUpdate({file:m,server:x,modules:g}){if(v&&f.test(m)&&!(b&&b.test(m))&&Te(m))return x.ws.send({type:"full-reload"}),[]},config(m,{mode:x}){return{esbuild:{jsx:"preserve"},optimizeDeps:{include:["what-framework"]}}}}}function $e(e,f){return!!(Ce.test(e)||f.includes("/pages/")||f.includes("\\pages\\"))}function Te(e){let f=Z.basename(e,Z.extname(e));return ke.test(f)}function Ie(e){return`
384
384
 
385
385
  // --- What Framework HMR Boundary ---
386
386
  if (import.meta.hot) {
@@ -393,5 +393,5 @@ if (import.meta.hot) {
393
393
  }
394
394
  });
395
395
  }
396
- `}export{ke as default,ke as what};
396
+ `}export{Le as default,Le as what};
397
397
  //# sourceMappingURL=vite-plugin.min.js.map