djc / askama

Type-safe, compiled Jinja-like templates for Rust
Apache License 2.0
3.25k stars 213 forks source link

`include` is not very efficient #1034

Open GuillaumeGomez opened 2 months ago

GuillaumeGomez commented 2 months ago

Currently, code like:

<script>{%- include "theme.js" -%}</script>

generates:

        ::std::write!(
            writer,
            "</title>\n\n        <script>",
            expr2 = &::askama::MarkupDisplay::new_unsafe(&(self.csp_nonce), ::askama::Html),
        )?;
        include_bytes! ("/home/imperio/rust/docs.rs/templates/theme.js");
        writer.write_str("(function() {\n    function applyTheme(theme) {\n        if (theme) {\n            document.documentElement.dataset.docsRsTheme = theme;\n        }\n    }\n\n    window.addEventListener(\"storage\", ev => {\n        if (ev.key === \"rustdoc-theme\") {\n            applyTheme(ev.newValue);\n        }\n    });\n\n    // see ./storage-change-detection.html for details\n    window.addEventListener(\"message\", ev => {\n        if (ev.data && ev.data.storage && ev.data.storage.key === \"rustdoc-theme\") {\n            applyTheme(ev.data.storage.value);\n        }\n    });\n\n    applyTheme(window.localStorage.getItem(\"rustdoc-theme\"));\n})();")?;
        writer.write_str("</script>")?;

It duplicates the content of the file instead of just doing write!("<script>{}</script>", include_str!("theme.js"));.

Planning to send a fix for this "soon".

GuillaumeGomez commented 2 months ago

After looking into it a bit more, I don't think we can do much about it since there is no macro to just check if a file was modified without including its content... So unless someone has a better idea, I'll just close this issue.

Kijewski commented 2 months ago

Will look into it.

GuillaumeGomez commented 2 months ago

I'm curious to see what you will come up with. :smiley: