Virtual Modules
PluginVirtualModulesAPI — declare virtual modules that Vite resolves at runtime without touching the file system.
ctx.virtualModules — PluginVirtualModulesAPI
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 also
- PluginContext — base context object
- HMR — file watching and custom dev server events
Last updated on October 18, 2026