-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Conversation
|
a9c51c5
to
c82a7d1
Compare
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. But let's see what others think. |
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. |
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: As you can see, I see no need to extend this support unlimitedly. I thought that the style such as |
Quickly noting that in the Emscripten issue thread it looks like the maintainer is eager to solve it on the Emscripten side 👀 Also if any Vite users stumble upon this PR, I also have a simple Vite Plugin that will patch the issue with Emscripten files (Plugin Gist Link) |
ヤッホー @sapphi-red @hi-ogawa 👋 , I wanted to propose relaxing worker options to be only partially static? 🤔 The main reasoning I understand for the restriction is determining whether the worker type is A valid use case for dynamic worker options is used in the Emscripten project. To help with debugging, custom names can be given to a worker (Code reference). e.g. worker = new Worker(new URL('target.js', import.meta.url), {
'type': 'module',
'name': 'em-pthread-' + PThread.nextWorkerID,
}) I could see other projects also wanting this functionality. To support this while also trying to balance code bloat and edge cases I wanted to propose only allowing inline worker options. The restrictions would be: // Worker options must be defined inline as an object
✅ new Worker(new URL(..., import.meta.url), { name: 'worker-' + someId });
❌ new Worker(new URL(..., import.meta.url), opts);
// The type field must be statically defined if used
✅ new Worker(new URL(..., import.meta.url), { name: 'worker-' + someId, type: 'module' });
✅ new Worker(new URL(..., import.meta.url), { name, type: 'module' });
❌ new Worker(new URL(..., import.meta.url), { name: 'worker-' + someId, type });
// All field names must be statically defined, no spread operator
✅ new Worker(new URL(..., import.meta.url), { name, credentials: 'include', type: 'module' });
❌ new Worker(new URL(..., import.meta.url), { type: 'module', ...opts }); A rough idea of the business logic
I totally understand if this invites too much complexity in regards to documentation and potential edge cases 😅 |
@jamsinclair vite/packages/vite/src/node/plugins/workerImportMetaUrl.ts Lines 35 to 41 in 3400a5e
|
Thanks @sapphi-red. That sounds like a good approach and falling back to AST would make that check quite easy. |
... to workaround a compatibility problem between Vite and Emscripten. emscripten-core/emscripten#22394 vitejs/vite#18396
... to workaround a compatibility problem between Vite and Emscripten. emscripten-core/emscripten#22394 vitejs/vite#18396
Closing as #19010 is merged and the remaining parts would probably be resolved on the emscripten side. |
Great, I will give it a try later. Thank you for your great work! |
Description
This PR allows a variable reference in worker options of
new Worker()
as follows: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