-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reverts #10521 Simplify __DEV__ polyfill to use imports instead of global scope
- Loading branch information
Showing
23 changed files
with
96 additions
and
96 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import * as fs from "fs"; | ||
import { EOL } from "os"; | ||
import { distDir } from './helpers'; | ||
|
||
export function applyDistSpotFixes() { | ||
sanitizeDEV(); | ||
} | ||
|
||
function sanitizeDEV() { | ||
const globalDTsPath = `${distDir}/utilities/globals/global.d.ts`; | ||
const globalDTs = fs.readFileSync(globalDTsPath, "utf8"); | ||
// The global.d.ts types are useful within the @apollo/client codebase to | ||
// provide a type for the global __DEV__ constant, but actually shipping that | ||
// declaration as a globally-declared type runs too much risk of conflict with | ||
// other __DEV__ declarations attempting to achieve the same thing, most | ||
// notably the one in @types/react-native/index.d.ts. We preserve the default | ||
// export so that index.d.ts can remain unchanged, but otherwise we remove all | ||
// traces of __DEV__ from global.d.ts. | ||
if (/__DEV__/.test(globalDTs)) { | ||
fs.writeFileSync(globalDTsPath, [ | ||
"declare const _default: typeof globalThis;", | ||
"export default _default;", | ||
].join(EOL)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,29 @@ | ||
import global from "./global"; | ||
import { maybe } from "./maybe"; | ||
|
||
export default ( | ||
"__DEV__" in global | ||
// We want it to be possible to set __DEV__ globally to control the result | ||
// of this code, so it's important to check global.__DEV__ instead of | ||
// assuming a naked reference like __DEV__ refers to global scope, since | ||
// those references could be replaced with true or false by minifiers. | ||
? Boolean(global.__DEV__) | ||
// To keep string-based find/replace minifiers from messing with __DEV__ inside | ||
// string literals or properties like global.__DEV__, we construct the "__DEV__" | ||
// string in a roundabout way that won't be altered by find/replace strategies. | ||
const __ = "__"; | ||
const GLOBAL_KEY = [__, __].join("DEV"); | ||
|
||
// In a buildless browser environment, maybe(() => process.env.NODE_ENV) | ||
// evaluates to undefined, so __DEV__ becomes true by default, but can be | ||
// initialized to false instead by a script/module that runs earlier. | ||
// | ||
// If you use tooling to replace process.env.NODE_ENV with a string like | ||
// "development", this code will become something like maybe(() => | ||
// "development") !== "production", which also works as expected. | ||
: maybe(() => process.env.NODE_ENV) !== "production" | ||
); | ||
function getDEV() { | ||
try { | ||
return Boolean(__DEV__); | ||
} catch { | ||
Object.defineProperty(global, GLOBAL_KEY, { | ||
// 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, | ||
writable: true, | ||
}); | ||
// Using computed property access rather than global.__DEV__ here prevents | ||
// string-based find/replace strategies from munging this to global.false: | ||
return (global as any)[GLOBAL_KEY]; | ||
} | ||
} | ||
|
||
export default getDEV(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters