HMR natively in Node.js (technical write up) One of the key factors in rapid development is discarding as little state as possible. In Node.js, this means the new --watch flags are not that useful, since they throw everything away. The ideal is to simply invalidate a module when it changes or when a module it depends on changes. This way, all imports and data are always fresh, but only partial module trees get re-evaluated. Previously, immaculata did the same thing Vite does now: use the built-in node:vm functionality to create an ad hoc module system that sits on top of Node's, and glues these systems together using custom logic. This effectively creates second class modules. The main drawbacks are that logic is duplicated and separated. Duplication happens in finding and loading files, parsing them, evaluating and storing their module objects, and gluing all these together with each other and with Node's own module system. And these systems are inherently separate, so that native Node module hooks will have no effect on the ad hoc system. By adding module hooks using Node's built-in node:module module, it's now possible to implement "hot module" functionality natively. First, we load source files from disk and keep them in memory. This won't hit memory limits for most projects and dev machines. To handle this need, we have a FileTree class, which does nothing other than load a file tree into memory, and optionally keep it up to date via .watch(), which returns an EventEmitter with a filesUpdated event. Node's native file watcher is now disk efficient, and returns all the information we need, so we don't need chokidar for this. Next, we have the useTree dual-hook which does two key things. First, it implements a loader hook that returns the source string using tree.files.get instead of fs.readFileSync. Second, it implements a resolver hook that appends ?ver=${file.version} to the URL of any given module. What ties all of this together is the fact that the FileTree ...
First seen: 2025-06-04 11:45
Last seen: 2025-06-04 13:45