Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

feat: Implement livereloading for wrangler preview #252

Closed
wants to merge 61 commits into from

Conversation

xortive
Copy link
Contributor

@xortive xortive commented Jun 14, 2019

this PR adds the --watch flag to build and preview, which watches your Wrangler project for changes and builds it when it's ready. It works for all project types

  • javascript -> watches the entry point in package.json
  • rust -> watches the /src directory, and the entry point in package.json
  • webpack -> uses webpack's watching, which just works

When run in --watch mode, preview hides the fiddle editor, since you would be using a local text editor instead.

to test:
cargo install --force --debug --git https://github.com/xortive/wrangler --branch malonso/hotreload

Closes #235

@xortive xortive removed this from the 1.1.0 milestone Jul 1, 2019
@xortive xortive self-assigned this Jul 1, 2019
@xortive xortive force-pushed the malonso/hotreload branch 3 times, most recently from 4b9b39a to 0e0034d Compare July 9, 2019 17:04
@xortive xortive requested a review from ejcx July 9, 2019 22:02
@xortive xortive changed the title WIP: feat: Implement livereloading for wrangler preview feat: Implement livereloading for wrangler preview Jul 9, 2019
@xortive
Copy link
Contributor Author

xortive commented Jul 9, 2019

This is feature complete!

Tagging @ejcx for security review on the wrangler side of this, specifically directed towards the websocket portion of this PR.

use std::thread;
use std::time::Duration;

pub const COOLDOWN_PERIOD: Duration = Duration::from_millis(2000);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering making this a config option, it refers to the amount of time wrangler will ignore new fs events when it detects the first one. Any new events during the cooldown period restart the timer.

@xortive
Copy link
Contributor Author

xortive commented Jul 10, 2019

the ctrl-c handler doesn't work after the notifier detects it's first change. Have no idea where to even begin debugging something like this

just going to revert, os should clean the temp file in the temp dir on reboot anyways so we shouldn't need an explicit ctrlc handler

@xortive
Copy link
Contributor Author

xortive commented Jul 15, 2019

I'm mildly concerned about the stability of this feature, and I think maybe we should include it in a RC release first or gate it under an experimental flag. The worst thing that can happen however is a poor user experience -- the watcher not seeing an update or wrangler panicing since it expects a file to be present that is not present.

Especially with the webpack project type that effectively has two file watchers connected together (webpack watches project, we watch webpack output) there may be a race condition or two lingering that could cause a panic. Better error handling or tests for livereloading would mitigate this concern.

@kristianfreeman
Copy link
Contributor

kristianfreeman commented Aug 15, 2019

this 502s for me locally :( unclear if it's related to #429, what would be useful for debugging here?

☁  worker  ~/.cargo/bin/wrangler preview --watch
⬇️ Installing wasm-pack...
✨   Built successfully, built project size is 2 KiB.
Error: https://cloudflareworkers.com/script: Server Error: 502 Bad Gateway

update: talked to @ashleygwilliams about this, the project is 502ing because it has KV bindings in wrangler.toml. keeping this project around for QA tomorrow...

@kristianfreeman
Copy link
Contributor

this worked really well for me with a new application, nice work @xortive!

one piece of feedback: as i watched the live reloading happen in the terminal, i found that it was unclear when the browser should be up-to-date. here's the terminal output i saw a few times as i made changes:

🌀  Detected changes...
🌀  Detected change during cooldown...
🌀  Cooldown over, propogating changes...
✨   Built successfully, built project size is 570 bytes.
👷  Updating preview with changes

it seems like the "Updating preview with changes" step is always the last thing that happens before the preview actually, well, updates. would it make sense to change that text output to "Updated preview with changes", or have another line of output that indicates when the preview window is done updating? not a blocker, just stood out to me as i was trying this out

&session.to_string(), ws_port, script_id, https_str, preview_host,
))?;

//don't do initial GET + POST with livereload as the expected behavior is unclear.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you mean here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we ignore the wrangler preview get --watch and wrangler preview post --watch commands as it is unclear what they should do. They will be addressed once we have a command for browserless preview

Copy link
Contributor

@EverlastingBugstopper EverlastingBugstopper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking really good with the new changes to authenticated preview! Want to wait for @ashleymichal and @signalnerve to review w/their KV projects before merging.

I think Kristian is going to take on writing up a section in the README.

One more todo for @xortive - please hide the sidebar for wrangler preview, this is looking awesome awesome!! just merge this PR xortive#1

@kristianfreeman
Copy link
Contributor

my previous issue w/ wrangler preview --watch on a project with kv has been fixed - yay! i'll work on some readme documentation, will track it in a separate PR

Copy link
Contributor

@EverlastingBugstopper EverlastingBugstopper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

Copy link
Contributor

@ashleygwilliams ashleygwilliams left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<3

src/commands/build/watch/mod.rs Outdated Show resolved Hide resolved
src/commands/build/watch/mod.rs Outdated Show resolved Hide resolved
Ok(Some(path)) => {
message::working("Detected changes...");
//wait for cooldown
while let Ok(_e) = rx.recv_timeout(cooldown) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

convention here would be to just write Ok(_)

project: &Project,
tx: Option<Sender<()>>,
) -> Result<(), failure::Error> {
let (mut command, temp_file, bundle) = setup_build(project)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

long term we probably want a better solution here ;)

let _command_guard = util::GuardedCommand::spawn(command);

let (watcher_tx, watcher_rx) = channel();
let mut watcher = watcher(watcher_tx, Duration::from_secs(1)).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i am worried about these unwraps!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we can: let's get rid of unwraps- make them errors if at all possible. if not possible: info debugging lines would be very useful. ask yourself- when this panics.. what am i gonna want to know

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(this is basically creating an expect)

src/commands/build/wranglerjs/mod.rs Outdated Show resolved Hide resolved
src/commands/publish/preview/upload.rs Show resolved Hide resolved
@@ -188,10 +200,18 @@ fn run() -> Result<(), failure::Error> {
None => None,
};
commands::init(name, project_type)?;
} else if matches.subcommand_matches("build").is_some() {
} else if let Some(matches) = matches.subcommand_matches("build") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need build --watch?

src/main.rs Outdated
@@ -207,7 +227,12 @@ fn run() -> Result<(), failure::Error> {
None => None,
};

commands::preview(project, user, method, body)?;
let watch = match matches.occurrences_of("watch") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

match presence of not occurences .. because what if someone types wrangler preview --watch --watch

@ashleygwilliams
Copy link
Contributor

closing in favor of #451

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add watch option to wrangler build
8 participants