Skip to main content

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