Skip to content
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

feat: allow worker options via variable #18396

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mame
Copy link

@mame mame commented Oct 18, 2024

Description

This PR allows a variable reference in worker options of new Worker() as follows:

const myWorkerOptions = { type: 'module' }
...
new Worker(new URL(..., import.meta.url), myWorkerOptions)

Background: Emscripten 3.1.58 (or later) generates such a code. I thought about fixing it on the emscripten side, but it would be better if the vite could support this style. See also emscripten-core/emscripten#22394

Copy link

stackblitz bot commented Oct 18, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@mame mame force-pushed the allow-worker-options-via-variable branch from a9c51c5 to c82a7d1 Compare October 18, 2024 13:23
@sapphi-red sapphi-red added p2-nice-to-have Not breaking anything but nice to have (priority) feat: web workers labels Oct 21, 2024
@sapphi-red
Copy link
Member

Thanks for the PR.

Even if we add support for this case, we'll still have many unsupported cases.

const type = 'module'
new Worker(new URL(..., import.meta.url), { type })

const opts = { type: 'module' }
new Worker(new URL(..., import.meta.url), { ...opts, name: 'foo' })

import { opts } from './worker.utils'
new Worker(new URL(..., import.meta.url), opts)

If we keep adding support for these cases, both the document and the implementation will be bloated.
So I prefer to keep it in the current state.

But let's see what others think.

@patak-dev
Copy link
Member

I also vote to keep things as is here. It is unfortunate that we have this issue with emscripten, but if it is possible to fix this there, that should be more clear and maintainable for the future. As Sapphi explained, once we open this door, we should probably support a lot more cases or it will be quite confusing.

@patak-dev patak-dev added p2-to-be-discussed Enhancement under consideration (priority) and removed p2-nice-to-have Not breaking anything but nice to have (priority) labels Oct 21, 2024
@mame
Copy link
Author

mame commented Oct 21, 2024

Thank you for your comments. If such a decision is made, I will comply.

I would like you to look at the corresponding generation code of Emscripten:

https://github.com/emscripten-core/emscripten/blob/10cb9d46cdd17e7a96de68137c9649d9a630fbc7/src/library_pthread.js#L396-L462

As you can see, workerOptions is used in 4 places. Inlining the definition in each place would be obviously never “more clear and maintainable”.

I see no need to extend this support unlimitedly. I thought that the style such as var workerOptions could be frequent in the generated code and is worth supporting in Vite. Just IMO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: web workers p2-to-be-discussed Enhancement under consideration (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants