Skip to content

Commit da8107e

Browse files
feat: add option to statically add js and css assets to the header at compile time
Signed-off-by: Omar Mohamed <[email protected]>
1 parent c55a29e commit da8107e

File tree

3 files changed

+67
-10
lines changed

3 files changed

+67
-10
lines changed

packages/cli/src/build/request.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4862,23 +4862,38 @@ __wbg_init({{module_or_path: "/{}/{wasm_path}"}}).then((wasm) => {{
48624862
match asset.options().variant() {
48634863
AssetVariant::Css(css_options) => {
48644864
if css_options.preloaded() {
4865-
head_resources.push_str(&format!(
4866-
"<link rel=\"preload\" as=\"style\" href=\"/{{base_path}}/assets/{asset_path}\" crossorigin>"
4867-
))
4865+
_ = write!(
4866+
head_resources,
4867+
r#"<link rel="preload" as="style" href="/{{base_path}}/assets/{asset_path}" crossorigin>"#
4868+
);
4869+
}
4870+
if css_options.static_head() {
4871+
_ = write!(
4872+
head_resources,
4873+
r#"<link rel="stylesheet" href="/{{base_path}}/assets/{asset_path}" type="text/css">"#
4874+
);
48684875
}
48694876
}
48704877
AssetVariant::Image(image_options) => {
48714878
if image_options.preloaded() {
4872-
head_resources.push_str(&format!(
4873-
"<link rel=\"preload\" as=\"image\" href=\"/{{base_path}}/assets/{asset_path}\" crossorigin>"
4874-
))
4879+
_ = write!(
4880+
head_resources,
4881+
r#"<link rel="preload" as="image" href="/{{base_path}}/assets/{asset_path}" crossorigin>"#
4882+
);
48754883
}
48764884
}
48774885
AssetVariant::Js(js_options) => {
48784886
if js_options.preloaded() {
4879-
head_resources.push_str(&format!(
4880-
"<link rel=\"preload\" as=\"script\" href=\"/{{base_path}}/assets/{asset_path}\" crossorigin>"
4881-
))
4887+
_ = write!(
4888+
head_resources,
4889+
r#"<link rel="preload" as="script" href="/{{base_path}}/assets/{asset_path}" crossorigin>"#
4890+
);
4891+
}
4892+
if js_options.static_head() {
4893+
_ = write!(
4894+
head_resources,
4895+
r#"<script src="/{{base_path}}/assets/{asset_path}"></script>"#
4896+
);
48824897
}
48834898
}
48844899
_ => {}

packages/manganis/manganis-core/src/css.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use const_serialize::SerializeConst;
1717
pub struct CssAssetOptions {
1818
minify: bool,
1919
preload: bool,
20+
static_head: bool,
2021
}
2122

2223
impl Default for CssAssetOptions {
@@ -36,6 +37,7 @@ impl CssAssetOptions {
3637
Self {
3738
preload: false,
3839
minify: true,
40+
static_head: false,
3941
}
4042
}
4143

@@ -44,6 +46,11 @@ impl CssAssetOptions {
4446
self.preload
4547
}
4648

49+
/// Check if the asset is statically created
50+
pub const fn static_head(&self) -> bool {
51+
self.static_head
52+
}
53+
4754
/// Check if the asset is minified
4855
pub const fn minified(&self) -> bool {
4956
self.minify
@@ -76,9 +83,23 @@ impl AssetOptionsBuilder<CssAssetOptions> {
7683
self
7784
}
7885

86+
/// Make the asset statically inserted (default: false)
87+
///
88+
/// Statically insert the file at compile time.
89+
///
90+
/// ```rust
91+
/// # use manganis::{asset, Asset, AssetOptions};
92+
/// const _: Asset = asset!("/assets/style.css", AssetOptions::css().with_static_head(true));
93+
/// ```
94+
#[allow(unused)]
95+
pub const fn with_static_head(mut self, static_head: bool) -> Self {
96+
self.variant.static_head = static_head;
97+
self
98+
}
99+
79100
/// Make the asset preloaded
80101
///
81-
/// Preloading css will make the image start to load as soon as possible. This is useful for css that is used soon after the page loads or css that may not be used immediately, but should start loading sooner
102+
/// Preloading css will make the file start to load as soon as possible. This is useful for css that is used soon after the page loads or css that may not be used immediately, but should start loading sooner
82103
///
83104
/// ```rust
84105
/// # use manganis::{asset, Asset, AssetOptions};

packages/manganis/manganis-core/src/js.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::{AssetOptions, AssetOptionsBuilder, AssetVariant};
1818
pub struct JsAssetOptions {
1919
minify: bool,
2020
preload: bool,
21+
static_head: bool,
2122
}
2223

2324
impl Default for JsAssetOptions {
@@ -37,6 +38,7 @@ impl JsAssetOptions {
3738
Self {
3839
preload: false,
3940
minify: true,
41+
static_head: false,
4042
}
4143
}
4244

@@ -45,6 +47,11 @@ impl JsAssetOptions {
4547
self.preload
4648
}
4749

50+
/// Check if the asset is statically created
51+
pub const fn static_head(&self) -> bool {
52+
self.static_head
53+
}
54+
4855
/// Check if the asset is minified
4956
pub const fn minified(&self) -> bool {
5057
self.minify
@@ -78,6 +85,20 @@ impl AssetOptionsBuilder<JsAssetOptions> {
7885
self
7986
}
8087

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+
81102
/// Make the asset preloaded
82103
///
83104
/// 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

0 commit comments

Comments
 (0)