Re:(ab?)using Node module hooks to speed up development
by noob_07 on Wednesday May 07, 2025 @04:25AM
(#43913360)
I do not follow, can anyone help with more code/config examples of how to leverage this?
[ Parent
| on HN ]
Re:(ab?)using Node module hooks to speed up development
by shakna on Wednesday May 07, 2025 @04:48AM
(#43913482)
One example from the site:
import module from 'node:module'
const tree = new FileTree('site', import.meta.url)
module.registerHooks(hooks.useTree(tree))
import('site/myfile.js')
Here, site/myfile.js doesn't exist. It gets created as a reference by the FileTree library. Node thinks it is importing it. The import is also automatically reloaded, if the backend changes it. Caches are invalidated and objects replaced.
Re:(ab?)using Node module hooks to speed up development
by sbjs on Wednesday May 07, 2025 @07:41AM
(#43914421)
Oh no, I must have mis-explained it.
The file `site/myfile.js` does exist. All FileTree does is recursively load all files in a dir into memory.
The `useTree` module hook does two things:
* Pulls the file from memory when loading it instead of from disk
* Adds a cache busting query string when resolving it for invalidation
Combined with tree.watch(), this essentially allows you to add a very lightweight but extremely accurate hot module replacement system into Node.js
1. const tree = new FileTree('src', import.meta.url)
2. tree.watch().on('filesUpdated', () => import(tree.root + '/myfile.js'))
3. registerHooks(useTree(tree))
4. Save src/myfile.js and see it re-executed