diff --git a/src/helpers.js b/src/helpers.js index b29f099..16d91b6 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -11,7 +11,8 @@ const writeFileAsync = promisify(fs.writeFile) const mkdirpAsync = promisify(mkdirp) /** - * Optimizes a single image, returning the orignal if the "optimized" version is larger + * Optimizes a single image + * returns the orignal if the "optimized" version is larger (only if the onlyUseIfSmaller option is true) * @param {Object} imageData * @param {Object} imageminOptions * @return {Promise(asset)} @@ -26,7 +27,7 @@ export async function optimizeImage (imageData, imageminOptions) { const optimizedImageBuffer = await imagemin.buffer(imageBuffer, imageminOptions) // If the optimization actually produced a smaller file, then return the optimized version - if (optimizedImageBuffer.length < originalSize) { + if (imageminOptions.onlyUseIfSmaller && optimizedImageBuffer.length < originalSize) { return optimizedImageBuffer } else { // otherwize return the orignal diff --git a/src/index.d.ts b/src/index.d.ts index ce320cb..8dbd248 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -38,5 +38,6 @@ declare namespace ImageminWebpackPlugin { minFileSize?: number; maxFileSize?: number; cacheFolder?: string; + onlyUseIfSmaller?: boolean; } } diff --git a/src/index.js b/src/index.js index dff285e..16d5d39 100644 --- a/src/index.js +++ b/src/index.js @@ -21,7 +21,6 @@ import { export default class ImageminPlugin { constructor (options = {}) { - // I love ES2015! const { disable = false, test = /.*/, @@ -41,7 +40,8 @@ export default class ImageminPlugin { svgo = {}, pngquant = null, externalImages = {}, - cacheFolder = null + cacheFolder = null, + onlyUseIfSmaller = false } = options this.options = { @@ -58,7 +58,8 @@ export default class ImageminPlugin { fileName: null, ...externalImages }, - cacheFolder + cacheFolder, + onlyUseIfSmaller } // As long as the options aren't `null` then include the plugin. Let the destructuring above