manganis_core/
js.rs

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