what-compiler 0.6.1 → 0.6.3

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