★ wanayoo — archive 1999 https://github.com/developit/unfetch/issues/31Nouvelle recherche | Portail wanayoo
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
Labels

Comments

@sapegin
Copy link
Contributor

@sapegin 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

@tunnckoCore tunnckoCore commented Mar 2, 2017

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

@developit
Copy link
Owner

@developit 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 sapegin commented Mar 2, 2017

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

@developit
Copy link
Owner

@developit 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 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
@developit developit closed this in #32 Mar 3, 2017
@enapupe
Copy link

@enapupe 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 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

@developit developit commented Apr 3, 2017

@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 enapupe commented Apr 3, 2017

Sitebase pushed a commit to ambassify/track-js that referenced this issue Aug 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.