Virtual Modules

PluginVirtualModulesAPI — declare virtual modules that Vite resolves at runtime without touching the file system.

ctx.virtualModules — PluginVirtualModulesAPILink

Declares virtual modules that Vite resolves and loads without touching the file system. Each plugin calls add() from beforeBuild or beforeDev.

ts
ctx.virtualModules.add(
  'virtual:my-plugin/theme.css',
  () => `:root { --primary: #6366f1; }`,
)

ctx.virtualModules.has('virtual:my-plugin/theme.css')
// → boolean

const todos: readonly RegisteredVirtualModule[] = ctx.virtualModules.list()

See alsoLink

  • PluginContext — base context object
  • HMR — file watching and custom dev server events
Last updated on October 18, 2026

Was this page helpful?