Skip to content

[Feature]: builtin RspackVirtualModule plugin #10856

Open
@stormslowly

Description

@stormslowly

Note

This is a bounty issue, $100 for implementing this feature

What problem does this feature solve?

We have supported InputFilesystem and it works well with Webpack-Virtual-Modules.
But while using the inputFileSysmte, We have to tell Rspack to access which FileSystem by configing experiments.useInputFileSystem by an array of regex.

It works fine when the mount of virtual files is small. If there are lots of virtual files, Rspack need to access these virtual files across NAPI.

So to keep the Rspack high performance and better virtual Module use experiences, We want to an builtin Rspack Virtual Modules plugin.

It will be used as below:

// rspack.config.mjs
import { rspack } from "@rspack/core"
const virtualModules = new rspack.VirtualModulesPlugin( 
  { 
    'module-foo.js': 'module.exports = { foo: "foo" };',
    'module-bar.js': 'module.exports = { bar: "bar" };'
  });

export default { 
   plugins: [ virtualModules ]
}

let a = 0;
setInterval(()=>{
	virtualModules.writeModule('module-foo.js', `module.exports = { foo: "foo" };`
}, 5000)

Requirements and TestCases

InputFilesystem merged in Virtual Files

With above config, Rspack can resolve ./module-foo and load ./module-foo.js successfully.

Rspack virtual modules works with rspack watcher

With above config and in Rspack dev mode, every 5s's update module-foo.js will notify watcher and trigger HMR.

Performance

When bundling with 1000 virtual Modules. VS bundling 1000 native FS modules, Rspack exec performance should not degraded.

Optional: Access virtual Files from Node.js

import { rspack } from "@rspack/core"; 
const files = {
  "/index.tsx": `import React from 'react'; import ReactDOM from 'react-dom'; ReactDOM.render(<div></div> , document.getElementById("root"));`,
}; 
const virtualModules = new rspack.VirtualModulesPlugin(files);


const compiler = rspack({
  mode: "production",
  plugins: [ virtualmodules ]
}); 

// in some hooks after rspack run 
compiler.inputFileSystem.read("index.tsx", (e, content)=> console.log(content))



### What does the proposed API of configuration look like?

according to:  https://www.npmjs.com/package/webpack-virtual-modules

Metadata

Metadata

Labels

featNew feature or requestteamThe issue/pr is created by the member of Rspack.💰 issue-bountyget reward

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions