Skip to content

Commit 0063d90

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 05f3b8d commit 0063d90

File tree

3 files changed

+71
-13
lines changed

3 files changed

+71
-13
lines changed

packages/cli/src/build/request.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4757,33 +4757,49 @@ __wbg_init({{module_or_path: "/{}/{wasm_path}"}}).then((wasm) => {{
47574757
match asset.options().variant() {
47584758
AssetVariant::Css(css_options) => {
47594759
if css_options.preloaded() {
4760-
head_resources.push_str(&format!(
4761-
"<link rel=\"preload\" as=\"style\" href=\"/{{base_path}}/assets/{asset_path}\" crossorigin>"
4762-
))
4760+
_ = write!(
4761+
head_resources,
4762+
r#"<link rel="preload" as="style" href="/{{base_path}}/assets/{asset_path}" crossorigin>"#
4763+
);
4764+
}
4765+
if css_options.static_head() {
4766+
_ = write!(
4767+
head_resources,
4768+
r#"<link rel="stylesheet" href="/{{base_path}}/assets/{asset_path}" type="text/css">"#
4769+
);
47634770
}
47644771
}
47654772
AssetVariant::Image(image_options) => {
47664773
if image_options.preloaded() {
4767-
head_resources.push_str(&format!(
4768-
"<link rel=\"preload\" as=\"image\" href=\"/{{base_path}}/assets/{asset_path}\" crossorigin>"
4769-
))
4774+
_ = write!(
4775+
head_resources,
4776+
r#"<link rel="preload" as="image" href="/{{base_path}}/assets/{asset_path}" crossorigin>"#
4777+
);
47704778
}
47714779
}
47724780
AssetVariant::Js(js_options) => {
47734781
if js_options.preloaded() {
4774-
head_resources.push_str(&format!(
4775-
"<link rel=\"preload\" as=\"script\" href=\"/{{base_path}}/assets/{asset_path}\" crossorigin>"
4776-
))
4782+
_ = write!(
4783+
head_resources,
4784+
r#"<link rel="preload" as="script" href="/{{base_path}}/assets/{asset_path}" crossorigin>"#
4785+
);
4786+
}
4787+
if js_options.static_head() {
4788+
_ = write!(
4789+
head_resources,
4790+
r#"<script src="/{{base_path}}/assets/{asset_path}"></script>"#
4791+
);
47774792
}
47784793
}
47794794
_ => {}
47804795
}
47814796
}
47824797

47834798
// Manually inject the wasm file for preloading. WASM currently doesn't support preloading in the manganis asset system
4784-
head_resources.push_str(&format!(
4785-
"<link rel=\"preload\" as=\"fetch\" type=\"application/wasm\" href=\"/{{base_path}}/{wasm_path}\" crossorigin>"
4786-
));
4799+
_ = write!(
4800+
head_resources,
4801+
r#"<link rel="preload" as="fetch" type="application/wasm" href="/{{base_path}}/{wasm_path}" crossorigin>"#
4802+
);
47874803
Self::replace_or_insert_before("{style_include}", "</head", &head_resources, html);
47884804

47894805
Ok(())

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)