-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Bug: global this is optimized to void 0, which will lead to runtime errors. #1225
Comments
I do not consider behavior you described to be a bug. The top-level value of I believe this is specifically because ECMAScript modules are always evaluated in strict mode (section 10.2.1 of the ES6 specification):
and strict mode functions do not coerce
The new let globalThisRef = new Function('return this')() Edit: Also
Since esbuild emulates both CommonJS module semantics and ECMAScript module semantics, |
I was actually also reading up on this, it seems that most of the embedded runtimes (jerryscript and others) are kind of spec-violating in this matter; as they have support for import/export but don't have support for globalThis (yet) and seem to execute most things in the same scope. PS: Thanks much for the detailed reply. I was assuming this fallback behaviour would be the same across all Browsers, as back then when I was verifying it, I was testing it in an outdated Safari 10ish version where module support was pretty new. So I guess in my case it's better to use |
In the source code, I'm using a way to detect a global variable that's checking against
global
,window
and then fallsback tothis
. In the bundled code of esbuild, thethis
part is optimized tovoid 0
(which isundefined
) and therefore will throw some errors in the fallback case of using the global scope as a definition scope.Why is this important? "Sandboxing" in Browsers is the reason. I had to implement helper methods like
isString()
,isArray()
etc in order to check against data types that are transferred across sandboxes (or iframes in the Web Browser case). In this scenario something likeinstanceof
will fail as it's checking against a datatype that's defined as a unique Function template in the current sandbox.So
window.Array
!==other_window.Array
and in nodejs, the same applies when using separate Context instances in v8.In order to fix this, I've built a little gluecode library that offers checks and exports of the same datatypes (along polyfills for Buffer etc). In my case however, this transpilation scenario seems to fail, as it would break in environments where only the
globalThis
is present (like it is the case in nodejs-compatible embedded runtimes like jerryscript, or when embeddinglibnode.so
on an Android App).How to reproduce:
Source Code:
Transformed Outfile (target:
es2020
and format:esm
):The text was updated successfully, but these errors were encountered: