-
Notifications
You must be signed in to change notification settings - Fork 34
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
Detect Web Workers environment #12
Comments
Unfortunately I have no idea about web workers. This answer suggests that your suggested code change won't work very well. And I couldn't find out what that |
In the script of Web Workers, the I reference the solution from the other library: // Environment detection
if (
typeof module === "object" && module && typeof module.exports === "object"
) {
// Node.js
module.exports = msgpack;
} else {
// Global object
var g;
if (typeof window !== "undefined") {
g = window;
} else if (typeof global !== "undefined") {
g = global;
} else if (typeof self !== "undefined") {
g = self;
} else {
g = this;
}
g[g.msgpackJsName || "msgpack"] = msgpack;
} |
https://caniuse.com/?search=globalThis Based on the above, pass the globalThis into your IIFE like this: (function (_global) { //bind globalThis to _global
...
// Environment detection
if (typeof module === "object" && module && typeof module.exports === "object") {
// Node.js
module.exports = msgpack;
}
else if (_global) {
// Global object
_global[_global.msgpackJsName || "msgpack"] = msgpack;
}
)(globalThis || this || self || window); Notes:
There won't be any perfect solution to polyfill Another option is to not define |
When the library is used in a web worker there is an issue with undefined
window
so i suggest adding Web Workers detection such as :The text was updated successfully, but these errors were encountered: