Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HMR/live-reload support #5

Open
shirakaba opened this issue Jan 30, 2021 · 0 comments
Open

HMR/live-reload support #5

shirakaba opened this issue Jan 30, 2021 · 0 comments

Comments

@shirakaba
Copy link
Collaborator

Live reload

The minimal bash script livereload.sh seems to do the trick for live reload, simply by killing the app and relaunching it upon any Webpack hot update; I'm not sure we need anything more than that. It remains to be seen whether it works on non-bash CLIs, like that of Windows.

I initially had some other thoughts about how we could implement it (https://github.com/remy/nodemon/ and https://github.com/napcs/node-livereload, though both may not be quite what we need).

I suppose the disadvantage of my approach of killing the app altogether is that your debugger would keep detaching. That might be par for the course for live reload implementations, however – not too sure.

Current implementation

In our Webpack config, we inject a globally-available variable into the runtime context: __HMR_MODE__. This variable accepts the values "live-reload" | "hmr" | "none".

The only place we accept hot updates is in the entrypoint file, app.ts. Depending on the value of __HMR_MODE__, we do the following upon detecting a hot update in that file (which may have bubbled up from another file):

  • "live-reload": Sends a special exit signal (64), so that the app will be relaunched if running via livereload.sh.
  • "hmr": Applies the hot update without exiting the app (not yet implemented – see "HMR" section below).
  • "none": The app remains alive, but doesn't apply the update.
  • undefined: Behaves as "none".

We'd then instruct users to run the app by defining their npm run start script as ./livereload.sh node_modules/.bin/qode ./dist/index.js.

livereload.sh simply runs the given command (in this case, node_modules/.bin/qode ./dist/index.js) in a loop until it receives an appropriate exit signal like SIGTERM. If it receives our custom exit signal 64 (which we emit upon receiving a hot update when __HMR_MODE__ is set to "live-reload", then we terminate the process and execute it again. In other words, we relaunch the app. The Ctrl+C signal will still kill the app as normal, and all console logs from the process are still captured and shown in the terminal.

Extra reading

Some of this reading may be relevant for HMR. I looked into how Live Reload was implemented (under the name "LiveSync") in NativeScript:

HMR

This is far tougher to solve. HMR is experimental even in Svelte for Web, and Rixo (who developed it) said that supporting HMR for Svelte Native required bespoke work. So this is not something we can get "for free" just by setting things up right.

Extra reading

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

No branches or pull requests

1 participant