What is @rollup/plugin-virtual?
@rollup/plugin-virtual is a Rollup plugin that allows you to create virtual modules. These are modules that do not exist as files on disk but are instead created in memory. This can be useful for injecting code, creating mock modules for testing, or dynamically generating content during the build process.
What are @rollup/plugin-virtual's main functionalities?
Creating a virtual module
This feature allows you to create a virtual module named 'my-virtual-module' with the content 'export default "This is virtual!"'. This module can then be imported and used in your Rollup bundle.
const virtual = require('@rollup/plugin-virtual');
module.exports = {
input: 'src/main.js',
plugins: [
virtual({
'my-virtual-module': 'export default "This is virtual!"'
})
],
output: {
file: 'bundle.js',
format: 'es'
}
};
Mocking modules for testing
This feature allows you to mock the 'fs' module with a virtual module that provides a custom implementation of the 'readFile' function. This can be useful for testing purposes.
const virtual = require('@rollup/plugin-virtual');
module.exports = {
input: 'src/main.js',
plugins: [
virtual({
'fs': 'export default { readFile: () => "mocked content" }'
})
],
output: {
file: 'bundle.js',
format: 'es'
}
};
Dynamically generating content
This feature allows you to create a virtual module with content that is dynamically generated at build time. In this example, the module exports a string with the current date and time.
const virtual = require('@rollup/plugin-virtual');
module.exports = {
input: 'src/main.js',
plugins: [
virtual({
'dynamic-module': `export default "Generated at ${new Date().toISOString()}"`
})
],
output: {
file: 'bundle.js',
format: 'es'
}
};
Other packages similar to @rollup/plugin-virtual
rollup-plugin-inject
rollup-plugin-inject allows you to inject variables or modules into your bundle. While it does not create virtual modules, it can be used to achieve similar results by injecting code into existing modules.
rollup-plugin-replace
rollup-plugin-replace allows you to replace strings in your code during the build process. It can be used to inject dynamic content or mock modules, similar to @rollup/plugin-virtual, but it operates by replacing existing code rather than creating new virtual modules.
rollup-plugin-alias
rollup-plugin-alias allows you to create aliases for module paths. While it does not create virtual modules, it can be used to redirect module imports to different files or modules, which can be useful for testing or injecting custom implementations.
@rollup/plugin-virtual
🍣 A Rollup plugin which loads virtual modules from memory.
Requirements
This plugin requires an LTS Node version (v8.0.0+) and Rollup v1.20.0+.
Install
Using npm:
npm install @rollup/plugin-virtual --save-dev
Usage
Note. Use this plugin before any others such as node-resolve or commonjs, so they do not alter the output.
Suppose an entry file containing the snippet below exists at src/entry.js
, and attempts to load batman
and src/robin.js
from memory:
import batman from 'batman';
import robin from './robin.js';
console.log(batman, robin);
Create a rollup.config.js
configuration file and import the plugin:
import virtual from '@rollup/plugin-virtual';
export default {
entry: 'src/entry.js',
plugins: [
virtual({
batman: `export default 'na na na na na'`,
'src/robin.js': `export default 'batmannnnn'`
})
]
};
Then call rollup
either via the CLI or the API.
Options
This plugin has no formal options. The lone parameter for this plugin is an Object
containing properties that correspond to a String
containing the virtual module's code.
License
MIT