rsnext/packages/next/server/node-polyfill-fetch.js
JJ Kasper 25d064f812
Pre-compile more dependencies (#32742)
This ncc's some remaining dependencies bringing us under 20 install time dependencies (including nested dependencies), this also reduces install size by another `2.75 MB`. A follow-up PR will investigate a custom install script for our swc packages to allow us to remove the `optionalDependencies` which is slowing down install time as well. 

x-ref: https://github.com/vercel/next.js/pull/32679
x-ref: https://github.com/vercel/next.js/pull/32627
x-ref: https://github.com/vercel/next.js/issues/31887
x-ref: https://github.com/vercel/styled-jsx/pull/770
2022-01-17 15:17:22 +00:00

23 lines
582 B
JavaScript

import fetch, {
Headers,
Request,
Response,
} from 'next/dist/compiled/node-fetch'
// Polyfill fetch() in the Node.js environment
if (!global.fetch) {
const agent = ({ protocol }) =>
protocol === 'http:' ? global.__NEXT_HTTP_AGENT : global.__NEXT_HTTPS_AGENT
const fetchWithAgent = (url, opts, ...rest) => {
if (!opts) {
opts = { agent }
} else if (!opts.agent) {
opts.agent = agent
}
return fetch(url, opts, ...rest)
}
global.fetch = fetchWithAgent
global.Headers = Headers
global.Request = Request
global.Response = Response
}