Node API – supporting of reading entry from memory

Do you want to request a feature or report a bug?
Feature request

What is the current behavior?
In webpack config entry accepts only a disk path to an entry.

What is the expected behavior?
It would be nice, If entry also accepts a file from memory stream.

I’m creating a webpack dynamic bundler as a service using webpack node api. I created the following very simple node code that allows me to return a bundle for a given entry from the disk.

var http = require('http');

 http.createServer(function(req, res) {
     res.writeHead(200, { 'Content-Type': 'text/plain' });
     const webpack = require('webpack');
     const MemoryFS = require("memory-fs");
	 
     const compiler = webpack({
         entry: './app.js',
         output: {
			 filename: 'bundle.js',
             libraryTarget: 'amd'
         }
     });
     
     compiler.outputFileSystem = new MemoryFS();

     compiler.run((err, stats) => {
         if (err || stats.hasErrors()) {
             // Handle errors here
         }

         const content = stats.compilation.assets['bundle.js'].source()
		 
         // done processing
         res.end(content);
         // ...
     });

 }).listen(process.env.PORT);

But the entry is from the disk. I want to read from memory instead like

     var MemoryFileSystem = require("memory-fs");
     var fs = new MemoryFileSystem();
     const compiler = webpack({
                     entry: fs.readFileSync("/a/src/entry.js"),
                     output: {
			 filename: 'bundle.js',
                         libraryTarget: 'umd'
                     }
       });

If this is a feature request, what is motivation or use case for changing the behavior?

I’ve a requirement to dynamically bundle a script based on dynamically generated entry file at run time.

This feature allows me to generate entry file at run time, put in memory and pass to the webpack — Instead of generating an entry, saving to disk and pass to webpack.

Thanks,
Bruk

Author: Fantashit

2 thoughts on “Node API – supporting of reading entry from memory

  1. Entries accept loaders. I think you could write something along entry: '!disk-loader?file=...' that would do what you want. You can read the file in the loader as you like. It can be async op too if you use this.async() to capture a callback.

  2. BTW, do you see any issue using webpack like this in production?

    Nope. My understanding is that this is just one little known way to use loaders. It happens to work here as you needed something dynamic.

Comments are closed.