GoogleChromeLabs / worker-plugin

👩‍🏭 Adds native Web Worker bundling support to Webpack.
https://npm.im/worker-plugin
Apache License 2.0
1.91k stars 79 forks source link

Creating a worker on electron doesn't include Node.js #53

Closed Fransebas closed 4 years ago

Fransebas commented 4 years ago

Hi, amazing library. I'm having problems because when I create a simple worker like this one:

// node.js library
var child_process = require('child_process');

console.log("This is a testing string");

If I create the worker like this:

var worker1 = new Worker('./workerTest.js');

It works, but when I create the worker like this:

var worker2 = new Worker('./workerTest.js', { type: 'module' });

It shows this message Module not found: Error: Can't resolve 'child_process' meaning that the worker2 doesn't include Node.js. I don't know if I should include another option apart from type: 'module' or if this is not supported by this library.

Also, I can't use worker1 method because the real worker is more complex and has a lot of imports.

Fransebas commented 4 years ago

I'm using electron": "^8.0.0 and node version v12.15.0

developit commented 4 years ago

Hiya - this module bundles Web Workers, not Node workers_threads. I think it might be possible to add an option for outputing worker_threads, but I worry that { type: 'module' } is misleading for that use-case, since Node does not actually have any plans to support a "type" option that I'm aware of.

FWIW it seems like in Electron you're getting a Web Worker. For a lot of use-cases, especially when you've already bundled source with Webpack, that might be a reasonable approach.

One thing you could try to get this working without any library changes would be to import the correct worker implementation yourself:

const { Worker } = require('worker_threads');

const worker = new Worker("./worker.js", { type: "module" });
developit commented 4 years ago

I'm actually going to close this issue since it's a duplicate of #10 - we can continue discussion there!

2xAA commented 3 years ago

Hey @developit - I'm also facing this issue. The scenario isn't to use worker_threads, it's to use a Worker with a NodeJS context in Electron: https://www.electronjs.org/docs/tutorial/multithreading#multi-threaded-nodejs

My use-case is to use OffscreenCanvas within a Worker, which is impossible in a worker_therad as OffscreenCanvas is a browser API, not a NodeJS one. However I need access to NodeJS to use a native module (Electron docs say to avoid doing this, but so-far it has been successful).

I'm trying to migrate from worker-loader, but facing the same Module not found: Error: Can't resolve 'child_process' as @Fransebas had back in Feb.

edenhermelin commented 3 years ago

@2xAA did did you manage to workaround this issue? Im facing it too..

2xAA commented 3 years ago

@edenhermelin Unfortunately not. I wrote some pretty awful __dirname hacks around a library we were consuming to make it work with worker-loader.

Worked for a while but has broken with Electron 11 for some reason.

rathboma commented 3 years ago

I'm trying to solve this exact issue also.

@edenhermelin or @2xAA did either of you figure this out?

I also followed the directions in #75, which DOES let me create a worker from the main process, but not from the renderer process.

rathboma commented 3 years ago

@developit can we re-open this issue? It is not the same as creating a node worker.

What this is about:

  1. Creating a webworker
  2. That references node.js APIs
  3. That also references externalized native dependencies.
2xAA commented 2 years ago

@edenhermelin or @2xAA did either of you figure this out?

Nope, sorry. This was my awful workaround for the library I was consuming. Might be quite specific, however.

https://github.com/vcync/modV/commit/81eac0a13017f776af913449fad8c06a7c408821#diff-7b4e11be73603a3ee2c4d37bd49f053930d6056507a005e5639cea7c15686d5fR7-R14