question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reload virtual module on demand

See original GitHub issue

Clear and concise description of the problem

As a developer I want to be able to reload a specific virtual module on demand via vite’s Server API.

The idea is that the contents of that virtual module change upon an external event / conditon. I want to be able to manually invalidate such module and reload it (and all modules who depend on it) on demand without restarting the whole server.

Suggested solution

const virtualModuleId = 'virtual:example'
const resolvedVirtualModuleId = '\0' + virtualModuleId

const custom = {
  name: 'example',
  enforce: 'pre',
  resolveId: (id) => {
    if (id === virtualModuleId) {
      return resolvedVirtualModuleId
    }
  },
  load: (id) => {
    if (id === resolvedVirtualModuleId) {
      return `...`
    }
  },
}

const server = await createServer({
  plugins: [custom],
  ...
})

// For demo purposes, this is an external condition.
setInterval(() => {
    // This is what I would expect somehow.
    server.reloadModule(resolvedVirtualModuleId)
}, 5000)

await server.listen()

Alternative

No response

Additional context

No response

Validations

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
bluwycommented, Oct 3, 2022

Re-opening this. We could expose a server.invalidateModule api to help invalidate a virtual module easily. The HMR update part has been fixed in #10324

2reactions
bluwycommented, Feb 13, 2022

I see 👍 Was just tossing ideas. Not familiar with the module graph API myself. I’d expect invalidating would trigger HMR as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I reload a module if I imported (possibly all) objects ...
I found an easy solution: Import from the module and the module itself. Then use reload() as usual, but also redo the import...
Read more >
RELOAD Modules and RESTART Components - TechDocs
Reload OPS/MVS modules and restart OPS/MVS components.
Read more >
Restart the Processes on a FireSIGHT System and a ... - Cisco
This section describes how to restart the processes that run on a managed device. FirePOWER Appliance, ASA FirePOWER Module, and NGIPS Virtual Device....
Read more >
Create, destroy, start, stop, and reboot a ProfitBricks virtual ...
Parameter Choices/Defaults Comments bus. ‑ Choices: IDE. VIRTIO ← The bus type for the volume. count. ‑ Default: 1 The number of virtual machines to...
Read more >
Restarting the Management agents in ESXi (1003490)
Virtual machine creation may fail because agent is unable to retrieve ... Instead restart independent services using the /etc/init.d/module ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found