-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix for when global
and window
object both not found.
#1894
Conversation
Hm... can we somehow check for not relying on Maybe by removing the line |
Hi, thanks for the feedback! When I remove the comment with global variable declarations, eslint starts to complain about the use of I think we could probably leave it in here, it's only a local declaration anyway which means their usage won't impact the any potential uses in other places (although I haven't found any other uses of Thanks! |
Hey! I'm uncertain if you're waiting for some more input from me, or if the PR will be able to move through whatever processes are in place to get it merged at some point. Happy to provide anything you need! Cheers! Marnix |
@marnixk I was still thinking about ways to test this, but until we ship native ES-module support, we can't test in web-workers. I read a little bit about how the get the root-object. I'd prefer to use an "official" solution, like the one from https://mathiasbynens.be/notes/globalthis. /* global globalThis */
export default function(Handlebars) {
/* 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 $Handlebars = globalThis.Handlebars;
/* istanbul ignore next */
Handlebars.noConflict = function() {
if (globalThis.Handlebars === Handlebars) {
globalThis.Handlebars = $Handlebars;
}
return Handlebars;
};
} Can you verify if this works in a Cloudflare worker? (I think it should) |
That's a fun bit of code! I've pushed your suggestion to the PR and tested it with my Cloudflare Worker -- it all seems to work great! Thanks for the suggestions. |
When using Handlebars in a Cloudflare Worker, an environment in which the `window` and `global` objects both don't exist, an error is thrown about `window` being undefined. According to the ECMA specification, only `self` is available in a worker. Since we support old runtimes in our 4.x branch, we can't use `globalThis` but have to use a polyfill.
Thanks! After #1864 is merged, I will also fix this in |
This is needed in order to use `globalThis`, see #1894.
Pulled from 4.x branch, see #1894.
This is needed in order to use `globalThis`, see #1894.
Pulled from 4.x branch, see #1894.
This is needed in order to use `globalThis`, see #1894. It also made it possible to remove some old polyfills and fallbacks.
Pulled from 4.x branch, see #1894.
This is needed in order to use `globalThis`, see #1894. It also made it possible to remove some old polyfills and fallbacks.
Pulled from 4.x branch, see #1894.
When using Handlebars in a Cloudflare Worker, an environment in which the
window
andglobal
objects both don't exist, an error is thrown aboutwindow
being undefined.In this PR I have slightly changed the code so that if both window and global do not exist, a Handlebars.noConflict function is generated that just returns the original
Handlebars
instance.