rspack icon indicating copy to clipboard operation
rspack copied to clipboard

worker-loader support

Open hardfist opened this issue 2 years ago • 5 comments

some internal projects depends on worker-loader

hardfist avatar Dec 01 '22 12:12 hardfist

worker-loader is required for basically any kind of Web Workers support so I think this should be high on the priority list.

thecodrr avatar Mar 13 '23 08:03 thecodrr

webpack says not to use worker-loader any more https://webpack.js.org/guides/web-workers/#syntax

We use the method describe in those docs but that is also broken with rspack.

beaucollins avatar Mar 13 '23 21:03 beaucollins

Rspack currently not support web-worker or worker-loader, but we will work on supporting them later.

ahabhgk avatar Mar 14 '23 00:03 ahabhgk

@beaucollins that method doesn't work on Firefox because there's no ESM support for Web Workers yet. Bundling is the only reliable way to make web workers work across browsers.

thecodrr avatar Mar 14 '23 05:03 thecodrr

@thecodrr webpack replaces the import.meta.url with a static string. Firefox handles it just fine. esm isn't involved in the bundled code.

beaucollins avatar Mar 14 '23 13:03 beaucollins

Looks like #2340 will solve this for us. I'll report back.

beaucollins avatar Mar 23 '23 23:03 beaucollins

Look like https://github.com/web-infra-dev/rspack/pull/2340 can't resolve the web worker as the same with webpack 5: Uncaught SyntaxError: Cannot use import statement outside a module there always exists some extra file need to be imported except set the worker.js to type: module?

towavephone avatar Apr 06 '23 13:04 towavephone

I can confirm that we can successfully use our worker code using rspack and the new URL('Module.ts', import.meta.url) syntax.

And yes, @thecodrr this also works in Firefox since the bundle output does not actually use ESM.

beaucollins avatar Apr 17 '23 22:04 beaucollins

I can confirm that we can successfully use our worker code using rspack and the new URL('Module.ts', import.meta.url) syntax.

I'm not sure what I'm doing differently.

With the following code using rspack 0.1.7, I get the TS file simply copied, untransformed (but renamed), to the output directory:

const dbWorker = new Worker(
  /* webpackChunkName: 'db-worker' */
  new URL('../db-worker/db-worker.ts', import.meta.url)
);

birtles avatar Apr 18 '23 01:04 birtles

@birtles it seems I spoke too soon. The builds work but the Worker based feature is not.

beaucollins avatar Apr 18 '23 02:04 beaucollins

I've the same issue as @birtles has, I would expect that a new entry will be added (with the path of the worker file path) dynamically with a worker target, so if the worker file path has imports it will be bundled together as it works in Webpack.

felixmosh avatar Apr 18 '23 08:04 felixmosh

a gentle follow-up, do we have an approx ETA for this fix?

pushkargupta9807 avatar May 22 '23 18:05 pushkargupta9807

Do we have a patch/ experimental branch for this fix I can try ?

pushkargupta9807 avatar May 31 '23 19:05 pushkargupta9807

Do we have a patch/ experimental branch for this fix I can try ?

it is not supported now, we will support it soon

hardfist avatar Jun 02 '23 12:06 hardfist

so if there is a milestone? this is a important feature for many complex system

ssddi456 avatar Jun 26 '23 08:06 ssddi456

I'm working on it, will support it in the next one or two weeks

ahabhgk avatar Jun 26 '23 12:06 ahabhgk

@ahabhgk I've tested v0.2.5 with the worker, looks like it splits the worker script into a separate file, but it is not bundling it's imports inside this bundle.

// some code
highlightWorker = new Worker(
        new URL('./worker.ts', import.meta.url)
);
// worker.ts
import config from './config'; // this file is not bundled inside the chunk that was created for worker.ts

console.log(config)

felixmosh avatar Jul 04 '23 10:07 felixmosh

@felixmosh Is there any runtime error? Could you provide a reproduction so I can dig into this

ahabhgk avatar Jul 04 '23 13:07 ahabhgk

@ahabhgk this is weird, with the rspack starter, it works properly.

On my project, it throws, image

You can try this branch: https://github.com/felixmosh/bull-board/tree/ui-with-rspack

yarn install
yarn start:dev

make sure you have radis running on a default port (via Docker or other)

felixmosh avatar Jul 05 '23 11:07 felixmosh

Love seeing worker support landing.

zackarychapple avatar Jul 05 '23 11:07 zackarychapple

@felixmosh Could you try 0.2.5-canary-7886e38-20230706011015, I believe it have been fixed by #3703

ahabhgk avatar Jul 06 '23 03:07 ahabhgk

We have completed the basic support for web worker in v0.2.6, enough to cover most common cases, you can checkout our document for more detail

But there are some uncommon cases we still not able to cover for now:

  • #3686: not support create worker by function call syntax navigator.serviceWorker.register()
  • #3656: not support customize worker syntax by module.parser.javascript.worker
  • #3680: not support webpackEntryOptions magic comments /* webpackEntryOptions: { filename: "workers/[name].js" } */

Since these issue are blocked by the implementation of compile time evaluation and parser hooks in Rspack, so I decide to close this one and tracking these in their own issue, if you have any problem when using worker with Rspack, feel free to open a new issue and ping me.

ahabhgk avatar Jul 11 '23 09:07 ahabhgk