-
Notifications
You must be signed in to change notification settings - Fork 215
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
Optionally allow polyfilling the browser global even when used with a bundler #376
Conversation
Codecov Report
@@ Coverage Diff @@
## master #376 +/- ##
=======================================
Coverage 91.03% 91.03%
=======================================
Files 1 1
Lines 145 145
=======================================
Hits 132 132
Misses 13 13 Continue to review full report at Codecov.
|
This looks reasonable and it doesn't seem that it should have impacts on other ways to use the polyfill. It would be nice to also cover it with a test case, I haven't yet looked into what kind of testing strategy we could use (e.g. if we can easily add a test case to one of the test suites we have or if we need a specific testing strategy) but in the meantime I wanted to ask you if you have any thoughts testing this change. |
The integration test code is a nit long so I'll need some time to look into it. I suppose the test would just assert whether |
I only just came across this but it is even more useful in Chrome's MV3, where none of the patterns currently suggested in the README are applicable when looking to polyfill globalThis:
We're currently working around this at 1Password with the following:
That said, this MR looks like a perfect solution! Testing it was a little rough but I built the branch locally, tested it in our code and things seem to be working well 🙌 (P.S: If we end up merging this it'd be nice to explicitly call out MV3 in the README and point people towards it as the easiest path to getting things setup) |
Thanks for the note! I'll keep that in mind for #378 as well. I haven't gotten around to adding tests for this and I'm not sure when I will have time to. I think this doesn't necessarily need it, because if this can't load the main polyfill then nothing else can. |
I won't be adding tests here, it's too much of an ask to build the whole testing harness to include webpack. Feel free to merge or close this |
I think this file solves the issue in an extremely lightweight, non-breaking, trouble-free way:
webextension-polyfill/register
andwebextension-polyfill
separately, it will not be bundled twiceregister
browser
global with a bundlerBenefits of having this file here
I don't have to create the same file in every project I use
it lets you safely polyfill the global without falling into this trap:
One would think that by the 4th line,
window.browser
will be set, but in reality all dependencies are loaded/run before the current file is executed, sowindow.browser
is undefined in"module-that-expects-browser-global"
. This is not common knowledge and it leads to frustration.This instead works flawlessly:
Note about ESM (and lack thereof)
This file is meant to be used exclusively with bundlers, so
require
is fine here (and it actually matches the UMD it's already using)Relation to #351
This offers an alternative way of using the polyfill:
import "webextension-polyfill/register"
ProvidePlugin
Note that
ProvidePlugin
is Webpack-only so its support does not preclude this new usage, which is actually why I'm submitting this PR.To do after approval