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