Add liveview support to the CLI and make fullstack runnable from dist (#2759)

* add liveview cli support
* Fix TUI fullstack deadlock
* look for fullstack assets in the public directory
* Fix fullstack with the CLI
* Fix static generation server
This commit is contained in:
Evan Almloff authored and GitHub committed 2024-08-01 17:46:35 -07:00
1 parent 9dbdf74a1e
commit e5e578d27b
14 files changed
+428 -272

No files matched your search

+47 -39
View File
@@ -1,14 +1,16 @@
use crate::builder::{
BuildMessage, MessageSource, MessageType, Stage, UpdateBuildProgress, UpdateStage,
BuildMessage, BuildRequest, MessageSource, MessageType, Stage, UpdateBuildProgress, UpdateStage,
};
use crate::dioxus_crate::DioxusCrate;
use crate::Result;
use anyhow::Context;
use brotli::enc::BrotliEncoderParams;
use futures_channel::mpsc::UnboundedSender;
use manganis_cli_support::{process_file, AssetManifest, AssetManifestExt, AssetType};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use std::fs;
use std::path::Path;
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;
use std::{ffi::OsString, path::PathBuf};
use std::{fs::File, io::Write};
use tracing::Level;
@@ -17,8 +19,8 @@ use walkdir::WalkDir;
/// The temp file name for passing manganis json from linker to current exec.
pub const MG_JSON_OUT: &str = "mg-out";
pub fn asset_manifest(config: &DioxusCrate) -> AssetManifest {
let file_path = config.out_dir().join(MG_JSON_OUT);
pub fn asset_manifest(build: &BuildRequest) -> AssetManifest {
let file_path = build.target_out_dir().join(MG_JSON_OUT);
let read = fs::read_to_string(&file_path).unwrap();
_ = fs::remove_file(file_path);
let json: Vec<String> = serde_json::from_str(&read).unwrap();
@@ -27,58 +29,64 @@ pub fn asset_manifest(config: &DioxusCrate) -> AssetManifest {
}
/// Create a head file that contains all of the imports for assets that the user project uses
pub fn create_assets_head(config: &DioxusCrate, manifest: &AssetManifest) -> Result<()> {
let mut file = File::create(config.out_dir().join("__assets_head.html"))?;
pub fn create_assets_head(build: &BuildRequest, manifest: &AssetManifest) -> Result<()> {
let out_dir = build.target_out_dir();
std::fs::create_dir_all(&out_dir)?;
let mut file = File::create(out_dir.join("__assets_head.html"))?;
file.write_all(manifest.head().as_bytes())?;
Ok(())
}
/// Process any assets collected from the binary
pub(crate) fn process_assets(
config: &DioxusCrate,
build: &BuildRequest,
manifest: &AssetManifest,
progress: &mut UnboundedSender<UpdateBuildProgress>,
) -> anyhow::Result<()> {
let static_asset_output_dir = config.out_dir();
let static_asset_output_dir = build.target_out_dir();
std::fs::create_dir_all(&static_asset_output_dir)
.context("Failed to create static asset output directory")?;
let mut assets_finished: usize = 0;
let assets_finished = Arc::new(AtomicUsize::new(0));
let assets = manifest.assets();
let asset_count = assets.len();
assets.iter().try_for_each(move |asset| {
if let AssetType::File(file_asset) = asset {
match process_file(file_asset, &static_asset_output_dir) {
Ok(_) => {
// Update the progress
_ = progress.start_send(UpdateBuildProgress {
stage: Stage::OptimizingAssets,
update: UpdateStage::AddMessage(BuildMessage {
level: Level::INFO,
message: MessageType::Text(format!(
"Optimized static asset {}",
file_asset
)),
source: MessageSource::Build,
}),
});
assets_finished += 1;
_ = progress.start_send(UpdateBuildProgress {
stage: Stage::OptimizingAssets,
update: UpdateStage::SetProgress(
assets_finished as f64 / asset_count as f64,
),
});
}
Err(err) => {
tracing::error!("Failed to copy static asset: {}", err);
return Err(err);
assets.par_iter().try_for_each_init(
|| progress.clone(),
move |progress, asset| {
if let AssetType::File(file_asset) = asset {
match process_file(file_asset, &static_asset_output_dir) {
Ok(_) => {
// Update the progress
_ = progress.start_send(UpdateBuildProgress {
stage: Stage::OptimizingAssets,
update: UpdateStage::AddMessage(BuildMessage {
level: Level::INFO,
message: MessageType::Text(format!(
"Optimized static asset {}",
file_asset
)),
source: MessageSource::Build,
}),
});
let assets_finished =
assets_finished.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
_ = progress.start_send(UpdateBuildProgress {
stage: Stage::OptimizingAssets,
update: UpdateStage::SetProgress(
assets_finished as f64 / asset_count as f64,
),
});
}
Err(err) => {
tracing::error!("Failed to copy static asset: {}", err);
return Err(err);
}
}
}
}
Ok::<(), anyhow::Error>(())
})?;
Ok::<(), anyhow::Error>(())
},
)?;
Ok(())
}