vinsonchuong / hot-esm

MIT License
12 stars 3 forks source link

help #78

Open amiruldev20 opened 1 year ago

amiruldev20 commented 1 year ago

any more detailed explanation, how to use this for reload detection of all files except those in node_modules?

vinsonchuong commented 1 year ago

Hi! Thanks for taking a look at my tool! I'm happy to help you get setup.

hot-esm is built to power Node.js apps that use only ES Modules. So, the package.json should ideally have "type": "module". It can't work with CommonJS, and it can't deal with code run in the browser.

It assumes that the Node.js app can be run using the node CLI command, like so:

node --loader hot-esm ./app.js

The hot-reloading works by "clearing cached modules". So, after you change a code file, subsequent imports for that file will return the new code. For example, if app.js contains:

import { setTimeout } from 'node:timers/promises'
await import('./code.js')
await setTimeout(10_000)
await import('./code.js')

If you update code.js during the setTimeout, the second import will return the new code.

If app.js instead contains:

import * as code from './code.js'

console.log(code)

Then, because there's nothing that re-imports the code, the hot-reloading "won't work".

Once everything is set up, hot-esm should be ignoring node_modules by default.

Let me know if that makes sense. I'm happy to answer further questions. Also, if you have examples of the code you're trying to use hot-esm with, I'm happy to take a look.