fullstack-build / tslog

πŸ“ tslog - Universal Logger for TypeScript and JavaScript
https://tslog.js.org
MIT License
1.35k stars 63 forks source link

Consider Updating 'window' to 'globalThis' #218

Closed justin0mcateer closed 1 year ago

justin0mcateer commented 1 year ago

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

Today I used patch-package to patch tslog@4.7.5 for the project I'm working on.

'window' does not work on WebWorkers, 'globalThis' has been available pretty much everywhere (browsers AND Node) for about four years.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis#browser_compatibility https://caniuse.com/?search=globalThis

Here is the diff that solved my problem:

diff --git a/node_modules/tslog/dist/esm/runtime/browser/index.js b/node_modules/tslog/dist/esm/runtime/browser/index.js
index adae196..b33903d 100644
--- a/node_modules/tslog/dist/esm/runtime/browser/index.js
+++ b/node_modules/tslog/dist/esm/runtime/browser/index.js
@@ -3,7 +3,7 @@ import { formatWithOptions } from "./util.inspect.polyfil.js";
 import { jsonStringifyRecursive } from "./helper.jsonStringifyRecursive.js";
 const meta = {
     runtime: "Browser",
-    browser: window?.["navigator"].userAgent,
+    browser: globalThis?.["navigator"].userAgent,
 };
 const pathRegex = /(?:(?:file|https?|global code|[^@]+)@)?(?:file:)?((?:\/[^:/]+){2,})(?::(\d+))?(?::(\d+))?/;
 export function getMeta(logLevelId, logLevelName, stackDepthLevel, hideLogPositionForPerformance, name, parentNames) {
@@ -29,7 +29,7 @@ export function getErrorTrace(error) {
     }, []);
 }
 function stackLineToStackFrame(line) {
-    const href = window.location.origin;
+    const href = globalThis.location.origin;
     const pathResult = {
         fullFilePath: undefined,
         fileName: undefined,

This issue body was partially generated by patch-package.

terehov commented 1 year ago

Nice one! I must have missed this and caniuse looks promising: https://caniuse.com/?search=globalThis

Thank you.

terehov commented 1 year ago

Check out V4.8.0.