-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for when global and window object both not found.
Changes: * use globalThis polyfill
- Loading branch information
Showing
1 changed file
with
19 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |