Skip to content

Commit b7e8389

Browse files
committed
feat: made web_log! cross-platform and only needing perseus
Before, it only worked in the browser, and needed `wasm_bindgen` and `web_sys` as dependencies of the user's crate. Now, it's entirely self-contained, and will use the logging function appropriate to its platform!
1 parent 891cd44 commit b7e8389

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

examples/basic/src/templates/index.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub struct IndexPageProps {
1313
#[perseus::template(IndexPage)]
1414
#[component(IndexPage<G>)]
1515
pub fn index_page(props: IndexPageProps) -> View<G> {
16+
perseus::web_log!("Cross-platform logging!");
1617
view! {
1718
p {(props.greeting)}
1819
a(href = "about", id = "about-link") { "About!" }

packages/perseus/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,9 @@ pub mod internal {
135135
pub use crate::export::*;
136136
}
137137
pub use crate::path_prefix::{get_path_prefix_client, get_path_prefix_server};
138+
/// Internal utilities for logging. These are just re-exports so that users don't have to have `web_sys` and `wasm_bindgen` to use `web_log!`.
139+
pub mod log {
140+
pub use wasm_bindgen::JsValue;
141+
pub use web_sys::console::log_1 as log_js_value;
142+
}
138143
}

packages/perseus/src/log.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
/// Logs the given `format!`-style data to the browser's console.
1+
/// Logs the given `format!`-style data to the browser's console, or to stdout as usual on the server.
22
#[macro_export]
3+
#[cfg(target_arch = "wasm32")]
34
macro_rules! web_log {
45
($format_str:literal $(, $data:expr)*) => {
5-
::web_sys::console::log_1(
6-
&::wasm_bindgen::JsValue::from(
6+
$crate::internal::log::log_js_value(
7+
&$crate::internal::log::JsValue::from(
78
format!($format_str $(, $data)*)
89
)
910
);
1011
};
1112
}
13+
14+
/// Logs the given `format!`-style data to the browser's console, or to stdout on the server.
15+
#[macro_export]
16+
#[cfg(not(target_arch = "wasm32"))]
17+
macro_rules! web_log {
18+
($format_str:literal $(, $data:expr)*) => {
19+
println!($format_str, $(, $data)*)
20+
};
21+
}

0 commit comments

Comments
 (0)