Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eleventy.before callback runs after ignores are processed #2207

Closed
TheDocTrier opened this issue Feb 7, 2022 · 8 comments
Closed

eleventy.before callback runs after ignores are processed #2207

TheDocTrier opened this issue Feb 7, 2022 · 8 comments
Labels
feature: 🛠 configuration Related to Eleventy’s Configuration file feature: 🔌 plugins Plugin API

Comments

@TheDocTrier
Copy link
Contributor

TheDocTrier commented Feb 7, 2022

Describe the bug

If a callback is added to the eleventy.before event, that callback is executed after Eleventy has already processed the eleventyConfig.ignores set.

To Reproduce

Create a template ignore.md.
Add callback on eleventy.before event which adds template's path to ignores.
Build the site.
The template will be rendered.

module.exports = function (eleventyConfig) {
  eleventyConfig.on("eleventy.before", function () {
    eleventyConfig.ignores.add("ignore.md");
  });
};

If eleventy --serve is used, then the template will be ignored on subsequent rebuilds.

Here is a simple repo that reproduces the behavior: https://github.com/TheDocTrier/eleventy-before-build-ignore

Expected behavior

The eleventy.before callbacks are run before Eleventy processes the ignores set such that modifications to it are reflected in the build.

Environment:

  • OS and Version: Ubuntu 18.04
  • Eleventy Version 8.1.2

Edit
Update issue and example to use eleventy.before instead of the deprecated beforeBuild.

@TheDocTrier
Copy link
Contributor Author

TheDocTrier commented Feb 7, 2022

See issue #1488 for another instance of beforeBuild running later than expected.

@pdehaan
Copy link
Contributor

pdehaan commented Feb 7, 2022

Quick note since I was looking at the docs for this earlier today, but it appears that the .on("beforeBuild", ...) is deprecated, per https://www.11ty.dev/docs/events/#eleventy.before, and you might want to retry w/ .on("eleventy.before", ...):

eleventy.before (New in v1.0.0)

  • Previously known as the now deprecated (but not removed) beforeBuild (New in v0.11.1) event.
-eleventyConfig.on("beforeBuild", function () {
+eleventyConfig.on('eleventy.before', async () => {

@TheDocTrier
Copy link
Contributor Author

Thank you @pdehaan for pointing that out. The problem is persistent with the new label and I have updated the example repository to reflect this.

@TheDocTrier TheDocTrier changed the title beforeBuild callback runs after ignores are processed eleventy.before callback runs after ignores are processed Feb 7, 2022
@pdehaan
Copy link
Contributor

pdehaan commented Feb 7, 2022

Yep, can confirm on v1.0.0 that calling eleventyConfig.ignores.add() from within .on("eleventy.before") doesn't work, but works if you add it in the top-level eleventy config.

  eleventyConfig.ignores.add("ignore.md"); // WORKS

  eleventyConfig.on("eleventy.before", function () {
    eleventyConfig.ignores.add("ignore.md"); // DOESNT WORK
  });

@TheDocTrier
Copy link
Contributor Author

Yeah, I've used that as a work-around in my Eleventy ignore plugin:
https://github.com/TheDocTrier/eleventy-plugin-ignore/blob/561dc0d3fa161fccf3f86f5980436d93e60e2d67/src/eleventy.ts#L48-L49

updateIgnores();
eleventyConfig.on("eleventy.before", updateIgnores);

@zachleat
Copy link
Member

Sorry, I misspoke a bit there (stand by 😅)

@zachleat
Copy link
Member

zachleat commented Feb 16, 2022

Hmmmmmm.

Configuration is the very first thing that happens, before anything else. It is needed in various constructors in the code (for good or bad, see #2103).

So, to be clear the eleventy.before is called after configuration is initialized.

However, a couple of things conspired here to cause this problem:

  1. plugin callbacks only run once at the beginning of a --watch or --serve (thus meaning you need to use the event, correctly).
  2. eleventy.before runs after Eleventy->init

I agree that it would be best to have eleventy.before run before init but I’m worried that would cause some breakages in how folks are using the event now.

Specifically, this is how the eleventy.before event has always worked.

I think as a stop-gap we can add an additional event (maybe eleventy.beforeInit—open to suggestions) and maybe move eleventy.before up to match as a breaking change in 2.0.

@zachleat zachleat added feature: 🛠 configuration Related to Eleventy’s Configuration file feature: 🔌 plugins Plugin API breaking-change This will have to be included with a major version as it breaks backwards compatibility. and removed needs-triage labels Feb 16, 2022
@zachleat zachleat added this to the Eleventy 3.0.0 milestone Nov 10, 2023
@zachleat
Copy link
Member

zachleat commented Jan 25, 2024

eleventy.beforeConfig is available in v3.0.0-alpha.3 and will ship with v3.0.

It was originally added as an async mechanism to modify configuration in synchronous config callbacks.

module.exports = function (eleventyConfig) {
  eleventyConfig.on("eleventy.beforeConfig", async function (eleventyConfig) {
    // You can do async things in here too

    eleventyConfig.ignores.add("ignore.md");
  });
};

@zachleat zachleat removed the breaking-change This will have to be included with a major version as it breaks backwards compatibility. label Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature: 🛠 configuration Related to Eleventy’s Configuration file feature: 🔌 plugins Plugin API
Projects
None yet
Development

No branches or pull requests

3 participants