Skip to content

Commit

Permalink
Minor code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed May 11, 2024
1 parent e21c418 commit 9df2d34
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
11 changes: 3 additions & 8 deletions img.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ const Util = require("./src/util.js");

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

const KEYS = {
requested: "requested"
};

const GLOBAL_OPTIONS = {
widths: ["auto"],
formats: ["webp", "jpeg"], // "png", "svg", "avif"
Expand Down Expand Up @@ -545,7 +541,7 @@ class Image {
let fullStats = this.getFullStats(metadata);
for(let outputFormat in fullStats) {
for(let stat of fullStats[outputFormat]) {
if(this.options.useCache && diskCache.isCached(stat.outputPath, input, this.options.generatedVia !== KEYS.requested)){
if(this.options.useCache && diskCache.isCached(stat.outputPath, input, !Util.isRequested(this.options.generatedVia))) {
// Cached images already exist in output
let contents;
if(this.options.dryRun) {
Expand Down Expand Up @@ -756,7 +752,7 @@ function logProcessedMessage(eleventyConfig, src, opts) {
}

function setupLogger(eleventyConfig, opts) {
if(typeof eleventyConfig?.logger?.logWithOptions !== "function" || opts.generatedVia === KEYS.requested) {
if(typeof eleventyConfig?.logger?.logWithOptions !== "function" || Util.isRequested(opts.generatedVia)) {
return;
}

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

module.exports.getHash = function getHash(src, options) {
let img = new Image(src, options);
Expand Down
3 changes: 1 addition & 2 deletions src/on-request-during-serve-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const fs = require("fs");
const { TemplatePath } = require("@11ty/eleventy-utils");

const eleventyImage = require("../img.js");
const KEYS = eleventyImage.keys;
const Util = require("./util.js");

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

transformOnRequest: false, // use the built images so we don’t go in a loop
generatedVia: KEYS.requested
generatedVia: Util.KEYS.requested,
});

if(eleventyConfig) {
Expand Down
8 changes: 8 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ const path = require("path");
const { URL } = require("url");

class Util {
static KEYS = {
requested: "requested"
};

/*
* Does not mutate, returns new Object.
*/
Expand Down Expand Up @@ -43,6 +47,10 @@ class Util {
// if the image src is absolute, make it relative to the input/content directory.
return path.join(input, src);
}

static isRequested(generatedVia) {
return generatedVia === this.KEYS.requested;
}
}

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

0 comments on commit 9df2d34

Please sign in to comment.