-
Notifications
You must be signed in to change notification settings - Fork 143
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
Hotreload: Ability to update templates without recompiling #451
Open
untitaker
wants to merge
45
commits into
lambda-fairy:main
Choose a base branch
from
untitaker:hotreload-prototype-v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,039
−194
Conversation
This file contains 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
Fixes lambda-fairy#441. proc_macro_error is marked as unmaintained in rustsec: https://rustsec.org/advisories/RUSTSEC-2024-0370.html Because proc_macro_error2 removed automatic nightly detection, the error messages changed slightly.
This reverts commit 362f6bd.
This is exactly the feature I’m looking for! |
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.
This is a continuation of #392, it adds a way to auto-reload templates to maud without going through rustc at all, for some situations.
Basic usage
Enable the
hotreload
feature, and thehtml!
macro will partially evaluate at runtime. usemaud::html_static!
to force the "normal", fully-compiled behavior.You can use
export MAUD_ON_ERROR=exit
to make maud callstd::process::exit
during template-rendering whenever it turns out the code changes are too severe for hot-reloading. In combination withcargo watch
, this can hot-reload your web app's templates when possible, and recompile when that fails:This has some limitations because now rust code changes unrelated to templates will not cause the application to restart. Needs further improvements.
Internals
maud+hotreload will compile your template into two parts:
file!()
, tries to parse out the original macro invocation, and attempts to use the existing "formatting arguments" with thatThis means that changes such as this can be hot-reloaded:
but this change requires proper recompilation:
the compiled code is very inefficient, as it re-parses rust sourcecode at runtime (still faster than rustc). there are some complications, such as the hot-reloading:
html!
macro is aliased or used from within another macrohtml! { }
is written in a weird way, such ashtml! /* { */ {
Is this really for maud?
Even though there are no added dependencies, the feature is still a lot to maintain and required a lot of restructuring. I think most of the restructuring was unavoidable, and now there are three crates instead of two.
I don't necessarily have expectations that this will be merged, but on the other hand I also really want a template engine that is type-safe, has fast iteration-speeds and can be composed in a JSX-like way using regular Rust functions. I think maud+hotreload is the only standalone templating engine in the Rust ecosystem that can do that right now. So I'd be willing to fork maud and continue development there if people are interested. It seems there are a few other PRs that need attention anyway.
finally, thanks to @wrapperup for the initial prototype and architecture of hot-reloading.