feat: add option to statically add js and css assets to the header at compile time (#4653)
Signed-off-by: Omar Mohamed <mohamed.omar67492@gmail.com>
This commit is contained in:
1 parent
3f931e8409
commit
a4d1fd848d
3 files changed
+67
-10
No files matched your search
@@ -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!(
|
||||
"<link rel=\"preload\" as=\"style\" href=\"/{{base_path}}/assets/{asset_path}\" crossorigin>"
|
||||
))
|
||||
_ = write!(
|
||||
head_resources,
|
||||
r#"<link rel="preload" as="style" href="/{{base_path}}/assets/{asset_path}" crossorigin>"#
|
||||
);
|
||||
}
|
||||
if css_options.static_head() {
|
||||
_ = write!(
|
||||
head_resources,
|
||||
r#"<link rel="stylesheet" href="/{{base_path}}/assets/{asset_path}" type="text/css">"#
|
||||
);
|
||||
}
|
||||
}
|
||||
AssetVariant::Image(image_options) => {
|
||||
if image_options.preloaded() {
|
||||
head_resources.push_str(&format!(
|
||||
"<link rel=\"preload\" as=\"image\" href=\"/{{base_path}}/assets/{asset_path}\" crossorigin>"
|
||||
))
|
||||
_ = write!(
|
||||
head_resources,
|
||||
r#"<link rel="preload" as="image" href="/{{base_path}}/assets/{asset_path}" crossorigin>"#
|
||||
);
|
||||
}
|
||||
}
|
||||
AssetVariant::Js(js_options) => {
|
||||
if js_options.preloaded() {
|
||||
head_resources.push_str(&format!(
|
||||
"<link rel=\"preload\" as=\"script\" href=\"/{{base_path}}/assets/{asset_path}\" crossorigin>"
|
||||
))
|
||||
_ = write!(
|
||||
head_resources,
|
||||
r#"<link rel="preload" as="script" href="/{{base_path}}/assets/{asset_path}" crossorigin>"#
|
||||
);
|
||||
}
|
||||
if js_options.static_head() {
|
||||
_ = write!(
|
||||
head_resources,
|
||||
r#"<script src="/{{base_path}}/assets/{asset_path}"></script>"#
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
|
||||
@@ -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<CssAssetOptions> {
|
||||
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};
|
||||
|
||||
@@ -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<JsAssetOptions> {
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user