-
Notifications
You must be signed in to change notification settings - Fork 424
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
Explicitly detect browser global in strict mode. #125
Conversation
In strict mode, `this` is no longer sufficient to detect the global object. This change adapts the detection of the global object for the case of exporting to the browser global object.
The problem statement might be: "How to get a handle on the global that works in at least a browser window and a web worker (what other JS envs might need to be considered) while in "use strict" mode and when not in a "use strict" mode?" This specific approach will not work in a web worker. The example code in this repo does not have "use strict" at the top of the file, so they are not really assuming running in a "use strict" environment. I will not comment any more on this PR, but wanted to at least give some quick feedback on this particular approach and to point to the difficulty in finding an answer given all the problem constraints. I also still believe it is incorrect to try to load UMD type="text/javascript" scripts in a type="module" environment, and I prefer if it is not encouraged. |
Thanks @jrburke , great point about web workers. There is also |
For reference, see the Seems like |
@justinfagnani Thanks for the pointer to What if |
Experimenting with
As we're adjusting existing UMD templates here rather than introducing a new one specific to strict-mode browser global detection, minimizing breakage is desirable - I think With respect to bundlers, perhaps @sokra might have some input on how well Webpack might treat such changes? Reading through https://github.com/tc39/proposal-global#rationale, paulmillr/es6-shim@2367e09 by @ljharb is interesting, but causes issues with CSP. |
The only guaranteed-correct approach is Short of that, |
Thanks @ljharb .
I like this for its completeness, but let me lay out some potential detection scenarios:
If we had
I agree that simply looking for a recognized global name is not completely satisfying. However, if you don't trust the name used to detect the global object, it seems only marginally more satisfying to do this extra check because an object masquerading as |
Don't forget about iframes, web workers, and also other contexts like One way you can know for sure if it's the global is something like |
Haha, yes, ExtendScript FTW! But now I'm curious: does ExtendScript support strict mode?
I think these are covered by |
Ah yes, good call; I missed that bullet point. Basically with the appropriate |
Ah yes, but if And I'm not very familiar with |
Indeed, if I'm not sure what browsers have strict mode but also don't have
|
This seems like a worthy bet to take you up on. I will do some research and try to discover the answer.
Considering the conflict this creates with CSP, I'm afraid this means that |
I have tested a selection of browsers for combinations of strict mode support and the presence of a global While this selection is not comprehensive, I have some preliminary conclusions:
Some potentially interesting areas for further exploration include:
Here are the results of my testing so far:
|
As an addendum it should be noted that |
I ran a subset of the browsers above through a test to see if their worker contexts had a global
|
So the problem is ES6 modules do not this defined, but they do have self defined? |
@EvanCarroll the problem is that
|
After carefully reviewing the due diligence @cdata has done here in the context of If folks do have concerns but haven't had a chance to air them, feel free to ping this PR or open a new issue for us to discuss. I know everyone is time-constrained and appreciate the input provided on this thread so far. Cheers folks. |
Still global / root is undefined in many scenarios. Not sure if we have to add the window / root fix here or the other devs have to change their implementations. |
In strict mode,
this
is no longer sufficient to detect theglobal object. This change adapts the detection of the global object
for the case of exporting to the browser global object.
Fixes #124
Note: this is but one potential strategy for correct global detection, and it explicitly supports the browser context. There are other (potentially more complicated) detections that will support non-browser contexts. Whether or not it is critical to support such contexts is a question I leave open for discussion.