diff --git a/keeper/.gitignore b/keeper/.gitignore index f3364f6..76fd571 100644 --- a/keeper/.gitignore +++ b/keeper/.gitignore @@ -62,4 +62,7 @@ typings/ # Distributive compiled -*.d.ts \ No newline at end of file +*.d.ts + +# Except these +!/__types__/*.d.ts diff --git a/keeper/__types__/global.d.ts b/keeper/__types__/global.d.ts new file mode 100644 index 0000000..afa75c4 --- /dev/null +++ b/keeper/__types__/global.d.ts @@ -0,0 +1 @@ +declare const global: any; diff --git a/keeper/src/keeper/keeper.ts b/keeper/src/keeper/keeper.ts index 238cb04..2fa97a5 100644 --- a/keeper/src/keeper/keeper.ts +++ b/keeper/src/keeper/keeper.ts @@ -1,11 +1,12 @@ const nil = null; const BOLD = 'font-weight: bold;'; -const globalThis = Function('return this')() as Window & { +type GlobalThis = Window & { Date: DateConstructor; perfKeeper?: { system: PerfKeeper; }; }; +const globalThis = typeof window === 'undefined' ? typeof global === 'undefined' ? this : global : window as GlobalThis; const Date = globalThis.Date; const dateNow = Date.now; diff --git a/performance/__types__/global.ts b/performance/__types__/global.ts new file mode 100644 index 0000000..afa75c4 --- /dev/null +++ b/performance/__types__/global.ts @@ -0,0 +1 @@ +declare const global: any; diff --git a/performance/src/performance/performance.ts b/performance/src/performance/performance.ts index 204f936..f2eb3a2 100644 --- a/performance/src/performance/performance.ts +++ b/performance/src/performance/performance.ts @@ -188,7 +188,8 @@ export function polyfill(global: T) { }; } -const globalThis = polyfill(new Function('return this')); +const globalObject = typeof window === 'undefined' ? typeof global === 'undefined' ? this : global : window; +const globalThis = polyfill(globalObject); if (typeof self !== 'undefined' && (self as any) !== globalThis) { polyfill(self);