-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ensure global resolves in strict mode
If CJS is bundled into ESM, then strict mode is forced. This patch ensures that global is resolved properly in such case Fixes #86 Thanks @mathiasbynens for hint on CSP safe global resolution
- Loading branch information
Showing
3 changed files
with
27 additions
and
5 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,5 +1,15 @@ | ||
/* eslint strict: "off" */ | ||
|
||
module.exports = (function () { | ||
return this; | ||
}()); | ||
if (this) return this; | ||
|
||
// Unexpected strict mode (may happen if e.g. bundled into ESM module) be nice | ||
|
||
// Thanks @mathiasbynens -> https://mathiasbynens.be/notes/globalthis | ||
// In all ES5 engines global object inherits from Object.prototype | ||
// (if you approached one that doesn't please report) | ||
Object.defineProperty(Object.prototype, "__global__", { | ||
get: function () { return this; }, | ||
configurable: true | ||
}); | ||
try { return __global__; } | ||
finally { delete Object.prototype.__global__; } | ||
})(); |
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
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