// @bun
var{defineProperty:$,getOwnPropertyNames:H,getOwnPropertyDescriptor:T}=Object,B=Object.prototype.hasOwnProperty;function F(A){return this[A]}var y=(A)=>{var q=(D??=new WeakMap).get(A),E;if(q)return q;if(q=$({},"__esModule",{value:!0}),A&&typeof A==="object"||typeof A==="function"){for(var G of H(A))if(!B.call(q,G))$(q,G,{get:F.bind(A,G),enumerable:!(E=T(A,G))||E.enumerable})}return D.set(A,q),q},D;var k=(A)=>A;function M(A,q){this[A]=k.bind(null,q)}var m=(A,q)=>{for(var E in q)$(A,E,{get:q[E],enumerable:!0,configurable:!0,set:M.bind(q,E)})};var j=(A,q)=>()=>(A&&(q=A(A=0)),q);var O=import.meta.require;import{createHash as z}from"crypto";import{existsSync as X,mkdirSync as x,readFileSync as w,writeFileSync as C,readdirSync as S,statSync as g}from"fs";import{basename as Y,dirname as W,extname as Z,join as V,relative as I}from"path";import{gzipSync as b}from"zlib";class N{config;startTime=0;outputs=[];chunks=[];assets=[];warnings=[];errors=[];moduleGraph=new Map;assetHashes=new Map;constructor(A){this.config={...v,...A}}async build(){this.startTime=Date.now(),this.outputs=[],this.chunks=[],this.assets=[],this.warnings=[],this.errors=[];try{this.ensureDir(this.config.outDir),await this.runPluginHook("setup"),await this.runPluginHook("buildStart");let A=this.collectEntries();for(let[E,G]of A)await this.buildEntry(E,G);if(this.config.css?.extract)await this.extractCss();if(await this.processAssets(),this.config.splitting)await this.performCodeSplitting();if(this.config.minify)await this.minifyOutputs();if(this.config.sourcemaps)await this.generateSourceMaps();if(await this.fingerprintAssets(),this.config.compression)await this.compressOutputs();if(this.config.analyze)await this.analyzeBundle();let q=this.getResult();return await this.runPluginHook("buildEnd",q),q}catch(A){return this.errors.push(String(A)),this.getResult()}}collectEntries(){let A=new Map,{entry:q}=this.config;if(typeof q==="string")A.set("main",q);else if(Array.isArray(q))q.forEach((E,G)=>{let J=Y(E,Z(E));A.set(J,E)});else if(typeof q==="object")Object.entries(q).forEach(([E,G])=>{A.set(E,G)});return A}async buildEntry(A,q){if(!await Bun.file(q).exists()){this.errors.push(`Entry point not found: ${q}`);return}let E=await Bun.file(q).text(),G=Z(q),J=E,L="js";if(G===".stx"||G===".html")L="html",J=await this.processTemplate(E,q);else if(G===".css")L="css",J=await this.processCss(E,q);else if(G===".js"||G===".ts")L="js",J=await this.processScript(E,q);J=await this.runPluginTransform(J,q);let K=this.generateHash(J),Q=this.getOutputName(A,L,K),R=V(this.config.outDir,Q);this.ensureDir(W(R)),await Bun.write(R,J),this.outputs.push({path:R,type:L,size:Buffer.byteLength(J),hash:K,sources:[q],isEntry:!0})}resolveRelativePath(A,q){if(q.startsWith("/"))return V(W(A),"..",q);return V(W(A),q)}async processTemplate(A,q){let E=A,G=[],J=[];if(E=E.replace(/<script\b([^>]*)\bstx-inline\b([^>]*)(?:><\/script>|><\/script>|\/>)/gi,(L,K,Q)=>{let R=(K+Q).match(/src=["']([^"']+)["']/);if(R){let U=this.resolveRelativePath(q,R[1]);if(X(U)){let _=w(U,"utf-8");G.push(`// Source: ${R[1]}
${_}`)}else this.warnings.push(`External script not found: ${U}`)}return""}),E=E.replace(/<link\b([^>]*)\bstx-inline\b([^>]*)(?:\/>|>)/gi,(L,K,Q)=>{let R=(K+Q).match(/href=["']([^"']+)["']/);if(R){let U=this.resolveRelativePath(q,R[1]);if(X(U)){let _=w(U,"utf-8");J.push(`/* Source: ${R[1]} */
${_}`)}else this.warnings.push(`External stylesheet not found: ${U}`)}return""}),E=E.replace(/<script\b(?![^>]*\bstx-inline\b)([^>]*)>([\s\S]*?)<\/script>/gi,(L,K,Q)=>{if(/\bsrc=/.test(K))return L;if(Q.trim())G.push(Q);return""}),E=E.replace(/<style\b[^>]*>([\s\S]*?)<\/style>/gi,(L,K)=>{if(K.trim())J.push(K);return""}),G.length>0){let L=G.join(`

`),K=this.generateHash(L).slice(0,8),Q=`assets/app.${K}.js`,R=V(this.config.outDir,Q);this.ensureDir(W(R)),await Bun.write(R,L),this.outputs.push({path:R,type:"js",size:Buffer.byteLength(L),hash:K,sources:[q]});let U=E.lastIndexOf("</body>");if(U!==-1)E=E.slice(0,U)+`<script src="${this.config.publicPath}${Q}"></script>
`+E.slice(U)}if(J.length>0){let L=J.join(`

`),K=this.generateHash(L).slice(0,8),Q=`assets/app.${K}.css`,R=V(this.config.outDir,Q);this.ensureDir(W(R)),await Bun.write(R,L),this.outputs.push({path:R,type:"css",size:Buffer.byteLength(L),hash:K,sources:[q]}),E=E.replace("</head>",`<link rel="stylesheet" href="${this.config.publicPath}${Q}">
</head>`)}return E}async processCss(A,q){let E=A;if(typeof this.config.minify==="object"&&this.config.minify.css)E=this.minifyCss(E);return E}async processScript(A,q){let E=A;if(this.config.define)for(let[G,J]of Object.entries(this.config.define))E=E.replace(new RegExp(G.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),"g"),J);return E}async extractCss(){let A=[];for(let L of this.outputs)if(L.type==="css"){let K=await Bun.file(L.path).text();A.push(K)}if(A.length===0)return;let q=A.join(`
`),E=this.generateHash(q).slice(0,8),G=(this.config.css?.filename||"assets/[name].[hash].css").replace("[name]","styles").replace("[hash]",E),J=V(this.config.outDir,G);this.ensureDir(W(J)),await Bun.write(J,q),this.outputs.push({path:J,type:"css",size:Buffer.byteLength(q),hash:E})}async processAssets(){let A=V(W(this.config.outDir),"public");if(!X(A))return;let q=this.config.assets||{},E=q.include||[];this.walkDir(A,(G)=>{let J=Z(G).slice(1).toLowerCase();if(!E.includes(J))return;let L=w(G),K=L.length;if(q.inlineLimit&&K<q.inlineLimit)return;let Q=q.hash?this.generateHash(L).slice(0,q.hashLength||8):"",R=(q.filename||"assets/[name].[hash][ext]").replace("[name]",Y(G,Z(G))).replace("[hash]",Q).replace("[ext]",Z(G)),U=V(this.config.outDir,R);this.ensureDir(W(U)),C(U,L),this.assetHashes.set(G,R),this.assets.push({name:Y(G),file:R,size:K,source:G}),this.outputs.push({path:U,type:"asset",size:K,hash:Q,sources:[G]})})}async performCodeSplitting(){if((typeof this.config.splitting==="object"?this.config.splitting:{}).vendor){let q=[];this.chunks.push({name:"vendor",file:"assets/vendor.js",size:0,modules:q,isVendor:!0,imports:[],dynamicImports:[]})}}async minifyOutputs(){let A=typeof this.config.minify==="object"?this.config.minify:{};for(let q of this.outputs){let E=await Bun.file(q.path).text(),G=E;if(q.type==="js"&&A.js)G=this.minifyJs(E);else if(q.type==="css"&&A.css)G=this.minifyCss(E);else if(q.type==="html"&&A.html)G=this.minifyHtml(E);if(G!==E)await Bun.write(q.path,G),q.size=Buffer.byteLength(G)}}minifyJs(A){let q=typeof this.config.minify==="object"?this.config.minify:{},E=A;if(E=E.replace(/\/\*[\s\S]*?\*\//g,""),E=E.replace(/\/\/.*$/gm,""),q.dropConsole)E=E.replace(/console\.(log|debug|info|warn)\([^)]*\);?/g,"");if(q.dropDebugger)E=E.replace(/debugger;?/g,"");return E=E.replace(/\s+/g," "),E=E.replace(/\s*([{};,:])\s*/g,"$1"),E=E.replace(/;\}/g,"}"),E.trim()}minifyCss(A){let q=A;return q=q.replace(/\/\*[\s\S]*?\*\//g,""),q=q.replace(/\s+/g," "),q=q.replace(/\s*([{};:,>+~])\s*/g,"$1"),q=q.replace(/;}/g,"}"),q=q.replace(/[^{}]+\{\s*\}/g,""),q.trim()}minifyHtml(A){let q=A;return q=q.replace(/<!--(?!\[if)[\s\S]*?-->/g,""),q=q.replace(/>\s+</g,"><"),q=q.replace(/\s+>/g,">"),q=q.replace(/<\s+/g,"<"),q=q.replace(/\s{2,}/g," "),q.trim()}async generateSourceMaps(){if(!this.config.sourcemaps)return;for(let A of this.outputs)if(A.type==="js"||A.type==="css"){let q=await Bun.file(A.path).text(),E=[];for(let Q of A.sources||[])if(await Bun.file(Q).exists())E.push(await Bun.file(Q).text());else E.push(null);let G={version:3,file:Y(A.path),sources:A.sources||[],sourcesContent:E,mappings:"",names:[]},J=`${A.path}.map`;await Bun.write(J,JSON.stringify(G));let L=new Uint8Array([10,47,47,35,32,115,111,117,114,99,101,77,97,112,112,105,110,103,85,82,76,61]),K=this.config.sourcemaps==="inline"?`data:application/json;base64,${Buffer.from(JSON.stringify(G)).toString("base64")}`:Y(J);if(this.config.sourcemaps!=="hidden")await Bun.write(A.path,Buffer.concat([Buffer.from(q),Buffer.from(L),Buffer.from(K)]));this.outputs.push({path:J,type:"map",size:g(J).size})}}async fingerprintAssets(){for(let A of this.outputs)if(A.type==="html"){let q=await Bun.file(A.path).text();for(let[E,G]of this.assetHashes){let J=I(W(A.path),E);q=q.replace(new RegExp(J.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),"g"),`${this.config.publicPath}${G}`)}await Bun.write(A.path,q)}}async compressOutputs(){let A=this.config.compression||{},q=A.threshold||1024;for(let E of this.outputs){if(E.type==="map")continue;if(E.size<q)continue;let G=Buffer.from(await Bun.file(E.path).arrayBuffer());if(A.gzip){let J=b(G,{level:9}),L=`${E.path}.gz`;await Bun.write(L,J),E.gzipSize=J.length}if(A.brotli)try{let{brotliCompressSync:J}=await import("zlib"),L=J(G);await Bun.write(`${E.path}.br`,L)}catch{}}}async analyzeBundle(){let A=typeof this.config.analyze==="object"?this.config.analyze:{},q=A.format||"text",E={outputs:this.outputs.map((K)=>({path:I(this.config.outDir,K.path),type:K.type,size:K.size,gzipSize:K.gzipSize,sizeFormatted:this.formatSize(K.size),gzipSizeFormatted:K.gzipSize?this.formatSize(K.gzipSize):void 0})),chunks:this.chunks,assets:this.assets,totalSize:this.outputs.reduce((K,Q)=>K+Q.size,0),totalGzipSize:this.outputs.reduce((K,Q)=>K+(Q.gzipSize||0),0)},G;if(q==="json")G=JSON.stringify(E,null,2);else if(q==="html")G=this.generateAnalysisHtml(E);else G=this.generateAnalysisText(E);let J=A.filename||`bundle-analysis.${q==="html"?"html":q==="json"?"json":"txt"}`,L=V(this.config.outDir,J);await Bun.write(L,G),console.log(`
\uD83D\uDCCA Bundle Analysis`),console.log("\u2500".repeat(50));for(let K of E.outputs){let Q=K.gzipSizeFormatted?` (gzip: ${K.gzipSizeFormatted})`:"";console.log(`  ${K.path}: ${K.sizeFormatted}${Q}`)}console.log("\u2500".repeat(50)),console.log(`  Total: ${this.formatSize(E.totalSize)} (gzip: ${this.formatSize(E.totalGzipSize)})`),console.log("")}generateAnalysisHtml(A){return`<!DOCTYPE html>
<html>
<head>
  <title>Bundle Analysis</title>
  <style>
    body { font-family: system-ui, sans-serif; max-width: 1200px; margin: 0 auto; padding: 20px; }
    h1 { color: #333; }
    table { width: 100%; border-collapse: collapse; margin: 20px 0; }
    th, td { padding: 12px; text-align: left; border-bottom: 1px solid #ddd; }
    th { background: #f5f5f5; }
    .size { font-family: monospace; text-align: right; }
    .bar { height: 20px; background: #4CAF50; border-radius: 4px; }
    .summary { background: #e3f2fd; padding: 20px; border-radius: 8px; margin: 20px 0; }
  </style>
</head>
<body>
  <h1>\uD83D\uDCCA Bundle Analysis</h1>
  <div class="summary">
    <strong>Total Size:</strong> ${this.formatSize(A.totalSize)}<br>
    <strong>Gzipped:</strong> ${this.formatSize(A.totalGzipSize)}<br>
    <strong>Files:</strong> ${A.outputs.length}
  </div>
  <table>
    <thead>
      <tr><th>File</th><th>Type</th><th class="size">Size</th><th class="size">Gzip</th><th>%</th></tr>
    </thead>
    <tbody>
      ${A.outputs.map((q)=>`
        <tr>
          <td>${q.path}</td>
          <td>${q.type}</td>
          <td class="size">${q.sizeFormatted}</td>
          <td class="size">${q.gzipSizeFormatted||"-"}</td>
          <td><div class="bar" style="width: ${Math.round(q.size/A.totalSize*100)}%"></div></td>
        </tr>
      `).join("")}
    </tbody>
  </table>
</body>
</html>`}generateAnalysisText(A){let q=["Bundle Analysis","=".repeat(50),""];for(let E of A.outputs)q.push(`${E.path}`),q.push(`  Size: ${E.sizeFormatted}${E.gzipSizeFormatted?` (gzip: ${E.gzipSizeFormatted})`:""}`),q.push("");return q.push("=".repeat(50)),q.push(`Total: ${this.formatSize(A.totalSize)} (gzip: ${this.formatSize(A.totalGzipSize)})`),q.join(`
`)}async runPluginHook(A,q){if(!this.config.plugins)return;for(let E of this.config.plugins){let G=E[A];if(typeof G==="function")await G.call(E,q||this)}}async runPluginTransform(A,q){if(!this.config.plugins)return A;let E=A;for(let G of this.config.plugins)if(G.transform){let J=await G.transform(E,q);if(J!=null)E=J}return E}generateHash(A){return z("sha256").update(A).digest("hex")}getOutputName(A,q,E){let G=q==="html"?".html":q==="css"?".css":q==="js"?".js":"";return`${A}.${E.slice(0,8)}${G}`}formatSize(A){if(A<1024)return`${A} B`;if(A<1048576)return`${(A/1024).toFixed(2)} KB`;return`${(A/1048576).toFixed(2)} MB`}ensureDir(A){if(!X(A))x(A,{recursive:!0})}walkDir(A,q){if(!X(A))return;let E=S(A,{withFileTypes:!0});for(let G of E){let J=V(A,G.name);if(G.isDirectory())this.walkDir(J,q);else q(J)}}getResult(){let A=Date.now()-this.startTime,q=this.outputs.reduce((G,J)=>G+J.size,0),E=this.outputs.reduce((G,J)=>G+(J.gzipSize||0),0);return{outputs:this.outputs,duration:A,totalSize:q,gzipSize:E,chunks:this.chunks,assets:this.assets,warnings:this.warnings,errors:this.errors}}}async function r(A){return new N(A).build()}function u(A){return new N(A)}function a(A){return{...v,...A}}var v;var P=j(()=>{v={outDir:"dist",publicPath:"/",sourcemaps:!0,minify:{js:!0,css:!0,html:!0,dropConsole:!0,dropDebugger:!0,mangle:!0},splitting:{vendor:!0,minSize:20000,maxSize:250000,chunkNames:"deterministic"},css:{extract:!0,minify:!0,filename:"assets/[name].[hash].css"},assets:{inlineLimit:4096,filename:"assets/[name].[hash][ext]",hash:!0,hashLength:8,include:["png","jpg","jpeg","gif","svg","webp","woff","woff2","ttf","eot"]},compression:{gzip:!0,brotli:!0,threshold:1024,deleteOriginal:!1}}});P();export{a as defineConfig,u as createProductionBuild,r as build,N as ProductionBuild};
