UNPKG

build-sizes

Version:

A small script that provides build sizes to assist with optimization

35 lines (26 loc) 3.81 kB
#!/usr/bin/env node var b=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var v=Object.getOwnPropertyNames;var D=Object.prototype.hasOwnProperty;var E=(e,t)=>{for(var n in t)b(e,n,{get:t[n],enumerable:!0})},$=(e,t,n,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of v(t))!D.call(e,o)&&o!==n&&b(e,o,{get:()=>t[o],enumerable:!(i=O(t,o))||i.enumerable});return e};var T=e=>$(b({},"__esModule",{value:!0}),e);var R={};E(R,{filterFilesByType:()=>S,formatBytes:()=>M,getBuildSizes:()=>G,getFileSizeBrotli:()=>k,getFileSizeGzip:()=>F,getFiles:()=>z,help:()=>l,saveBuildSizes:()=>I});module.exports=T(R);var a=require("path"),r=require("fs/promises"),d=require("util"),B=require("child_process"),f=require("zlib"),j=(0,d.promisify)(f.gzip),x=(0,d.promisify)(f.brotliCompress),C=(0,d.promisify)(B.exec);function M(e,t=2,n=!1){try{if(!e)return"0 B";let i=n?1024:1e3,o=Math.floor(n?Math.log10(e)/3:Math.log2(e)/10),s=(e/Math.pow(i,o)).toFixed(t),u=`${"KMGTPEZY"[o-1]||""}B`;return`${s} ${u}`}catch(i){l(i,` Occurred while formatting bytes. Double check the inputs:`,` bytes:`,e,` decimals:`,t,` binary:`,n)}}async function z(e){try{let t=await(0,r.readdir)(e,{withFileTypes:!0}),n=[];for(let i of t)if(i.isDirectory()){let o=await z((0,a.resolve)(e,i.name));n.push(...o)}else{let o=(0,a.resolve)(e,i.name),{size:s}=await(0,r.stat)(o);n.push({name:i.name,path:o,size:s})}return Promise.all(n)}catch(t){t.code==="ENOENT"?l(`Error: Could not find build at specified path: `,e):l(t,` Occurred while finding build files.`,`Double check the file path: `,e)}}var S=(e,t)=>e.filter(n=>new RegExp(`.${t}$`,"i").test(n.name)),F=e=>(0,r.readFile)(e).then(j).then(t=>t.length).catch(t=>l(t,` Occurred while getting gzipped file size.`,`Double check the file path: `,e)),k=e=>(0,r.readFile)(e).then(x).then(t=>t.length).catch(t=>l(t,` Occurred while getting brotli compressed file size.`,`Double check the file path: `,e));async function G(e,t="js"){try{let n=(0,a.resolve)(process.cwd(),e),i=await z(n),o=S(i,t),s=o.length?o.reduce((h,p)=>h.size>p.size?h:p):null,u=s?s.size:0,m=s?s.name:"Not found",g=s?await F(s.path):0,c=s?await k(s.path):0,w=i.reduce((h,p)=>h+p.size,0),y=process.platform!=="win32"?Number((await C(`du -sb ${n} | cut -f1`)).stdout.trim()):NaN,N=i.length;return{mainBundleName:m,mainBundleSize:u,mainBundleSizeGzip:g,mainBundleSizeBrotli:c,buildSize:w,buildSizeOnDisk:y,buildFileCount:N}}catch(n){l(n,` Occurred while getting build sizes.`,"Double check the inputs:",` build path:`,(0,a.resolve)(e),` bundle filetype:`,t)}}async function I(e,t){try{let n=(0,a.resolve)(t),i="";try{i=JSON.parse(await(0,r.readFile)("package.json","utf8")).version}catch(c){c.code==="ENOENT"&&c.path==="package.json"&&console.warn(`No package.json file found in the current working directory. The package version will not be specified. `)}let o=new Intl.DateTimeFormat("default",{dateStyle:"short",timeStyle:"long"}).format(Date.now()).replace(","," at"),s=[i?"Version,":"","Timestamp"],u=[i?`${i},`:"",o];for(let[c,w]of Object.entries(e))s.push(c),u.push(w);s.push("(File sizes in bytes)");let m=`${s.join(",")} `,g=`${u.join(",")} `;try{await(0,r.writeFile)(n,m,{flag:"wx"})}catch(c){c.code!=="EEXIST"&&l(c,` Occurred while saving build sizes.`,`Double check the output path: `,(0,a.resolve)(t))}await(0,r.appendFile)(n,g)}catch(n){l(n,` Error saving build sizes to CSV.`)}}function l(...e){e&&console.error(...e),console.error(` Add the -h or --help flag for usage information when on the CLI. Read the documentation for assistance with the exported functions: https://benelan.github.io/build-sizes/global.html `),process.exit(1)}0&&(module.exports={filterFilesByType,formatBytes,getBuildSizes,getFileSizeBrotli,getFileSizeGzip,getFiles,help,saveBuildSizes});