Skip to content

Allow plugins to run before or after expressions plugin - #263

Merged
cossssmin merged 1 commit into
posthtml:mainfrom
wzrdjstr:feature/plugin-run-order
Feb 12, 2026
Merged

Allow plugins to run before or after expressions plugin#263
cossssmin merged 1 commit into
posthtml:mainfrom
wzrdjstr:feature/plugin-run-order

Conversation

@wzrdjstr

Copy link
Copy Markdown
Contributor

Adds a new {before:[], after: []} option to the plugins config, which allows plugins to run before or after posthtml-expressions.

This is helpful when a plugin needs to modify the AST before expressions are evaluated.

For example:

Maizzle uses posthtml-components under the hood.

I have a posthtml plugin that converts Maizzle templates into my web app's native template format (Twig). During conversion, it replaces <if> nodes marked with a twigify attribute with the corresponding {%- if -%} Twig syntax. This plugin must run before posthtml-expressions, because I don't want the tagged <if> nodes to be evaluated and because <if> nodes are removed from the AST when expressions are evaluated, which prevents plugins from processing them later.

I also have another plugin that removes <script locals> before they're evaluated. This allows me to use <script locals> to inject static placeholder data into prototype templates, while preserving uninterpolated {{ }} delimiters in the converted template so they can be filled by my app. This plugin needs to run before posthtml-expressions, so <script locals> are removed before they're evaluated.

This change is backwards-compatible; docs and tests also updated.


Please note the plugin run order was changed after the 2022 refactor (45f00f5). Prior to the refactor, plugins ran before posthtml-expressions. This can be seen in code that's currently commented-out:

// const plugins = [...options.plugins, expressions(options.expressions)];

I'm guessing this was an optimization, e.g. to skip processing nodes that are ultimately removed from the AST by an expression, but just wanted to give you a heads up in case there was some other reason.

@cossssmin

Copy link
Copy Markdown
Member

Hey, this looks great, thanks for working on it!

@cossssmin
cossssmin merged commit 9f03894 into posthtml:main Feb 12, 2026
3 checks passed
@cossssmin

Copy link
Copy Markdown
Member

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.

2 participants