diff --git a/eleventy-image.webc b/eleventy-image.webc index 17fd5cf..bd072fb 100644 --- a/eleventy-image.webc +++ b/eleventy-image.webc @@ -98,6 +98,14 @@ async function imagePlugin(attributes, globalPluginOptions) { let options = Object.assign({}, globalPluginOptions, instanceOptions); + // see Util.addConfig + if(globalPluginOptions.eleventyConfig) { + Object.defineProperty(options, "eleventyConfig", { + value: globalPluginOptions.eleventyConfig, + enumerable: false, + }); + } + let metadata = await imagePackage(src, options); let imageAttributes = Object.assign({}, defaultGlobalAttributes, attributes); diff --git a/src/global-options.js b/src/global-options.js index 6ce24f7..9d528c3 100644 --- a/src/global-options.js +++ b/src/global-options.js @@ -1,4 +1,5 @@ const path = require("path"); +const Util = require("./util.js"); function getGlobalOptions(eleventyConfig, options, via) { let directories = eleventyConfig.directories; @@ -9,14 +10,10 @@ function getGlobalOptions(eleventyConfig, options, via) { outputDir: path.join(directories.output, options.urlPath || ""), }, options); - // globalOptions.eleventyConfig = eleventyConfig; globalOptions.directories = directories; globalOptions.generatedVia = via; - Object.defineProperty(globalOptions, "eleventyConfig", { - value: eleventyConfig, - enumerable: false, - }); + Util.addConfig(eleventyConfig, globalOptions); return globalOptions; } diff --git a/src/image-attrs-to-posthtml-node.js b/src/image-attrs-to-posthtml-node.js index 329efe9..70d37bf 100644 --- a/src/image-attrs-to-posthtml-node.js +++ b/src/image-attrs-to-posthtml-node.js @@ -1,4 +1,5 @@ const eleventyImage = require("../img.js"); +const Util = require("./util.js"); const ATTR_PREFIX = "eleventy:"; @@ -57,13 +58,8 @@ async function imageAttributesToPosthtmlNode(attributes, instanceOptions, global } } - let cfg = globalPluginOptions.eleventyConfig; let options = Object.assign({}, globalPluginOptions, instanceOptions); - - Object.defineProperty(options, "eleventyConfig", { - value: cfg, - enumerable: false, - }); + Util.addConfig(globalPluginOptions.eleventyConfig, options); let metadata = await eleventyImage(attributes.src, options); let imageAttributes = Object.assign({}, globalPluginOptions.defaultAttributes, attributes); diff --git a/src/on-request-during-serve-plugin.js b/src/on-request-during-serve-plugin.js index f4e95d9..48e3360 100644 --- a/src/on-request-during-serve-plugin.js +++ b/src/on-request-during-serve-plugin.js @@ -48,12 +48,7 @@ function eleventyImageOnRequestDuringServePlugin(eleventyConfig, options = {}) { generatedVia: Util.KEYS.requested, }); - if(eleventyConfig) { - Object.defineProperty(opts, "eleventyConfig", { - value: eleventyConfig, - enumerable: false, - }); - } + Util.addConfig(eleventyConfig, opts); debug( `%o transformed on request to %o at %o width.`, src, imageFormat, width ); diff --git a/src/util.js b/src/util.js index 8db9400..cb58f86 100644 --- a/src/util.js +++ b/src/util.js @@ -51,6 +51,17 @@ class Util { static isRequested(generatedVia) { return generatedVia === this.KEYS.requested; } + + static addConfig(eleventyConfig, options) { + if(!eleventyConfig) { + return; + } + + Object.defineProperty(options, "eleventyConfig", { + value: eleventyConfig, + enumerable: false, + }); + } } // Temporary alias for changes made in https://github.com/11ty/eleventy-img/pull/138