Skip to content

Commit

Permalink
Adds eleventyConfig.resolvePlugin to get built-in plugins by name (to…
Browse files Browse the repository at this point in the history
… avoid global import from app or plugin code). Makes built-in plugins unique (de-dupes multiple adds). Fixes #3314
  • Loading branch information
zachleat committed Jun 8, 2024
1 parent ab8482f commit d187d2c
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/Plugins/HtmlBasePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,11 @@ Object.defineProperty(eleventyHtmlBasePlugin, "eleventyPackage", {
value: "@11ty/eleventy/html-base-plugin",
});

Object.defineProperty(eleventyHtmlBasePlugin, "eleventyPluginOptions", {
value: {
unique: true,
},
});

export default eleventyHtmlBasePlugin;
export { transformUrl as applyBaseToUrl };
14 changes: 12 additions & 2 deletions src/Plugins/I18nPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ function getLocaleUrlsMap(urlToInputPath, extensionMap, options = {}) {
return urlMap;
}

function EleventyPlugin(eleventyConfig, opts = {}) {
function eleventyI18nPlugin(eleventyConfig, opts = {}) {
let options = DeepCopy(
{
defaultLanguage: "",
Expand Down Expand Up @@ -304,4 +304,14 @@ function EleventyPlugin(eleventyConfig, opts = {}) {

export { Comparator, LangUtils };

export default EleventyPlugin;
Object.defineProperty(eleventyI18nPlugin, "eleventyPackage", {
value: "@11ty/eleventy/i18n-plugin",
});

Object.defineProperty(eleventyI18nPlugin, "eleventyPluginOptions", {
value: {
unique: true,
},
});

export default eleventyI18nPlugin;
12 changes: 12 additions & 0 deletions src/Plugins/InputPathToUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,16 @@ function TransformPlugin(eleventyConfig, defaultOptions = {}) {
});
}

Object.defineProperty(TransformPlugin, "eleventyPackage", {
value: "@11ty/eleventy/inputpath-to-url-plugin",
});

Object.defineProperty(TransformPlugin, "eleventyPluginOptions", {
value: {
unique: true,
},
});

export default TransformPlugin;

export { FilterPlugin, TransformPlugin };
16 changes: 13 additions & 3 deletions src/Plugins/RenderPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ async function renderShortcodeFn(fn, data) {
* @param {module:11ty/eleventy/UserConfig} eleventyConfig - User-land configuration instance.
* @param {Object} options - Plugin options
*/
function EleventyPlugin(eleventyConfig, options = {}) {
function eleventyRenderPlugin(eleventyConfig, options = {}) {
/**
* @typedef {Object} options
* @property {string} [tagName] - The shortcode name to render a template string.
Expand Down Expand Up @@ -380,7 +380,7 @@ class RenderManager {
this.templateConfig.setDirectories(new ProjectDirectories());

// This is the only plugin running on the Edge
this.templateConfig.userConfig.addPlugin(EleventyPlugin, {
this.templateConfig.userConfig.addPlugin(eleventyRenderPlugin, {
templateConfig: this.templateConfig,
accessGlobalData: true,
});
Expand Down Expand Up @@ -447,6 +447,16 @@ class RenderManager {
}
}

export default EleventyPlugin;
Object.defineProperty(eleventyRenderPlugin, "eleventyPackage", {
value: "@11ty/eleventy/render-plugin",
});

Object.defineProperty(eleventyRenderPlugin, "eleventyPluginOptions", {
value: {
unique: true,
},
});

export default eleventyRenderPlugin;

export { compileFile as File, compile as String, RenderManager };
28 changes: 25 additions & 3 deletions src/UserConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,11 @@ class UserConfig {
}

/* Async friendly in 3.0 */
addPlugin(plugin, options) {
addPlugin(plugin, options = {}) {
if (plugin?.eleventyPluginOptions?.unique && this.hasPlugin(plugin)) {
return;
}

if (this.isPluginExecution() || options?.immediate) {
// might return a promise
return this._executePlugin(plugin, options);
Expand All @@ -410,8 +414,26 @@ class UserConfig {
}
}

hasPlugin(name) {
return this.plugins.some((entry) => this._getPluginName(entry.plugin) === name);
async resolvePlugin(name) {
let filenameLookup = {
"@11ty/eleventy/html-base-plugin": "./Plugins/HtmlBasePlugin.js",
"@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.`);
}
// TODO add support for any npm package name.
let plugin = await import(filenameLookup[name]);
return plugin.default;
}

hasPlugin(plugin) {
if (typeof plugin !== "string") {
plugin = this._getPluginName(plugin);
}
return this.plugins.some((entry) => this._getPluginName(entry.plugin) === plugin);
}

// Using Function.name https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name#examples
Expand Down
41 changes: 41 additions & 0 deletions test/HtmlBasePluginTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,3 +565,44 @@ test("Using the HTML base plugin with pathPrefix: /test/ and transformed attribu
</html>`
);
});

test("HTML base plugin only adds once (unique)", async (t) => {
t.plan(2);
let elev = new Eleventy("./test/stubs-base/", "./test/stubs-base/_site", {
configPath: false,
config: function (eleventyConfig) {
// Runs before defaultConfig.js
t.is(eleventyConfig.plugins.length, 0);
eleventyConfig.addPlugin(HtmlBasePlugin);
eleventyConfig.addPlugin(HtmlBasePlugin);
eleventyConfig.addPlugin(HtmlBasePlugin);
eleventyConfig.addPlugin(HtmlBasePlugin);
t.is(eleventyConfig.plugins.length, 1);
},
});
await elev.init();
});

test("HTML base plugin can resolve by name", async (t) => {
t.plan(2);
let elev = new Eleventy("./test/stubs-base/", "./test/stubs-base/_site", {
configPath: false,
config: async function (eleventyConfig) {
// Runs before defaultConfig.js
t.is(eleventyConfig.plugins.length, 0);

let plugin = await eleventyConfig.resolvePlugin("@11ty/eleventy/html-base-plugin");
eleventyConfig.addPlugin(plugin);

// does not add duplicate
eleventyConfig.addPlugin(plugin);

// does not add duplicate even with a different reference
eleventyConfig.addPlugin(HtmlBasePlugin);
eleventyConfig.addPlugin(HtmlBasePlugin);

t.is(eleventyConfig.plugins.length, 1);
},
});
await elev.init();
});

0 comments on commit d187d2c

Please sign in to comment.