kadirahq / lokka-transport-http

HTTP Transport Layer for Lokka
MIT License
41 stars 38 forks source link

fix the way fetch dependencies are loaded #16

Closed leimonio closed 8 years ago

leimonio commented 8 years ago

According to the issue https://github.com/kadirahq/lokka-transport-http/issues/13 fetch loading is not possible on Safari.

The problem is that typeof __dirname !== 'undefined' condition is compiled to true with webpack bundle loader. A good check could be typeof module !== 'undefined' && module.exports which also underscore.js is using. This is not possible though because transpiled code in dist/index.js defines the following code, defining module.exports:

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Transport = undefined;

So the solution on checking the current environment can be this check var isNode=new Function("try {return this===global;}catch(e){return false;}"); better explained here http://stackoverflow.com/questions/17575790/environment-detection-node-js-or-browser.

Another issue solved here is that the require('whatwg-fetch') doesn't export the fetch function, but it defines it on the global scope, so we have to assign fetchUrl = fetch; just like the native browser function.

Finally, tests pass, so let me know if you are ok with this PR. :)

Note: Chrome and Firefox have native browser implementation for fetch.

arunoda commented 8 years ago

Okay. This is perfect. Thanks for this.

arunoda commented 8 years ago

Released with v1.4.1