Skip to content

Commit

Permalink
add onlyUseIfSmaller option
Browse files Browse the repository at this point in the history
  • Loading branch information
Klathmon committed Jul 29, 2019
1 parent 435acb0 commit 9629c0b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ declare namespace ImageminWebpackPlugin {
minFileSize?: number;
maxFileSize?: number;
cacheFolder?: string;
onlyUseIfSmaller?: boolean;
}
}
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {

export default class ImageminPlugin {
constructor (options = {}) {
// I love ES2015!
const {
disable = false,
test = /.*/,
Expand All @@ -41,7 +40,8 @@ export default class ImageminPlugin {
svgo = {},
pngquant = null,
externalImages = {},
cacheFolder = null
cacheFolder = null,
onlyUseIfSmaller = false
} = options

this.options = {
Expand All @@ -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
Expand Down

0 comments on commit 9629c0b

Please sign in to comment.