From abb12e7b3d8dcba11b0f5e063d7fd4c78777195b Mon Sep 17 00:00:00 2001 From: Valerio Ageno <51341197+Valerioageno@users.noreply.github.com> Date: Sat, 29 Mar 2025 14:49:47 +0100 Subject: [PATCH] refactor: move static string into templates/ folder (#676) --- crates/tuono/Cargo.toml | 2 +- crates/tuono/src/source_builder.rs | 94 +++++++--------------------- crates/tuono/templates/client.ts | 9 +++ crates/tuono/templates/fallback.html | 19 ++++++ crates/tuono/templates/server.rs | 26 ++++++++ crates/tuono/templates/server.ts | 7 +++ 6 files changed, 86 insertions(+), 71 deletions(-) create mode 100644 crates/tuono/templates/client.ts create mode 100644 crates/tuono/templates/fallback.html create mode 100644 crates/tuono/templates/server.rs create mode 100644 crates/tuono/templates/server.ts diff --git a/crates/tuono/Cargo.toml b/crates/tuono/Cargo.toml index 487348dd..4766a3b5 100644 --- a/crates/tuono/Cargo.toml +++ b/crates/tuono/Cargo.toml @@ -10,7 +10,7 @@ repository = "https://github.com/tuono-labs/tuono" readme = "../../README.md" license-file = "../../LICENSE.md" categories = ["web-programming"] -include = ["src/**/*.rs", "Cargo.toml"] +include = ["src/**/*.rs", "templates/*", "Cargo.toml"] [lib] name = "tuono" diff --git a/crates/tuono/src/source_builder.rs b/crates/tuono/src/source_builder.rs index 6edda842..d4625119 100644 --- a/crates/tuono/src/source_builder.rs +++ b/crates/tuono/src/source_builder.rs @@ -13,76 +13,14 @@ use crate::mode::Mode; use crate::route::AxumInfo; use crate::route::Route; -const FALLBACK_HTML: &str = r#" - -
- - - - - -"#; - -const SERVER_ENTRY_DATA: &str = "// File automatically generated by tuono -// Do not manually update this file -import { routeTree } from './routeTree.gen' -import { serverSideRendering } from 'tuono/ssr' - -export const renderFn = serverSideRendering(routeTree) -"; - -const CLIENT_ENTRY_DATA: &str = "// File automatically generated by tuono -// Do not manually update this file -import 'vite/modulepreload-polyfill' -import { hydrate } from 'tuono/hydration' - -// Import the generated route tree -import { routeTree } from './routeTree.gen' - -hydrate(routeTree) -"; - -const AXUM_ENTRY_POINT: &str = r##" -// File automatically generated -// Do not manually change it - -use tuono_lib::{tokio, Mode, Server, axum::Router, tuono_internal_init_v8_platform}; -// AXUM_GET_ROUTE_HANDLER - -const MODE: Mode = /*MODE*/; - -// MODULE_IMPORTS - -//MAIN_FILE_IMPORT// - -#[tokio::main] -async fn main() { - tuono_internal_init_v8_platform(); - println!("\n ⚡ Tuono v/*VERSION*/"); - - //MAIN_FILE_DEFINITION// - - let router = Router::new() - // ROUTE_BUILDER - //MAIN_FILE_USAGE//; - - Server::init(router, MODE).await.start().await -} -"##; - -#[cfg(target_os = "windows")] -const MAIN_FILE_PATH: &str = ".\\.tuono\\main.rs"; - -#[cfg(target_os = "windows")] -const FALLBACK_HTML_PATH: &str = ".\\.tuono\\index.html"; +#[cfg(not(target_os = "windows"))] +const FALLBACK_HTML: &str = include_str!("../templates/fallback.html"); +#[cfg(not(target_os = "windows"))] +const SERVER_ENTRY_DATA: &str = include_str!("../templates/server.ts"); +#[cfg(not(target_os = "windows"))] +const CLIENT_ENTRY_DATA: &str = include_str!("../templates/client.ts"); +#[cfg(not(target_os = "windows"))] +const AXUM_ENTRY_POINT: &str = include_str!("../templates/server.rs"); #[cfg(not(target_os = "windows"))] const MAIN_FILE_PATH: &str = "./.tuono/main.rs"; @@ -93,6 +31,21 @@ const FALLBACK_HTML_PATH: &str = "./.tuono/index.html"; const ROUTE_FOLDER: &str = "src/routes"; const DEV_FOLDER: &str = ".tuono"; +#[cfg(target_os = "windows")] +const FALLBACK_HTML: &str = include_str!("..\\templates\\fallback.html"); +#[cfg(target_os = "windows")] +const SERVER_ENTRY_DATA: &str = include_str!("..\\templates\\server.ts"); +#[cfg(target_os = "windows")] +const CLIENT_ENTRY_DATA: &str = include_str!("..\\templates\\client.ts"); +#[cfg(target_os = "windows")] +const AXUM_ENTRY_POINT: &str = include_str!("..\\templates\\server.rs"); + +#[cfg(target_os = "windows")] +const MAIN_FILE_PATH: &str = ".\\.tuono\\main.rs"; + +#[cfg(target_os = "windows")] +const FALLBACK_HTML_PATH: &str = ".\\.tuono\\index.html"; + // Use this function to instruct the users on how to // fix their setup to make tuono work fn recoverable_error(message: &str) -> ! { @@ -152,6 +105,7 @@ impl SourceBuilder { let Self { app, mode, .. } = &self; let src = AXUM_ENTRY_POINT + .replace("\r", "") .replace( "// ROUTE_BUILDER\n", &create_routes_declaration(&app.route_map), diff --git a/crates/tuono/templates/client.ts b/crates/tuono/templates/client.ts new file mode 100644 index 00000000..b1bc7049 --- /dev/null +++ b/crates/tuono/templates/client.ts @@ -0,0 +1,9 @@ +// File automatically generated by tuono +// Do not manually update this file +import 'vite/modulepreload-polyfill' +import { hydrate } from 'tuono/hydration' + +// Import the generated route tree +import { routeTree } from './routeTree.gen' + +hydrate(routeTree) diff --git a/crates/tuono/templates/fallback.html b/crates/tuono/templates/fallback.html new file mode 100644 index 00000000..2bda889d --- /dev/null +++ b/crates/tuono/templates/fallback.html @@ -0,0 +1,19 @@ + + + + + + + + + + + diff --git a/crates/tuono/templates/server.rs b/crates/tuono/templates/server.rs new file mode 100644 index 00000000..c12dd56b --- /dev/null +++ b/crates/tuono/templates/server.rs @@ -0,0 +1,26 @@ +// File automatically generated +// Do not manually change it + +use tuono_lib::{tokio, Mode, Server, axum::Router, tuono_internal_init_v8_platform}; +// AXUM_GET_ROUTE_HANDLER + +const MODE: Mode = /*MODE*/; + +// MODULE_IMPORTS + +//MAIN_FILE_IMPORT// + +#[tokio::main] +async fn main() { + tuono_internal_init_v8_platform(); + println!("\n ⚡ Tuono v/*VERSION*/"); + + //MAIN_FILE_DEFINITION// + + let router = Router::new() + // ROUTE_BUILDER + //MAIN_FILE_USAGE//; + + Server::init(router, MODE).await.start().await +} + diff --git a/crates/tuono/templates/server.ts b/crates/tuono/templates/server.ts new file mode 100644 index 00000000..8d7b985d --- /dev/null +++ b/crates/tuono/templates/server.ts @@ -0,0 +1,7 @@ +// File automatically generated by tuono +// Do not manually update this file +import { serverSideRendering } from 'tuono/ssr' + +import { routeTree } from './routeTree.gen' + +export const renderFn = serverSideRendering(routeTree)