Skip to navigation Skip to main content
11ty Logo Sustainability Fundraising
Eleventy
Eleventy Documentation
Stable
2.0.1
Canary
3.0.0-alpha.16
Toggle Menu
Eleventy 1.93s
Astro 22.90s

Plugins

Contents

Plugins are custom code that Eleventy can import into a project from an external repository.

安装插件 Jump to heading

Installation Jump to heading

We use the npm command line tool (included with Node.js) to install plugins.

Looking for a plugin? Check out the official plugins or community-contributed plugins.

npm install @11ty/eleventy-plugin-rss --save

在配置文件中为 Eleventy 添加插件 Jump to heading

假设你的配置文件名为 .eleventy.js

Filename .eleventy.js
const pluginRss = require("@11ty/eleventy-plugin-rss");
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(pluginRss);
};

插件的配置参数 Jump to heading

addPlugin 函数设置第二个参数(可选)来自定义插件的行为。这一参数是特定于插件的。请查阅相应插件的文档 (例如,eleventy-plugin-syntaxhighlight 的 README 文件) 以了解其支持哪些参数。

const pluginSyntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(pluginSyntaxHighlight, {
// only install the markdown highlighter
templateFormats: ["md"],

init: function ({ Prism }) {
// Add your own custom language to Prism!
},
});
};
Advanced Usage: Namespacing a plugin

It’s unlikely you’ll need this feature but you can namespace parts of your configuration using eleventyConfig.namespace. This will add a string prefix to all filters, tags, helpers, shortcodes, collections, and transforms.

Filename .eleventy.js
const pluginRss = require("@11ty/eleventy-plugin-rss");
module.exports = function (eleventyConfig) {
eleventyConfig.namespace("myPrefix_", () => {
// the rssLastUpdatedDate filter is now myPrefix_rssLastUpdatedDate
eleventyConfig.addPlugin(pluginRss);
});
};
WARNING:
插件的命名空间是 Eleventy 赋予网站程序的功能,不能用在你自己创建的插件(配置)中。请查看 Issue #256

Creating a Plugin Jump to heading

A plugin primarily provides a “configuration function.” This function is called when Eleventy is first initialized, and operates similarly to a user’s configuration function (the same eleventyConfig argument passed to the user’s .eleventy.js file is passed here), in addition to any config passed by the user:

Filename plugin.js
module.exports = function (eleventyConfig, pluginOptions) {
// Your plugin code goes here
};

Note that plugins run as a second stage after the user’s primary configuration file has executed (to have access to the return object values).

Advanced Usage: Custom Plugin Arguments

If you want to allow developers to use custom arguments provided by your plugin, you can export an object. Prefer using the above syntax unless you need this behavior. For an example of how this is used, see the syntax highlighting plugin

Filename fancy-plugin.js
module.exports = {
initArguments: {},
configFunction: function (eleventyConfig, pluginOptions) {
// Your plugin code goes here
},
};
Filename .eleventy.js
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(require("./fancy-plugin.js"), {
init: function (initArguments) {
// `this` is the eleventyConfig object
// initArguments will be the `myInitArguments` object from above
},
});
};

Feature Testing Jump to heading

If your plugin requires a specific feature in Eleventy, you should feature test it!

module.exports = function (eleventyConfig, pluginOptions) {
if(!("addTemplate" in eleventyConfig)) {
console.log( `[my-test-plugin] WARN Eleventy plugin compatibility: Virtual Templates are required for this plugin, please use Eleventy v3.0 or newer.` );
}
};

Version Checking Jump to heading

If feature testing is not available for your specific use case, you can add this code to your plugin configuration to show a warning if the plugin consumer does not have a compatible version of Eleventy:

module.exports = function (eleventyConfig, pluginOptions) {
try {
// Emit a warning message if the application is not using Eleventy 3.0 or newer (including prereleases).
eleventyConfig.versionCheck(">=3.0");
} catch(e) {
console.log( `[my-test-plugin] WARN Eleventy plugin compatibility: ${e.message}` );
}
};

Distributing a Plugin Jump to heading

If you’re distributing your plugin as a package, consider following these conventions. These are not hard requirements.

Advanced: Execute a plugin immediately Pre-release only: v3.0.0-alpha.5 Jump to heading

Plugins (by default) execute in a second stage of configuration after the user’s configuration file has finished, in order to have access to the return object in the configuration callback.

You are unlikely to need this, but you can execute a plugin’s code immediately using the immediate option.

module.exports = function (eleventyConfig, pluginOptions) {
console.log( "first" );

eleventyConfig.addPlugin(eleventyConfig => {
console.log("fourth");
});

eleventyConfig.addPlugin(eleventyConfig => {
console.log("second");
}, {
immediate: true
});

console.log("third");
};

Plugins: