use reinda::Assets;
const EMBEDS: reinda::Embeds = reinda::embed! {
print_stats: true,
base_path: "examples/assets",
files: [
"index.html",
"robots.txt",
"logo.svg",
"style.css",
"fonts/*.woff2",
"bundle.*.js",
"bundle.*.js.map",
],
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("--- Entries in embeds:");
for entry in EMBEDS.entries() {
println!(
"'{}' -> {:?}",
entry.embed_pattern(),
entry.files().map(|f| f.path()).collect::<Vec<_>>(),
);
}
println!();
let mut builder = Assets::builder();
builder.add_embedded("robots.txt", &EMBEDS["robots.txt"]);
builder.add_embedded("img/logo-foo.svg", &EMBEDS["logo.svg"]);
builder.add_file("dummy/exe", std::env::current_exe().unwrap());
let font_paths = builder.add_embedded("static/font/open-sans/", &EMBEDS["fonts/*.woff2"])
.with_hash()
.http_paths();
let css_path = builder.add_embedded("static/style.css", &EMBEDS["style.css"])
.with_path_fixup(font_paths)
.with_hash()
.single_http_path()
.unwrap();
let bundle_path = builder.add_embedded("static/", &EMBEDS["bundle.*.js"])
.single_http_path()
.unwrap();
builder.add_embedded("static/", &EMBEDS["bundle.*.js.map"]);
let dependencies = [css_path.clone(), bundle_path.clone()];
builder.add_embedded("index.html", &EMBEDS["index.html"])
.with_modifier(dependencies, move |original, ctx| {
reinda::util::replace_many(&original, &[
(css_path.as_ref(), ctx.resolve_path(&css_path)),
("{{ bundle_path }}", ctx.resolve_path(&bundle_path)),
("{{ foo }}", "foxes are best"),
]).into()
});
let assets = builder.build().await?;
let index_html = assets.get("index.html").unwrap();
assert!(!index_html.is_filename_hashed());
let index_content = index_html.content().await?;
let index_content = std::str::from_utf8(&index_content).unwrap();
assert!(index_content.contains(r#"<script src="/static/bundle.8f29ad31.js" />"#));
assert!(index_content.contains(
r#"<script>console.log("Secret variable: foxes are best");</script>"#));
println!("--- index.html:\n{index_content}---\n");
let (css_path, style_css) = assets.iter().find(|(path, _)| path.ends_with(".css")).unwrap();
assert_eq!(
style_css.is_filename_hashed(),
cfg!(any(feature = "always-prod", not(debug_assertions))),
);
let style_content = style_css.content().await?;
let style_content = std::str::from_utf8(&style_content).unwrap();
println!("--- {css_path}:\n{style_content}---\n");
println!("--- All asset paths:");
for (path, _) in assets.iter() {
println!("{path}");
}
Ok(())
}