简体   繁体   English

如何在 webpack 包中包含子进程?

[英]How to include child processes in a webpack bundle?

I have a Node app which uses the fork method to run a background process.我有一个 Node 应用程序,它使用fork方法运行后台进程。 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.问题是从索引运行 webpack 配置不会捆绑后台进程的文件,导致到达 fork 时出错。

All the code uses Babel syntax along with some other goodies.所有代码都使用 Babel 语法以及其他一些好东西。

How do I tell webpack to also bundle the forked files?我如何告诉 webpack 也捆绑分叉的文件?

Thanks in advance.提前致谢。

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 :我自己偶然发现了这个问题,我想我可以提到一个快速解决方法是在你的 webpack 配置中为你的子进程添加一个额外的条目(为你的子进程创建一个单独的包),然后让它使用这个包通过一些解决-rules,或者简单地通过string-replace-loader

Some example webpack config:一些示例 webpack 配置:

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.也许不是最干净的解决方案,但它对我有用,而且我认为这很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM