Skip to content

Conversation

@RMcNeely
Copy link

Adds support for options.after that runs at the very end of the compile function, directly before returning the template code.

When using WebC components with this plugin you cannot provide a second override for WebC templates or you cause an error (). Adding this hook inside the options allows you to take the compiled parts of the page and do something with them, alleviate the need for a second override.

This function mimics the behavior of the before function with the addition of the compiled parts passed as a second argument.

after
option that runs before returning.
@RMcNeely
Copy link
Author

I am using this for running ESLint but I imagine that there are other possible use cases where one might want to do some action on the compiled parts of a page. This is what my very eleventyConfig looks like with this option being passed as a rough example of intended use:

export default async function (eleventyConfig) {
  eleventyConfig.addPlugin(pluginWebc, {
    components: [
      "src/_includes/components/*.webc",
      "src/_includes/components/**/*.webc",
    ],
    after: async (page, { html, css, js, buckets, components }) => {

      const eslint = new ESLint({
        overrideConfig: {
          languageOptions: {
            ecmaVersion: 2023,
            globals: globals.browser,
          },
        },
      });

      // From ESLint docs on Node.js API reference
      // see: https://eslint.org/docs/latest/integrate/nodejs-api#eslint-class
      const results = await eslint.lintText(js.join(""));
      const formatter = await eslint.loadFormatter("stylish");
      const resultText = formatter.format(results);

      // 4. Output it.
      console.info(resultText);
    },
    ...
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant