use autumn_web::{Markup, html};
use crate::assets::{CORE_JS, INIT_JS, MODULE_JS, PARSE_JS, THREE_ASSETS, THREE_CSS};
const MODULE_GRAPH: [&str; 3] = [CORE_JS, MODULE_JS, PARSE_JS];
#[must_use]
pub fn three_script() -> Markup {
html! {
@for path in MODULE_GRAPH {
@if let Some(asset) = THREE_ASSETS.get(path) {
link rel="modulepreload" href=(asset.plain_url())
integrity=(asset.integrity()) crossorigin="anonymous";
}
}
@if let Some(init) = THREE_ASSETS.get(INIT_JS) {
script type="module" src=(init.url()) integrity=(init.integrity())
crossorigin="anonymous" {}
}
}
}
#[must_use]
pub fn three_stylesheet() -> Markup {
THREE_ASSETS.stylesheet_tag(THREE_CSS)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::assets::{CORE_JS, INIT_JS, MODULE_JS, PARSE_JS, THREE_ASSETS, THREE_CSS};
fn asset(path: &str) -> &'static autumn_web::assets::PluginAsset {
THREE_ASSETS.get(path).expect("file is bundled")
}
#[test]
fn module_graph_is_preloaded_with_sri_at_plain_urls() {
let html = three_script().into_string();
for path in [CORE_JS, MODULE_JS, PARSE_JS] {
let a = asset(path);
let tag = format!(
r#"<link rel="modulepreload" href="{}" integrity="{}" crossorigin="anonymous">"#,
a.plain_url(),
a.integrity()
);
assert!(html.contains(&tag), "{path}: {html}");
}
}
#[test]
fn entry_is_a_module_script_with_sri_and_fingerprinted_url() {
let html = three_script().into_string();
let init = asset(INIT_JS);
let tag = format!(
r#"<script type="module" src="{}" integrity="{}" crossorigin="anonymous"></script>"#,
init.url(),
init.integrity()
);
assert!(html.contains(&tag), "{html}");
assert_eq!(html.matches("<script").count(), 1, "one entry: {html}");
assert!(!html.contains("importmap"), "no inline import map: {html}");
}
#[test]
fn preloads_come_before_the_entry() {
let html = three_script().into_string();
let entry = html.find("<script").expect("entry");
let last_preload = html.rfind("modulepreload").expect("preload");
assert!(last_preload < entry, "{html}");
}
#[test]
fn stylesheet_link_carries_sri_and_fingerprinted_url() {
let html = three_stylesheet().into_string();
let css = asset(THREE_CSS);
assert!(html.contains(r#"rel="stylesheet""#), "{html}");
assert!(html.contains(&format!(r#"href="{}""#, css.url())), "{html}");
assert!(
html.contains(&format!(r#"integrity="{}""#, css.integrity())),
"{html}"
);
}
}