Allow plugins to run before or after expressions plugin - #263
Merged
Conversation
Member
|
Hey, this looks great, thanks for working on it! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a new
{before:[], after: []}option to thepluginsconfig, which allows plugins to run before or afterposthtml-expressions.This is helpful when a plugin needs to modify the AST before expressions are evaluated.
For example:
Maizzle uses
posthtml-componentsunder the hood.I have a
posthtmlplugin that converts Maizzle templates into my web app's native template format (Twig). During conversion, it replaces<if>nodes marked with atwigifyattribute with the corresponding{%- if -%}Twig syntax. This plugin must run beforeposthtml-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 beforeposthtml-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:posthtml-components/src/index.js
Line 219 in e1a2361
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.