Skip to content

Commit

Permalink
Applies to 11ty/eleventy-plugin-rss#52 resolvePlugin only needs to be…
Browse files Browse the repository at this point in the history
… async for i18n plugin.
  • Loading branch information
zachleat committed Jun 17, 2024
1 parent 81f1f1d commit 7513f21
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
28 changes: 22 additions & 6 deletions src/UserConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import debugUtil from "debug";
import { RetrieveGlobals } from "node-retrieve-globals";
import { DeepCopy, TemplatePath } from "@11ty/eleventy-utils";

import HtmlBasePlugin from "./Plugins/HtmlBasePlugin.js";
import RenderPlugin from "./Plugins/RenderPlugin.js";
import InputPathToUrlPlugin from "./Plugins/InputPathToUrl.js";
// import I18nPlugin from "./Plugins/I18nPlugin.js";

import EventEmitter from "./Util/AsyncEventEmitter.js";
import EleventyCompatibility from "./Util/Compatibility.js";
import EleventyBaseError from "./Errors/EleventyBaseError.js";
Expand Down Expand Up @@ -405,7 +410,7 @@ class UserConfig {
}

if (this.isPluginExecution() || options?.immediate) {
// might return a promise
// this might return a promise
return this._executePlugin(plugin, options);
} else {
this.plugins.push({
Expand All @@ -418,19 +423,30 @@ class UserConfig {

async resolvePlugin(name) {
let filenameLookup = {
"@11ty/eleventy/html-base-plugin": "./Plugins/HtmlBasePlugin.js",
"@11ty/eleventy/html-base-plugin": HtmlBasePlugin,
"@11ty/eleventy/render-plugin": RenderPlugin,
"@11ty/eleventy/inputpath-to-url-plugin": InputPathToUrlPlugin,

// requires async (`await resolvePlugin("@11ty/eleventy/i18n-plugin")`)
// to avoid preloading i18n dependencies.
// see https://github.com/11ty/eleventy-plugin-rss/issues/52
"@11ty/eleventy/i18n-plugin": "./Plugins/I18nPlugin.js",
"@11ty/eleventy/render-plugin": "./Plugins/RenderPlugin.js",
"@11ty/eleventy/inputpath-to-url-plugin": "./Plugins/InputPathToUrl.js",
};

if (!filenameLookup[name]) {
throw new Error(
`Invalid name "${name}" passed to resolvePlugin. Valid options: ${Object.keys(filenameLookup).join(", ")}`,
);
}

// Future improvement: add support for any npm package name?
let plugin = await import(filenameLookup[name]);
return plugin.default;
if (typeof filenameLookup[name] === "string") {
// returns promise
return import(filenameLookup[name]).then((plugin) => plugin.default);
}

// return reference
return filenameLookup[name];
}

hasPlugin(plugin) {
Expand Down
20 changes: 10 additions & 10 deletions test/EleventyVirtualTemplatesTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ test("Virtual templates conflict", async (t) => {
});

let e = await t.throwsAsync(async () => {
await elev.toJSON();
await elev.toJSON();
});

t.is(e.message, "Virtual template conflict: you can’t add multiple virtual templates that have the same inputPath: virtual.md");
Expand All @@ -141,20 +141,20 @@ test("RSS virtual templates plugin", async (t) => {
config: function (eleventyConfig) {
eleventyConfig.addTemplate("virtual.md", `# Hello`, { tag: "posts" })

eleventyConfig.addPlugin(feedPlugin, {
type: "atom", // or "rss", "json"
outputPath: "/feed.xml",
collection: {
name: "posts", // iterate over `collections.posts`
limit: 10, // 0 means no limit
},
});
eleventyConfig.addPlugin(feedPlugin, {
type: "atom", // or "rss", "json"
outputPath: "/feed.xml",
collection: {
name: "posts", // iterate over `collections.posts`
limit: 10, // 0 means no limit
},
});
},
});

let results = await elev.toJSON();

t.deepEqual(results.length, 2);
let [ feed ] = results.filter(entry => entry.outputPath.endsWith(".xml"));
let [ feed ] = results.filter(entry => entry.outputPath.endsWith(".xml"));
t.truthy(feed.content.startsWith(`<?xml version="1.0" encoding="utf-8"?>`));
});

0 comments on commit 7513f21

Please sign in to comment.