-
-
Notifications
You must be signed in to change notification settings - Fork 86
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
Enable es-module-shims usage in web workers #300
Conversation
…in web workers. However, web workers cannot access objects like document or window. Therefore, I had to do the following changes: - add some simple pieces of code that check whether document/window exists before doing something with document/window object; - add a function that computed the baseUrl. In fact, this function was already defined in es-module-shims version 0.4.7. - I also added a new function setImportMap which can be used to set the import map from outside (obviously). This function is useful when you start a web worker which uses es-module-shims. It’s important to mention that in web workers, I use the es-module-shims.wasm.js output. Why? Because of the dynamicImportScript function; I’m not sure if there is a solution for the CSP version in web workers.
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.
Thanks this looks good!
Could you add a small documentation section for workers, explaining how this can be used? This may well be useful to others if they know about it.
See note about unifying some of the guards.
Perhaps worker CSP may be possible using import scripts of a blob? Not sure, but may well make sense to keep current CSP separation just unsupported for now.
Hello @guybedford and sorry for the delay. I applied the suggested changes and I added a small documentation section; however, I'm not sure if it's in the right place. Thank you. |
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.
Couple more doc and code comments, but I think we're there. Thanks I really like how simple this approach turned out.
I'll aim to merge in the next few days.
I added the |
The purpose of this commit is to enable the usage of
es-module-shims
in module web workers, which at the moment are supported in Chrome based browsers and Safari > 15. As you know, in Firefox the support is still missing.However, web workers cannot access objects like
document
orwindow
. Therefore, I had to do the following changes ines-modules-shims
code:document/window
exists before doing something with thedocument/window
object; it's not elegant, but these checks are necessary;baseUrl
. In fact, this function was already defined ines-module-shims
version0.4.7
.setImportMap
which can be used to set the import map from outside (obviously). This function is useful when you start a web worker which useses-module-shims
.It’s important to mention that in web workers, I use the
es-module-shims.wasm.js
output. Why? Because of thedynamicImportScript
function; I’m not sure if there is a solution for the CSP version in web workers.Also, I want to emphasize that I didn't add new tests because no new functionality was added. The existing tests cover the changes from this pull request. The usage of
es-module-shims
in web worker is a decision that belongs to the client code.Below, I attach a piece of code that starts a web worker which uses
es-module-shims
: