what-compiler 0.6.1 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/babel-plugin.js +3 -0
- package/dist/babel-plugin.js.map +2 -2
- package/dist/babel-plugin.min.js +1 -1
- package/dist/babel-plugin.min.js.map +2 -2
- package/dist/index.js +3 -0
- package/dist/index.js.map +2 -2
- package/dist/index.min.js +2 -2
- package/dist/index.min.js.map +2 -2
- package/dist/vite-plugin.js +3 -0
- package/dist/vite-plugin.js.map +2 -2
- package/dist/vite-plugin.min.js +3 -3
- package/dist/vite-plugin.min.js.map +2 -2
- package/package.json +1 -1
- package/src/babel-plugin.js +7 -0
package/dist/index.min.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var xe=new Set(["preventDefault","stopPropagation","once","capture","passive","self"]),Ee=new Set(["once","capture","passive"]),be=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]),Se=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"]),te=new Set(["useSignal","signal","computed","useComputed","useState","useReducer","createResource","useSWR","useQuery","useInfiniteQuery"]);function W({types:e}){function f(n){let i=n.split("|"),r=i[0],t=i.slice(1).filter(a=>xe.has(a));return{eventName:r,modifiers:t}}function b(n){return n.startsWith("bind:")}function I(n){return n.slice(5)}function w(n){return/^[A-Z]/.test(n)}function P(n){return be.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 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),te.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 T(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=>k(t,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(t=>k(t,i,r)):e.isIdentifier(n.callee)?n.arguments.some(t=>k(t,i,r)):k(n.callee,i,r)||n.arguments.some(t=>k(t,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(t=>k(t,i,r)):e.isObjectExpression(n)?n.properties.some(t=>e.isObjectProperty(t)&&k(t.value,i,r)):e.isArrayExpression(n)?n.elements.some(t=>t&&k(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(w(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 z(n){if(e.isJSXText(n)){let s=n.value.replace(/\n\s+/g," ").trim();return s?K(s):""}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 t=`<${r}`;for(let s of i.attributes){if(e.isJSXSpreadAttribute(s))continue;let o=L(s);if(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}="${ue(s.value.value)}"`;else if(e.isJSXExpressionContainer(s.value))continue}let a=n.openingElement.selfClosing;if(a&&P(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+=K(o))}else e.isJSXExpressionContainer(s)?e.isJSXEmptyExpression(s.expression)||(t+="<!--$-->"):e.isJSXElement(s)&&(w(s.openingElement.name.name)?t+="<!--$-->":t+=z(s));return t+=`</${r}>`,t}function K(n){return n.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}function ue(n){return n.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function X(n,i){let{node:r}=n,t=r.openingElement,a=t.name.name;if(w(a))return ie(n,i);if(a==="For")return de(n,i);if(a==="Show")return me(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=L(E);return!p?.startsWith("on")&&!p?.startsWith("bind:")});if(c&&u&&S){let E=z(r);if(E){let p=re(i,E);return i.needsTemplate=!0,e.callExpression(e.identifier(p),[])}}let y=z(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,U(n,i)}let M=re(i,y);i.needsTemplate=!0;let l=i.nextVarId(),h=[e.variableDeclaration("const",[e.variableDeclarator(e.identifier(l),e.callExpression(e.identifier(M),[]))])];return ee(h,l,s,i),ne(h,l,o,r,i),i._pendingSetup||(i._pendingSetup=[]),i._pendingSetup.push(...h),e.identifier(l)}function U(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=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 ee(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=L(s);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);Se.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=>Ee.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(k(c,t.signalNames,t.importedIdentifiers)){t.needsEffect=!0;let S=e.callExpression(e.identifier("_$effect"),[e.arrowFunctionExpression([],a(u,c))]);T(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 ne(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(w(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)&&L(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),G(i,l.childIndex))]))}function M(l){return y.has(l)?e.identifier(y.get(l)):G(i,l)}for(let l of s){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]);T(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),G(i,l.childIndex))]))),ee(n,h,l.child.openingElement.attributes,a),ne(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 G(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 ie(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 _;if(e.isJSXNamespacedName(p.name)?_=`${p.name.namespace.name}:${p.name.name.name}`:_=p.name.name,_&&typeof _=="string"&&_.startsWith("client:")){let O=_.slice(7);p.value?c={type:O,value:p.value.value}:c={type:O};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 O=L(_),A=v(_.value);p.push(e.objectProperty(e.identifier(O),A))}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 A=I(_),N=p.value.expression;A==="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"))]))))):A==="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:A,modifiers:N}=f(_),he=v(p.value),ge=$(he,N);S.push(e.objectProperty(e.identifier(A),ge));continue}let O=v(p.value);S.push(e.objectProperty(/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(_)?e.identifier(_):e.stringLiteral(_),O))}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 de(n,i){let{node:r}=n,t=r.openingElement.attributes,a=r.children,s=null;for(let c of t)e.isJSXAttribute(c)&&L(c)==="each"&&(s=v(c.value));if(!s)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"),[s,o])):(console.warn("[what-compiler] <For> element missing render function child."),i.needsH=!0,U(n,i))}function me(n,i){return i.needsCreateComponent=!0,ie(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 re(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),te.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&&ye(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 ye(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(P=>f.isImportSpecifier(P)).map(P=>P.imported.name));for(let P of b)w.has(P.imported.name)||I.specifiers.push(P)}else{let w=f.importDeclaration(b,f.stringLiteral("what-framework"));e.unshiftContainer("body",w)}}import Y from"path";import{transformSync as Ce}from"@babel/core";import D from"fs";import F from"path";var se=new Set([".jsx",".tsx",".js",".ts"]),ve=new Set(["_layout","_error","_loading","_404"]);function le(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=F.join(v,$.name);if($.isDirectory()){if($.name.match(/^\((.+)\)$/)){w(d,C);continue}if($.name==="api"&&C===""){P(d,"/api");continue}w(d,C+"/"+V($.name));continue}let m=F.extname($.name);if(!se.has(m))continue;let x=F.basename($.name,m);if(x==="_layout"){b.push({filePath:d,urlPrefix:C||"/"});continue}if(ve.has(x))continue;let g=V(x),T=x==="index"?C||"/":C+"/"+g;f.push({filePath:d,routePath:oe(T),isDynamic:T.includes(":")||T.includes("*")})}}function P(v,C){if(!D.existsSync(v))return;let L=D.readdirSync(v,{withFileTypes:!0});for(let $ of L){let d=F.join(v,$.name);if($.isDirectory()){P(d,C+"/"+V($.name));continue}let m=F.extname($.name);if(!se.has(m))continue;let x=F.basename($.name,m),g=V(x),T=x==="index"?C:C+"/"+g;I.push({filePath:d,routePath:oe(T)})}}return w(e),f.sort((v,C)=>{let L=ae(v.routePath),$=ae(C.routePath);return L-$}),{pages:f,layouts:b,apiRoutes:I}}function V(e){let f=e.match(/^\[\.\.\.(\w+)\]$/);if(f)return"*"+f[1];let b=e.match(/^\[(\w+)\]$/);return b?":"+b[1]:e.toLowerCase()}function oe(e){let f=e.replace(/\/+/g,"/");return f.length>1&&f.endsWith("/")&&(f=f.slice(0,-1)),f||"/"}function ae(e){return e.includes("*")?100:e.includes(":")?10:0}function ce(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 Z(e,f){let{pages:b,layouts:I,apiRoutes:w}=le(e),P=[],v=[],C=new Map;I.forEach((d,m)=>{let x=`_layout${m}`,g=q(d.filePath,f);P.push(`import ${x} from '${g}';`),C.set(d.urlPrefix,x)}),b.forEach((d,m)=>{let x=`_page${m}`,g=q(d.filePath,f);P.push(`import ${x} from '${g}';`);let T={mode:"client"};try{let j=D.readFileSync(d.filePath,"utf-8");T=ce(j)}catch{}let k=we(d.routePath,C),J={path:d.routePath,component:x,mode:T.mode||"client",layout:k||null};v.push(J)});let L=[];return w.forEach((d,m)=>{let x=`_api${m}`,g=q(d.filePath,f);P.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","",...P,"","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
|
+
var xe=new Set(["preventDefault","stopPropagation","once","capture","passive","self"]),Ee=new Set(["once","capture","passive"]),be=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]),Se=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"]),te=new Set(["useSignal","signal","computed","useComputed","useState","useReducer","createResource","useSWR","useQuery","useInfiniteQuery"]);function W({types:e}){function f(n){let i=n.split("|"),r=i[0],t=i.slice(1).filter(a=>xe.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 P(n){return be.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),te.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 T(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 z(n){if(e.isJSXText(n)){let s=n.value.replace(/\n\s+/g," ").trim();return s?K(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}="${ue(s.value.value)}"`;else if(e.isJSXExpressionContainer(s.value))continue}let a=n.openingElement.selfClosing;if(a&&P(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+=K(o))}else e.isJSXExpressionContainer(s)?e.isJSXEmptyExpression(s.expression)||(t+="<!--$-->"):e.isJSXElement(s)&&(_(s.openingElement.name.name)?t+="<!--$-->":t+=z(s));return t+=`</${r}>`,t}function K(n){return n.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}function ue(n){return n.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function X(n,i){let{node:r}=n,t=r.openingElement,a=t.name.name;if(_(a))return ie(n,i);if(a==="For")return de(n,i);if(a==="Show")return me(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=z(r);if(E){let p=re(i,E);return i.needsTemplate=!0,e.callExpression(e.identifier(p),[])}}let y=z(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,U(n,i)}let M=re(i,y);i.needsTemplate=!0;let l=i.nextVarId(),h=[e.variableDeclaration("const",[e.variableDeclarator(e.identifier(l),e.callExpression(e.identifier(M),[]))])];return ee(h,l,s,i),ne(h,l,o,r,i),i._pendingSetup||(i._pendingSetup=[]),i._pendingSetup.push(...h),e.identifier(l)}function U(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 ee(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);Se.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=>Ee.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))]);T(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 ne(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),G(i,l.childIndex))]))}function M(l){return y.has(l)?e.identifier(y.get(l)):G(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]);T(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),G(i,l.childIndex))]))),ee(n,h,l.child.openingElement.attributes,a),ne(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 G(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 ie(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 O=w.slice(7);p.value?c={type:O,value:p.value.value}:c={type:O};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 O=k(w),A=v(w.value);p.push(e.objectProperty(e.identifier(O),A))}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 A=I(w),N=p.value.expression;A==="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"))]))))):A==="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:A,modifiers:N}=f(w),he=v(p.value),ge=$(he,N);S.push(e.objectProperty(e.identifier(A),ge));continue}let O=v(p.value);S.push(e.objectProperty(/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(w)?e.identifier(w):e.stringLiteral(w),O))}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 de(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,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"),[s,o])):(console.warn("[what-compiler] <For> element missing render function child."),i.needsH=!0,U(n,i))}function me(n,i){return i.needsCreateComponent=!0,ie(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 re(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),te.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&&ye(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 ye(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(P=>f.isImportSpecifier(P)).map(P=>P.imported.name));for(let P of b)_.has(P.imported.name)||I.specifiers.push(P)}else{let _=f.importDeclaration(b,f.stringLiteral("what-framework"));e.unshiftContainer("body",_)}}import Y from"path";import{transformSync as Ce}from"@babel/core";import D from"fs";import F from"path";var se=new Set([".jsx",".tsx",".js",".ts"]),ve=new Set(["_layout","_error","_loading","_404"]);function le(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=F.join(v,$.name);if($.isDirectory()){if($.name.match(/^\((.+)\)$/)){_(d,C);continue}if($.name==="api"&&C===""){P(d,"/api");continue}_(d,C+"/"+V($.name));continue}let m=F.extname($.name);if(!se.has(m))continue;let x=F.basename($.name,m);if(x==="_layout"){b.push({filePath:d,urlPrefix:C||"/"});continue}if(ve.has(x))continue;let g=V(x),T=x==="index"?C||"/":C+"/"+g;f.push({filePath:d,routePath:oe(T),isDynamic:T.includes(":")||T.includes("*")})}}function P(v,C){if(!D.existsSync(v))return;let k=D.readdirSync(v,{withFileTypes:!0});for(let $ of k){let d=F.join(v,$.name);if($.isDirectory()){P(d,C+"/"+V($.name));continue}let m=F.extname($.name);if(!se.has(m))continue;let x=F.basename($.name,m),g=V(x),T=x==="index"?C:C+"/"+g;I.push({filePath:d,routePath:oe(T)})}}return _(e),f.sort((v,C)=>{let k=ae(v.routePath),$=ae(C.routePath);return k-$}),{pages:f,layouts:b,apiRoutes:I}}function V(e){let f=e.match(/^\[\.\.\.(\w+)\]$/);if(f)return"*"+f[1];let b=e.match(/^\[(\w+)\]$/);return b?":"+b[1]:e.toLowerCase()}function oe(e){let f=e.replace(/\/+/g,"/");return f.length>1&&f.endsWith("/")&&(f=f.slice(0,-1)),f||"/"}function ae(e){return e.includes("*")?100:e.includes(":")?10:0}function ce(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 Z(e,f){let{pages:b,layouts:I,apiRoutes:_}=le(e),P=[],v=[],C=new Map;I.forEach((d,m)=>{let x=`_layout${m}`,g=q(d.filePath,f);P.push(`import ${x} from '${g}';`),C.set(d.urlPrefix,x)}),b.forEach((d,m)=>{let x=`_page${m}`,g=q(d.filePath,f);P.push(`import ${x} from '${g}';`);let T={mode:"client"};try{let j=D.readFileSync(d.filePath,"utf-8");T=ce(j)}catch{}let L=we(d.routePath,C),J={path:d.routePath,component:x,mode:T.mode||"client",layout:L||null};v.push(J)});let k=[];return _.forEach((d,m)=>{let x=`_api${m}`,g=q(d.filePath,f);P.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","",...P,"","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"/"+F.relative(f,e).split(F.sep).join("/")}function we(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 _e=`
|
|
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 fe(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 pe="virtual:what-routes",B="\0"+pe,
|
|
383
|
+
`;function fe(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 pe="virtual:what-routes",B="\0"+pe,ke=/export\s+(?:default\s+)?function\s+([A-Z]\w*)/,Le=/(?:store|signal|state|context|util|helper|lib|config)\b/i;function Q(e={}){let{include:f=/\.[jt]sx$/,exclude:b=/node_modules/,sourceMaps:I=!0,production:_=!0,pages:P="src/pages",hot:v=!_}=e,C="",k="",$=null,d=!1;return{name:"vite-plugin-what",configResolved(m){C=m.root,k=Y.resolve(C,P),d=m.command==="serve"},configureServer(m){$=m,fe(m),m.watcher.on("add",x=>{if(x.startsWith(k)){let g=m.moduleGraph.getModuleById(B);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(B);g&&(m.moduleGraph.invalidateModule(g),m.ws.send({type:"full-reload"}))}})},resolveId(m){if(m===pe)return B},load(m){if(m===B)return Z(k,C)},transform(m,x){if(!f.test(x)||b&&b.test(x))return null;try{let g=Ce(m,{filename:x,sourceMaps:I,plugins:[[W,{production:_}]],parserOpts:{plugins:["jsx","typescript"]}});if(!g||!g.code)return null;let T=g.code;return v&&d&&!_&&$e(m,x)&&(T+=Ie(x)),{code:T,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))&&Pe(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!!(ke.test(e)||f.includes("/pages/")||f.includes("\\pages\\"))}function Pe(e){let f=Y.basename(e,Y.extname(e));return Le.test(f)}function Ie(e){return`
|
|
384
384
|
|
|
385
385
|
// --- What Framework HMR Boundary ---
|
|
386
386
|
if (import.meta.hot) {
|