-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
fix(worker): handle self reference url worker in dependency for build #17846
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export function startWorker(handler) { | ||
const worker = new Worker(new URL('./worker.js', import.meta.url), { | ||
type: 'module', | ||
}) | ||
worker.postMessage('main') | ||
worker.addEventListener('message', (e) => { | ||
handler(e) | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "@vitejs/test-dep-self-reference-url-worker", | ||
"private": true, | ||
"version": "1.0.0", | ||
"type": "module", | ||
"main": "index.js" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// copy of playground/worker/self-reference-url-worker.js | ||
self.addEventListener('message', (e) => { | ||
if (e.data === 'main') { | ||
const selfWorker = new Worker(new URL('./worker.js', import.meta.url), { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In emscripten we use the self-references a lot (to start pthread workers). Would it be possible to allow the shorter form of this which is the even more explicit self reference: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that should work as Vite would skip analysis and don't have to rewrite it.
I confirmed it on my repro, but if you see something different, please submit a separate issue to track it. |
||
type: 'module', | ||
}) | ||
selfWorker.postMessage('nested') | ||
selfWorker.addEventListener('message', (e) => { | ||
self.postMessage(e.data) | ||
}) | ||
} | ||
|
||
self.postMessage(`pong: ${e.data}`) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to also allow
./index.js
here? I.e. a self reference from the main script? This is how emscripten starts its pthread workers. All workers (including the main page) use the same script.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that should work too though one caveat is that Vite needs to bundle
index.js
twice for main script and as worker script. So, it's likely that some js might got duplicated but IIRC static wasm assets can be de-duped.