-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose ts-invariant/process entry point for optional polyfill.
Though ts-invariant stopped polyfilling global.process in #94, there's a chance existing applications were accidentally depending on that API, so we can make it easy to install (and then remove!) the global.process polyfill if you still need it: import { install, remove } from "ts-invariant/process" install(); ... remove(); By default, the polyfill will be installed upon importing ts-invariant/process for the first time, so you may not need to call install() in most cases (though it doesn't hurt). Calling remove() cleans up the global namespace, removing the stub global.process object (if ts-invariant/process defined one). This allows you to import other packages that depend on process.env before removing the polyfill, without permanently polluting the global namespace.
- Loading branch information
Showing
9 changed files
with
141 additions
and
39 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,2 @@ | ||
export declare function install(): void; | ||
export declare function remove(): void; |
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,37 @@ | ||
function maybe(thunk) { | ||
try { return thunk() } catch (_) {} | ||
} | ||
|
||
let needToRemove = false; | ||
|
||
export function install() { | ||
if (!maybe(() => process.env.NODE_ENV) && | ||
!maybe(() => process)) { | ||
Object.defineProperty(global, "process", { | ||
value: { | ||
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 and released. | ||
NODE_ENV: "production", | ||
}, | ||
}, | ||
// Let anyone else change global.process as they see fit, but hide it from | ||
// Object.keys(global) enumeration. | ||
configurable: true, | ||
enumerable: false, | ||
writable: true, | ||
}); | ||
needToRemove = true; | ||
} | ||
} | ||
|
||
// Call install() at least once, when this module is imported. | ||
install(); | ||
|
||
export function remove() { | ||
if (needToRemove) { | ||
delete global.process; | ||
needToRemove = false; | ||
} | ||
} |
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,10 @@ | ||
{ | ||
"name": "ts-invariant/process", | ||
"main": "./main.js", | ||
"module": "./module.js", | ||
"types": "./module.d.ts", | ||
"sideEffects": [ | ||
"./module.js", | ||
"./index.js" | ||
] | ||
} |
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