From a4d1fd848df74b3cf717e61b1227bbe4c1453326 Mon Sep 17 00:00:00 2001 From: Omar Mohamed Khallaf <51155980+omar-mohamed-khallaf@users.noreply.github.com> Date: Fri, 31 Oct 2025 01:55:51 +0000 Subject: [PATCH] feat: add option to statically add js and css assets to the header at compile time (#4653) Signed-off-by: Omar Mohamed --- packages/cli/src/build/request.rs | 33 ++++++++++++++++------ packages/manganis/manganis-core/src/css.rs | 23 ++++++++++++++- packages/manganis/manganis-core/src/js.rs | 21 ++++++++++++++ 3 files changed, 67 insertions(+), 10 deletions(-) diff --git a/packages/cli/src/build/request.rs b/packages/cli/src/build/request.rs index 3a1b7454b..ce31eabd9 100644 --- a/packages/cli/src/build/request.rs +++ b/packages/cli/src/build/request.rs @@ -4886,23 +4886,38 @@ __wbg_init({{module_or_path: "/{}/{wasm_path}"}}).then((wasm) => {{ match asset.options().variant() { AssetVariant::Css(css_options) => { if css_options.preloaded() { - head_resources.push_str(&format!( - "" - )) + _ = write!( + head_resources, + r#""# + ); + } + if css_options.static_head() { + _ = write!( + head_resources, + r#""# + ); } } AssetVariant::Image(image_options) => { if image_options.preloaded() { - head_resources.push_str(&format!( - "" - )) + _ = write!( + head_resources, + r#""# + ); } } AssetVariant::Js(js_options) => { if js_options.preloaded() { - head_resources.push_str(&format!( - "" - )) + _ = write!( + head_resources, + r#""# + ); + } + if js_options.static_head() { + _ = write!( + head_resources, + r#""# + ); } } _ => {} diff --git a/packages/manganis/manganis-core/src/css.rs b/packages/manganis/manganis-core/src/css.rs index 67166409d..e832cca20 100644 --- a/packages/manganis/manganis-core/src/css.rs +++ b/packages/manganis/manganis-core/src/css.rs @@ -17,6 +17,7 @@ use const_serialize::SerializeConst; pub struct CssAssetOptions { minify: bool, preload: bool, + static_head: bool, } impl Default for CssAssetOptions { @@ -36,6 +37,7 @@ impl CssAssetOptions { Self { preload: false, minify: true, + static_head: false, } } @@ -44,6 +46,11 @@ impl CssAssetOptions { self.preload } + /// Check if the asset is statically created + pub const fn static_head(&self) -> bool { + self.static_head + } + /// Check if the asset is minified pub const fn minified(&self) -> bool { self.minify @@ -76,9 +83,23 @@ impl AssetOptionsBuilder { self } + /// Make the asset statically inserted (default: false) + /// + /// Statically insert the file at compile time. + /// + /// ```rust + /// # use manganis::{asset, Asset, AssetOptions}; + /// const _: Asset = asset!("/assets/style.css", AssetOptions::css().with_static_head(true)); + /// ``` + #[allow(unused)] + pub const fn with_static_head(mut self, static_head: bool) -> Self { + self.variant.static_head = static_head; + self + } + /// Make the asset preloaded /// - /// Preloading css will make the image start to load as soon as possible. This is useful for css that is used soon after the page loads or css that may not be used immediately, but should start loading sooner + /// Preloading css will make the file start to load as soon as possible. This is useful for css that is used soon after the page loads or css that may not be used immediately, but should start loading sooner /// /// ```rust /// # use manganis::{asset, Asset, AssetOptions}; diff --git a/packages/manganis/manganis-core/src/js.rs b/packages/manganis/manganis-core/src/js.rs index 8ac9613d6..75c6274fd 100644 --- a/packages/manganis/manganis-core/src/js.rs +++ b/packages/manganis/manganis-core/src/js.rs @@ -18,6 +18,7 @@ use crate::{AssetOptions, AssetOptionsBuilder, AssetVariant}; pub struct JsAssetOptions { minify: bool, preload: bool, + static_head: bool, } impl Default for JsAssetOptions { @@ -37,6 +38,7 @@ impl JsAssetOptions { Self { preload: false, minify: true, + static_head: false, } } @@ -45,6 +47,11 @@ impl JsAssetOptions { self.preload } + /// Check if the asset is statically created + pub const fn static_head(&self) -> bool { + self.static_head + } + /// Check if the asset is minified pub const fn minified(&self) -> bool { self.minify @@ -78,6 +85,20 @@ impl AssetOptionsBuilder { self } + /// Make the asset statically inserted (default: false) + /// + /// Statically insert the file at compile time. + /// + /// ```rust + /// # use manganis::{asset, Asset, AssetOptions}; + /// const _: Asset = asset!("/assets/script.js", AssetOptions::js().with_static_head(true)); + /// ``` + #[allow(unused)] + pub const fn with_static_head(mut self, static_head: bool) -> Self { + self.variant.static_head = static_head; + self + } + /// Make the asset preloaded /// /// Preloading the javascript will make the javascript start to load as soon as possible. This is useful for javascript that will be used soon after the page loads or javascript that may not be used immediately, but should start loading sooner