Skip to content

Commit

Permalink
add vite base option (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dsaquel authored Dec 5, 2024
1 parent 4fd405f commit fcd9c89
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased]

- Remove static lifetime from `Page` struct.
- Supports base public path in development


## [0.5.1] 2024-12-03
Expand Down
41 changes: 33 additions & 8 deletions src/vite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use sha1::{Digest, Sha1};
use std::collections::HashMap;

pub struct Development {
base: &'static str,
port: u16,
main: &'static str,
lang: &'static str,
Expand All @@ -46,6 +47,7 @@ pub struct Development {
impl Default for Development {
fn default() -> Self {
Development {
base: "",
port: 5173,
main: "src/main.ts",
lang: "en",
Expand All @@ -57,6 +59,20 @@ impl Default for Development {
}

impl Development {
/// Adds a path as a prefix to included vite client assets, e.g., "`/app`"
///
/// ```rust
/// use axum_inertia::vite;
///
/// vite::Development::default()
/// .base("/app")
/// .into_config();
/// ```
pub fn base(mut self, base: &'static str) -> Self {
self.base = base;
self
}

pub fn port(mut self, port: u16) -> Self {
self.port = port;
self
Expand Down Expand Up @@ -94,8 +110,14 @@ impl Development {
pub fn into_config(self) -> InertiaConfig {
let layout = Box::new(move |props| {
let http_protocol = if self.https { "https" } else { "http" };
let vite_src = format!("{}://localhost:{}/@vite/client", http_protocol, self.port);
let main_src = format!("{}://localhost:{}/{}", http_protocol, self.port, self.main);
let vite_src = format!(
"{}://localhost:{}{}/@vite/client",
http_protocol, self.port, self.base
);
let main_src = format!(
"{}://localhost:{}{}/{}",
http_protocol, self.port, self.base, self.main
);
let preamble_code = if self.react {
Some(PreEscaped(self.build_react_preamble()))
} else {
Expand Down Expand Up @@ -129,13 +151,13 @@ impl Development {
let http_protocol = if self.https { "https" } else { "http" };
format!(
r#"
import RefreshRuntime from "{}://localhost:{}/@react-refresh"
import RefreshRuntime from "{}://localhost:{}{}/@react-refresh"
RefreshRuntime.injectIntoGlobalHook(window)
window.$RefreshReg$ = () => {{}}
window.$RefreshSig$ = () => (type) => type
window.__vite_plugin_react_preamble_installed__ = true
"#,
http_protocol, self.port
http_protocol, self.port, self.base
)
}
}
Expand Down Expand Up @@ -294,23 +316,26 @@ mod tests {
}

#[test]
fn test_development_encryption() {
let development = Development::default().https(true);
fn test_development_url() {
let development = Development::default().base("/app").https(true);
assert!(development.https);
assert_eq!(development.base, "/app");

let config = development.into_config();

let config_layout = config.layout();
let binding = config_layout(r#"{"someprops": "somevalues"}"#.to_string());
let rendered_layout = binding.as_str();

assert!(rendered_layout.contains(r#"https://localhost:5173/@vite/client"#));
assert!(rendered_layout.contains(r#"https://localhost:5173/app/@vite/client"#));
assert!(rendered_layout.contains(r#"https://localhost:5173/app/src/main.ts"#));
}

#[test]
fn test_development_into_config() {
let main_script = "src/index.ts";
let development = Development::default()
.base("/app")
.port(8080)
.main(main_script)
.lang("lang-id")
Expand All @@ -328,7 +353,7 @@ mod tests {
assert!(rendered_layout.contains(r#"<html lang="lang-id">"#));
assert!(rendered_layout.contains(r#"<title>app-title-here</title>"#));
assert!(rendered_layout.contains(r#"{&quot;someprops&quot;: &quot;somevalues&quot;}"#));
assert!(rendered_layout.contains(r#"http://localhost:8080/@vite/client"#));
assert!(rendered_layout.contains(r#"http://localhost:8080/app/@vite/client"#));
assert!(
rendered_layout.contains(r#"window.__vite_plugin_react_preamble_installed__ = true"#)
);
Expand Down

0 comments on commit fcd9c89

Please sign in to comment.