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
typeof window === "undefined"
not simplified when --define:window=undefined
#1407
Comments
typeof window === "undefined"
not simplified when --define:window=undefined
Yes, I've also noticed this before as well. The problem is that |
As an extreme, with simplifying A lot of this is due to JavaScriptCore starting up faster than V8 (unrelated to esbuild), but part of that is because it doesn't have to parse Source files
esbuild command:
|
Command:
input.js:
Output:
It works as expected if you replace
window
withundefined
.input.js:
Output:
It also works as expected if you set
define
tonull
Command:
input.js:
Output:
Different since
typeof null
==='object'
I noticed this first in my bundler's implementation, realized it was reproducible in esbuild as well and wanted to give you a heads up.
This particular example --
typeof window
is common for running separate code in Node/web workers. Sometimes people hardcode it this way in their webpack config to skip requiring backend-only code. See the example in Webpack's documentationIn webpack's case, I think people usually make it
"typeof window": "undefined"
so that they theoretically could still have awindow
variable in the code somewhere. That wouldn't work here since it's parsed as JSON which lacks atypeof
operator, but it probably doesn't matter in practiceThe text was updated successfully, but these errors were encountered: