-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
JS Cache loader errors due to trying to instantiate a script by accessing document
inside a web worker
#9051
Comments
document
inside a workerdocument
inside a web worker
For Parcel to work correctly, you have to do this (syntactically, no variables or function calls inbetween): new Worker(
new URL("./search/worker-workarounds/search-worker-entry.js", import.meta.url),
{ type: "module" }
) If not, Parcel doesn't know if some file is running in a worker or not and cannot select the correct helper (as you've noticed) parcel/packages/runtimes/js/src/JSRuntime.js Lines 32 to 61 in 5c5dc30
Webpack behaves the same, see the yellow warning here: https://webpack.js.org/guides/web-workers/ If I do this in |
There's still this question: #8924 (comment)
One solution here might be to have a version of the code I posted above and then whatever fallback you need for Node/non-bundler browser use, but select between these versions with some if-condition/exports/package.json alias/browser field |
Thanks for the replies!
As mentioned above, we unfortunately cannot do this if we want our non-fallback code paths to be compatible across environments. 😕 I'll see if I can add it without too many hacks, but even after removing support for classic workers we still have to maintain a lot of code paths for cross-compatible worker support and I want to avoid maintaining an additional set of workarounds to the existing workarounds (which are already expensive to maintain, since they trigger bugs like the one documented here).
For a variety of reasons, we only publish a single ESM version of our library, so that is not an option for us either. 😞
I still think the answer to that one is crystal-clear — there is one way to implement it that preserves the semantics of import resolution, and solves a critical problem that library authors like me cannot work around in bundlers that do not support it. And since it's new as of a year ago, different syntax from I believe I've done all the advocacy on that issue that I can, let me know if there's something more I can do to help. 🤓 |
This works around parcel-bundler/parcel#9051, which makes it impossible for us to recover from instantiation errors using the more portable `new URL(…, import.meta.url)` strategy.
Okay, I've implemented this workaround and I think we can live with it for now. But I'd still love to see this particular issue addressed (or #8924 implemented), so that we have fewer constraints on what workarounds we can use in what order. |
This works around parcel-bundler/parcel#9051, which makes it impossible for us to recover from instantiation errors using the more portable `new URL(…, import.meta.url)` strategy.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. |
🐛 bug report
Given the following repro:
… Parcel hits (a compiled instance of) the following line while trying to load a web worker:
parcel/packages/runtimes/js/src/helpers/browser/js-loader.js
Line 6 in 5c5dc30
🤔 Expected Behavior
Worker instantiation works.
😯 Current Behavior
The given line is hit. It attempts to access
document
in a worker and errors.💁 Possible Solution
I'm not sure. I suspect it's because Parcel's web worker instantiation support does not accommodate when the URL is calculated using a cross-origin trampoline (which is necessary for our code) and/or indirect use of the worker constructor (which is needed to publish library code that works in
node
as well as the browser).Ideally, Parcel would love to see Parcel implement
import.meta.resolve(…)
: #8924 , as:node
/bun
/deno
also do, albeit with some caveats.new URL(…, import.meta.url).href
.🔦 Context
With the release of Firefox 114 around the corner (June 6), I'm trying to update our library to rely purely on module workers.
I've tried to implement workarounds based on various worker-related Parcel issues I've filed over the years, but I can't figure out what specifically about our current code is not working.
💻 Code Sample
See above.
🌍 Your Environment
The text was updated successfully, but these errors were encountered: