Skip to content

Commit 9df2d34

Browse files
committed
Minor code refactor
1 parent e21c418 commit 9df2d34

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

img.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ const Util = require("./src/util.js");
1818

1919
const debug = require("debug")("Eleventy:Image");
2020

21-
const KEYS = {
22-
requested: "requested"
23-
};
24-
2521
const GLOBAL_OPTIONS = {
2622
widths: ["auto"],
2723
formats: ["webp", "jpeg"], // "png", "svg", "avif"
@@ -545,7 +541,7 @@ class Image {
545541
let fullStats = this.getFullStats(metadata);
546542
for(let outputFormat in fullStats) {
547543
for(let stat of fullStats[outputFormat]) {
548-
if(this.options.useCache && diskCache.isCached(stat.outputPath, input, this.options.generatedVia !== KEYS.requested)){
544+
if(this.options.useCache && diskCache.isCached(stat.outputPath, input, !Util.isRequested(this.options.generatedVia))) {
549545
// Cached images already exist in output
550546
let contents;
551547
if(this.options.dryRun) {
@@ -756,7 +752,7 @@ function logProcessedMessage(eleventyConfig, src, opts) {
756752
}
757753

758754
function setupLogger(eleventyConfig, opts) {
759-
if(typeof eleventyConfig?.logger?.logWithOptions !== "function" || opts.generatedVia === KEYS.requested) {
755+
if(typeof eleventyConfig?.logger?.logWithOptions !== "function" || Util.isRequested(opts.generatedVia)) {
760756
return;
761757
}
762758

@@ -811,7 +807,7 @@ function queueImage(src, opts) {
811807
if(resolvedOptions.useCache) {
812808
// we don’t know the output format yet, but this hash is just for the in memory cache
813809
key = img.getInMemoryCacheKey();
814-
let cached = memCache.get(key, !opts.transformOnRequest && opts.generatedVia !== KEYS.requested);
810+
let cached = memCache.get(key, !opts.transformOnRequest && !Util.isRequested(opts.generatedVia));
815811
if(cached) {
816812
return cached;
817813
}
@@ -884,7 +880,6 @@ module.exports.statsSync = Image.statsSync;
884880
module.exports.statsByDimensionsSync = Image.statsByDimensionsSync;
885881
module.exports.getFormats = Image.getFormatsArray;
886882
module.exports.getWidths = Image.getValidWidths;
887-
module.exports.keys = KEYS;
888883

889884
module.exports.getHash = function getHash(src, options) {
890885
let img = new Image(src, options);

src/on-request-during-serve-plugin.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const fs = require("fs");
22
const { TemplatePath } = require("@11ty/eleventy-utils");
33

44
const eleventyImage = require("../img.js");
5-
const KEYS = eleventyImage.keys;
65
const Util = require("./util.js");
76

87
const debug = require("debug")("Eleventy:Image");
@@ -46,7 +45,7 @@ function eleventyImageOnRequestDuringServePlugin(eleventyConfig, options = {}) {
4645
},
4746

4847
transformOnRequest: false, // use the built images so we don’t go in a loop
49-
generatedVia: KEYS.requested
48+
generatedVia: Util.KEYS.requested,
5049
});
5150

5251
if(eleventyConfig) {

src/util.js

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ const path = require("path");
22
const { URL } = require("url");
33

44
class Util {
5+
static KEYS = {
6+
requested: "requested"
7+
};
8+
59
/*
610
* Does not mutate, returns new Object.
711
*/
@@ -43,6 +47,10 @@ class Util {
4347
// if the image src is absolute, make it relative to the input/content directory.
4448
return path.join(input, src);
4549
}
50+
51+
static isRequested(generatedVia) {
52+
return generatedVia === this.KEYS.requested;
53+
}
4654
}
4755

4856
// Temporary alias for changes made in https://github.com/11ty/eleventy-img/pull/138

0 commit comments

Comments
 (0)