-
Notifications
You must be signed in to change notification settings - Fork 11
fix: Hot reloading with chokidar & busting of the require cache. #515
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
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Somehow I managed to close this off unintentionally. I am reopening. |
robblovell
commented
Sep 12, 2022
robblovell
commented
Sep 12, 2022
The readme should be updated to document the new options for hot reload. |
7b0ae7e
to
f993d8c
Compare
cf9e044
to
38f2da6
Compare
38f2da6
to
ede96b5
Compare
mattstrom
approved these changes
Sep 19, 2022
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution! I resolved two small conflicts in package.json and package-lock.json
github-actions bot
pushed a commit
that referenced
this pull request
Sep 19, 2022
## [1.2.2](1.2.1...1.2.2) (2022-09-19) ### Bug Fixes * Hot reloading with chokidar & busting of the require cache. ([#515](#515)) ([703d1f4](703d1f4))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hot Reload
This is a fix for issue #505, hot reload not working.
The solution applied here is to use chokidar to watch files in the distribution/bundle folder, wether that be esbuild, webpack, or typescript doing the build.
Upon a rebuild, the node.js require cache is busted for the changed handler 'extractBehaviors()' is re-called to reload the handlers. The reload is via an import statement which caches imported code, so a cache bust is required for the newly compiled/bundled code to be loaded.
Chokidar is debounced so that the handlers are reloaded at the end of a build. Any file in the directory specified by the "path" option will be watched and if changed hot-reloaded. Note that the default path is the current directory (".").
I use esbuild and have not tested this with typescript based builds, but the same principles will apply and it should work.
The watch/reload mechanism is disabled by default so that current usage is unchanged. The flag "watchReload: true" will turn on the watcher so that typescript and esbuild solutions use the watcher to hot reload the handlers.
example:
Additional options can be used to modify the behavior of the file watcher and debounce logic (ignoreInitial, awaitWriteFinish, interval, debounce).
For example:
Implementations using webpack appears to have a solution already in place for hot reload. However, my experimentation with the server restart mechanism used by the webpack solution's onReload() function appears to not work very well. It might be better for the onReload() to call extractBehaviors() instead of server.restart(). This could be implemented in a future pull request.