autumn_plugin_three/
script.rs1use autumn_web::{Markup, html};
17
18use crate::assets::{CORE_JS, INIT_JS, MODULE_JS, PARSE_JS, THREE_ASSETS, THREE_CSS};
19
20const MODULE_GRAPH: [&str; 3] = [CORE_JS, MODULE_JS, PARSE_JS];
22
23#[must_use]
39pub fn three_script() -> Markup {
40 html! {
41 @for path in MODULE_GRAPH {
42 @if let Some(asset) = THREE_ASSETS.get(path) {
43 link rel="modulepreload" href=(asset.plain_url())
44 integrity=(asset.integrity()) crossorigin="anonymous";
45 }
46 }
47 @if let Some(init) = THREE_ASSETS.get(INIT_JS) {
48 script type="module" src=(init.url()) integrity=(init.integrity())
49 crossorigin="anonymous" {}
50 }
51 }
52}
53
54#[must_use]
67pub fn three_stylesheet() -> Markup {
68 THREE_ASSETS.stylesheet_tag(THREE_CSS)
69}
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74 use crate::assets::{CORE_JS, INIT_JS, MODULE_JS, PARSE_JS, THREE_ASSETS, THREE_CSS};
75
76 fn asset(path: &str) -> &'static autumn_web::assets::PluginAsset {
77 THREE_ASSETS.get(path).expect("file is bundled")
78 }
79
80 #[test]
81 fn module_graph_is_preloaded_with_sri_at_plain_urls() {
82 let html = three_script().into_string();
83 for path in [CORE_JS, MODULE_JS, PARSE_JS] {
84 let a = asset(path);
85 let tag = format!(
86 r#"<link rel="modulepreload" href="{}" integrity="{}" crossorigin="anonymous">"#,
87 a.plain_url(),
88 a.integrity()
89 );
90 assert!(html.contains(&tag), "{path}: {html}");
91 }
92 }
93
94 #[test]
95 fn entry_is_a_module_script_with_sri_and_fingerprinted_url() {
96 let html = three_script().into_string();
97 let init = asset(INIT_JS);
98 let tag = format!(
99 r#"<script type="module" src="{}" integrity="{}" crossorigin="anonymous"></script>"#,
100 init.url(),
101 init.integrity()
102 );
103 assert!(html.contains(&tag), "{html}");
104 assert_eq!(html.matches("<script").count(), 1, "one entry: {html}");
105 assert!(!html.contains("importmap"), "no inline import map: {html}");
106 }
107
108 #[test]
109 fn preloads_come_before_the_entry() {
110 let html = three_script().into_string();
111 let entry = html.find("<script").expect("entry");
112 let last_preload = html.rfind("modulepreload").expect("preload");
113 assert!(last_preload < entry, "{html}");
114 }
115
116 #[test]
117 fn stylesheet_link_carries_sri_and_fingerprinted_url() {
118 let html = three_stylesheet().into_string();
119 let css = asset(THREE_CSS);
120 assert!(html.contains(r#"rel="stylesheet""#), "{html}");
121 assert!(html.contains(&format!(r#"href="{}""#, css.url())), "{html}");
122 assert!(
123 html.contains(&format!(r#"integrity="{}""#, css.integrity())),
124 "{html}"
125 );
126 }
127}