Events Jump to heading
You may want to run some code at certain times during the compiling process. To do that, you can use configuration events, which will run at specific times during the compiling process.
All events are configured in your .eleventy.js
configuration file, with the code run every time the event triggers.
Asynchronous callback function support added in v1.0.
eleventy.before
New in v1.0.0
Jump to heading
- Previously known as the now deprecated (but not removed)
beforeBuild
New in v0.11.1 event.
The eleventy.before
event runs every time Eleventy starts building, so it will run before the start of each stand-alone build, as well as each time building starts as either part of --watch
or --serve
. To use it, attach the event handler to your Eleventy config:
module.exports = function (eleventyConfig) {
// Async-friendly in 1.0+
eleventyConfig.on('eleventy.before', async () => {
// Run me before the build starts
});
};
eleventy.after
New in v1.0.0
Jump to heading
- Previously known as the now deprecated (but not removed)
afterBuild
New in v0.11.1 event.
The eleventy.after
event runs every time Eleventy finishes building, so it will run after the end of each stand-alone build, as well as each time building ends as either part of --watch
or --serve
. To use it, attach the event handler to your Eleventy config:
module.exports = function (eleventyConfig) {
// Async-friendly in 1.0+
eleventyConfig.on('eleventy.after', async () => {
// Run me after the build ends
});
};
Event arguments Coming soon in v2.0.0 Jump to heading
Eleventy now provides an object with metadata on the build as an argument to the eleventy.before
and eleventy.after
event callbacks.
module.exports = function (eleventyConfig) {
eleventyConfig.on('eleventy.before', async ({ dir, runMode, outputMode }) => {
// Read more below
});
eleventyConfig.on('eleventy.after', async ({ dir, results, runMode, outputMode }) => {
// Read more below
});
};
dir
: an object with current project directories, set in your configuration file (or populated with Eleventy defaults).dir.input
(default"."
)dir.output
(default"_site"
)dir.includes
(default"_includes"
)dir.data
(default"_data"
)dir.layouts
(no default value)
outputMode
: a string representing the value of--to
on the command linefs
(default)json
ndjson
runMode
: a string representing--serve
or--watch
usage on the command line. One of:build
(default)watch
serve
results
: only available oneleventy.after
. An array with the processed Eleventy output (similar to--to=json
output)- Individual entries will have:
{ inputPath, outputPath, url, content }
- Individual entries will have:
eleventy.beforeWatch
New in v1.0.0
Jump to heading
- Previously known as the now deprecated (but not removed)
beforeWatch
New in v0.11.0 event.
The eleventy.beforeWatch
event runs before a build is run only if it's a re-run during --watch
or --serve
. This means it will neither run during the initial build nor during stand-alone builds. To use it, attach the event handler to your Eleventy config:
module.exports = function (eleventyConfig) {
// Async-friendly in 1.0+
eleventyConfig.on('eleventy.beforeWatch', async (changedFiles) => {
// Run me before --watch or --serve re-runs
// changedFiles is an array of files that changed
// to trigger the watch/serve build
});
};
The changedFiles
parameter was New in v0.11.1.