-
Notifications
You must be signed in to change notification settings - Fork 162
Hotreload: Ability to update templates without recompiling #451
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
base: main
Are you sure you want to change the base?
Conversation
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! |
This is a great contribution! Exactly what I'm looking for. I'm in favour of a fork with new contributors if this isn't being actively maintained. |
I would also love something like this. I really like the experience of using Maud but the reload time makes web development quite a bit clunkier than working with a JS framework. |
If this doesn't get merged I'd love to see a fork with this functionality! I am currently using a super simple Poem-Maud-HTMX stack and this would improve the dev experience dramatically! |
When compiling a release build it should always be static-only, no need to reconstruct trees at runtime for compile speed when compile speed isn't a concern :D |
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.