A Rollup plugin which loads virtual modules from memory instead of the filesystem
npx @tessl/cli install tessl/npm-rollup--plugin-virtual@2.1.0@rollup/plugin-virtual is a Rollup plugin that loads virtual modules from memory instead of the filesystem. It enables developers to define module content programmatically by mapping module IDs to their source code strings, which is particularly useful for dynamically generated code, configuration modules, or modules that don't exist as physical files.
npm install @rollup/plugin-virtual --save-devimport virtual from "@rollup/plugin-virtual";For CommonJS:
const virtual = require("@rollup/plugin-virtual");import virtual from "@rollup/plugin-virtual";
export default {
input: "src/entry.js",
plugins: [
virtual({
// Define virtual modules by mapping IDs to source code
batman: `export default 'na na na na na'`,
"src/robin.js": `export default 'batmannnnn'`
})
]
};Important: Place this plugin before other plugins like @rollup/plugin-node-resolve or @rollup/plugin-commonjs in your plugin array to ensure proper module transformation.
The plugin implements a virtual module system with several key components:
\0virtual: to distinguish them from filesystem modulesModule Resolution Flow:
Creates a Rollup plugin that loads virtual modules from memory.
/**
* A Rollup plugin which loads virtual modules from memory.
* @param modules - Object mapping module IDs to their source code strings
* @returns Virtual plugin instance with resolveId and load hooks
*/
function virtual(modules: RollupVirtualOptions): VirtualPlugin;
interface RollupVirtualOptions {
[id: string]: string;
}Parameters:
modules: Object where keys are module identifiers and values are the module source code as stringsModule ID Patterns:
"batman" - Direct module name lookup"./robin.js" - Resolved relative to importing module"src/foo.js" - Resolved to absolute filesystem pathUsage Examples:
// Bare module example
virtual({
"my-module": `
export const config = { debug: true };
export default function() { return "Hello!"; }
`
})
// Relative path example
virtual({
"./utils.js": `
export function formatDate(date) {
return date.toISOString().split('T')[0];
}
`
})
// Dynamic entry point example
virtual({
entry: `
import batman from 'batcave';
import { formatDate } from './utils.js';
console.log(batman, formatDate(new Date()));
`
})The plugin can be used to specify virtual entry points for bundles:
import virtual from "@rollup/plugin-virtual";
export default {
input: "entry", // Virtual module name
plugins: [
virtual({
entry: `
import batman from 'batcave';
import robin from './robin.js';
console.log(batman, robin);
`,
batcave: `export default 'I am Batman!'`,
"./robin.js": `export default 'I am Robin!'`
})
]
};import { Plugin } from 'rollup';
interface RollupVirtualOptions {
/** Module ID to source code mapping */
[id: string]: string;
}
/**
* Plugin instance returned by virtual() function
* Implements Rollup's Plugin interface with virtual module support
*/
interface VirtualPlugin extends Plugin {
name: 'virtual';
/**
* Resolves virtual module IDs with internal prefix system
* @param id - Module identifier to resolve
* @param importer - Path of the importing module
* @returns Prefixed virtual module ID or null if not found
*/
resolveId(id: string, importer?: string): string | null;
/**
* Loads virtual module content from memory
* @param id - Resolved module ID (with virtual prefix)
* @returns Module source code or null if not a virtual module
*/
load(id: string): string | null;
}The plugin implements custom module resolution through two main hooks:
/**
* Resolves module IDs to virtual modules with prefix system
* @param id - The module ID to resolve (e.g., 'batman', './robin.js')
* @param importer - The path of the importing module
* @returns Virtual module ID with '\0virtual:' prefix, or null if not found
*/
resolveId(id: string, importer?: string): string | null;Resolution Logic:
id exists directly in the modules object, returns \0virtual:${id}\0virtual: prefix from importer if present\0virtual:${resolvedPath} if foundnull to let other plugins handle the module/**
* Loads virtual module content from memory
* @param id - The resolved module ID (must start with '\0virtual:')
* @returns Module source code string or null for non-virtual modules
*/
load(id: string): string | null;Loading Logic:
\0virtual:\0virtual: prefix to get the original IDInternal Data Structures:
\0virtual: used to identify virtual modules