DRY resize-images options

This commit is contained in:
Eric Jinks 2018-05-11 09:16:21 +10:00
parent 7865b630f9
commit 65a67da803
2 changed files with 14 additions and 6 deletions

View file

@ -7,10 +7,13 @@ const sharp = require('sharp')
const glob = util.promisify(globCb)
const readFile = util.promisify(fs.readFile)
const { sizes, resizedDir, outputDir } = require('../src/util/getImageUrl')
const dirPrefix = './public'
const options = {
inputDir: './public/images/uploads',
outputDir: './public/images/uploads/resized',
sizes: [10, 300, 600, 1200, 1800],
sizes,
inputDir: `${dirPrefix}${outputDir}`,
outputDir: `${dirPrefix}${resizedDir}`,
imageFormats: ['jpg', 'jpeg', 'png', 'gif', 'webp']
}

View file

@ -11,7 +11,9 @@ const parseFilename = filename => {
}
const getImageSrcset = path => {
if (!path || path.match(/^http/) || path.match(/svg$/) || window.CMS) { return null }
if (!path || path.match(/^http/) || path.match(/svg$/) || window.CMS) {
return null
}
const { filename, extname } = parseFilename(path)
const pathname = encodeURI(filename.replace(outputDir, resizedDir))
@ -22,7 +24,9 @@ const getImageSrcset = path => {
}
const getImageSrc = (path, sizeRequested) => {
if (!path || path.match(/^http/) || path.match(/svg$/) || window.CMS) { return path }
if (!path || path.match(/^http/) || path.match(/svg$/) || window.CMS) {
return path
}
sizeRequested = parseInt(sizeRequested, 10)
let size
if (sizeRequested) {
@ -43,5 +47,6 @@ module.exports = {
getImageSrcset,
getImageSrc,
sizes,
outputDir
outputDir,
resizedDir
}