From e626d14bef9ccb526564ab2f496f1e0b52aa18c7 Mon Sep 17 00:00:00 2001 From: Marnix Kok Date: Sun, 16 Oct 2022 15:33:43 +1300 Subject: [PATCH] Fix for when global and window object both not found. Changes: * use globalThis polyfill --- lib/handlebars/no-conflict.js | 45 +++++++++++++++-------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/lib/handlebars/no-conflict.js b/lib/handlebars/no-conflict.js index fcfc058f..91ef1da5 100644 --- a/lib/handlebars/no-conflict.js +++ b/lib/handlebars/no-conflict.js @@ -1,30 +1,23 @@ -/* global global, window */ +/* global globalThis */ export default function(Handlebars) { - function getRoot() { - if (typeof global !== 'undefined') { - return global; - } else if (typeof window !== 'undefined') { - return window; - } - - return null; - } + /* istanbul ignore next */ + // https://mathiasbynens.be/notes/globalthis + (function() { + if (typeof globalThis === 'object') return; + Object.prototype.__defineGetter__('__magic__', function() { + return this; + }); + __magic__.globalThis = __magic__; // eslint-disable-line no-undef + delete Object.prototype.__magic__; + })(); - const root = getRoot(); + const $Handlebars = globalThis.Handlebars; - if (root !== null) { - root.$Handlebars = root.Handlebars; - - /* istanbul ignore next */ - Handlebars.noConflict = function() { - if (root.Handlebars === Handlebars) { - root.Handlebars = root.$Handlebars; - } - return Handlebars; - }; - } else { - Handlebars.noConflict = function() { - return Handlebars; - }; - } + /* istanbul ignore next */ + Handlebars.noConflict = function() { + if (globalThis.Handlebars === Handlebars) { + globalThis.Handlebars = $Handlebars; + } + return Handlebars; + }; }