import imagemin from 'imagemin' ; import imageminWebp from 'imagemin-webp' ; import imageminGif2webp from 'imagemin-gif2webp' ; import { CompressOptions , ImgInfo } from '../utils/interfaces' ; import { getImageBuffer } from '../utils/getImage' ; export function imageminWebPCompress ( { ctx , info } ) { ctx . log . info ( 'imagemin_webp 压缩开始' ) ; return getImageBuffer ( ctx , info . url ) . then ( ( buffer ) => { return imagemin . buffer ( buffer , { plugins : [ imageminWebp ( { quality : 75 } ) , imageminGif2webp ( { quality : 75 , lossy : true } ) ] , } ) ; } ) . then ( ( buffer ) => { ctx . log . info ( 'imagemin_webp 压缩完成' ) ; info . extname = 'webp' ; info . fileName = changeExt ( info . fileName , 'webp' ) ; return { ... info , buffer , } ; } ) ; } function changeExt ( fileName , newExt ) { var pos = fileName . includes ( '.' ) ? fileName . lastIndexOf ( '.' ) : fileName . length ; var fileRoot = fileName . substr ( 0 , pos ) ; return `${ fileRoot }.${ newExt }` ; }