feat: add custom html support

This commit is contained in:
mrxiaozhuox committed 2022-03-20 19:31:30 +08:00
1 parent 6b0d57abe5
commit e5dfe84eb1
3 files changed
+26 -6

No files matched your search

-2
View File
@@ -4,11 +4,9 @@
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" /> <meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<meta charset="UTF-8" /> <meta charset="UTF-8" />
{style_include} {style_include}
</head> </head>
<body> <body>
<div id="main"></div> <div id="main"></div>
<!-- Note the usage of `type=module` here as this is an ES6 module -->
<script type="module"> <script type="module">
import init from "./assets/dioxus/{app_name}.js"; import init from "./assets/dioxus/{app_name}.js";
init("./assets/dioxus/{app_name}_bg.wasm"); init("./assets/dioxus/{app_name}_bg.wasm");
+15 -2
View File
@@ -4,7 +4,8 @@ use crate::{
DioxusConfig, DioxusConfig,
}; };
use std::{ use std::{
fs::{copy, create_dir_all, remove_dir_all}, fs::{copy, create_dir_all, remove_dir_all, File},
io::Read,
panic, panic,
path::PathBuf, path::PathBuf,
process::Command, process::Command,
@@ -272,7 +273,19 @@ pub fn build_desktop(config: &CrateConfig, is_serve: bool) -> Result<()> {
} }
pub fn gen_page(config: &DioxusConfig, serve: bool) -> String { pub fn gen_page(config: &DioxusConfig, serve: bool) -> String {
let mut html = String::from(include_str!("./assets/index.html")); let crate_root = crate::cargo::crate_root().unwrap();
let custom_html_file = crate_root.join("index.html");
let mut html = if custom_html_file.is_file() {
let mut buf = String::new();
let file = File::open(custom_html_file).unwrap();
if let Ok(v) = file.read_to_string(&mut buf) {
buf
} else {
String::from(include_str!("./assets/index.html"))
}
} else {
String::from(include_str!("./assets/index.html"));
};
let resouces = config.web.resource.clone(); let resouces = config.web.resource.clone();
+11 -2
View File
@@ -18,8 +18,10 @@ pub enum Config {
#[clap(long, default_value = "web")] #[clap(long, default_value = "web")]
platform: String, platform: String,
}, },
/// Format Print Dioxus-Config. /// Format print Dioxus config.
FormatPrint {} FormatPrint {},
/// Create a custom html file.
CustomHtml {}
} }
impl Config { impl Config {
@@ -48,6 +50,13 @@ impl Config {
Config::FormatPrint {} => { Config::FormatPrint {} => {
println!("{:#?}", crate::CrateConfig::new()?.dioxus_config); println!("{:#?}", crate::CrateConfig::new()?.dioxus_config);
} }
Config::CustomHtml {} => {
let html_path = crate_root.join("index.html");
let mut file = File::create(html_path)?;
let content = include_str!("../../assets/index.html");
file.write_all(content.as_bytes())?;
log::info!("🚩 Create custom html file done.");
}
} }
Ok(()) Ok(())
} }