Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions packages/cli/src/build/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4545,33 +4545,49 @@ __wbg_init({{module_or_path: "/{}/{wasm_path}"}}).then((wasm) => {{
match asset.options().variant() {
AssetVariant::Css(css_options) => {
if css_options.preloaded() {
head_resources.push_str(&format!(
"<link rel=\"preload\" as=\"style\" href=\"/{{base_path}}/assets/{asset_path}\" crossorigin>"
))
_ = write!(
head_resources,
r#"<link rel="preload" as="style" href="/{{base_path}}/assets/{asset_path}" crossorigin>"#
);
}
if css_options.static_head() {
_ = write!(
head_resources,
r#"<link rel="stylesheet" href="/{{base_path}}/assets/{asset_path}" type="text/css">"#
);
}
}
AssetVariant::Image(image_options) => {
if image_options.preloaded() {
head_resources.push_str(&format!(
"<link rel=\"preload\" as=\"image\" href=\"/{{base_path}}/assets/{asset_path}\" crossorigin>"
))
_ = write!(
head_resources,
r#"<link rel="preload" as="image" href="/{{base_path}}/assets/{asset_path}" crossorigin>"#
);
}
}
AssetVariant::Js(js_options) => {
if js_options.preloaded() {
head_resources.push_str(&format!(
"<link rel=\"preload\" as=\"script\" href=\"/{{base_path}}/assets/{asset_path}\" crossorigin>"
))
_ = write!(
head_resources,
r#"<link rel="preload" as="script" href="/{{base_path}}/assets/{asset_path}" crossorigin>"#
);
}
if js_options.static_head() {
_ = write!(
head_resources,
r#"<script src="/{{base_path}}/assets/{asset_path}"></script>"#
);
}
}
_ => {}
}
}

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

Ok(())
Expand Down
23 changes: 22 additions & 1 deletion packages/manganis/manganis-core/src/css.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use const_serialize::SerializeConst;
pub struct CssAssetOptions {
minify: bool,
preload: bool,
static_head: bool,
}

impl Default for CssAssetOptions {
Expand All @@ -36,6 +37,7 @@ impl CssAssetOptions {
Self {
preload: false,
minify: true,
static_head: false,
}
}

Expand All @@ -44,6 +46,11 @@ impl CssAssetOptions {
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
Expand Down Expand Up @@ -76,9 +83,23 @@ impl AssetOptionsBuilder<CssAssetOptions> {
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/style.css", AssetOptions::css().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 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
/// 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
///
/// ```rust
/// # use manganis::{asset, Asset, AssetOptions};
Expand Down
21 changes: 21 additions & 0 deletions packages/manganis/manganis-core/src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{AssetOptions, AssetOptionsBuilder, AssetVariant};
pub struct JsAssetOptions {
minify: bool,
preload: bool,
static_head: bool,
}

impl Default for JsAssetOptions {
Expand All @@ -37,6 +38,7 @@ impl JsAssetOptions {
Self {
preload: false,
minify: true,
static_head: false,
}
}

Expand All @@ -45,6 +47,11 @@ impl JsAssetOptions {
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
Expand Down Expand Up @@ -78,6 +85,20 @@ impl AssetOptionsBuilder<JsAssetOptions> {
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
Expand Down
Loading