4

Using Web Workers for the browser is handled by the official worker-loader Webpack module.

Unfortunately worker-loader does not handle Node.js Worker threads.

I'm using Webpack to bundle an AWS Lambda function running on Node.js. And I want to use Node.js Worker threads that requires having dedicated files for each worker implementation.

So what is the best way to handle Node.js Worker threads within a Webpack bundle?

Note: This is a question about handling Node.js workers server side (not in browser).

Edit: Webpack handles Worker threads but need to switch from CommonJS to ESM https://github.com/webpack/webpack/issues/15961

Yves M.
  • 29,855
  • 23
  • 108
  • 144
  • 1
    Why use webPack at all for server-side code? What problem are you trying to solve by using webPack? – jfriend00 Dec 08 '20 at 15:18
  • @jfriend00 did you remove my comments? – Yves M. Dec 09 '20 at 09:15
  • Again @friend00 Webpack is a very common practice to bundle Node.js applications for Electron, serverless fonctions such AWS Lambda, Azure functions. Reduce number of files to deploy. Reduce cold start. Do a Google search please https://www.google.com/search?q=aws+lambda+webpack that's not the point of the question common – Yves M. Dec 09 '20 at 09:17
  • I don't have any ability to remove comments other than my own. – jfriend00 Dec 09 '20 at 16:57
  • I asked because you're using a tool that is causing you a problem. If you stopped using that tool, your app could still work and you would not have that problem. So, it's a natural question to try to figure out if the benefit of the tool is greater than the pain it is causing you. I don't personally find that dealing with all these types of issues (as some tool reorganizes all my code) is worth the benefit for my server-side code. But, others have different opinions. – jfriend00 Dec 09 '20 at 17:07
  • Given the enormity of the webpack configuration options and its programmability, there is probably a way to configure it to not bundle certain files so they can still be separate files that will work with WorkerThreads, but the details of how to do are beyond my understanding of webpack so maybe someone else will come along and offer some ideas. – jfriend00 Dec 09 '20 at 17:07
  • Webpack flatten all the dependencies in a single file, including AWS dependencies. In Node.js that produces A LOT of files and that slower cold start. Tree shaking is also cool to reduce total size. Even AWS recommend Webpack (or Parcel or whatever). That's why deno natively includes a bundler https://deno.land/manual/tools/bundler maybe Node.js will also do that one day – Yves M. Dec 09 '20 at 23:14

0 Answers0