From 4c62cfc74385556f74c9d638923d72b54574ab9c Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Mon, 12 Feb 2024 07:58:05 -0600 Subject: [PATCH] Small refactor --- img.js | 46 ++----------------- src/global-options.js | 18 ++++++++ ...ode.js => image-attrs-to-posthtml-node.js} | 2 +- ...transformPlugin.js => transform-plugin.js} | 22 +++++++-- src/webc-options-plugin.js | 18 ++++++++ 5 files changed, 61 insertions(+), 45 deletions(-) create mode 100644 src/global-options.js rename src/{imageAttributesToPosthtmlNode.js => image-attrs-to-posthtml-node.js} (98%) rename src/{transformPlugin.js => transform-plugin.js} (82%) create mode 100644 src/webc-options-plugin.js diff --git a/img.js b/img.js index f5256b7..568b6d4 100644 --- a/img.js +++ b/img.js @@ -796,45 +796,9 @@ const generateHTML = require("./src/generate-html.js"); module.exports.generateHTML = generateHTML; module.exports.generateObject = generateHTML.generateObject; -function getGlobalOptions(eleventyDirectories, options) { - return Object.assign({ - packages: { - image: module.exports, - }, - outputDir: path.join(eleventyDirectories.output, options.urlPath || ""), - }, options); -} - -module.exports.eleventyImagePlugin = function(eleventyConfig, options = {}) { - let eleventyDirectories; - eleventyConfig.on("eleventy.directories", (dirs) => { - eleventyDirectories = dirs; - }); - - // 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(eleventyDirectories, options); - }); -}; +const { eleventyWebcOptionsPlugin } = require("./src/webc-options-plugin.js"); +module.exports.eleventyImagePlugin = eleventyWebcOptionsPlugin; +module.exports.eleventyImageWebcOptionsPlugin = eleventyWebcOptionsPlugin; -const transformPlugin = require("./src/transformPlugin.js"); -module.exports.eleventyImageTransformPlugin = function(eleventyConfig, options = {}) { - options = Object.assign({ - extensions: "html", - }, options); - - let eleventyDirectories; - eleventyConfig.on("eleventy.directories", (dirs) => { - eleventyDirectories = dirs; - }); - - // Notably, global options are not shared automatically with the WebC `eleventyImagePlugin` above. - // Devs can pass in the same object to both if they want! - transformPlugin(eleventyConfig, options, () => { - let opts = getGlobalOptions(eleventyDirectories, options); - opts.eleventyDirectories = eleventyDirectories; - delete opts.packages; - return opts; - }); -}; +const { eleventyImageTransformPlugin } = require("./src/transform-plugin.js"); +module.exports.eleventyImageTransformPlugin = eleventyImageTransformPlugin; diff --git a/src/global-options.js b/src/global-options.js new file mode 100644 index 0000000..4402256 --- /dev/null +++ b/src/global-options.js @@ -0,0 +1,18 @@ +const path = require("path"); + +function getGlobalOptions(eleventyDirectories, options) { + let globalOptions = Object.assign({ + packages: { + image: require("../"), + }, + outputDir: path.join(eleventyDirectories.output, options.urlPath || ""), + }, options); + + globalOptions.eleventyDirectories = eleventyDirectories; + + return globalOptions; +} + +module.exports = { + getGlobalOptions, +}; diff --git a/src/imageAttributesToPosthtmlNode.js b/src/image-attrs-to-posthtml-node.js similarity index 98% rename from src/imageAttributesToPosthtmlNode.js rename to src/image-attrs-to-posthtml-node.js index 5fd44be..4991828 100644 --- a/src/imageAttributesToPosthtmlNode.js +++ b/src/image-attrs-to-posthtml-node.js @@ -1,4 +1,4 @@ -const eleventyImage = require("../"); +const eleventyImage = require("../img"); const ATTR_PREFIX = "eleventy:"; diff --git a/src/transformPlugin.js b/src/transform-plugin.js similarity index 82% rename from src/transformPlugin.js rename to src/transform-plugin.js index e5d9cc8..b8ef1da 100644 --- a/src/transformPlugin.js +++ b/src/transform-plugin.js @@ -1,5 +1,6 @@ const path = require("path"); -const { imageAttributesToPosthtmlNode, getOutputDirectory, cleanTag, isIgnored } = require("./imageAttributesToPosthtmlNode.js"); +const { imageAttributesToPosthtmlNode, getOutputDirectory, cleanTag, isIgnored } = require("./image-attrs-to-posthtml-node.js"); +const { getGlobalOptions } = require("./global-options.js"); function isFullUrl(url) { try { @@ -75,9 +76,20 @@ function transformTag(context, node, opts) { }); } -module.exports = function(eleventyConfig, options, globalOptionsCallback) { +function eleventyImageTransformPlugin(eleventyConfig, options = {}) { + options = Object.assign({ + extensions: "html", + }, options); + + let eleventyDirectories; + eleventyConfig.on("eleventy.directories", (dirs) => { + eleventyDirectories = dirs; + }); + function posthtmlPlugin(context) { - let opts = globalOptionsCallback(); + // 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(eleventyDirectories, options); return (tree) => { let promises = []; @@ -102,4 +114,8 @@ module.exports = function(eleventyConfig, options, globalOptionsCallback) { eleventyConfig.htmlTransformer.addPosthtmlPlugin(options.extensions, posthtmlPlugin, { priority: -1, // we want this to go before or inputpath to url }); +} + +module.exports = { + eleventyImageTransformPlugin, }; diff --git a/src/webc-options-plugin.js b/src/webc-options-plugin.js new file mode 100644 index 0000000..b3d62e2 --- /dev/null +++ b/src/webc-options-plugin.js @@ -0,0 +1,18 @@ +const { getGlobalOptions } = require("./global-options.js"); + +function eleventyWebcOptionsPlugin(eleventyConfig, options = {}) { + let eleventyDirectories; + eleventyConfig.on("eleventy.directories", (dirs) => { + eleventyDirectories = dirs; + }); + + // 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(eleventyDirectories, options); + }); +} + +module.exports = { + eleventyWebcOptionsPlugin, +};