(ab?)using Node module hooks to speed up development
immaculata.dev
(ab?)using Node module hooks to speed up development
1–10 of 17 posts
Re: (ab?)using Node module hooks to speed up development
#2Re: (ab?)using Node module hooks to speed up development
#3(ab)?using ?
Re: (ab?)using Node module hooks to speed up development
#4For 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.
Re: (ab?)using Node module hooks to speed up development
#5Re: (ab?)using Node module hooks to speed up development
#6I do not follow, can anyone help with more code/config examples of how to leverage this?
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
#7Re: (ab?)using Node module hooks to speed up development
#8Am I missing something, or is the content here too minimal? 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.
Re: (ab?)using Node module hooks to speed up development
#9I do not follow, can anyone help with more code/config examples of how to leverage this?
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.
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-executed