Skip to content

Conversation

robblovell
Copy link
Contributor

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:

   offlineEdgeLambda: {
      path: '.esbuild/.build',
      watchReload: true,
    },

Additional options can be used to modify the behavior of the file watcher and debounce logic (ignoreInitial, awaitWriteFinish, interval, debounce).

For example:

   offlineEdgeLambda: {
      path: '.esbuild/.build',
      watchReload: true,
      ignoreInitial: true,
      awaitWriteFinish: true,
      interval: 500,
      debounce: 750,
   },

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.

@robblovell robblovell closed this Sep 11, 2022
@robblovell robblovell deleted the feature/505-hot-reload branch September 11, 2022 08:41
@robblovell robblovell restored the feature/505-hot-reload branch September 12, 2022 17:35
@robblovell
Copy link
Contributor Author

Somehow I managed to close this off unintentionally. I am reopening.

@robblovell robblovell reopened this Sep 12, 2022
@robblovell
Copy link
Contributor Author

The readme should be updated to document the new options for hot reload.

@robblovell robblovell force-pushed the feature/505-hot-reload branch from 7b0ae7e to f993d8c Compare September 12, 2022 18:24
@robblovell robblovell force-pushed the feature/505-hot-reload branch from cf9e044 to 38f2da6 Compare September 12, 2022 19:07
@robblovell robblovell force-pushed the feature/505-hot-reload branch from 38f2da6 to ede96b5 Compare September 12, 2022 19:15
Copy link
Collaborator

@mattstrom mattstrom left a 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

@mattstrom mattstrom merged commit 703d1f4 into evolv-ai:master Sep 19, 2022
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))
@robblovell robblovell deleted the feature/505-hot-reload branch September 21, 2022 17:36
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

Successfully merging this pull request may close these issues.

2 participants