rollup-generate-image-sizes
Advanced tools
Comparing version 0.0.12 to 0.0.13
@@ -6,2 +6,3 @@ 'use strict'; | ||
var jimp = require('jimp'); | ||
var Queue = require('promise-queue'); | ||
@@ -13,2 +14,3 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
var jimp__default = /*#__PURE__*/_interopDefaultLegacy(jimp); | ||
var Queue__default = /*#__PURE__*/_interopDefaultLegacy(Queue); | ||
@@ -33,3 +35,4 @@ /** | ||
forceUpscale = false, // whether or not we should forcibly upscale | ||
skipExisting = true, // whether we should skip existing images that have already been resized. | ||
skipExisting = true, // whether we should skip existing images that have already been resized | ||
maxParallel = 4, // the max number of parallel images that can be processed concurrently | ||
} = options; | ||
@@ -56,76 +59,84 @@ | ||
return globby__default['default']( | ||
// Finds all the images we want based on dir and inputFormat | ||
`${dirGlob}/**/*.{${inputFormat.join(',')}}`, | ||
) | ||
.then((images) => Promise.allSettled( | ||
// Map them into jimp objects | ||
images | ||
.filter((d) => d.indexOf('@') < 0 && d.indexOf('#') < 0) | ||
.map((image) => { | ||
// generate the output path | ||
const imagePathSplit = image.split('.'); | ||
const imagePathPre = imagePathSplit.slice(0, -1).join('.'); | ||
const imageFormat = imagePathSplit[imagePathSplit.length - 1]; | ||
return new Promise((resolve) => { | ||
// Promise queue so that we only process maxParallel parallel files at a time | ||
const q = new Queue__default['default'](maxParallel, Infinity, { onEmpty: () => resolve() }); | ||
// 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)), | ||
)); | ||
globby__default['default']( | ||
// Finds all the images we want based on dir and inputFormat | ||
`${dirGlob}/**/*.{${inputFormat.join(',')}}`, | ||
) | ||
.then((images) => { | ||
// Map them into jimp objects | ||
images | ||
.filter((d) => d.indexOf('@') < 0 && d.indexOf('#') < 0) | ||
.forEach((image) => { | ||
// generate the output path | ||
const imagePathSplit = image.split('.'); | ||
const imagePathPre = imagePathSplit.slice(0, -1).join('.'); | ||
const imageFormat = imagePathSplit[imagePathSplit.length - 1]; | ||
// 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 }))], | ||
[], | ||
); | ||
// 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)), | ||
)); | ||
// 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}`), | ||
// 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 images already exist, we can skip this rest of this process | ||
if (outputs.length === 0) return null; | ||
} | ||
// 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}`), | ||
); | ||
// //////////////////////////////////////////// | ||
// Everything below is expensive, so we want to short-circuit this as much as possible | ||
// load in the image | ||
return jimp__default['default'].read(image) | ||
.then((jimpObj) => Promise.allSettled( | ||
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 > jimpObj.bitmap.width && !forceUpscale) { | ||
return Promise.resolve(); | ||
} | ||
// if images already exist, we can skip this rest of this process | ||
if (outputs.length === 0) return; | ||
} | ||
// 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) => jimpObj | ||
.clone() | ||
.resize(scaleWidth, jimp__default['default'].AUTO) | ||
.quality(quality) | ||
.write(`${imagePathPre}@${scaleWidth}w.${format}`)), | ||
); | ||
}), | ||
)); | ||
}).filter((d) => !!d), | ||
)); | ||
// //////////////////////////////////////////// | ||
// Everything below is expensive, so short-circuit this as much as possible | ||
// load in the image | ||
q.add(() => jimp__default['default'].read(image) | ||
.then((jimpObj) => Promise.allSettled( | ||
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 > jimpObj.bitmap.width && !forceUpscale) { | ||
return Promise.resolve(); | ||
} | ||
// Save all 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) => jimpObj | ||
.clone() | ||
.resize(scaleWidth, jimp__default['default'].AUTO) | ||
.quality(quality) | ||
.write(`${imagePathPre}@${scaleWidth}w.${format}`)), | ||
); | ||
}), | ||
))); | ||
}); | ||
}); | ||
}); | ||
}, | ||
@@ -132,0 +143,0 @@ }; |
import fs from 'fs'; | ||
import globby from 'globby'; | ||
import jimp from 'jimp'; | ||
import Queue from 'promise-queue'; | ||
@@ -23,3 +24,4 @@ /** | ||
forceUpscale = false, // whether or not we should forcibly upscale | ||
skipExisting = true, // whether we should skip existing images that have already been resized. | ||
skipExisting = true, // whether we should skip existing images that have already been resized | ||
maxParallel = 4, // the max number of parallel images that can be processed concurrently | ||
} = options; | ||
@@ -46,76 +48,84 @@ | ||
return globby( | ||
// Finds all the images we want based on dir and inputFormat | ||
`${dirGlob}/**/*.{${inputFormat.join(',')}}`, | ||
) | ||
.then((images) => Promise.allSettled( | ||
// Map them into jimp objects | ||
images | ||
.filter((d) => d.indexOf('@') < 0 && d.indexOf('#') < 0) | ||
.map((image) => { | ||
// generate the output path | ||
const imagePathSplit = image.split('.'); | ||
const imagePathPre = imagePathSplit.slice(0, -1).join('.'); | ||
const imageFormat = imagePathSplit[imagePathSplit.length - 1]; | ||
return new Promise((resolve) => { | ||
// Promise queue so that we only process maxParallel parallel files at a time | ||
const q = new Queue(maxParallel, Infinity, { onEmpty: () => resolve() }); | ||
// 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)), | ||
)); | ||
globby( | ||
// Finds all the images we want based on dir and inputFormat | ||
`${dirGlob}/**/*.{${inputFormat.join(',')}}`, | ||
) | ||
.then((images) => { | ||
// Map them into jimp objects | ||
images | ||
.filter((d) => d.indexOf('@') < 0 && d.indexOf('#') < 0) | ||
.forEach((image) => { | ||
// generate the output path | ||
const imagePathSplit = image.split('.'); | ||
const imagePathPre = imagePathSplit.slice(0, -1).join('.'); | ||
const imageFormat = imagePathSplit[imagePathSplit.length - 1]; | ||
// 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 }))], | ||
[], | ||
); | ||
// 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)), | ||
)); | ||
// if skipExisting is set | ||
if (skipExisting) { | ||
// Filter out images that already exist | ||
outputs = outputs.filter( | ||
({ format, scaleWidth }) => !fs.existsSync(`${imagePathPre}@${scaleWidth}w.${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 images already exist, we can skip this rest of this process | ||
if (outputs.length === 0) return null; | ||
} | ||
// if skipExisting is set | ||
if (skipExisting) { | ||
// Filter out images that already exist | ||
outputs = outputs.filter( | ||
({ format, scaleWidth }) => !fs.existsSync(`${imagePathPre}@${scaleWidth}w.${format}`), | ||
); | ||
// //////////////////////////////////////////// | ||
// Everything below is expensive, so we want to short-circuit this as much as possible | ||
// load in the image | ||
return jimp.read(image) | ||
.then((jimpObj) => Promise.allSettled( | ||
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 > jimpObj.bitmap.width && !forceUpscale) { | ||
return Promise.resolve(); | ||
} | ||
// if images already exist, we can skip this rest of this process | ||
if (outputs.length === 0) return; | ||
} | ||
// 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) => jimpObj | ||
.clone() | ||
.resize(scaleWidth, jimp.AUTO) | ||
.quality(quality) | ||
.write(`${imagePathPre}@${scaleWidth}w.${format}`)), | ||
); | ||
}), | ||
)); | ||
}).filter((d) => !!d), | ||
)); | ||
// //////////////////////////////////////////// | ||
// Everything below is expensive, so short-circuit this as much as possible | ||
// load in the image | ||
q.add(() => jimp.read(image) | ||
.then((jimpObj) => Promise.allSettled( | ||
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 > jimpObj.bitmap.width && !forceUpscale) { | ||
return Promise.resolve(); | ||
} | ||
// Save all 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) => jimpObj | ||
.clone() | ||
.resize(scaleWidth, jimp.AUTO) | ||
.quality(quality) | ||
.write(`${imagePathPre}@${scaleWidth}w.${format}`)), | ||
); | ||
}), | ||
))); | ||
}); | ||
}); | ||
}); | ||
}, | ||
@@ -122,0 +132,0 @@ }; |
{ | ||
"name": "rollup-generate-image-sizes", | ||
"version": "0.0.12", | ||
"version": "0.0.13", | ||
"description": "Automatically generate image sizes for use in srcsets.", | ||
@@ -37,3 +37,4 @@ "main": "dist/rollup-generate-image-sizes.cjs.js", | ||
"globby": "11.0.1", | ||
"jimp": "^0.16.1" | ||
"jimp": "^0.16.1", | ||
"promise-queue": "^2.2.5" | ||
}, | ||
@@ -40,0 +41,0 @@ "devDependencies": { |
@@ -28,2 +28,3 @@ # rollup-generate-image-sizes | ||
skipExisting: true, | ||
maxParallel: 4, | ||
}) | ||
@@ -53,3 +54,5 @@ ] | ||
`maxParallel` (default: 4 | `int`): the max number of parallel images that can be processed concurrently. | ||
## License | ||
MIT |
146
src/index.js
import fs from 'fs'; | ||
import globby from 'globby'; | ||
import jimp from 'jimp'; | ||
import Queue from 'promise-queue'; | ||
@@ -23,3 +24,4 @@ /** | ||
forceUpscale = false, // whether or not we should forcibly upscale | ||
skipExisting = true, // whether we should skip existing images that have already been resized. | ||
skipExisting = true, // whether we should skip existing images that have already been resized | ||
maxParallel = 4, // the max number of parallel images that can be processed concurrently | ||
} = options; | ||
@@ -46,78 +48,86 @@ | ||
return globby( | ||
// Finds all the images we want based on dir and inputFormat | ||
`${dirGlob}/**/*.{${inputFormat.join(',')}}`, | ||
) | ||
.then((images) => Promise.allSettled( | ||
// Map them into jimp objects | ||
images | ||
.filter((d) => d.indexOf('@') < 0 && d.indexOf('#') < 0) | ||
.map((image) => { | ||
// generate the output path | ||
const imagePathSplit = image.split('.'); | ||
const imagePathPre = imagePathSplit.slice(0, -1).join('.'); | ||
const imageFormat = imagePathSplit[imagePathSplit.length - 1]; | ||
return new Promise((resolve) => { | ||
// Promise queue so that we only process maxParallel parallel files at a time | ||
const q = new Queue(maxParallel, Infinity, { onEmpty: () => resolve() }); | ||
// 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)), | ||
)); | ||
globby( | ||
// Finds all the images we want based on dir and inputFormat | ||
`${dirGlob}/**/*.{${inputFormat.join(',')}}`, | ||
) | ||
.then((images) => { | ||
// Map them into jimp objects | ||
images | ||
.filter((d) => d.indexOf('@') < 0 && d.indexOf('#') < 0) | ||
.forEach((image) => { | ||
// generate the output path | ||
const imagePathSplit = image.split('.'); | ||
const imagePathPre = imagePathSplit.slice(0, -1).join('.'); | ||
const imageFormat = imagePathSplit[imagePathSplit.length - 1]; | ||
// 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 }))], | ||
[], | ||
); | ||
// 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)), | ||
)); | ||
// if skipExisting is set | ||
if (skipExisting) { | ||
// Filter out images that already exist | ||
outputs = outputs.filter( | ||
({ format, scaleWidth }) => !fs.existsSync(`${imagePathPre}@${scaleWidth}w.${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 images already exist, we can skip this rest of this process | ||
if (outputs.length === 0) return null; | ||
} | ||
// if skipExisting is set | ||
if (skipExisting) { | ||
// Filter out images that already exist | ||
outputs = outputs.filter( | ||
({ format, scaleWidth }) => !fs.existsSync(`${imagePathPre}@${scaleWidth}w.${format}`), | ||
); | ||
// //////////////////////////////////////////// | ||
// Everything below is expensive, so we want to short-circuit this as much as possible | ||
// load in the image | ||
return jimp.read(image) | ||
.then((jimpObj) => Promise.allSettled( | ||
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 > jimpObj.bitmap.width && !forceUpscale) { | ||
return Promise.resolve(); | ||
} | ||
// if images already exist, we can skip this rest of this process | ||
if (outputs.length === 0) return; | ||
} | ||
// 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) => jimpObj | ||
.clone() | ||
.resize(scaleWidth, jimp.AUTO) | ||
.quality(quality) | ||
.write(`${imagePathPre}@${scaleWidth}w.${format}`)), | ||
); | ||
}), | ||
)); | ||
}).filter((d) => !!d), | ||
)); | ||
// //////////////////////////////////////////// | ||
// Everything below is expensive, so short-circuit this as much as possible | ||
// load in the image | ||
q.add(() => jimp.read(image) | ||
.then((jimpObj) => Promise.allSettled( | ||
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 > jimpObj.bitmap.width && !forceUpscale) { | ||
return Promise.resolve(); | ||
} | ||
// Save all 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) => jimpObj | ||
.clone() | ||
.resize(scaleWidth, jimp.AUTO) | ||
.quality(quality) | ||
.write(`${imagePathPre}@${scaleWidth}w.${format}`)), | ||
); | ||
}), | ||
))); | ||
}); | ||
}); | ||
}); | ||
}, | ||
}; | ||
}; |
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
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
21916
359
57
3
+ Addedpromise-queue@^2.2.5
+ Addedpromise-queue@2.2.5(transitive)