VirtualModulesPlugin allows you to create, modify, and delete files in memory, and Rspack treats these virtual files as if they were real files existing in the file system.
This plugin is a Rust implementation of webpack-virtual-modules, deeply integrated with Rspack to provide equivalent functionality with better performance.
When creating a VirtualModulesPlugin instance, you can directly configure virtual modules in the constructor:
modules (optional): An object where keys are file paths and values are file contents.You can dynamically create or modify virtual modules using the writeModule method.
Because the plugin's virtual modules are managed on the Rust side,
the writeModule method becomes available only after the Rust compiler has been initialized.
For example, invoking writeModule within the beforeCompile or compile hooks
will throw the following error: Error: Virtual file store has not been initialized.
Parameters:
filePath: The virtual file path relative to compiler.contextcontents: The content of the virtual fileExample: