如何在 Gatsby 中集成 whatwg-fetch

How to integrate whatwg-fetch in Gatsby

有人知道如何在 gatsby 中集成 whatwg-fetch fetch polyfill 吗?

我目前所做的是 import 'whatwg-fetch'; 中的 gatsby-browser.js。现在我不确定如何将它添加为 webpack 的第一个元素 entry 属性 大概在 gatsby-node.js.

是的,这是我的副本(经过测试和工作)gatsby-browser.js:

import 'whatwg-fetch' // require('whatwg-fetch') // if it's gatsby v2 - https://gatsby.app/no-mixed-modules

exports.onClientEntry = () => {
  // Don't need to do anything here, but if you don't
  // export something, the import won't work.
}

不需要在webpack的entry属性中添加whatwg-fetchgatsby-node.js.

同样 whatwg-fetch 依赖于 Promises,但是 Promisealready polyfilled in gatsby,所以不需要为 Promise.

添加额外的 polyfill