Skip to content

Commit

Permalink
Turn several PR comments from #8347 into code comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Jun 4, 2021
1 parent eb1532c commit 292d669
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/utilities/fixes/__DEV__.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ function getDEV() {
return Boolean(__DEV__);
} catch {
Object.defineProperty(global, "__DEV__", {
// In a buildless browser environment, maybe(() => process.env.NODE_ENV)
// evaluates as undefined, so __DEV__ becomes true by default, but can be
// initialized to false instead by a script/module that runs earlier.
value: maybe(() => process.env.NODE_ENV) !== "production",
enumerable: false,
configurable: true,
Expand Down
7 changes: 7 additions & 0 deletions src/utilities/fixes/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
// The ordering of these imports is important, because it ensures the temporary
// process.env.NODE_ENV polyfill is defined globally (if necessary) before we
// import { isType } from 'graphql'. The instanceOf function that we really care
// about (the one that uses process.env.NODE_ENV) is not exported from the
// top-level graphql package, but isType uses instanceOf, and is exported.
import { undo } from './process';
import { isType } from 'graphql';

export function applyFixes() {
// Calling isType here just to make sure it won't be tree-shaken away,
// provided applyFixes is called elsewhere.
isType(null);
return undo();
}
11 changes: 10 additions & 1 deletion src/utilities/fixes/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
export { default as __DEV__ } from "./__DEV__";
// Just in case the graphql package switches from process.env.NODE_ENV to
// __DEV__, make sure __DEV__ is polyfilled before importing graphql.
import { default as __DEV__ } from "./__DEV__";
export { __DEV__ }

// Import graphql/jsutils/instanceOf safely, working around its unchecked usage
// of process.env.NODE_ENV and https://github.com/graphql/graphql-js/pull/2894.
import { applyFixes } from "./graphql";

// Synchronously undo the global process.env.NODE_ENV polyfill that we created
// temporarily while importing the offending graphql/jsutils/instanceOf module.
applyFixes();
3 changes: 3 additions & 0 deletions src/utilities/fixes/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ let needToUndo = false;
if (global && maybe(() => process.env.NODE_ENV) === void 0) {
const stub = {
env: {
// This default needs to be "production" instead of "development", to
// avoid the problem https://github.com/graphql/graphql-js/pull/2894 will
// eventually solve, once merged.
NODE_ENV: "production",
},
};
Expand Down

0 comments on commit 292d669

Please sign in to comment.