Skip to content

[Feature]: Option to inline web workers! #4762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
zardoy opened this issue Mar 11, 2025 · 3 comments
Open

[Feature]: Option to inline web workers! #4762

zardoy opened this issue Mar 11, 2025 · 3 comments

Comments

@zardoy
Copy link

zardoy commented Mar 11, 2025

What problem does this feature solve?

Hello! Opening this issue for #3240 (comment)

Context

https://rsbuild.dev/config/output/inline-scripts#async-chunks says that dynamicImportMode: 'eager' should not generate async chunks anymore, however when importing workers like

const worker = new Worker(new URL('test.worker.ts', import.meta.url))
worker.postMessage('hello')
worker.onmessage = (e) => {
  console.log(e.data)
}

It still produces an HTML file and a single async chunk which is the worker.

Unfortunately I'm not good with understanding webpack options, but I also tried configuring loader like that (which also didn't work unfortunately)

rspack: {
    resolveLoader: {
        alias: {
            'worker-loader': require.resolve('worker-rspack-loader')
        },
    },
    module: {
        rules: [
            {
                test: /\.worker\.{js,ts}$/,
                loader: 'worker-rspack-loader',
                options: { inline: 'no-fallback' }
            }
        ],
    },
}

What does the proposed API look like?

I think web workers should be inlined when dynamicImportMode = eager

Context: FYI in my case half of workers are generated by esbuild (yes I'm using npm-run-all) and I inline them manually, but now I'm in transition to rebuild and AFAIS it still doesn't support it. Thanks in advance!

@9aoy
Copy link
Contributor

9aoy commented Mar 12, 2025

The dynamicImportMode configuration is ineffective for the worker constructor, and the built-in Worker in Rspack does not yet support inline. You can use worker-rspack-loader, referring to this example.

@9aoy
Copy link
Contributor

9aoy commented Mar 12, 2025

By the way, you can subscribe to this issue to get more discussions about inline worker. 👉 webpack/webpack#19098

@zardoy
Copy link
Author

zardoy commented May 3, 2025

The dynamicImportMode configuration is ineffective for the worker constructor, and the built-in Worker in Rspack does not yet support inline. You can use worker-rspack-loader, referring to this example.

@9aoy Hm, I'm so sorry for coming back late. Just tried your example, but unfortunately, its behavior is different from the built-in one, and it doesn't work with TypeScript. I know this question might be off-topic a bit, but is there a way to make your work with the TS worker files? I tried ts-loader which didn't work so I believe some other way should be used instead...

(just in case I'm trying to make a working example of it here: https://github.com/zardoy/rsbuild-workers-repro any guidance would be appreciated :)

Also, it seems that example doesn't work with js modules imported from node_modules either

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants