Skip to content

Commit

Permalink
Restore via param for #223 to reuse correct global options.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Apr 21, 2024
1 parent 3a04484 commit f3e8e8d
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 8 deletions.
4 changes: 2 additions & 2 deletions img.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ class Image {

// Compatible with eleventy-dev-server and Eleventy 3.0.0-alpha.7+ in serve mode.
if(this.options.transformOnRequest && !this.options.urlFormat) {
this.options.urlFormat = function({ src, width, format }/*, imageOptions*/) {
return `/.11ty/image/?src=${encodeURIComponent(src)}&width=${width}&format=${format}`;
this.options.urlFormat = function({ src, width, format }/*, imageOptions*/, options) {
return `/.11ty/image/?src=${encodeURIComponent(src)}&width=${width}&format=${format}${options.generatedVia ? `&via=${options.generatedVia}` : ""}`;
};

this.options.statsOnly = true;
Expand Down
3 changes: 2 additions & 1 deletion src/global-options.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require("path");

function getGlobalOptions(directories, options) {
function getGlobalOptions(directories, options, via) {
let globalOptions = Object.assign({
packages: {
image: require("../"),
Expand All @@ -9,6 +9,7 @@ function getGlobalOptions(directories, options) {
}, options);

globalOptions.directories = directories;
globalOptions.generatedVia = via;

return globalOptions;
}
Expand Down
2 changes: 1 addition & 1 deletion src/transform-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function eleventyImageTransformPlugin(eleventyConfig, options = {}) {

// Notably, global options are not shared automatically with the WebC `eleventyImagePlugin` above.
// Devs can pass in the same object to both if they want!
let opts = getGlobalOptions(eleventyConfig.directories, options);
let opts = getGlobalOptions(eleventyConfig.directories, options, "transform");

eleventyConfig.addJavaScriptFunction("__private_eleventyImageTransformConfigurationOptions", () => {
return opts;
Expand Down
2 changes: 1 addition & 1 deletion src/webc-options-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function eleventyWebcOptionsPlugin(eleventyConfig, options = {}) {
// Notably, global options are not shared automatically with the `eleventyImageTransformPlugin` below.
// Devs can pass in the same object to both if they want!
eleventyConfig.addJavaScriptFunction("__private_eleventyImageConfigurationOptions", () => {
return getGlobalOptions(eleventyConfig.directories, options);
return getGlobalOptions(eleventyConfig.directories, options, "webc");
});

if(options.transformOnRequest !== false) {
Expand Down
2 changes: 1 addition & 1 deletion test/test-webc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@ test("With transform on request during dev mode", async t => {
});

let results = await elev.toJSON();
t.is(normalizeEscapedPaths(results[0].content), `<img loading="lazy" src="/.11ty/image/?src=.%2Ftest%2Fbio-2017.jpg&amp;width=1280&amp;format=jpeg" alt="My ugly mug" width="1280" height="853">`);
t.is(normalizeEscapedPaths(results[0].content), `<img loading="lazy" src="/.11ty/image/?src=.%2Ftest%2Fbio-2017.jpg&amp;width=1280&amp;format=jpeg&amp;via=webc" alt="My ugly mug" width="1280" height="853">`);
});
4 changes: 2 additions & 2 deletions test/transform-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ test("Using the transform plugin with transform on request during dev mode", asy
});

let results = await elev.toJSON();
t.is(normalizeEscapedPaths(results[0].content), `<img src="/.11ty/image/?src=test%2Fbio-2017.jpg&width=1280&format=jpeg" alt="My ugly mug" width="1280" height="853">`);
t.is(normalizeEscapedPaths(results[0].content), `<img src="/.11ty/image/?src=test%2Fbio-2017.jpg&width=1280&format=jpeg&via=transform" alt="My ugly mug" width="1280" height="853">`);
});

test("Using the transform plugin with transform on request during dev mode (with default attributes)", async t => {
Expand All @@ -72,7 +72,7 @@ test("Using the transform plugin with transform on request during dev mode (with
});

let results = await elev.toJSON();
t.is(normalizeEscapedPaths(results[0].content), `<img loading="lazy" src="/.11ty/image/?src=test%2Fbio-2017.jpg&width=1280&format=jpeg" alt="My ugly mug" width="1280" height="853">`);
t.is(normalizeEscapedPaths(results[0].content), `<img loading="lazy" src="/.11ty/image/?src=test%2Fbio-2017.jpg&width=1280&format=jpeg&via=transform" alt="My ugly mug" width="1280" height="853">`);
});


Expand Down

0 comments on commit f3e8e8d

Please sign in to comment.