From c3e855662418a1d02ad39d4f7d70a1cc8a63f72c Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Thu, 3 Dec 2020 17:52:02 -0500 Subject: [PATCH 1/2] Better polyfill for globalThis. Horrifying, sure, but much better than using Function: https://mathiasbynens.be/notes/globalthis Fixes #23. --- packages/ts-invariant/package-lock.json | 10 ++++++++++ packages/ts-invariant/package.json | 2 ++ packages/ts-invariant/rollup.config.js | 1 + packages/ts-invariant/src/invariant.ts | 21 ++++++++------------- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/packages/ts-invariant/package-lock.json b/packages/ts-invariant/package-lock.json index 7da1634..f4335eb 100644 --- a/packages/ts-invariant/package-lock.json +++ b/packages/ts-invariant/package-lock.json @@ -27,6 +27,16 @@ "integrity": "sha512-lYUtmJ9BqUN688fGY1U1HZoWT1/Jrmgigx2loq4ZcJpICECm/Om3V314BxdzypO0u5PORKGMM6x0OXaljV1YFg==", "dev": true }, + "@types/ungap__global-this": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/@types/ungap__global-this/-/ungap__global-this-0.3.1.tgz", + "integrity": "sha512-+/DsiV4CxXl6ZWefwHZDXSe1Slitz21tom38qPCaG0DYCS1NnDPIQDTKcmQ/tvK/edJUKkmuIDBJbmKDiB0r/g==" + }, + "@ungap/global-this": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/@ungap/global-this/-/global-this-0.4.1.tgz", + "integrity": "sha512-29JHci18v6MeCxaRDwaHb6Efm29nhd/ooeueH5rpg9jveUzFwesi7d0skGqLbcmnWY2CcEaJm6r/9+sUNzOekQ==" + }, "@ungap/promise-all-settled": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/@ungap/promise-all-settled/-/promise-all-settled-1.1.2.tgz", diff --git a/packages/ts-invariant/package.json b/packages/ts-invariant/package.json index 4d768c1..a040826 100644 --- a/packages/ts-invariant/package.json +++ b/packages/ts-invariant/package.json @@ -28,6 +28,8 @@ "test": "npm run build && npm run mocha" }, "dependencies": { + "@types/ungap__global-this": "^0.3.1", + "@ungap/global-this": "^0.4.1", "tslib": "^1.9.3" }, "devDependencies": { diff --git a/packages/ts-invariant/rollup.config.js b/packages/ts-invariant/rollup.config.js index 97f09f4..615a256 100644 --- a/packages/ts-invariant/rollup.config.js +++ b/packages/ts-invariant/rollup.config.js @@ -4,6 +4,7 @@ import typescript from 'typescript'; const globals = { __proto__: null, tslib: "tslib", + "@ungap/global-this": "globalThisPolyfill", }; function external(id) { diff --git a/packages/ts-invariant/src/invariant.ts b/packages/ts-invariant/src/invariant.ts index 14eedc7..4d3a1cb 100644 --- a/packages/ts-invariant/src/invariant.ts +++ b/packages/ts-invariant/src/invariant.ts @@ -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; From f1093311d73590c7acfd54c4cc65751ff7fa37c9 Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Fri, 4 Dec 2020 10:54:14 -0500 Subject: [PATCH 2/2] Update to @ungap/global-this@0.4.2. --- packages/ts-invariant/package-lock.json | 6 +++--- packages/ts-invariant/package.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/ts-invariant/package-lock.json b/packages/ts-invariant/package-lock.json index f4335eb..c3d9eb7 100644 --- a/packages/ts-invariant/package-lock.json +++ b/packages/ts-invariant/package-lock.json @@ -33,9 +33,9 @@ "integrity": "sha512-+/DsiV4CxXl6ZWefwHZDXSe1Slitz21tom38qPCaG0DYCS1NnDPIQDTKcmQ/tvK/edJUKkmuIDBJbmKDiB0r/g==" }, "@ungap/global-this": { - "version": "0.4.1", - "resolved": "https://registry.npmjs.org/@ungap/global-this/-/global-this-0.4.1.tgz", - "integrity": "sha512-29JHci18v6MeCxaRDwaHb6Efm29nhd/ooeueH5rpg9jveUzFwesi7d0skGqLbcmnWY2CcEaJm6r/9+sUNzOekQ==" + "version": "0.4.2", + "resolved": "https://registry.npmjs.org/@ungap/global-this/-/global-this-0.4.2.tgz", + "integrity": "sha512-uFg7Kz+E12RBlgBLMlWVjmn2OIeE2J1Lzij0RseNcCVsrJX+LEB4fQ9MnoPXkXJmO5cHtTEzI5ATtb3IJfQ9tQ==" }, "@ungap/promise-all-settled": { "version": "1.1.2", diff --git a/packages/ts-invariant/package.json b/packages/ts-invariant/package.json index a040826..8cbf0e1 100644 --- a/packages/ts-invariant/package.json +++ b/packages/ts-invariant/package.json @@ -29,7 +29,7 @@ }, "dependencies": { "@types/ungap__global-this": "^0.3.1", - "@ungap/global-this": "^0.4.1", + "@ungap/global-this": "^0.4.2", "tslib": "^1.9.3" }, "devDependencies": {