Skip to content

Commit

Permalink
fix(polyfill): using try catch to determine if browser or node enviro…
Browse files Browse the repository at this point in the history
…nment
  • Loading branch information
Dimitri Kopriwa committed Feb 7, 2019
1 parent d4eee4c commit 645cfbe
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,32 @@ const defaultOptions = {
*/
export function polyfill(options) {
const opts = { ...defaultOptions, ...options };
let root = null;
try {
root = window || global;
} catch (e) {
root = global;
}
if (opts.isomorphicFetch) {
const Tough = require('tough-cookie');
const Store = new Tough.MemoryCookieStore();
const cookieJar = new Tough.CookieJar(Store);
const fetch = require('fetch-cookie')(require('isomorphic-fetch'), cookieJar);
global.fetch = fetch;
global.cookieJar = cookieJar;
root.fetch = fetch;
root.cookieJar = cookieJar;
require('whatwg-fetch');
}

if (opts.fetch && !opts.isomorphicFetch) {
require('whatwg-fetch');
global.fetchMock = require('fetch-mock');
root.fetchMock = require('fetch-mock');
}

if (opts.localStorage && !global.localStorage) {
global.localStorage = require('localStorage');
if (root.localStorage && !root.localStorage) {
root.localStorage = require('localStorage');
}

if (opts.media) {
const root = window || global;
root.matchMedia = root.matchMedia
|| function () {
return {
Expand Down

0 comments on commit 645cfbe

Please sign in to comment.