Skip to content

Commit

Permalink
Fix for when global and window object both not found.
Browse files Browse the repository at this point in the history
Changes:

* use globalThis polyfill
  • Loading branch information
marnixk committed Oct 16, 2022
1 parent 51bb05c commit e626d14
Showing 1 changed file with 19 additions and 26 deletions.
45 changes: 19 additions & 26 deletions lib/handlebars/no-conflict.js
Original file line number Diff line number Diff line change
@@ -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;
};
}

0 comments on commit e626d14

Please sign in to comment.