-
Notifications
You must be signed in to change notification settings - Fork 2k
Prevents injecting Node.js globals like process, which can break browser features when loading graphql from ESM-based CDN like esm.sh #4434
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
Conversation
Our 16.x.x But checking out the code you actually get with esm from your link above, it looks like NODE_ENV got erased, and we are just checking globalThis.process, and if that's truthy, you get the production build, which is....interesting. Not sure I'm reading it right. So maybe we would want to set process to true? But that wouldn't get rid of the ponyfill? Seems like there might be a workaround. Right now, if you use "https://esm.sh/graphql?target=node" you get no ponyfill, and the same erasure of NODE_ENV, so I guess a workaround for now might be to use that link, and just make sure that process is truthy before calling that link? In v17, we are going to switch the default to be production code, so we wouldn't need to touch the browser field there, assuming that nothing else changes. |
When loading
I strongly suggest this change to unify behaviour when loading |
Thanks for bringing this up, your expertise is very helpful. Apologies though for my obtuseness, I thought process is ponyfilled by esm.sh, not polyfilled, I tested just from my chrome console by just running
and it doesn't seem to be setting globalThis.process => at least for me. Checking the transformed code, it imports a ponyfill and then transforms our package to use the For example, I tested this change by pushing a branch of the compiled library to my fork with Checking https://esm.sh/gh/yaacovCR/graphql-js@e3112d32 (the commit short hash, using the branch name seems to get the first version of the branch without the additional commit) we get: /* esm.sh - gh/yaacovCR/graphql-js@e3112d32 */
export * from "/gh/yaacovCR/graphql-js@e3112d32/es2022/graphql-js.mjs"; And then running this in the console, we get our development version of const { isObjectType } = await import('https://esm.sh/gh/yaacovCR/graphql-js@e3112d32');
function getFakeObjectType() {
class GraphQLObjectType {
get [Symbol.toStringTag]() {
return 'GraphQLObjectType';
}
}
return new GraphQLObjectType();
};
isObjectType(getFakeObjectType());
|
Why does it force production mode? because this code: graphql-js/src/jsutils/instanceOf.ts Lines 4 to 7 in bb87e73
seems to be rewritten to: const isProduction =
globalThis.process &&
'production' === 'production'; and then to const isProduction =
globalThis.process and then to const isProduction =
__Process$ And then supplying the polyfill means that the default for node is slow unless NODE_ENV is explicitly set to production In contrast, the default for esm.sh is production, unless you explicitly ask for dev, which is the behavior we prefer and have decided to switch to in v17. |
Thanks Yaacov for your amazing investigation!
I might be wrong about this. Recently I’ve struggled a lot in GraphiQL 5 with importing That issue was caused because I fixed it in But after I saw that while importing So I suggested this change in Feel free to close this PR if you don't want to break current behaviour, I just want to suggest avoid including unneeded ponyfills! |
Thanks so @dimaMachina much for bringing this up. I was not actually aware of how things worked with From what I understand, the bug in |
see https://esm.sh/graphql
where process is polyfilled
ref esm-dev/esm.sh#1134 (comment)