-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Enforce that __DEV__
is polyfilled in every entry point that uses it
#8689
Conversation
2aee938
to
e1f01bf
Compare
import { checkDEV } from "../../utilities"; | ||
checkDEV(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It turns out a number of entry points that weren't obviously using __DEV__
actually do need it, because they use invariant
or InvariantError
, which we transform to an expression containing __DEV__
to enable stripping long error messages in production.
export function checkDEV() { | ||
invariant("boolean" === typeof DEV, DEV); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This checkDEV
function not only checks that the DEV.ts
module successfully exported a boolean value (DEV
), but also indirectly ensures __DEV__
actually works, because this invariant(...)
expression is transformed to __DEV__ ? invariant(...) : invariant(...)
during build.
This merge of the still-open PR #8689 into `release-3.5` will allow us to test those changes in an `@apollo/client@beta` release before merging to `main` and releasing in v3.4.x.
This merge of the still-open PR #8689 into `release-3.5` will allow us to test those changes in an `@apollo/client@beta` release before merging to `main` and releasing in v3.4.x.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great fix @benjamn - thanks! 🎉
This PR started out as a quick fix for #8674, but in the process I realized the best way to prevent
ReferenceError
s for__DEV__
once and for all is to write a build step that examines compileddist/
code for usage of__DEV__
, and enforces that the corresponding entry pointindex.js
file imports and callscheckDEV()
.To test these changes using a published version of
@apollo/client
, I merged this PR branch intorelease-3.5
and published@apollo/client@3.5.0-beta.7
with this PR included. From my testing, this version works as before with ESM-aware CDNs like esm.run. In particular, the following code works in a browser console:See #8266 for more background on why this matters here.
Related issues and PRs:
__DEV__
internally instead ofprocess.env.NODE_ENV
. #8347__DEV__
from mis-minification / aggressive tree-shaking #8393stringifyCanon
untilcanonicalStringify
used. #8558Uncaught ReferenceError: __DEV__ is not defined
after updating to 3.4.0 #8557