For this to be genuinely useful, I’d expect at least a few code examples—and ideally a link to a working repo to show it in action.
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.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
const tree = new FileTree('src', import.meta.url)
registerHooks(useTree(tree))
tree.watch().on('filesUpdated', () => import(tree.root + '/myfile.js'))
import(tree.root + '/myfile.js')
Now save src/myfile.js and see it re-executedConsider Vite's node-side HMR implementation. It creates its own module system on top of Node's native module system, using `node:vm`. So its modules are really second class citizens that have to be glued to the native module system.
This library used to do that, but moved to using Node's native module hooks, so that there's nothing magical going on, and you can still use the `import` expression to import your HMR modules, they just auto-update when saving.
feisuzhu•7mo ago
kaeruct•7mo ago
carlosneves•7mo ago
/(ab?)using/ matches:
- ausing
- abusing
while /(ab)?using/ matches:
- using
- abusing
sbjs•7mo ago