All files make_webp.js

93.33% Statements 14/15
100% Branches 0/0
100% Functions 3/3
93.33% Lines 14/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 232x 2x   2x   2x 4x   2x 4x   2x 4x 4x 4x 4x 4x 4x          
const { CWebp } = require('cwebp')
const { resolve } = require('path')
 
const WEBP_REGEXP = /^(~images)([\w\d/\-_]+)(\.[jpe?g|png]+)/
 
const getImagePath = ({ imageFolder, resolvePath }, path) =>
  resolve(path.replace(imageFolder, resolvePath))
 
const getWebpPath = ({ webpFolder }, path) =>
  resolve(path.replace(WEBP_REGEXP, webpFolder + '$2.webp'))
 
module.exports = async (options, path) => {
  let imagePath = getImagePath(options, path)
  let webpPath = getWebpPath(options, path)
  let encoder = new CWebp(imagePath)
  encoder.quality(options.quality)
  try {
    await encoder.write(webpPath)
  } catch (e) {
    console.error(e)
  }
}