Skip to content

Commit

Permalink
Merge pull request #28 from hexojs/improve-deprecation-warning
Browse files Browse the repository at this point in the history
Improve the deprecation warning
  • Loading branch information
ertrzyiks committed Dec 9, 2018
2 parents b017be6 + 2ab7814 commit f2a0f7b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 57 deletions.
6 changes: 3 additions & 3 deletions lib/responsive_images.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function generateResponsiveImages() {
return Promise.all(sizes.map(function (sizeSets) {
return Promise.all(Object.keys(sizeSets).map(function (name) {
var newPath = getNewPath(filePath, {prefix: name})
return route.set(newPath, resizeImageFn(buffer, sizeSets[name]))
return route.set(newPath, resizeImageFn(hexo, buffer, sizeSets[name]))
}))
}))
})
Expand All @@ -54,10 +54,10 @@ function getSizesFor(filePath, rules) {
}, [])
}

function resizeImageFn(buffer, config) {
function resizeImageFn(hexo, buffer, config) {
return function () {
var img = sharp(buffer)
var resizeOptions = getResizeOptions(config)
var resizeOptions = getResizeOptions(hexo, config)

return applySharpApiOptions(img, config).resize(config.width, config.height, resizeOptions).toBuffer()
}
Expand Down
78 changes: 24 additions & 54 deletions lib/sharp_options.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,14 @@
function deprecateWarning(message) {
console.log(message)
}
var sharpApiOptions = [
createOption('options', function (img, value) {
deprecateWarning('Pass resize options next to width and height, without the options: key')
return img
}),
createOption('crop', function (img, value) {
deprecateWarning('Use `fit: "cover"` with a `position` instead.')
return img
}),
createOption('embed', function (img, value) {
deprecateWarning('Use `fit: "container"` with a `position` instead.')
return img
}),
createOption('ignoreAspectRatio', function (img, value) {
deprecateWarning('Use `fit: "fill"` instead.')
return img
}),
createOption('max', function (img, value) {
deprecateWarning('Use `fit: "inside"` instead.')
return img
}),
createOption('min', function (img, value) {
deprecateWarning('Use `fit: "outside"` instead.')
return img
}),
createOption('quality', function (img, value) {
if (typeof value === 'undefined') {
return img
}

return img
.jpeg({quality: value, force: false})
.webp({quality: value, force: false})
.tiff({quality: value, force: false})
})
]

function createOption(name, applyFn) {
return {
configName: name,
applyFn: applyFn
function applyOptions(rawImage, options) {
if (typeof options.quality === 'undefined') {
return rawImage
}
}

function applyOptions(rawImage, options) {
return sharpApiOptions.reduce(function (img, option) {
var value = options[option.configName]
if (typeof value === 'undefined') {
return img
}
var value = options.quality

return option.applyFn(img, value)
}, rawImage)
return rawImage
.jpeg({quality: value, force: false})
.webp({quality: value, force: false})
.tiff({quality: value, force: false})
}

function pickResizeOptions(options) {
Expand All @@ -67,31 +22,42 @@ function pickResizeOptions(options) {
}
return result
}
function getResizeOptions(config) {

function getResizeOptions(hexo, config) {
// Legacy `config.options`, we accepted resize options that way
// but now we pick them directly from the config
var options = Object.assign(pickResizeOptions(config), config.options)

if (config.options) {
deprecateOption(hexo, 'options','Pass resize options next to width and height, without the options: key')
}

if (config.embed) {
deprecateOption(hexo, 'embed','Use `fit: "contain"` with a `position` instead')

options.fit = 'contain'
if (typeof config.embed != 'boolean') {
options.position = config.embed
}
}

if (config.ignoreAspectRatio) {
deprecateOption(hexo, 'ignoreAspectRatio', 'Use `fit: "fill"` instead')
options.fit = 'fill'
}

if (config.min) {
deprecateOption(hexo, 'min', 'Use `fit: "outside"` instead')
options.fit = 'outside'
}

if (config.max) {
deprecateOption(hexo, 'max', 'Use `fit: "inside"` instead')
options.fit = 'inside'
}

if (config.crop) {
deprecateOption(hexo, 'crop', 'Use `fit: "cover"` with a `position` instead')
options.fit = 'cover'
if (typeof config.crop != 'boolean') {
options.position = config.crop
Expand All @@ -101,5 +67,9 @@ function getResizeOptions(config) {
return options
}

function deprecateOption(hexo, name, message) {
hexo.log.warn('[Responsive images plugin] Deprecated option "' + name + '" found in the config. ' + message)
}

exports.applyOptions = applyOptions
exports.getResizeOptions = getResizeOptions

0 comments on commit f2a0f7b

Please sign in to comment.