Skip to content

Reload virtual module on demand #6871

@ConsoleTVs

Description

@ConsoleTVs

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions