Skip to content
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

Better polyfill for globalThis. #53

Merged
merged 2 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/ts-invariant/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/ts-invariant/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"test": "npm run build && npm run mocha"
},
"dependencies": {
"@types/ungap__global-this": "^0.3.1",
"@ungap/global-this": "^0.4.2",
"tslib": "^1.9.3"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/ts-invariant/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import typescript from 'typescript';
const globals = {
__proto__: null,
tslib: "tslib",
"@ungap/global-this": "globalThisPolyfill",
};

function external(id) {
Expand Down
21 changes: 8 additions & 13 deletions packages/ts-invariant/src/invariant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,15 @@ export function setVerbosity(level: VerbosityLevel): VerbosityLevel {
// However, because most ESM-to-CJS compilers will rewrite the process import
// as tsInvariant.process, which prevents proper replacement by minifiers, we
// also attempt to define the stub globally when it is not already defined.
let processStub = { env: {} } as typeof process;
import globalThis from "@ungap/global-this";
const processStub = globalThis.process || { env: {} };
export { processStub as process };
if (typeof process === "object") {
processStub = process;
} else try {
// Using Function to evaluate this assignment in global scope also escapes
// the strict mode of the current module, thereby allowing the assignment.
// Inspired by https://github.com/facebook/regenerator/pull/369.
Function("stub", "process = stub")(processStub);
} catch (atLeastWeTried) {
// The assignment can fail if a Content Security Policy heavy-handedly
// forbids Function usage. In those environments, developers should take
// extra care to replace process.env.NODE_ENV in their production builds,
// or define an appropriate global.process polyfill.
if (!globalThis.process) try {
Object.defineProperty(globalThis, "process", {
value: processStub,
});
} catch {
// If this fails, it isn't the end of the world.
}

export default invariant;