unjs / jiti

Runtime Typescript and ESM support for Node.js
MIT License
1.49k stars 52 forks source link

Not updating an import when reloading using chokidar #236

Closed nethriis closed 1 month ago

nethriis commented 1 month ago

Environment


Reproduction

const watcher = chokidar.watch('.', { ignoreInitial: true })

watcher.on('all', () => {
  const _jiti = jiti('.', {
    interopDefault: true,
    cache: false,
    v8cache: false
  })
  const mod = _jiti('./path/to/file')
  console.log(mod)
})

Describe the bug

When I create a new file the log who display mod displays {} and when I modify my file and exports something else like { type: 'opt' }, the log display {} to. The same when I just move a file containing export default { type: 'opt' } the log displays { type: 'opt' } and when I empty the file it displays me { type: 'opt' } too.

Additional context

No response

Logs

No response

pi0 commented 1 month ago

Hi. You should disable requireCache to allow HMR behavior. cache is for disk cache and you can even keep it enabled to leverage faster reloads (jiti will still compare the contents)

pi0 commented 1 month ago

I noticed it is undocumented, if you had time and worked for you, PR welcome to add docs for requireCache

nethriis commented 1 month ago

Thank you, it does indeed work. I'll try to document the requireCache shortly.