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

Support webpack with node environments (e.g. electron) #239

Merged
merged 8 commits into from
May 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
bundler: [browserify, webpack]
bundler: [browserify, webpack, "webpack:node"]
steps:
- name: checkout
uses: actions/checkout@master
Expand Down
7 changes: 5 additions & 2 deletions fetch-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ function wrapFetchForNode(fetch) {
}

module.exports = function (context) {
// Support webpack module import weirdness.
var fetchFn = fetch.default ? fetch.default : fetch;

// This modifies the global `node-fetch` object, which isn't great, since
// different callers to `fetch-ponyfill` which pass a different Promise
// implementation would each expect to have their implementation used. But,
// given the way `node-fetch` is implemented, this is the only way to make
// it work at all.
if (context && context.Promise) {
fetch.Promise = context.Promise;
fetchFn.Promise = context.Promise;
}

return {
fetch: wrapFetchForNode(fetch),
fetch: wrapFetchForNode(fetchFn),
Headers: fetch.Headers,
Request: fetch.Request,
Response: fetch.Response
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"test:browserify": "testem ci",
"pretest:webpack": "npm run build && webpack --mode development tests/fetch-browser.spec.js -o build/browser-test-bundle.js",
"test:webpack": "testem ci",
"pretest:webpack:node": "webpack --mode development --target node tests/fetch-node.spec.js -o build/node-test-bundle.js",
"test:webpack:node": "mocha build/node-test-bundle.js",
"build": "rimraf build && mkdirp build && node build.js > build/fetch-browser.js",
"prepublishOnly": "npm run build"
},
Expand Down