Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: fixed wrapper that hijacks the global object
Browse files Browse the repository at this point in the history
lquixada committed Dec 15, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 4d7f5d6 commit 3a18d6e
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -25,29 +25,35 @@ export default [
name: 'irrelevant',
strict: false,
banner: outdent(`
var global = typeof self !== 'undefined' ? self : this;
var __self__ = (function () {
// Save global object in a variable
var __global__ =
(typeof globalThis !== 'undefined' && globalThis) ||
(typeof self !== 'undefined' && self) ||
(typeof global !== 'undefined' && global);
// Create an object that extends from __global__ without the fetch function
var __globalThis__ = (function () {
function F() {
this.fetch = false;
this.DOMException = global.DOMException
this.DOMException = __global__.DOMException
}
F.prototype = global;
F.prototype = __global__; // Needed for feature detection on whatwg-fetch's code
return new F();
})();
(function(self) {
// Wraps whatwg-fetch with a function scope to hijack the global object
// "globalThis" that's going to be patched
(function(globalThis) {
`),
footer: outdent(`
})(__self__);
__self__.fetch.ponyfill = true;
})(__globalThis__);
// Remove "polyfill" property added by whatwg-fetch
delete __self__.fetch.polyfill;
// This is a ponyfill, so...
__globalThis__.fetch.ponyfill = true;
delete __globalThis__.fetch.polyfill;
// Choose between native implementation (global) or custom implementation (__self__)
// var ctx = global.fetch ? global : __self__;
var ctx = __self__; // this line disable service worker support temporarily
// Choose between native implementation (__global__) or custom implementation (__globalThis__)
var ctx = __global__.fetch ? __global__ : __globalThis__;
exports = ctx.fetch // To enable: import fetch from 'cross-fetch'
exports.default = ctx.fetch // For TypeScript consumers without esModuleInterop.

0 comments on commit 3a18d6e

Please sign in to comment.