josdejong/workerpool

Is it possible to use workerpool with Webpack 5 for browsers?

im7mortal opened this issue · 4 comments

I couldn't find in the documentation if it can be used with Webpack 5 for Web Workers.

So in the official documentation it's told:

new Worker(new URL('./worker.ts', import.meta.url));

new Worker(
  /* webpackChunkName: "foo-worker" */ new URL('./worker.js', import.meta.url)
);

Webpack 5 will recognize that the TypeScript file inside the URL within the Worker must be compiled separately. It will then substitute the path string with a unique hash name of the separately generated file.

However, since workerpool doesn't use the new Worker(new URL("path")) pattern, Webpack 5 cannot compile it separately and perform the substitution.

Possible workaround for now

I use workerpool for my personal project, so I can experiment with unconventional solutions. I know that inside the worker we can actually get the path of the file with:

self.addEventListener('message', function(event) {
  console.log('Worker script path:', self.location.href);
});

Therefore, I am considering initializing the worker with a function that will retrieve the href and then initialize workerpool with it.

@josdejong Thank you!
It worked!

I am thinking if it would be possible to contribute to docs so it would be easier to find. I will close issue after

Good to hear.

A PR improving the docs would be welcome :)

I'll close this issue since the question is answered. A PR improving the docs is still welcome of course.