feat(fullstack): support wasm target

This commit is contained in:
Koji AGAWA committed 2024-02-17 05:12:33 +09:00
1 parent a454f2c7ac
commit 1f0e03ca19
9 files changed
+190 -128

No files matched your search

+16 -2
View File
@@ -13,6 +13,7 @@ use dioxus_lib::prelude::*;
#[derive(Clone)]
pub struct ServeConfigBuilder {
pub(crate) root_id: Option<&'static str>,
pub(crate) index_html: Option<String>,
pub(crate) index_path: Option<PathBuf>,
pub(crate) assets_path: Option<PathBuf>,
pub(crate) incremental:
@@ -44,6 +45,7 @@ impl ServeConfigBuilder {
pub fn new() -> Self {
Self {
root_id: None,
index_html: None,
index_path: None,
assets_path: None,
incremental: None,
@@ -56,6 +58,12 @@ impl ServeConfigBuilder {
self
}
/// Set the contents of the index.html file to be served. (precedence over index_path)
pub fn index_html(mut self, index_html: String) -> Self {
self.index_html = Some(index_html);
self
}
/// Set the path of the index.html file to be served. (defaults to {assets_path}/index.html)
pub fn index_path(mut self, index_path: PathBuf) -> Self {
self.index_path = Some(index_path);
@@ -90,8 +98,11 @@ impl ServeConfigBuilder {
let root_id = self.root_id.unwrap_or("main");
let index = load_index_html(index_path, root_id);
let index_html = self
.index_html
.unwrap_or_else(|| load_index_path(index_path));
let index = load_index_html(index_html, root_id);
ServeConfig {
index,
assets_path,
@@ -100,13 +111,16 @@ impl ServeConfigBuilder {
}
}
fn load_index_html(path: PathBuf, root_id: &'static str) -> IndexHtml {
fn load_index_path(path: PathBuf) -> String {
let mut file = File::open(path).expect("Failed to find index.html. Make sure the index_path is set correctly and the WASM application has been built.");
let mut contents = String::new();
file.read_to_string(&mut contents)
.expect("Failed to read index.html");
contents
}
fn load_index_html(contents: String, root_id: &'static str) -> IndexHtml {
let (pre_main, post_main) = contents.split_once(&format!("id=\"{root_id}\"")).unwrap_or_else(|| panic!("Failed to find id=\"{root_id}\" in index.html. The id is used to inject the application into the page."));
let post_main = post_main.split_once('>').unwrap_or_else(|| {