#![doc = include_str!("../README.md")]
#[cfg(any(feature = "ssr", feature = "csr"))]
pub use inventory;
use leptos::config::LeptosOptions;
use std::{ops::Deref, path::Path};
pub use turf::{inline_style_sheet as inline_style_sheet_inner, style_sheet as style_sheet_inner};
mod component;
mod macros;
pub use component::StyleSheets;
#[derive(Debug)]
pub struct StyleSheet {
pub name: &'static str,
pub content: Option<&'static str>,
}
impl StyleSheet {
pub const fn new(name: &'static str, content: Option<&'static str>) -> Self {
Self { name, content }
}
}
#[cfg(any(feature = "ssr", feature = "csr"))]
inventory::collect!(StyleSheet);
#[cfg(feature = "ssr")]
pub fn generate_style_sheets(leptos_option: LeptosOptions) {
use std::collections::HashMap;
let base_path =
Path::new(leptos_option.site_root.deref()).join(leptos_option.site_pkg_dir.deref());
let mut grouped: HashMap<&str, String> = HashMap::new();
for style_sheet in inventory::iter::<StyleSheet>() {
let file_name = if style_sheet.name.ends_with(".css") {
style_sheet.name
} else {
Box::leak(format!("{}.css", style_sheet.name).into_boxed_str())
};
grouped
.entry(file_name)
.or_default()
.push_str(style_sheet.content.unwrap_or_default());
}
for (file_name, content) in grouped {
let file_path = base_path.join(file_name);
std::fs::write(file_path, content).unwrap();
}
}
#[cfg(target_family = "wasm")]
unsafe extern "C" {
fn __wasm_call_ctors();
}
#[cfg(feature = "csr")]
pub fn init() {
unsafe {
#[cfg(target_family = "wasm")]
__wasm_call_ctors();
}
}