Skip to content

Commit

Permalink
Fixes #259
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Dec 12, 2024
1 parent 9588fa5 commit 529cc70
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/image-attrs-to-posthtml-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const ATTR = {
WIDTHS: `${ATTR_PREFIX}widths`,
FORMATS: `${ATTR_PREFIX}formats`,
OUTPUT: `${ATTR_PREFIX}output`,
OPTIONAL: `${ATTR_PREFIX}optional`,
};

function convertToPosthtmlNode(obj) {
Expand Down Expand Up @@ -82,6 +83,10 @@ function isIgnored(node) {
return node?.attrs && node?.attrs?.[ATTR.IGNORE] !== undefined;
}

function isOptional(node) {
return node?.attrs && node?.attrs?.[ATTR.OPTIONAL] !== undefined;
}

function getOutputDirectory(node) {
return node?.attrs?.[ATTR.OUTPUT];
}
Expand All @@ -90,5 +95,6 @@ module.exports = {
imageAttributesToPosthtmlNode,
cleanTag,
isIgnored,
isOptional,
getOutputDirectory,
};
17 changes: 9 additions & 8 deletions src/transform-plugin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require("path");
const Util = require("./util.js");
const { imageAttributesToPosthtmlNode, getOutputDirectory, cleanTag, isIgnored } = require("./image-attrs-to-posthtml-node.js");
const { imageAttributesToPosthtmlNode, getOutputDirectory, cleanTag, isIgnored, isOptional } = require("./image-attrs-to-posthtml-node.js");
const { getGlobalOptions } = require("./global-options.js");
const { eleventyImageOnRequestDuringServePlugin } = require("./on-request-during-serve-plugin.js");

Expand Down Expand Up @@ -58,7 +58,13 @@ function transformTag(context, node, opts) {

Object.assign(node, obj);
}, (error) => {
return Promise.reject(error);
if(!isOptional(node) && opts.failOnError) {
return Promise.reject(error);
}

cleanTag(node);

return Promise.resolve();
});
}

Expand Down Expand Up @@ -88,18 +94,13 @@ function eleventyImageTransformPlugin(eleventyConfig, options = {}) {
if(isIgnored(node) || node?.attrs?.src?.startsWith("data:")) {
cleanTag(node);
} else {
// TODO: eleventy:optional
promises.push(transformTag(context, node, opts));
}

return node;
});

if(opts.failOnError) {
await Promise.all(promises);
} else {
await Promise.allSettled(promises);
}
await Promise.all(promises);

return tree;
};
Expand Down
50 changes: 49 additions & 1 deletion test/transform-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,55 @@ test("Don’t throw an error when failOnError: false with a bad remote image req
});
}
});
// elev.disableLogger();
elev.disableLogger();

let results = await elev.toJSON();
t.is(normalizeEscapedPaths(results[0].content), `<img src="https://images.opencollective.com/sdkljflksjdflksdjf_DOES_NOT_EXIST/NOT_EXIST/avatar.png" alt="My ugly mug">`);
});

test("Don’t throw an error when failOnError: true but `eleventy:optional` attribute with a bad remote image request", async t => {
let elev = new Eleventy( "test", "test/_site", {
config: eleventyConfig => {
eleventyConfig.addTemplate("virtual.html", `<img src="https://images.opencollective.com/sdkljflksjdflksdjf_DOES_NOT_EXIST/NOT_EXIST/avatar.png" alt="My ugly mug" eleventy:optional>`);

eleventyConfig.addPlugin(eleventyImageTransformPlugin, {
formats: ["auto"],
// transformOnRequest: true,
// dryRun: true, // don’t write image files!

failOnError: true,

defaultAttributes: {
loading: "lazy",
}
});
}
});
elev.disableLogger();

let results = await elev.toJSON();
t.is(normalizeEscapedPaths(results[0].content), `<img src="https://images.opencollective.com/sdkljflksjdflksdjf_DOES_NOT_EXIST/NOT_EXIST/avatar.png" alt="My ugly mug">`);
});

test("Don’t throw an error when failOnError: false and `eleventy:optional` attribute with a bad remote image request", async t => {
let elev = new Eleventy( "test", "test/_site", {
config: eleventyConfig => {
eleventyConfig.addTemplate("virtual.html", `<img src="https://images.opencollective.com/sdkljflksjdflksdjf_DOES_NOT_EXIST/NOT_EXIST/avatar.png" alt="My ugly mug" eleventy:optional>`);

eleventyConfig.addPlugin(eleventyImageTransformPlugin, {
formats: ["auto"],
// transformOnRequest: true,
// dryRun: true, // don’t write image files!

failOnError: false,

defaultAttributes: {
loading: "lazy",
}
});
}
});
elev.disableLogger();

let results = await elev.toJSON();
t.is(normalizeEscapedPaths(results[0].content), `<img src="https://images.opencollective.com/sdkljflksjdflksdjf_DOES_NOT_EXIST/NOT_EXIST/avatar.png" alt="My ugly mug">`);
Expand Down

0 comments on commit 529cc70

Please sign in to comment.