refactor: move static string into templates/ folder (#676)

This commit is contained in:
Valerio Ageno
2025-03-29 14:49:47 +01:00
committed by GitHub
parent 66c3d91dfa
commit abb12e7b3d
6 changed files with 86 additions and 71 deletions
+1 -1
View File
@@ -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"
+24 -70
View File
@@ -13,76 +13,14 @@ use crate::mode::Mode;
use crate::route::AxumInfo;
use crate::route::Route;
const FALLBACK_HTML: &str = r#"<!doctype html>
<html>
<body>
<script>
window['__TUONO_SERVER_PAYLOAD__'] = [SERVER_PAYLOAD]
</script>
<script type="module" async>
import RefreshRuntime from '[BASE_URL]/vite-server/@react-refresh'
RefreshRuntime.injectIntoGlobalHook(window)
window.$RefreshReg$ = () => { }
window.$RefreshSig$ = () => (type) => type
window.__vite_plugin_react_preamble_installed__ = true
</script>
<script type="module" async src="[BASE_URL]/vite-server/@vite/client"></script>
<script type="module" async src="[BASE_URL]/vite-server/client-main.tsx"></script>
</body>
</html>"#;
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),
+9
View File
@@ -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)
+19
View File
@@ -0,0 +1,19 @@
<!doctype html>
<html>
<body>
<script>
window['__TUONO_SERVER_PAYLOAD__'] = [SERVER_PAYLOAD]
</script>
<script type="module" async>
import RefreshRuntime from '[BASE_URL]/vite-server/@react-refresh'
RefreshRuntime.injectIntoGlobalHook(window)
window.$RefreshReg$ = () => { }
window.$RefreshSig$ = () => (type) => type
window.__vite_plugin_react_preamble_installed__ = true
</script>
<script type="module" async src="[BASE_URL]/vite-server/@vite/client"></script>
<script type="module" async src="[BASE_URL]/vite-server/client-main.tsx"></script>
</body>
</html>
+26
View File
@@ -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
}
+7
View File
@@ -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)