Skip to content

Commit 385868d

Browse files
fix: update node-fetch to 3.x
node-fetch is now ESM module only, thus this will be a breaking change and will requires at least Node.js 12.20.0. As suggested[1] by `node-fetch`, async import() function is used to load `node-fetch` asynchronously. BREAKING CHANGE: requires at least node 12.20.0 [1]: https://github.com/node-fetch/node-fetch#commonjs
1 parent 4dabeb6 commit 385868d

File tree

7 files changed

+7257
-5451
lines changed

7 files changed

+7257
-5451
lines changed

package-lock.json

Lines changed: 7196 additions & 5443 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"cross-var": "^1.1.0",
6262
"eslint": "^3.13.1",
6363
"eslint-config-developit": "^1.1.1",
64-
"jest": "^23.6.0",
64+
"jest": "^27.4.7",
6565
"microbundle": "^0.10.1"
6666
}
6767
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
function r(m){return m && m.default || m;}
22
module.exports = global.fetch = global.fetch || (
33
typeof process=='undefined' ? r(require('unfetch')) : (function(url, opts) {
4-
return r(require('node-fetch'))(String(url).replace(/^\/\//g,'https://'), opts);
4+
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
5+
return r(fetch)(String(url).replace(/^\/\//g,'https://'), opts);
56
})
67
);

packages/isomorphic-unfetch/package-lock.json

Lines changed: 46 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/isomorphic-unfetch/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"main": "index.js",
1414
"types": "index.d.ts",
1515
"dependencies": {
16-
"node-fetch": "^2.6.1",
16+
"node-fetch": "^3.2.0",
1717
"unfetch": "^4.2.0"
1818
}
1919
}

packages/isomorphic-unfetch/readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Switches between [unfetch](https://github.com/developit/unfetch) & [node-fetch](
66

77
This project uses [node](http://nodejs.org) and [npm](https://npmjs.com). Go check them out if you don't have them locally installed.
88

9+
> Note: This module uses node-fetch 3.x, which is ESM module and requires at least node 12.20.0.
10+
911
```sh
1012
$ npm i isomorphic-unfetch
1113
```

test/isomorphic.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,15 @@ describe('isomorphic-unfetch', () => {
112112
sandbox.global.process = sandbox.process;
113113
sandbox.module = { exports: sandbox.exports };
114114
let filename = require.resolve('../packages/isomorphic-unfetch');
115-
vm.runInNewContext(fs.readFileSync(filename), sandbox, filename);
115+
vm.runInNewContext(fs.readFileSync(filename), sandbox, {
116+
filename,
117+
importModuleDynamically: () => {
118+
const module = new vm.SyntheticModule(['node-fetch'], function() {
119+
this.setExport('default', 'I AM NODE-FETCH');
120+
});
121+
return module;
122+
}
123+
});
116124

117125
expect(sandbox.module.exports('/')).toBe(modules['node-fetch']('/'));
118126
});

0 commit comments

Comments
 (0)