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
Accessing unknown globals inside a typeof guard shouldn't have side-effects #3115
Comments
I guess as part of this, I'm asking for the following code to also treeshake out: if (typeof SomeUnknownGlobal !== 'undefined') { // or any other pure expression involving `typeof`
// do nothing
} Merely checking |
The unknown global side effects PR is indeed the reason for the change in behavior, but not due to The actual issue is
Likewise, as far as Rollup is concerned, Given:
using rollup v1.20.3:
using rollup v1.21.2:
Notice that the unused
What you could do is ignore any side effects from
or disable |
Created #3117 to address this |
Thank you! I can confirm that the issue I was seeing in Svelte app bundles is fixed when using Rollup 1.21.4. |
FWIW, referencing any binding in the global scope can have side effects. The typeof operator isn’t related to that — the side effect doesn’t need to be a ReferenceError. Var bindings at that scope are realized as properties of the global object. Lots of builtin globals are implemented as accessors, and some of them really may have side effects. There are global vars(properties) which do or do not throw on reference(access) based on the nature of the document’s origin / iframe sandbox constraints / etc. |
Feature Use Case
This Svelte issue. Svelte's internal library has code that looks like:
Since Rollup 1.21.0, this is being included in all bundles, whether or not they use
SvelteElement
, as the reference to the unknown globalHTMLElement
as the super class is assumed to possibly have the side-effect of throwing an error if it is not defined.Feature Proposal
Code that references unknown globals inside of a
typeof
guard for that global shouldn't be assumed to have side effects.The text was updated successfully, but these errors were encountered: