Skip to content

Commit

Permalink
fix: upgraded deps, simplified codebase and fixed logic for watch/not…
Browse files Browse the repository at this point in the history
…ify/patch (#404)

* fix: upgraded deps, simplified codebase and fixed logic for watch/notify/patch

* fix: update examples wasm-bindgen version

* fix: typo

* chore: bump `wasm-bindgen` to `0.2.99`

* fix: server reloads the connection by itself

* chore: update `Cargo.lock`

* chore: update `swc` to `9.0`

* fix: simplified bindgen usage in front

* fix: minify and optimize tailwindcss on release mode

* chore: cargo update

* fix: update cargo-dist

* Revert "fix: update cargo-dist" in favor of #405

This reverts commit 405e813.
  • Loading branch information
sabify authored Dec 19, 2024
1 parent 0f71b75 commit b900521
Show file tree
Hide file tree
Showing 23 changed files with 547 additions and 748 deletions.
524 changes: 231 additions & 293 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ flexi_logger = "0.29.3"
tokio = { version = "1.40.0", default-features = false, features = ["full"] }
axum = { version = "0.7.7", features = ["ws"] }
# not using notify 5.0 because it uses Crossbeam which has an issue with tokio
notify = "4.0"
notify = "7.0"
lazy_static = "1.4"
which = "6"
cargo_metadata = { version = "0.18", features = ["builder"] }
which = "7"
cargo_metadata = { version = "0.19", features = ["builder"] }
serde_json = "1.0.128"
wasm-bindgen-cli-support = "0.2.99"
ansi_term = "0.12"
Expand All @@ -85,14 +85,14 @@ zip = { version = "2.2", default-features = false, features = ["deflate"] }
tar = "0.4.42"
dunce = "1.0"
bytes = "1.7.2"
leptos_hot_reload = "0.6"
leptos_hot_reload = "0.7"
pathdiff = { version = "0.2.2", features = ["camino"] }
semver = "1.0.23"
async-trait = "0.1.83"
md-5 = "0.10.6"
base64ct = { version = "1.6.0", features = ["alloc"] }
swc = "1.0.1"
swc_common = "1.0.0"
swc = "9.0"
swc_common = "5.0"
shlex = "1.3.0"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion examples/project/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions examples/project/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ pub async fn run() -> std::io::Result<()> {
_ = dotenvy::dotenv();

let conf = get_configuration(None).await.unwrap();
let addr = conf.leptos_options.site_addr.clone();
let addr = conf.leptos_options.site_addr;

log::info!("serving at {addr}");

// Generate the list of routes in your Leptos App
let routes = generate_route_list(|| view! { <App/> });
let routes = generate_route_list(|| view! { <App /> });

HttpServer::new(move || {
let leptos_options = &conf.leptos_options;
Expand All @@ -24,7 +24,7 @@ pub async fn run() -> std::io::Result<()> {
.leptos_routes(
leptos_options.to_owned(),
routes.to_owned(),
|| view! { <App/> },
|| view! { <App /> },
)
.service(Files::new("/", site_root.to_owned()))
.wrap(middleware::Compress::default())
Expand Down
2 changes: 1 addition & 1 deletion examples/workspace/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/workspace/project1/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub async fn main() -> std::io::Result<()> {
_ = dotenvy::dotenv();

let conf = get_configuration(None).await.unwrap();
let addr = conf.leptos_options.site_addr.clone();
let addr = conf.leptos_options.site_addr;

log::info!("serving at {addr}");

Expand Down
2 changes: 1 addition & 1 deletion examples/workspace/project2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ log = "0.4"
cfg-if = "1.0"

# dependecies for client (enable hydrate set)
wasm-bindgen = { version = "0.2.95", optional = true }
wasm-bindgen = { version = "=0.2.99", optional = true }
console_log = { version = "1.0", optional = true }
console_error_panic_hook = { version = "0.1", optional = true }

Expand Down
2 changes: 1 addition & 1 deletion examples/workspace/project2/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub async fn run() -> std::io::Result<()> {
simple_logger::init_with_level(log::Level::Debug).expect("couldn't initialize logging");

let conf = get_configuration(None).await.unwrap();
let addr = conf.leptos_options.site_addr.clone();
let addr = conf.leptos_options.site_addr;

log::info!("serving at {addr}");

Expand Down
6 changes: 1 addition & 5 deletions src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ pub async fn build_proj(proj: &Arc<Project>) -> Result<bool> {
if !compile::front(proj, &changes).await.await??.is_success() {
return Ok(false);
}
if !compile::assets(proj, &changes, true)
.await
.await??
.is_success()
{
if !compile::assets(proj, &changes).await.await??.is_success() {
return Ok(false);
}
if !compile::style(proj, &changes).await.await??.is_success() {
Expand Down
110 changes: 52 additions & 58 deletions src/command/watch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::sync::Arc;

use super::build::build_proj;
use crate::{
compile::{self},
config::Project,
Expand All @@ -9,10 +8,9 @@ use crate::{
};
use anyhow::Result;
use leptos_hot_reload::ViewMacros;
use std::sync::Arc;
use tokio::try_join;

use super::build::build_proj;

pub async fn watch(proj: &Arc<Project>) -> Result<()> {
// even if the build fails, we continue
build_proj(proj).await?;
Expand Down Expand Up @@ -50,66 +48,62 @@ pub async fn run_loop(proj: &Arc<Project>) -> Result<()> {
let mut int = Interrupt::subscribe_any();
loop {
log::debug!("Watch waiting for changes");

int.recv().await.dot()?;

if Interrupt::is_shutdown_requested().await {
log::debug!("Shutting down");
return Ok(());
}

let changes = Interrupt::get_source_changes().await;

// spawn separate style-update process
tokio::spawn({
let changes = changes.to_owned();
let proj = Arc::clone(proj);
async move {
let style = compile::style(&proj, &changes).await;
if let Ok(Ok(Outcome::Success(Product::Style(_)))) = style.await {
ReloadSignal::send_style();
log::info!("Watch updated style");
Interrupt::clear_source_changes().await;
}
}
});

let server_hdl = compile::server(proj, &changes).await;
let front_hdl = compile::front(proj, &changes).await;
let assets_hdl = compile::assets(proj, &changes, false).await;

let (serve, front, assets) = try_join!(server_hdl, front_hdl, assets_hdl)?;

let outcomes = vec![serve?, front?, assets?];

let failed = outcomes.iter().any(|outcome| *outcome == Outcome::Failed);
let interrupted = outcomes.iter().any(|outcome| *outcome == Outcome::Stopped);

if failed {
log::warn!("Build failed");
Interrupt::clear_source_changes().await;
} else if interrupted {
log::info!("Build interrupted. Restarting.");
} else {
let set = ProductSet::from(outcomes);

if set.is_empty() {
log::trace!("Build step done with no changes");
} else {
log::trace!("Build step done with changes: {set}");
}

if set.only_style() {
ReloadSignal::send_style();
log::info!("Watch updated style")
} else if set.contains(&Product::Server) {
// send product change, then the server will send the reload once it has restarted
ServerRestart::send();
log::info!("Watch updated {set}. Server restarting")
} else if set.contains_any(&[Product::Front, Product::Assets]) {
ReloadSignal::send_full();
log::info!("Watch updated {set}")
}
Interrupt::clear_source_changes().await;
}
runner(proj).await?;
}
}

pub async fn runner(proj: &Arc<Project>) -> Result<()> {
let changes = Interrupt::get_source_changes().await;

let server_hdl = compile::server(proj, &changes).await;
let front_hdl = compile::front(proj, &changes).await;
let assets_hdl = compile::assets(proj, &changes).await;
let style_hdl = compile::style(proj, &changes).await;

let (server, front, assets, style) = try_join!(server_hdl, front_hdl, assets_hdl, style_hdl)?;

let outcomes = vec![server?, front?, assets?, style?];

let interrupted = outcomes.iter().any(|outcome| *outcome == Outcome::Stopped);
if interrupted {
log::info!("Build interrupted. Restarting.");
return Ok(());
}

let failed = outcomes.iter().any(|outcome| *outcome == Outcome::Failed);
if failed {
log::warn!("Build failed");
Interrupt::clear_source_changes().await;
return Ok(());
}

let set = ProductSet::from(outcomes);

if set.is_empty() {
log::trace!("Build step done with no changes");
} else {
log::trace!("Build step done with changes: {set}");
}

if set.contains(&Product::Server) {
// send product change, then the server will send the reload once it has restarted
ServerRestart::send();
log::info!("Watch updated {set}. Server restarting")
} else if set.only_style() {
ReloadSignal::send_style();
log::info!("Watch updated style")
} else if set.contains_any(&[Product::Front, Product::Assets]) {
ReloadSignal::send_full();
log::info!("Watch updated {set}")
}
Interrupt::clear_source_changes().await;
Ok(())
}
96 changes: 11 additions & 85 deletions src/compile/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use std::sync::Arc;
use super::ChangeSet;
use crate::config::Project;
use crate::ext::anyhow::{Context, Result};
use crate::service::notify::Watched;
use crate::service::site::SourcedSiteFile;
use crate::signal::{Outcome, Product};
use crate::{ext::PathExt, fs, logger::GRAY};
use camino::{Utf8Path, Utf8PathBuf};
Expand All @@ -13,100 +11,28 @@ use tokio::task::JoinHandle;
pub async fn assets(
proj: &Arc<Project>,
changes: &ChangeSet,
first_sync: bool,
) -> JoinHandle<Result<Outcome<Product>>> {
let changes = changes.clone();

let proj = proj.clone();
tokio::spawn(async move {
if !changes.need_assets_change() {
return Ok(Outcome::Success(Product::None));
}
let Some(assets) = &proj.assets else {
return Ok(Outcome::Success(Product::None));
};
let dest_root = &proj.site.root_dir;
let pkg_dir = &proj.site.pkg_dir;

let change = if first_sync {
log::trace!("Assets starting full resync");
resync(&assets.dir, dest_root, pkg_dir).await?;
true
} else {
let mut changed = false;
for watched in changes.asset_iter() {
log::trace!("Assets processing {watched:?}");
let change =
update_asset(&proj, watched.clone(), &assets.dir, dest_root, pkg_dir, &[])
.await?;
changed |= change;
}
changed
};
if change {
log::debug!("Assets finished (with changes)");
Ok(Outcome::Success(Product::Assets))
} else {
log::debug!("Assets finished (no changes)");
Ok(Outcome::Success(Product::None))
}
})
}

async fn update_asset(
proj: &Project,
watched: Watched,
src_root: &Utf8Path,
dest_root: &Utf8Path,
pkg_dir: &Utf8Path,
reserved: &[Utf8PathBuf],
) -> Result<bool> {
if let Some(path) = watched.path() {
if reserved.contains(path) {
log::warn!("Assets reserved filename for Leptos. Please remove {path:?}");
return Ok(false);
}
}
Ok(match watched {
Watched::Create(f) => {
let to = f.rebase(src_root, dest_root)?;
if f.is_dir() {
fs::copy_dir_all(f, to).await?;
} else {
fs::copy(&f, &to).await?;
}
true
}
Watched::Remove(f) => {
let path = f.rebase(src_root, dest_root)?;
if path.is_dir() {
fs::remove_dir_all(&path)
.await
.context(format!("remove dir recursively {path:?}"))?;
} else {
fs::remove_file(&path)
.await
.context(format!("remove file {path:?}"))?;
}
false
}
Watched::Rename(from, to) => {
let from = from.rebase(src_root, dest_root)?;
let to = to.rebase(src_root, dest_root)?;
fs::rename(&from, &to)
.await
.context(format!("rename {from:?} to {to:?}"))?;
true
}
Watched::Write(f) => {
let file = SourcedSiteFile {
source: f.clone(),
dest: f.rebase(src_root, dest_root)?,
site: f.unbase(src_root)?,
};
proj.site.updated(&file).await?
}
Watched::Rescan => {
resync(src_root, dest_root, pkg_dir).await?;
true
}
// if reserved.contains(assets.dir) {
// log::warn!("Assets reserved filename for Leptos. Please remove {watched:?}");
// return Ok(false);
// }
log::trace!("Assets starting resync");
resync(&assets.dir, dest_root, pkg_dir).await?;
log::debug!("Assets finished");
Ok(Outcome::Success(Product::Assets))
})
}

Expand Down
Loading

0 comments on commit b900521

Please sign in to comment.