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