diff --git a/packages/cli/src/build/request.rs b/packages/cli/src/build/request.rs index 178dc26f4c..4c58142522 100644 --- a/packages/cli/src/build/request.rs +++ b/packages/cli/src/build/request.rs @@ -4781,23 +4781,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#""# + ); } } _ => {} @@ -4805,9 +4820,10 @@ __wbg_init({{module_or_path: "/{}/{wasm_path}"}}).then((wasm) => {{ } // Manually inject the wasm file for preloading. WASM currently doesn't support preloading in the manganis asset system - head_resources.push_str(&format!( - "" - )); + _ = write!( + head_resources, + r#""# + ); Self::replace_or_insert_before("{style_include}", " 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 8ac9613d6d..75c6274fd8 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