Skip to content

Commit

Permalink
Small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Feb 12, 2024
1 parent d29b521 commit 4c62cfc
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 45 deletions.
46 changes: 5 additions & 41 deletions img.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
18 changes: 18 additions & 0 deletions src/global-options.js
Original file line number Diff line number Diff line change
@@ -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,
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const eleventyImage = require("../");
const eleventyImage = require("../img");

const ATTR_PREFIX = "eleventy:";

Expand Down
22 changes: 19 additions & 3 deletions src/transformPlugin.js → src/transform-plugin.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 = [];
Expand All @@ -102,4 +114,8 @@ module.exports = function(eleventyConfig, options, globalOptionsCallback) {
eleventyConfig.htmlTransformer.addPosthtmlPlugin(options.extensions, posthtmlPlugin, {
priority: -1, // we want this to go before <base> or inputpath to url
});
}

module.exports = {
eleventyImageTransformPlugin,
};
18 changes: 18 additions & 0 deletions src/webc-options-plugin.js
Original file line number Diff line number Diff line change
@@ -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,
};

0 comments on commit 4c62cfc

Please sign in to comment.