manganis_core/js.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
use const_serialize::SerializeConst;
use crate::AssetOptions;
/// Options for a javascript asset
#[derive(
Debug,
PartialEq,
PartialOrd,
Clone,
Copy,
Hash,
SerializeConst,
serde::Serialize,
serde::Deserialize,
)]
pub struct JsAssetOptions {
minify: bool,
preload: bool,
}
impl Default for JsAssetOptions {
fn default() -> Self {
Self::new()
}
}
impl JsAssetOptions {
/// Create a new js asset builder
pub const fn new() -> Self {
Self {
minify: true,
preload: false,
}
}
/// Sets whether the js should be minified (default: true)
///
/// Minifying the js can make your site load faster by loading less data
///
/// ```rust
/// # use manganis::{asset, Asset, JsAssetOptions};
/// const _: Asset = asset!("/assets/script.js", JsAssetOptions::new().with_minify(false));
/// ```
#[allow(unused)]
pub const fn with_minify(self, minify: bool) -> Self {
Self { minify, ..self }
}
/// Check if the asset is minified
pub const fn minified(&self) -> bool {
self.minify
}
/// 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
///
/// ```rust
/// # use manganis::{asset, Asset, JsAssetOptions};
/// const _: Asset = asset!("/assets/script.js", JsAssetOptions::new().with_preload(true));
/// ```
#[allow(unused)]
pub const fn with_preload(self, preload: bool) -> Self {
Self { preload, ..self }
}
/// Check if the asset is preloaded
pub const fn preloaded(&self) -> bool {
self.preload
}
/// Convert the options into options for a generic asset
pub const fn into_asset_options(self) -> AssetOptions {
AssetOptions::Js(self)
}
}