UNPKG

astro-compress

Version:
2 lines (1 loc) 2.88 kB
let d;const{default:w}=await import("../Variable/Option.js"),{default:{Cache:{Search:J}}}=await import("@playform/pipe/Target/Variable/Option.js"),{default:n}=await import("./Merge.js");let y;var N=((...[p={}])=>{Object.entries(p).forEach(([e,r])=>Object.defineProperty(p,e,{value:r===!0?w[e]:p[e]}));const{Path:i,Cache:m,Logger:u,Map:g,Exclude:b,Action:S,CSS:j,HTML:O,Image:c,JavaScript:A,SVG:I,Parser:h,JSON:P}=n(w,p),f=new Set;return typeof i<"u"&&((Array.isArray(i)||i instanceof Set)&&i.forEach(e=>f.add(e)),i instanceof Map&&f.add(i)),typeof h=="object"&&Object.entries(h).forEach(([e,r])=>Object.defineProperty(h,e,{value:Array.isArray(r)?r:[r]})),{name:"astro-compress",hooks:{"astro:config:done":async({config:{outDir:{pathname:e}}})=>{d=(await import("node:path")).parse(e).dir.replace(/\\/g,"/"),d.startsWith("/")&&(d=d.substring(1))},"astro:build:done":async({dir:e})=>{if(typeof g=="object"){f.size===0&&f.add(e),typeof m=="object"&&m.Search===J&&(m.Search=e.toString());for(const[r,o]of Object.entries({CSS:j,HTML:O,Image:c,JavaScript:A,SVG:I,JSON:P}))if(!(!(o&&g[r])||typeof o!="object"&&r!=="JSON")){y=n(S,n(S,{Wrote:async({Buffer:t,Input:s})=>{switch(r){case"CSS":{let a=t.toString();try{o.lightningcss&&(a=(await import("lightningcss")).transform(n({code:(await import("node:buffer")).Buffer.from(a),filename:s},o.lightningcss)).code.toString()),o.csso&&(a=(await import("csso")).minify(a,o.csso).css)}catch(l){console.log(l)}return a}case"HTML":return await(await import("html-minifier-terser")).minify(t.toString(),o["html-minifier-terser"]);case"JavaScript":return(await(await import("terser")).minify(t.toString(),o.terser)).code??t;case"Image":try{return t instanceof(await import("sharp")).default?await(await import("./Image/Writesharp.js")).default(o.sharp,{Buffer:t,Input:s}):t}catch(a){return console.log(a),t}case"SVG":return(await import("svgo")).optimize(t.toString(),o.svgo).data??t;case"JSON":try{return JSON.stringify(JSON.parse(t.toString()))}catch(a){return console.log(a),t}default:return t}},Fulfilled:async({File:t,Info:{Total:s}})=>t>0?`${(await import("kleur/colors")).green(`\u2713\u2001Successfully compressed a total of ${t} ${r} file${t!==1?"s":""} for ${(await import("@playform/pipe/Target/Function/Bytes.js")).default(s)}.`)}`:!1})),r==="Image"&&(y=n(y,{Read:async({Input:t,Buffer:s})=>{try{(await import("sharp")).default.cache(!1);const{format:a}=await(await import("sharp")).default(t).metadata(),l={animated:a==="webp"||a==="gif"};return(await import("sharp")).default(t,typeof c=="object"&&typeof c.sharp=="object"&&typeof c.sharp.sharp=="object"?n(l,c.sharp?.sharp):l)}catch(a){return console.log(a),s}}}));for(const t of f)await(await(await(await new(await import("@playform/pipe")).default(m,u).In(t)).By(g[r]??"**/*")).Not(b)).Pipe(y)}}}}}});export{w as Default,n as Merge,J as Search,d as System,y as _Action,N as default};