Skip to main content

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    module: bool,
23}
24
25impl Default for JsAssetOptions {
26    fn default() -> Self {
27        Self::default()
28    }
29}
30
31impl JsAssetOptions {
32    /// Create a new js asset options builder
33    pub const fn new() -> AssetOptionsBuilder<JsAssetOptions> {
34        AssetOptions::js()
35    }
36
37    /// Create a default js asset options
38    pub const fn default() -> Self {
39        Self {
40            preload: false,
41            minify: true,
42            static_head: false,
43            module: false,
44        }
45    }
46
47    /// Check if the asset is preloaded
48    pub const fn preloaded(&self) -> bool {
49        self.preload
50    }
51
52    /// Check if the asset is statically created
53    pub const fn static_head(&self) -> bool {
54        self.static_head
55    }
56
57    /// Check if the asset is minified
58    pub const fn minified(&self) -> bool {
59        self.minify
60    }
61
62    /// Check whether the asset is declared as an ES module
63    pub const fn is_module(&self) -> bool {
64        self.module
65    }
66}
67
68impl AssetOptions {
69    /// Create a new js asset builder
70    ///
71    /// ```rust
72    /// # use manganis::{asset, Asset, AssetOptions};
73    /// const _: Asset = asset!("/assets/script.js", AssetOptions::js());
74    /// ```
75    pub const fn js() -> AssetOptionsBuilder<JsAssetOptions> {
76        AssetOptionsBuilder::variant(JsAssetOptions::default())
77    }
78}
79
80impl AssetOptionsBuilder<JsAssetOptions> {
81    /// Sets whether the js should be minified (default: true)
82    ///
83    /// Minifying the js can make your site load faster by loading less data
84    ///
85    /// ```rust
86    /// # use manganis::{asset, Asset, AssetOptions};
87    /// const _: Asset = asset!("/assets/script.js", AssetOptions::js().with_minify(false));
88    /// ```
89    #[allow(unused)]
90    pub const fn with_minify(mut self, minify: bool) -> Self {
91        self.variant.minify = minify;
92        self
93    }
94
95    /// Make the asset statically inserted (default: false)
96    ///
97    /// Statically insert the file at compile time.
98    ///
99    /// ```rust
100    /// # use manganis::{asset, Asset, AssetOptions};
101    /// const _: Asset = asset!("/assets/script.js", AssetOptions::js().with_static_head(true));
102    /// ```
103    #[allow(unused)]
104    pub const fn with_static_head(mut self, static_head: bool) -> Self {
105        self.variant.static_head = static_head;
106        self
107    }
108
109    /// Make the asset preloaded
110    ///
111    /// 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
112    ///
113    /// ```rust
114    /// # use manganis::{asset, Asset, AssetOptions};
115    /// const _: Asset = asset!("/assets/script.js", AssetOptions::js().with_preload(true));
116    /// ```
117    #[allow(unused)]
118    pub const fn with_preload(mut self, preload: bool) -> Self {
119        self.variant.preload = preload;
120        self
121    }
122
123    /// Mark the asset as an ES module (default: false)
124    ///
125    /// When true, the script tag emitted via `with_static_head(true)` is rendered as
126    /// `<script type="module" ...>`, and the file is delivered with its module syntax
127    /// (`import`/`export`) preserved so the browser can resolve imports natively at
128    /// runtime. The default treats the file as a classic script: `<script>` is emitted
129    /// without `type="module"`, and minification preserves classic-script semantics.
130    ///
131    /// Note: this does not perform build-time bundling. If you need imports resolved
132    /// into a single file, pre-bundle with your tool of choice and ship the result.
133    ///
134    /// ```rust
135    /// # use manganis::{asset, Asset, AssetOptions};
136    /// const _: Asset = asset!("/assets/script.js", AssetOptions::js().with_module(true));
137    /// ```
138    #[allow(unused)]
139    pub const fn with_module(mut self, module: bool) -> Self {
140        self.variant.module = module;
141        self
142    }
143
144    /// Convert the builder into asset options with the given variant
145    pub const fn into_asset_options(self) -> AssetOptions {
146        AssetOptions {
147            add_hash: self.add_hash,
148            variant: AssetVariant::Js(self.variant),
149        }
150    }
151}