From 65a67da80376782b6e1aeb467aed46a4f367e0c7 Mon Sep 17 00:00:00 2001 From: Eric Jinks Date: Fri, 11 May 2018 09:16:21 +1000 Subject: [PATCH] DRY resize-images options --- functions/resize-images.js | 9 ++++++--- src/util/getImageUrl.js | 11 ++++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/functions/resize-images.js b/functions/resize-images.js index 261a848..f41e352 100644 --- a/functions/resize-images.js +++ b/functions/resize-images.js @@ -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'] } diff --git a/src/util/getImageUrl.js b/src/util/getImageUrl.js index bda795f..aa9c59e 100644 --- a/src/util/getImageUrl.js +++ b/src/util/getImageUrl.js @@ -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 }