From 5290626b37e7d8233f7d91263ee69ddf446252ee Mon Sep 17 00:00:00 2001 From: Leo Singer Date: Sun, 15 Dec 2024 13:32:02 -0500 Subject: [PATCH] Use builtin Web Fetch; polyfill not needed in Node.js >= 20 Note that we must manually exclude the two polyfill libraries, unidici and @remix-run/web-fetch, because they are not properly tree-shaken. --- remix.config.js | 19 ++++++++++++++++++- server.ts | 2 -- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/remix.config.js b/remix.config.js index f4ed1cb98..375cf6877 100644 --- a/remix.config.js +++ b/remix.config.js @@ -45,6 +45,15 @@ const esmOnlyModules = [ /^vfile/, ] +// These packages should never be bundled. +const neverBundledModules = [ + // Included in AWS Lambda base image + /@?aws-sdk(?:\/|$)/, + // Used to polyfill Web Fetch; not needed for Node.js >= 20 + 'undici', + '@remix-run/web-fetch', +] + /** @type {import('@remix-run/dev').AppConfig} */ export default { mdx: { @@ -80,7 +89,15 @@ export default { serverModuleFormat: 'cjs', serverDependenciesToBundle: [ ...esmOnlyModules, - ...(isProduction ? [/^(?!@?aws-sdk(\/|$))/] : []), + ...(isProduction + ? [ + new RegExp( + `^(?!${neverBundledModules + .map((item) => (item instanceof RegExp ? item.source : item)) + .join('|')})` + ), + ] + : []), ], future: { v3_relativeSplatPath: true }, } diff --git a/server.ts b/server.ts index efe4d2859..37023fa54 100644 --- a/server.ts +++ b/server.ts @@ -1,11 +1,9 @@ import { type RequestHandler, createRequestHandler } from '@remix-run/architect' import * as build from '@remix-run/dev/server-build' -import { installGlobals } from '@remix-run/node' import { type APIGatewayProxyStructuredResultV2 } from 'aws-lambda' import sourceMapSupport from 'source-map-support' sourceMapSupport.install() -installGlobals({ nativeFetch: true }) const remixHandler = createRequestHandler({ build,