rollup-generate-image-sizes
Advanced tools
Comparing version 0.0.4 to 0.0.5
'use strict'; | ||
var fs = require('fs'); | ||
var globby = require('globby'); | ||
@@ -8,2 +9,3 @@ var sharp = require('sharp'); | ||
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs); | ||
var globby__default = /*#__PURE__*/_interopDefaultLegacy(globby); | ||
@@ -18,3 +20,3 @@ var sharp__default = /*#__PURE__*/_interopDefaultLegacy(sharp); | ||
// A helper function for transform single items to array | ||
const arrayify = (a) => (Array.isArray(a) ? a : [a]); | ||
const arrayify = (a) => (Array.isArray(a) ? [...a] : [a]); | ||
@@ -31,2 +33,3 @@ var index = (options = {}) => { | ||
forceUpscale = false, // whether or not we should forcibly upscale | ||
skipExisting = true, // whether we should skip existing images that have already been resized. | ||
} = options; | ||
@@ -47,2 +50,5 @@ | ||
// The sizes and formats that we will output to, arrayified. | ||
const sizes = arrayify(size); | ||
// Glob for the directories | ||
@@ -58,4 +64,2 @@ const dirGlob = arrayify(dir).length > 1 ? `{${arrayify(dir).join(',')}}` : arrayify(dir)[0]; | ||
images.map((image) => { | ||
const sharpObj = sharp__default['default'](image); | ||
// generate the output path | ||
@@ -66,31 +70,62 @@ const imagePathSplit = image.split('.'); | ||
// process image format options | ||
const formats = Array.from(new Set( | ||
arrayify(outputFormat) | ||
// If format is match, we match to the input format | ||
.map((format) => (format === 'match' ? imageFormat : format)) | ||
// If format is jpeg, we map to jpg | ||
.map((format) => (format === 'jpeg' ? 'jpg' : format)), | ||
)); | ||
// An array of objects that contain sizes and formats of all our outputs. | ||
let outputs = sizes.reduce( | ||
(acc, scaleWidth) => [...acc, ...formats.map((format) => ({ format, scaleWidth }))], | ||
[], | ||
); | ||
// if skipExisting is set | ||
if (skipExisting) { | ||
// Filter out images that already exist | ||
outputs = outputs.filter( | ||
({ format, scaleWidth }) => !fs__default['default'].existsSync(`${imagePathPre}@${scaleWidth}w.${format}`), | ||
); | ||
// if images already exist, we can skip this rest of this process | ||
if (outputs.length === 0) return null; | ||
} | ||
// //////////////////////////////////////////// | ||
// Everything below is expensive, so we want to short-circuit this as much as possible | ||
// load in the image | ||
const sharpObj = sharp__default['default'](image); | ||
// Read the sharp metadata so we know what the input width is. | ||
return sharpObj.metadata() | ||
.then((metadata) => Promise.allSettled( | ||
(arrayify(size)).map((scaleWidth) => { | ||
// If the width we want to scale to is larger than the original | ||
// width and forceUpscale is not set, we skip this. | ||
if (scaleWidth > metadata.width && !forceUpscale) return Promise.resolve(); | ||
outputs | ||
// Get only the sizes that we need to generate | ||
.reduce((acc, val) => { | ||
if (acc.indexOf(val.scaleWidth) < 0) return [...acc, val.scaleWidth]; | ||
return acc; | ||
}, []) | ||
.map((scaleWidth) => { | ||
// If the width we want to scale to is larger than the original | ||
// width and forceUpscale is not set, we skip this. | ||
if (scaleWidth > metadata.width && !forceUpscale) return Promise.resolve(); | ||
// Process output format list and dedup | ||
const outputFormats = Array.from(new Set( | ||
arrayify(outputFormat) | ||
// If format is match, we match to the input format | ||
.map((format) => (format === 'match' ? imageFormat : format)) | ||
// If format is jpeg, we map to jpg | ||
.map((format) => (format === 'jpeg' ? 'jpg' : format)), | ||
)); | ||
// Save all of the output images | ||
return Promise.all( | ||
(outputFormats) | ||
.map((format) => sharpObj | ||
.clone() | ||
.resize(scaleWidth) | ||
.toFormat(format, { quality }) | ||
.toFile(`${imagePathPre}@${scaleWidth}w.${format}`)), | ||
); | ||
}), | ||
// Save all of the output images | ||
return Promise.all( | ||
outputs | ||
// only get the outputs of the current width | ||
.filter((d) => d.scaleWidth === scaleWidth) | ||
.map((d) => d.format) | ||
.map((format) => sharpObj | ||
.clone() | ||
.resize(scaleWidth) | ||
.toFormat(format, { quality }) | ||
.toFile(`${imagePathPre}@${scaleWidth}w.${format}`)), | ||
); | ||
}), | ||
)); | ||
}), | ||
}).filter((d) => !!d), | ||
)); | ||
@@ -97,0 +132,0 @@ }, |
@@ -0,1 +1,2 @@ | ||
import fs from 'fs'; | ||
import globby from 'globby'; | ||
@@ -10,3 +11,3 @@ import sharp from 'sharp'; | ||
// A helper function for transform single items to array | ||
const arrayify = (a) => (Array.isArray(a) ? a : [a]); | ||
const arrayify = (a) => (Array.isArray(a) ? [...a] : [a]); | ||
@@ -23,2 +24,3 @@ var index = (options = {}) => { | ||
forceUpscale = false, // whether or not we should forcibly upscale | ||
skipExisting = true, // whether we should skip existing images that have already been resized. | ||
} = options; | ||
@@ -39,2 +41,5 @@ | ||
// The sizes and formats that we will output to, arrayified. | ||
const sizes = arrayify(size); | ||
// Glob for the directories | ||
@@ -50,4 +55,2 @@ const dirGlob = arrayify(dir).length > 1 ? `{${arrayify(dir).join(',')}}` : arrayify(dir)[0]; | ||
images.map((image) => { | ||
const sharpObj = sharp(image); | ||
// generate the output path | ||
@@ -58,31 +61,62 @@ const imagePathSplit = image.split('.'); | ||
// process image format options | ||
const formats = Array.from(new Set( | ||
arrayify(outputFormat) | ||
// If format is match, we match to the input format | ||
.map((format) => (format === 'match' ? imageFormat : format)) | ||
// If format is jpeg, we map to jpg | ||
.map((format) => (format === 'jpeg' ? 'jpg' : format)), | ||
)); | ||
// An array of objects that contain sizes and formats of all our outputs. | ||
let outputs = sizes.reduce( | ||
(acc, scaleWidth) => [...acc, ...formats.map((format) => ({ format, scaleWidth }))], | ||
[], | ||
); | ||
// if skipExisting is set | ||
if (skipExisting) { | ||
// Filter out images that already exist | ||
outputs = outputs.filter( | ||
({ format, scaleWidth }) => !fs.existsSync(`${imagePathPre}@${scaleWidth}w.${format}`), | ||
); | ||
// if images already exist, we can skip this rest of this process | ||
if (outputs.length === 0) return null; | ||
} | ||
// //////////////////////////////////////////// | ||
// Everything below is expensive, so we want to short-circuit this as much as possible | ||
// load in the image | ||
const sharpObj = sharp(image); | ||
// Read the sharp metadata so we know what the input width is. | ||
return sharpObj.metadata() | ||
.then((metadata) => Promise.allSettled( | ||
(arrayify(size)).map((scaleWidth) => { | ||
// If the width we want to scale to is larger than the original | ||
// width and forceUpscale is not set, we skip this. | ||
if (scaleWidth > metadata.width && !forceUpscale) return Promise.resolve(); | ||
outputs | ||
// Get only the sizes that we need to generate | ||
.reduce((acc, val) => { | ||
if (acc.indexOf(val.scaleWidth) < 0) return [...acc, val.scaleWidth]; | ||
return acc; | ||
}, []) | ||
.map((scaleWidth) => { | ||
// If the width we want to scale to is larger than the original | ||
// width and forceUpscale is not set, we skip this. | ||
if (scaleWidth > metadata.width && !forceUpscale) return Promise.resolve(); | ||
// Process output format list and dedup | ||
const outputFormats = Array.from(new Set( | ||
arrayify(outputFormat) | ||
// If format is match, we match to the input format | ||
.map((format) => (format === 'match' ? imageFormat : format)) | ||
// If format is jpeg, we map to jpg | ||
.map((format) => (format === 'jpeg' ? 'jpg' : format)), | ||
)); | ||
// Save all of the output images | ||
return Promise.all( | ||
(outputFormats) | ||
.map((format) => sharpObj | ||
.clone() | ||
.resize(scaleWidth) | ||
.toFormat(format, { quality }) | ||
.toFile(`${imagePathPre}@${scaleWidth}w.${format}`)), | ||
); | ||
}), | ||
// Save all of the output images | ||
return Promise.all( | ||
outputs | ||
// only get the outputs of the current width | ||
.filter((d) => d.scaleWidth === scaleWidth) | ||
.map((d) => d.format) | ||
.map((format) => sharpObj | ||
.clone() | ||
.resize(scaleWidth) | ||
.toFormat(format, { quality }) | ||
.toFile(`${imagePathPre}@${scaleWidth}w.${format}`)), | ||
); | ||
}), | ||
)); | ||
}), | ||
}).filter((d) => !!d), | ||
)); | ||
@@ -89,0 +123,0 @@ }, |
{ | ||
"name": "rollup-generate-image-sizes", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Automatically generate image sizes for use in srcsets.", | ||
@@ -5,0 +5,0 @@ "main": "dist/rollup-generate-image-sizes.cjs.js", |
@@ -27,2 +27,3 @@ # rollup-generate-image-sizes | ||
forceUpscale: false, | ||
skipExisting: true, | ||
}) | ||
@@ -50,3 +51,5 @@ ] | ||
`skipExisting` (default: true | `boolean`): whether we should skip existing images that have already been resized. a false value means that images will be regenerated and overwritten every single time this script is run. | ||
## License | ||
MIT |
@@ -0,1 +1,2 @@ | ||
import fs from 'fs'; | ||
import globby from 'globby'; | ||
@@ -10,3 +11,3 @@ import sharp from 'sharp'; | ||
// A helper function for transform single items to array | ||
const arrayify = (a) => (Array.isArray(a) ? a : [a]); | ||
const arrayify = (a) => (Array.isArray(a) ? [...a] : [a]); | ||
@@ -23,2 +24,3 @@ export default (options = {}) => { | ||
forceUpscale = false, // whether or not we should forcibly upscale | ||
skipExisting = true, // whether we should skip existing images that have already been resized. | ||
} = options; | ||
@@ -39,2 +41,5 @@ | ||
// The sizes and formats that we will output to, arrayified. | ||
const sizes = arrayify(size); | ||
// Glob for the directories | ||
@@ -50,4 +55,2 @@ const dirGlob = arrayify(dir).length > 1 ? `{${arrayify(dir).join(',')}}` : arrayify(dir)[0]; | ||
images.map((image) => { | ||
const sharpObj = sharp(image); | ||
// generate the output path | ||
@@ -58,31 +61,62 @@ const imagePathSplit = image.split('.'); | ||
// process image format options | ||
const formats = Array.from(new Set( | ||
arrayify(outputFormat) | ||
// If format is match, we match to the input format | ||
.map((format) => (format === 'match' ? imageFormat : format)) | ||
// If format is jpeg, we map to jpg | ||
.map((format) => (format === 'jpeg' ? 'jpg' : format)), | ||
)); | ||
// An array of objects that contain sizes and formats of all our outputs. | ||
let outputs = sizes.reduce( | ||
(acc, scaleWidth) => [...acc, ...formats.map((format) => ({ format, scaleWidth }))], | ||
[], | ||
); | ||
// if skipExisting is set | ||
if (skipExisting) { | ||
// Filter out images that already exist | ||
outputs = outputs.filter( | ||
({ format, scaleWidth }) => !fs.existsSync(`${imagePathPre}@${scaleWidth}w.${format}`), | ||
); | ||
// if images already exist, we can skip this rest of this process | ||
if (outputs.length === 0) return null; | ||
} | ||
// //////////////////////////////////////////// | ||
// Everything below is expensive, so we want to short-circuit this as much as possible | ||
// load in the image | ||
const sharpObj = sharp(image); | ||
// Read the sharp metadata so we know what the input width is. | ||
return sharpObj.metadata() | ||
.then((metadata) => Promise.allSettled( | ||
(arrayify(size)).map((scaleWidth) => { | ||
// If the width we want to scale to is larger than the original | ||
// width and forceUpscale is not set, we skip this. | ||
if (scaleWidth > metadata.width && !forceUpscale) return Promise.resolve(); | ||
outputs | ||
// Get only the sizes that we need to generate | ||
.reduce((acc, val) => { | ||
if (acc.indexOf(val.scaleWidth) < 0) return [...acc, val.scaleWidth]; | ||
return acc; | ||
}, []) | ||
.map((scaleWidth) => { | ||
// If the width we want to scale to is larger than the original | ||
// width and forceUpscale is not set, we skip this. | ||
if (scaleWidth > metadata.width && !forceUpscale) return Promise.resolve(); | ||
// Process output format list and dedup | ||
const outputFormats = Array.from(new Set( | ||
arrayify(outputFormat) | ||
// If format is match, we match to the input format | ||
.map((format) => (format === 'match' ? imageFormat : format)) | ||
// If format is jpeg, we map to jpg | ||
.map((format) => (format === 'jpeg' ? 'jpg' : format)), | ||
)); | ||
// Save all of the output images | ||
return Promise.all( | ||
(outputFormats) | ||
.map((format) => sharpObj | ||
.clone() | ||
.resize(scaleWidth) | ||
.toFormat(format, { quality }) | ||
.toFile(`${imagePathPre}@${scaleWidth}w.${format}`)), | ||
); | ||
}), | ||
// Save all of the output images | ||
return Promise.all( | ||
outputs | ||
// only get the outputs of the current width | ||
.filter((d) => d.scaleWidth === scaleWidth) | ||
.map((d) => d.format) | ||
.map((format) => sharpObj | ||
.clone() | ||
.resize(scaleWidth) | ||
.toFormat(format, { quality }) | ||
.toFile(`${imagePathPre}@${scaleWidth}w.${format}`)), | ||
); | ||
}), | ||
)); | ||
}), | ||
}).filter((d) => !!d), | ||
)); | ||
@@ -89,0 +123,0 @@ }, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19812
325
54
3