UNPKG

@hilosiva/vite-plugin-image-optimizer

Version:

画像アセットを最適化する Vite 用プラグイン。

4 lines 3.1 kB
import u from"sharp";import g from"path";var a=class{outputDir;sharpImageLists;originalSize;optimizeSize;extFunctions;defaultOptions;options;constructor(t,i={},e){this.outputDir=t,this.sharpImageLists=[],this.originalSize=0,this.optimizeSize=0,this.extFunctions={".jpg":"jpeg",".jpeg":"jpeg",".png":"png",".gif":"gif",".webp":"webp",".avif":"avif"},this.defaultOptions={supportedExts:[".jpg",".jpeg",".png",".gif",".webp",".avif"],generate:{inputExts:[".jpg",".jpeg",".png"],outputExts:[".webp",".avif"],preserveExt:!1},jpg:{},jpeg:{},png:{},gif:{},webp:{},avif:{}},this.options=this.deepMerge(this.defaultOptions,i),this._init(e)}deepMerge(t,i){if(typeof t!="object"||typeof i!="object")return i;let e=Object.keys(i);for(let s of e)s in t&&typeof t[s]=="object"&&typeof i[s]=="object"?t[s]=this.deepMerge(t[s],i[s]):t[s]=i[s];return t}async _init(t){let i=Object.keys(t).filter(e=>{let s=g.extname(e);return this.options.supportedExts.includes(s)});this.sharpImageLists=i.map(e=>{let s=u(t[e].source);return{file:e,sharpImage:s}}),this.sharpImageLists.length>0&&await this._run()}async _run(){await Promise.all(this.sharpImageLists.map(async(t,i)=>{if(await this._optimize(t,i),this.isGenerateFormat(t.file))for(let e=0;e<this.options.generate.outputExts.length;e++)await this._createImage(t,this.options.generate.outputExts[e])})),console.log(` \u2728 Total \x1B[30m${this.formatBytes(this.optimizeSize)} / ${this.formatBytes(this.originalSize)} ${this.getRatio(this.originalSize,this.optimizeSize)}`)}async _optimize(t,i){let{sharpImage:e,file:s}=t,n=g.extname(s),p=this.extFunctions[n],r=await e.metadata(),o=await e[p](this.options[n.replace(".","")]).toFile(`${this.outputDir}/${s}`).catch(m=>console.log(m));i===0&&console.log(` \u2728 \x1B[36m[vite-plugin-image-optimizer] \x1B[39m- optimized images successfully:`),this.setInfo(s,r,o)}async _createImage(t,i){let{sharpImage:e,file:s}=t,n=this.extFunctions[i],p=await e.metadata(),r=await e[n](this.options[i.replace(".","")]).toFile(`${this.outputDir}/${this.changeExtension(s,i)}`).catch(o=>console.log(o));this.setInfo(this.changeExtension(s,i),p,r)}getRatio(t,i){if(t<=0||i<=0)return"N/A";let e=(t-i)/t*100,s=Math.abs(e);return`${(s>100?"+":"-")+s.toFixed(2)}%`}getLastDirName(t){let i=t.split("/");return i[i.length-1]}formatBytes(t){let i=["bytes","KB","MB","GB","TB","PB","EB","ZB","YB"],e=0;for(;t>=1024&&e<i.length-1;)t/=1024,e++;return`${t.toFixed(2)} ${i[e]}`}isGenerateFormat(t){let i=g.extname(t);return this.options.generate.inputExts&&this.options.generate.inputExts.includes(i.toLowerCase())}changeExtension(t,i){return this.options.generate.preserveExt?t+i:t.substring(0,t.lastIndexOf("."))+i}setInfo(t,i,e){this.originalSize+=i.size,this.optimizeSize+=e.size,console.log(`\x1B[30m${this.getLastDirName(this.outputDir)}/\x1B[34m${t} \x1B[32m${this.getRatio(i.size,e.size)} \x1B[30m${this.formatBytes(i.size)} \u2192 ${this.formatBytes(e.size)}`)}};function y(h={}){return{name:"@hilosiva/viteImageOptimizer",async writeBundle(t,i){new a(t.dir,h,i)}}}export{y as viteImageOptimizer}; //# sourceMappingURL=index.mjs.map