How to include child processes in a webpack bundle?

Viewed 1136

I have a Node app which uses the fork method to run a background process. The problem is that running the web pack configuration from the index doesn't bundle the background process's files resulting in an error when reaching the fork.

All the code uses Babel syntax along with some other goodies.

How do I tell webpack to also bundle the forked files?

Thanks in advance.

1 Answers

Just stumbled into this problem myself, and thought I could mention that a quick fix is to add an additional entry in your webpack config for your child process (creates a separate bundle for your child process) and then make it use this bundle by some resolve-rules, or simply by string-replace-loader:

Some example webpack config:

module.exports = {
   // ...
   target: 'node',
   entry: {
     server: './server/server.js',
     daemon: './daemon.js'
   },
   output: {
     path: path.resolve(__dirname, '../serverdist'),
     filename: '[name].bundle.js'
   },
   module: {
     rules: [
       // ... your other existing rules for building the server code
       {
         test: /placeWhereYouAreCallingFork.js$/,
         loader: 'string-replace-loader',
         options: {
           search: 'daemon.js',
           replace: 'serverdist/daemon.bundle.js'
         }
       }
     ]
   }
   // Other webpack stuff...
};

This depends on the replace loader:

npm install --save-dev string-replace-loader

Maybe not the cleanest solution but it it worked for me, and I thougth it was quite simple.

Related