UNPKG

@nikolarhristov/pipeline

Version:
2 lines (1 loc) • 3.73 kB
import{fileURLToPath as l}from"url";import c from"../options/index.js";import h from"./apply-to.js";import f from"./format-bytes.js";import e from"./deepmerge.js";import a from"./files.js";import{minify as u}from"csso";import{minify as m}from"html-minifier-terser";import w from"sharp";import{optimize as y}from"svgo";import{minify as d}from"terser";import n from"../options/lib/compress/index.js";import g from"./vendor/sharp-read.js";import b from"critters";import p from"../options/lib/critters/index.js";class G{options;paths=new Set;constructor(s={}){this.options=s,this.mergeDefaultOptions(c)}mergeDefaultOptions(s){for(const i in this.options)Object.prototype.hasOwnProperty.call(s,i)&&this.options[i]===!0&&(this.options[i]=s[i]);if(this.options=e(s,this.options),typeof this.options.path<"u")if(this.options.path instanceof Array||this.options.path instanceof Set)for(const i of this.options.path)this.paths.add(i);else this.paths.add(this.options.path)}async process(){for(const s of this.paths)(await(await(await new a(this.options.logger).in(s)).by(this.options.files)).not(this.options.exclude)).pipeline(this.options.pipeline);return this}async compress(){this.mergeDefaultOptions(n);for(const[s,i]of Object.entries(this.options))if(!!i)for(const o of this.paths)switch(s){case"css":{await(await(await(await new a(this.options.logger).in(o)).by("**/*.css")).not(this.options.exclude)).pipeline(e(n.pipeline,{wrote:async t=>u(t.buffer.toString(),i).css,fulfilled:async t=>t.files>0?`Successfully compressed a total of ${t.files} CSS ${t.files===1?"file":"files"} for ${await f(t.info.total)}.`:!1}));break}case"html":{await(await(await(await new a(this.options.logger).in(o)).by("**/*.html")).not(this.options.exclude)).pipeline(e(n.pipeline,{wrote:async t=>await m(t.buffer.toString(),i),fulfilled:async t=>t.files>0?`Successfully compressed a total of ${t.files} HTML ${t.files===1?"file":"files"} for ${await f(t.info.total)}.`:!1}));break}case"js":{await(await(await(await new a(this.options.logger).in(o)).by("**/*.{js,mjs,cjs}")).not(this.options.exclude)).pipeline(e(n.pipeline,{wrote:async t=>{const{code:r}=await d(t.buffer.toString(),i);return r||t.buffer},fulfilled:async t=>t.files>0?`Successfully compressed a total of ${t.files} JS ${t.files===1?"file":"files"} for ${await f(t.info.total)}.`:!1}));break}case"img":{await(await(await(await new a(this.options.logger).in(o)).by("**/*.{avci,avcs,avif,avifs,gif,heic,heics,heif,heifs,jfif,jif,jpe,jpeg,jpg,png,raw,tiff,webp}")).not(this.options.exclude)).pipeline(e(n.pipeline,{wrote:async t=>g(t,i),read:async t=>w(t.inputPath,{failOn:"none",sequentialRead:!0,unlimited:!0}),fulfilled:async t=>t.files>0?`Successfully compressed a total of ${t.files} IMG ${t.files===1?"file":"files"} for ${await f(t.info.total)}.`:!1}));break}case"svg":{await(await(await(await new a(this.options.logger).in(o)).by("**/*.svg")).not(this.options.exclude)).pipeline(e(n.pipeline,{wrote:async t=>{const{data:r}=y(t.buffer.toString(),i);return typeof r<"u"?r:t.buffer},fulfilled:async t=>t.files>0?`Successfully compressed a total of ${t.files} SVG ${t.files===1?"file":"files"} for ${await f(t.info.total)}.`:!1}));break}default:break}return this}async critters(){this.mergeDefaultOptions(p);for(const s of this.paths){const i=await h(s,t=>t instanceof URL?l(t):t),o=new b(e(this.options.critters,{path:i instanceof Map?i.keys().next().value:i,logLevel:(()=>{switch(this.options.logger){case 0:return"silent";case 1:return"silent";case 2:return"info";default:return"info"}})()}));await(await(await(await new a(this.options.logger).in(s)).by("**/*.html")).not(this.options.exclude)).pipeline(e(p.pipeline,{wrote:async t=>o.process(t.buffer)}))}return this}}export{G as default};