Skip to content

Commit

Permalink
add autoreload to trunk serve
Browse files Browse the repository at this point in the history
triggered by an injected websocket script
enabled with --autoreload (default false)
  • Loading branch information
kcking committed Jul 2, 2021
1 parent 7126872 commit 0fec6a3
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 70 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Subheadings to categorize changes are `added, changed, deprecated, removed, fixe

## Unreleased
- Added an example application for using Trunk with a vanilla (no frameworks) Rust application.
- Added `--autoreload` flag to `trunk serve` that automatically reloads the web frontend on a new build finishing (default `false`).

## 0.11.0
### added
Expand Down
107 changes: 67 additions & 40 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ansi_term = "0.12.1"
anyhow = "1"
async-process = "1"
async-std = { version = "1.9", default-features = false, features = ["attributes", "std", "unstable"] }
async-tungstenite = { version = "0.10.0", default-features = false, features = ["async-std-runtime"] }
async-tungstenite = { version = "0.13.0", default-features = false, features = ["async-std-runtime"] }
cargo_metadata = "0.12"
console = "0.14"
dunce = "1"
Expand All @@ -40,10 +40,11 @@ structopt = "0.3"
structopt-derive = "0.4"
surf = "2"
tide = { version = "0.16.0", default-features = false, features = ["h1-server", "sessions", "unstable"] }
tide-websockets = "0.3.0"
tide-websockets = "0.4.0"
toml = "0.5"
tracing = "0.1.25"
tracing-subscriber = "0.2.16"
kuchiki = "0.8.1"

[dev-dependencies]
insta = "0.16.1"
9 changes: 9 additions & 0 deletions src/autoreload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(function () {
var ws = new WebSocket('ws://' + window.location.host);
ws.onmessage = (ev) => {
const msg = JSON.parse(ev.data);
if (msg.reload) {
window.location.reload();
}
};
})()
2 changes: 1 addition & 1 deletion src/cmd/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Watch {
#[tracing::instrument(level = "trace", skip(self, config))]
pub async fn run(self, config: Option<PathBuf>) -> Result<()> {
let cfg = ConfigOpts::rtc_watch(self.build, self.watch, config)?;
let mut system = WatchSystem::new(cfg).await?;
let mut system = WatchSystem::new(cfg, None).await?;
system.build().await;
system.run().await;
Ok(())
Expand Down
15 changes: 7 additions & 8 deletions src/config/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ pub struct ConfigOptsServe {
#[structopt(long = "proxy-ws")]
#[serde(default)]
pub proxy_ws: bool,
/// Whether to auto-reload the web page when a build completes.
#[structopt(long)]
#[serde(default)]
pub autoreload: bool,
}

/// Config options for the serve system.
Expand Down Expand Up @@ -169,10 +173,7 @@ impl ConfigOpts {
}

fn cli_opts_layer_watch(cli: ConfigOptsWatch, cfg_base: Self) -> Self {
let opts = ConfigOptsWatch {
watch: cli.watch,
ignore: cli.ignore,
};
let opts = ConfigOptsWatch { watch: cli.watch, ignore: cli.ignore };
let cfg = ConfigOpts {
build: None,
watch: Some(opts),
Expand All @@ -190,6 +191,7 @@ impl ConfigOpts {
proxy_backend: cli.proxy_backend,
proxy_rewrite: cli.proxy_rewrite,
proxy_ws: cli.proxy_ws,
autoreload: cli.autoreload,
};
let cfg = ConfigOpts {
build: None,
Expand All @@ -202,10 +204,7 @@ impl ConfigOpts {
}

fn cli_opts_layer_clean(cli: ConfigOptsClean, cfg_base: Self) -> Self {
let opts = ConfigOptsClean {
dist: cli.dist,
cargo: cli.cargo,
};
let opts = ConfigOptsClean { dist: cli.dist, cargo: cli.cargo };
let cfg = ConfigOpts {
build: None,
watch: None,
Expand Down
Loading

0 comments on commit 0fec6a3

Please sign in to comment.