hot-watch
🔥👀 Watch files and hot reload them
Installation
yarn add -D hot-watch
Usage
const http = ;const watch = ; const server = http; server;
Express middleware
There is also an useful middleware to help get started in express application.
const express = ;const hotWatchMiddleware = ; const app = ; // It's also very common to only use it during developmentif processenvNODE_ENV !== 'production' // Initialize it and insert it before request handler or router app; app; app;
Once setup, the express server will wait for the files changed and perform necessary cleanups/restart before running the next request handlers or sending back the data.
API
// watch accepts an optional option object and here are their default valuesconst unwatch = ; ; // Stop and close the watcher
How
The basic idea is to clean up require.cache
key whenever the files changed. We have to walk up the require tree to delete the caches for all it's parents. The native module API only stores the first cached parent for each module. In order to get all the parents and their parents and so on, we monkey-patch require
to store all their parents in an internal parentsMap
.
License
MIT