// @bun
var{defineProperty:O,getOwnPropertyNames:b,getOwnPropertyDescriptor:g}=Object,f=Object.prototype.hasOwnProperty;function v(z){return this[z]}var o=(z)=>{var H=(T??=new WeakMap).get(z),W;if(H)return H;if(H=O({},"__esModule",{value:!0}),z&&typeof z==="object"||typeof z==="function"){for(var M of b(z))if(!f.call(H,M))O(H,M,{get:v.bind(z,M),enumerable:!(W=g(z,M))||W.enumerable})}return T.set(z,H),H},T;var m=(z)=>z;function u(z,H){this[z]=m.bind(null,H)}var h=(z,H)=>{for(var W in H)O(z,W,{get:H[W],enumerable:!0,configurable:!0,set:u.bind(H,W)})};var j=(z,H)=>()=>(z&&(H=z(z=0)),H);var s=import.meta.require;import N from"process";function x(z="STX_PUBLIC_"){let H={},W={};for(let[M,$]of Object.entries(N.env)){if(!M.startsWith(z)||$===void 0)continue;H[`import.meta.env.${M}`]=JSON.stringify($),W[M]=$}return H["import.meta.env"]=JSON.stringify(W),H}function r(z="STX_PUBLIC_"){let H={};for(let[W,M]of Object.entries(N.env))if(W.startsWith(z)&&M!==void 0)H[W]=M;return H}var F=()=>{};var i={};h(i,{hasUserImports:()=>n,bundleClientScript:()=>p});import Y from"path";import U from"fs";function n(z){let H=/^\s*import\s+(?!type\s)/gm;if(!z.match(H))return!1;let M=/^\s*import\s+(?!type\s).*?\s+from\s+['"]([^'"]+)['"]/gm,$;while(($=M.exec(z))!==null){let K=$[1];if(!y.some((Q)=>Q.test(K)))return console.log("[stx:bundler] detected user import:",K),!0}let C=/^\s*import\s+['"]([^'"]+)['"]/gm;while(($=C.exec(z))!==null){let K=$[1];if(/\.css(?:\?.*)?$/.test(K))continue;if(!y.some((Q)=>Q.test(K)))return console.log("[stx:bundler] detected side-effect import:",K),!0}return!1}function c(z,H,W,M){let $=(C,K)=>{let J=C===W?H:Y.dirname(C),Q=Y.resolve(J,K),q=[Q,`${Q}.ts`,`${Q}.tsx`,`${Q}.js`,`${Q}.mjs`,`${Q}.jsx`,Y.join(Q,"index.ts"),Y.join(Q,"index.tsx"),Y.join(Q,"index.js"),Y.join(Q,"index.mjs")];for(let _ of q)try{if(U.existsSync(_)&&U.statSync(_,{throwIfNoEntry:!1})?.isFile())return _}catch{}return Q};return{name:"stx-client-bundle",setup(C){C.onResolve({filter:/^(stx|@stacksjs\/stx|@stacksjs\/browser)$/},(K)=>({path:K.path,external:!0})),C.onResolve({filter:/^(@stores|@composables|stx\/stores)$/},(K)=>({path:K.path,external:!0})),C.onResolve({filter:/^\.\.?\//},(K)=>{let J=$(K.importer,K.path);return M.add(J),{path:J}}),C.onResolve({filter:/^[@~]\//},(K)=>{let J=Y.resolve(z,K.path.slice(2)),Q=[J,`${J}.ts`,`${J}.tsx`,`${J}.js`,`${J}.mjs`,`${J}.jsx`,Y.join(J,"index.ts"),Y.join(J,"index.tsx"),Y.join(J,"index.js"),Y.join(J,"index.mjs")];for(let q of Q)if(U.existsSync(q)&&U.statSync(q,{throwIfNoEntry:!1})?.isFile())return console.log(`[stx:bundler] resolved ${K.path[0]}/ import:`,K.path,"\u2192",q),M.add(q),{path:q};return console.warn(`[stx:bundler] could not resolve ${K.path[0]}/ import:`,K.path),{path:J}})}}}async function p(z,H,W={}){let M=W.projectRoot||process.cwd(),$=W.minify??!1,C=W.cacheDir||Y.join(M,".stx","bundle-cache"),K=new Bun.CryptoHasher("md5");K.update(z+H);let J=K.digest("hex").slice(0,12),Q=Y.join(C,`${J}.js`),q=Y.join(C,`${J}.deps.json`),_=Bun.file(Q);if(await _.exists()){let G=Bun.file(q),Z=!0;if(await G.exists())try{let B=JSON.parse(await G.text());for(let I of B.files)try{if(U.statSync(I.path).mtimeMs!==I.mtimeMs){Z=!1;break}}catch{Z=!1;break}}catch{Z=!1}if(Z)return console.log("[stx:bundler] cache hit:",J),await _.text();console.log("[stx:bundler] cache invalidated by dep change:",J)}let A=Y.join(M,".stx","bundle-tmp"),S=Y.join(A,`${J}.ts`),V=Y.join(A,"out",J);U.mkdirSync(A,{recursive:!0}),U.mkdirSync(V,{recursive:!0});let D=Y.dirname(H),L=[],R=/^(export\s+)?(?:const|let|var)\s+(?:\{([^}]+)\}|(\w+))/gm,E=/^(export\s+)?(?:async\s+)?function\s+(\w+)/gm,w;while((w=R.exec(z))!==null){if(w[1])continue;if(w[2])w[2].split(",").forEach((G)=>{let Z=G.split(":")[0].trim();if(Z)L.push(Z)});else if(w[3])L.push(w[3])}while((w=E.exec(z))!==null){if(w[1])continue;L.push(w[2])}let P=L.length>0?`
export { ${L.join(", ")} }`:"";await Bun.write(S,z+P),console.log("[stx:bundler] bundling:",J,"from:",Y.basename(H));let k=new Set;try{let G=await Bun.build({entrypoints:[S],outdir:V,target:"browser",format:"esm",minify:$,plugins:[c(M,D,S,k)],define:{"process.env.NODE_ENV":$?'"production"':'"development"',...x()},root:M});if(!G.success){let I=G.logs.filter((X)=>X.level==="error").map((X)=>X.message).join(", ");return console.warn("[stx:bundler] build failed:",I),z}let Z="";for(let I of G.outputs){Z=await I.text();break}Z=Z.replace(/^\s*export\s*\{[^}]*\}\s*;?\s*$/gm,"").replace(/^\s*export\s+default\s+.*$/gm,"").trim(),console.log("[stx:bundler] bundled:",J,"output:",Z.length,"bytes"),U.mkdirSync(C,{recursive:!0}),await Bun.write(Q,Z);let B=Array.from(k).map((I)=>{try{return{path:I,mtimeMs:U.statSync(I).mtimeMs}}catch{return null}}).filter((I)=>I!==null);return await Bun.write(q,JSON.stringify({files:B})),Z}catch(G){let Z=G instanceof Error?G.message:String(G),B=G?.errors,I=Array.isArray(B)&&B.length>0?`
  ${B.map((X)=>`- ${X?.message??X}`).join(`
  `)}`:"";return console.warn(`[stx:bundler] error: ${Z}${I}`),z}finally{try{U.rmSync(S,{force:!0}),U.rmSync(V,{recursive:!0,force:!0})}catch{}}}var y;var l=j(()=>{F();y=[/^stx$/,/^@stacksjs\/stx$/,/^@stacksjs\/browser$/,/^@stores$/,/^stx\/stores$/,/^@composables$/]});l();export{n as hasUserImports,p as bundleClientScript};
