manganis_core/
js.rs

1use const_serialize::SerializeConst;
2
3use crate::{AssetOptions, AssetOptionsBuilder, AssetVariant};
4
5/// Options for a javascript asset
6#[derive(
7    Debug,
8    Eq,
9    PartialEq,
10    PartialOrd,
11    Clone,
12    Copy,
13    Hash,
14    SerializeConst,
15    serde::Serialize,
16    serde::Deserialize,
17)]
18pub struct JsAssetOptions {
19    minify: bool,
20    preload: bool,
21    static_head: bool,
22}
23
24impl Default for JsAssetOptions {
25    fn default() -> Self {
26        Self::default()
27    }
28}
29
30impl JsAssetOptions {
31    /// Create a new js asset options builder
32    pub const fn new() -> AssetOptionsBuilder<JsAssetOptions> {
33        AssetOptions::js()
34    }
35
36    /// Create a default js asset options
37    pub const fn default() -> Self {
38        Self {
39            preload: false,
40            minify: true,
41            static_head: false,
42        }
43    }
44
45    /// Check if the asset is preloaded
46    pub const fn preloaded(&self) -> bool {
47        self.preload
48    }
49
50    /// Check if the asset is statically created
51    pub const fn static_head(&self) -> bool {
52        self.static_head
53    }
54
55    /// Check if the asset is minified
56    pub const fn minified(&self) -> bool {
57        self.minify
58    }
59}
60
61impl AssetOptions {
62    /// Create a new js asset builder
63    ///
64    /// ```rust
65    /// # use manganis::{asset, Asset, AssetOptions};
66    /// const _: Asset = asset!("/assets/script.js", AssetOptions::js());
67    /// ```
68    pub const fn js() -> AssetOptionsBuilder<JsAssetOptions> {
69        AssetOptionsBuilder::variant(JsAssetOptions::default())
70    }
71}
72
73impl AssetOptionsBuilder<JsAssetOptions> {
74    /// Sets whether the js should be minified (default: true)
75    ///
76    /// Minifying the js can make your site load faster by loading less data
77    ///
78    /// ```rust
79    /// # use manganis::{asset, Asset, AssetOptions};
80    /// const _: Asset = asset!("/assets/script.js", AssetOptions::js().with_minify(false));
81    /// ```
82    #[allow(unused)]
83    pub const fn with_minify(mut self, minify: bool) -> Self {
84        self.variant.minify = minify;
85        self
86    }
87
88    /// Make the asset statically inserted (default: false)
89    ///
90    /// Statically insert the file at compile time.
91    ///
92    /// ```rust
93    /// # use manganis::{asset, Asset, AssetOptions};
94    /// const _: Asset = asset!("/assets/script.js", AssetOptions::js().with_static_head(true));
95    /// ```
96    #[allow(unused)]
97    pub const fn with_static_head(mut self, static_head: bool) -> Self {
98        self.variant.static_head = static_head;
99        self
100    }
101
102    /// Make the asset preloaded
103    ///
104    /// 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
105    ///
106    /// ```rust
107    /// # use manganis::{asset, Asset, AssetOptions};
108    /// const _: Asset = asset!("/assets/script.js", AssetOptions::js().with_preload(true));
109    /// ```
110    #[allow(unused)]
111    pub const fn with_preload(mut self, preload: bool) -> Self {
112        self.variant.preload = preload;
113        self
114    }
115
116    /// Convert the builder into asset options with the given variant
117    pub const fn into_asset_options(self) -> AssetOptions {
118        AssetOptions {
119            add_hash: self.add_hash,
120            variant: AssetVariant::Js(self.variant),
121        }
122    }
123}