Live data from Hacker News

(ab?)using Node module hooks to speed up development

immaculata.dev

1–10 of 17 posts

Re: (ab?)using Node module hooks to speed up development

#6
post #5

I 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.

Re: (ab?)using Node module hooks to speed up development

#8

Am 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.

Just added some code samples, thanks for the suggestion.

Re: (ab?)using Node module hooks to speed up development

#9
post #6
post #5

I 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.

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

    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
Post reply on HN