Skip to content

Instantly share code, notes, and snippets.

@Dari4sho
Created November 11, 2024 19:32
Show Gist options
  • Select an option

  • Save Dari4sho/71d5ea6eae4d50352d4244e61ebe4c6e to your computer and use it in GitHub Desktop.

Select an option

Save Dari4sho/71d5ea6eae4d50352d4244e61ebe4c6e to your computer and use it in GitHub Desktop.
Patch for `@codesandbox/sandpack-react` using patch-package to not use `eval()`
diff --git a/node_modules/@codesandbox/sandpack-react/dist/index.js b/node_modules/@codesandbox/sandpack-react/dist/index.js
index a419147..59f82ef 100644
--- a/node_modules/@codesandbox/sandpack-react/dist/index.js
+++ b/node_modules/@codesandbox/sandpack-react/dist/index.js
@@ -5037,8 +5037,20 @@ var MAX_MESSAGE_COUNT = MAX_KEYS * 2;
// Const
var GLOBAL = (function getGlobal() {
// NOTE: see http://www.ecma-international.org/ecma-262/6.0/index.html#sec-performeval step 10
- var savedEval = eval;
- return savedEval("this");
+ if (typeof globalThis !== "undefined") {
+ // Modern standard
+ return globalThis;
+ } else if (typeof window !== "undefined") {
+ // Browsers
+ return window;
+ } else if (typeof global !== "undefined") {
+ // Node.js
+ return global;
+ } else if (typeof self !== "undefined") {
+ // Web Workers
+ return self;
+ }
+ throw new Error("Unable to locate global object.");
})();
var ARRAY_BUFFER_SUPPORTED = typeof ArrayBuffer === "function";
var MAP_SUPPORTED = typeof Map === "function";
diff --git a/node_modules/@codesandbox/sandpack-react/dist/index.mjs b/node_modules/@codesandbox/sandpack-react/dist/index.mjs
index fb56146..bcec85a 100644
--- a/node_modules/@codesandbox/sandpack-react/dist/index.mjs
+++ b/node_modules/@codesandbox/sandpack-react/dist/index.mjs
@@ -5010,8 +5010,20 @@ var MAX_MESSAGE_COUNT = MAX_KEYS * 2;
// Const
var GLOBAL = (function getGlobal() {
// NOTE: see http://www.ecma-international.org/ecma-262/6.0/index.html#sec-performeval step 10
- var savedEval = eval;
- return savedEval("this");
+ if (typeof globalThis !== "undefined") {
+ // Modern standard
+ return globalThis;
+ } else if (typeof window !== "undefined") {
+ // Browsers
+ return window;
+ } else if (typeof global !== "undefined") {
+ // Node.js
+ return global;
+ } else if (typeof self !== "undefined") {
+ // Web Workers
+ return self;
+ }
+ throw new Error("Unable to locate global object.");
})();
var ARRAY_BUFFER_SUPPORTED = typeof ArrayBuffer === "function";
var MAP_SUPPORTED = typeof Map === "function";
diff --git a/node_modules/@codesandbox/sandpack-react/dist/unstyled/index.js b/node_modules/@codesandbox/sandpack-react/dist/unstyled/index.js
index b74ba42..dc6ed9d 100644
--- a/node_modules/@codesandbox/sandpack-react/dist/unstyled/index.js
+++ b/node_modules/@codesandbox/sandpack-react/dist/unstyled/index.js
@@ -5170,8 +5170,20 @@ var MAX_KEYS = 400;
var MAX_MESSAGE_COUNT = MAX_KEYS * 2;
var GLOBAL = (function getGlobal() {
- var savedEval = eval;
- return savedEval("this");
+ if (typeof globalThis !== "undefined") {
+ // Modern standard
+ return globalThis;
+ } else if (typeof window !== "undefined") {
+ // Browsers
+ return window;
+ } else if (typeof global !== "undefined") {
+ // Node.js
+ return global;
+ } else if (typeof self !== "undefined") {
+ // Web Workers
+ return self;
+ }
+ throw new Error("Unable to locate global object.");
})();
var ARRAY_BUFFER_SUPPORTED = typeof ArrayBuffer === "function";
var MAP_SUPPORTED = typeof Map === "function";
diff --git a/node_modules/@codesandbox/sandpack-react/dist/unstyled/index.mjs b/node_modules/@codesandbox/sandpack-react/dist/unstyled/index.mjs
index 71a2a3c..5382845 100644
--- a/node_modules/@codesandbox/sandpack-react/dist/unstyled/index.mjs
+++ b/node_modules/@codesandbox/sandpack-react/dist/unstyled/index.mjs
@@ -5143,8 +5143,20 @@ var MAX_KEYS = 400;
var MAX_MESSAGE_COUNT = MAX_KEYS * 2;
var GLOBAL = (function getGlobal() {
- var savedEval = eval;
- return savedEval("this");
+ if (typeof globalThis !== "undefined") {
+ // Modern standard
+ return globalThis;
+ } else if (typeof window !== "undefined") {
+ // Browsers
+ return window;
+ } else if (typeof global !== "undefined") {
+ // Node.js
+ return global;
+ } else if (typeof self !== "undefined") {
+ // Web Workers
+ return self;
+ }
+ throw new Error("Unable to locate global object.");
})();
var ARRAY_BUFFER_SUPPORTED = typeof ArrayBuffer === "function";
var MAP_SUPPORTED = typeof Map === "function";
@Dari4sho
Copy link
Author

Dari4sho commented Nov 11, 2024

According issue in the library's repository:
codesandbox/sandpack#1221

Put the patch file to a patches directory in the root of your project (using npm - might differ slightly for other package managers)

Apply patch using:
npx patch-package

Don't forget to add
"postinstall": "npx patch-package"
to package.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment