Skip to content
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

unfetch/polyfill doesn’t work in webpack 2 #31

Closed
sapegin opened this issue Mar 2, 2017 · 9 comments
Closed

unfetch/polyfill doesn’t work in webpack 2 #31

sapegin opened this issue Mar 2, 2017 · 9 comments

Comments

@sapegin
Copy link
Contributor

sapegin commented Mar 2, 2017

When I do:

import 'unfetch/polyfill';

I see this error in Safari 10:

TypeError: fetch is not a function.

Because fetch is actually a ES module wrapper:

{__esModule: true, default: function}

I suppose it’s because you have require('.') in polyfll.js and webpack 2 resolves it to index.es.js. So this code works for me:

if (!window.fetch) {
    window.fetch = require('unfetch').default;
}

I’d be happy to submit a PR but not sure what would be the right fix for this.

@tunnckoCore
Copy link
Collaborator

that is why i saying for years to not use single dot

@developit
Copy link
Owner

developit commented Mar 2, 2017

Ah good catch - @sapegin @tunnckoCore, are we good just to change . to unfetch? If so, PR welcome.

@sapegin
Copy link
Contributor Author

sapegin commented Mar 2, 2017

That should be the same, so probably to ./dist/unfetch.

@developit
Copy link
Owner

developit commented Mar 2, 2017

Hm - but if someone is using this via webpack 2 the entrypoint would actually be dist/unfetch.es.js right?

I just re-read the story here and I actually think we're fine with . - that will give people whatever the package.json defines as a main field based on their bundler (webpack 2 etc). The only thing that needs to be done is adding an interop:

if (!window.fetch) {
  window.fetch = (require('unfetch').default || require('unfetch'));
}

Seem reasonable?

alternatively:

if (!window.fetch) {
  window.fetch = (function(a){ return a.default || a })(require('unfetch'));
}

@sapegin
Copy link
Contributor Author

sapegin commented Mar 2, 2017

Yeah, that should work too. Will send a PR.

sapegin added a commit to sapegin/unfetch that referenced this issue Mar 2, 2017
@enapupe
Copy link

enapupe commented Apr 2, 2017

What do you guys do about window not being defined on webpack context? Just define it as an empty obj?

@enapupe
Copy link

enapupe commented Apr 2, 2017

Nevermind. I think my comment is unrelated because I'm talking about server bundling with target: node..

@developit
Copy link
Owner

@enapupe indeed - for that case, are you able to use isomorphic-unfetch? It'll use node-fetch when bundled/run under node.

@enapupe
Copy link

enapupe commented Apr 3, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants