// @bun
var{defineProperty:Y,getOwnPropertyNames:R,getOwnPropertyDescriptor:N}=Object,E=Object.prototype.hasOwnProperty;function I(j){return this[j]}var d=(j)=>{var K=(w??=new WeakMap).get(j),U;if(K)return K;if(K=Y({},"__esModule",{value:!0}),j&&typeof j==="object"||typeof j==="function"){for(var Q of R(j))if(!E.call(K,Q))Y(K,Q,{get:I.bind(j,Q),enumerable:!(U=N(j,Q))||U.enumerable})}return w.set(j,K),K},w;var S=(j)=>j;function T(j,K){this[j]=S.bind(null,K)}var c=(j,K)=>{for(var U in K)Y(j,U,{get:K[U],enumerable:!0,configurable:!0,set:T.bind(K,U)})};var m=(j,K)=>()=>(j&&(K=j(j=0)),K);var r=import.meta.require;import L from"fs";import C from"path";import{gzipSync as x}from"zlib";import{createHash as g}from"crypto";var A={".js":"js",".mjs":"js",".cjs":"js",".ts":"js",".jsx":"js",".tsx":"js",".css":"css",".scss":"css",".sass":"css",".less":"css",".html":"html",".htm":"html",".png":"image",".jpg":"image",".jpeg":"image",".gif":"image",".svg":"image",".webp":"image",".avif":"image",".ico":"image",".woff":"font",".woff2":"font",".ttf":"font",".eot":"font",".otf":"font"},k=[/node_modules/,/vendor/,/bower_components/,/\.min\./],z=[/index\.(js|ts|html)$/,/main\.(js|ts)$/,/app\.(js|ts)$/,/entry\.(js|ts)$/];async function i(j,K={}){let{includeSourceMaps:U=!1,parseImports:Q=!0,detectDuplicates:W=!0,topModulesCount:X=20}=K;if(!L.existsSync(j))throw Error(`Build directory not found: ${j}`);if(!L.statSync(j).isDirectory())throw Error(`Not a directory: ${j}`);let J=await M(j,U),q=[],V=[];for(let G of J)try{let O=await P(G,j,Q);q.push(O)}catch(O){V.push(`Failed to process ${G}: ${O}`)}let $=b(q,j),B=q.reduce((G,O)=>G+O.size,0),F=q.reduce((G,O)=>G+O.gzipSize,0),v=h(q),H=W?p(q):[],_=[...q].sort((G,O)=>O.size-G.size).slice(0,X);return{timestamp:Date.now(),buildDir:C.resolve(j),chunks:$,totalSize:B,totalGzipSize:F,moduleCount:q.length,duplicates:H,byType:v,largestModules:_,warnings:V}}async function M(j,K){let U=[];async function Q(W){let X=await L.promises.readdir(W,{withFileTypes:!0});for(let Z of X){let J=C.join(W,Z.name);if(Z.isDirectory()){if(!Z.name.startsWith(".")&&Z.name!=="node_modules")await Q(J)}else if(Z.isFile()){if(!K&&Z.name.endsWith(".map"))continue;U.push(J)}}}return await Q(j),U}async function P(j,K,U){let Q=await L.promises.readFile(j),W=C.relative(K,j),X=C.extname(j).toLowerCase(),Z=Q.length,J=f(Q),q=D(Q,X),V=g("md5").update(Q).digest("hex").slice(0,8),$=A[X]||"other",B=[];if(U&&$==="js")B.push(...y(Q.toString("utf-8")));return{id:W,path:W,size:Z,gzipSize:J,parsedSize:q,extension:X,type:$,imports:B,importedBy:[],hash:V}}function f(j){try{let K=x(j,{level:9}).length;return Math.min(K,j.length)}catch{return j.length}}function D(j,K){if(K.includes(".min.")||j.length<1000)return j.length;let Q=A[K];if(Q==="js")return Math.round(j.length*0.6);if(Q==="css")return Math.round(j.length*0.7);return j.length}function y(j){let K=[],U=/import\s+(?:[\w*{}\s,]+\s+from\s+)?['"]([^'"]+)['"]/g,Q;while((Q=U.exec(j))!==null)K.push(Q[1]);let W=/import\s*\(\s*['"]([^'"]+)['"]\s*\)/g;while((Q=W.exec(j))!==null)K.push(Q[1]);let X=/require\s*\(\s*['"]([^'"]+)['"]\s*\)/g;while((Q=X.exec(j))!==null)K.push(Q[1]);return[...new Set(K)]}function b(j,K){let U=new Map;for(let W of j){let X=W.path.split(C.sep),Z;if(X.length===1)Z=C.basename(W.path,W.extension);else Z=X[0];let J=U.get(Z)||[];J.push(W),U.set(Z,J)}let Q=[];for(let[W,X]of U){let Z=X.reduce(($,B)=>$+B.size,0),J=X.reduce(($,B)=>$+B.gzipSize,0),q=X.some(($)=>z.some((B)=>B.test($.path))),V=X.some(($)=>k.some((B)=>B.test($.path)));Q.push({name:W,files:X.map(($)=>$.path),modules:X,size:Z,gzipSize:J,isEntry:q,isVendor:V})}return Q.sort((W,X)=>X.size-W.size)}function h(j){let K={js:{count:0,size:0,gzipSize:0},css:{count:0,size:0,gzipSize:0},html:{count:0,size:0,gzipSize:0},image:{count:0,size:0,gzipSize:0},font:{count:0,size:0,gzipSize:0},other:{count:0,size:0,gzipSize:0}};for(let U of j)K[U.type].count++,K[U.type].size+=U.size,K[U.type].gzipSize+=U.gzipSize;return K}function p(j){let K=new Map;for(let Q of j){let W=K.get(Q.hash)||[];W.push(Q),K.set(Q.hash,W)}let U=[];for(let[,Q]of K)if(Q.length>1){let W=Q[0];U.push({path:W.path,count:Q.length,wastedBytes:W.size*(Q.length-1),locations:Q.map((X)=>X.path)})}return U.sort((Q,W)=>W.wastedBytes-Q.wastedBytes)}function s(j){if(j<1024)return`${j} B`;if(j<1048576)return`${(j/1024).toFixed(2)} KB`;return`${(j/1048576).toFixed(2)} MB`}function t(j){let K=j.match(/^([\d.]+)\s*(B|KB|MB|GB)?$/i);if(!K)return 0;let U=Number.parseFloat(K[1]);switch((K[2]||"B").toUpperCase()){case"KB":return U*1024;case"MB":return U*1024*1024;case"GB":return U*1024*1024*1024;default:return U}}function e(j,K){if(K===0)return"0%";return`${(j/K*100).toFixed(1)}%`}export{e as percentage,t as parseSize,s as formatBytes,i as collectBundleStats};
