Skip to content
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
7 changes: 7 additions & 0 deletions .changeset/shy-news-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@opennextjs/cloudflare": patch
---

drop unused react-dom modules

This saves ~500kB on the output bundle
2 changes: 2 additions & 0 deletions packages/cloudflare/src/cli/build/bundle-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { patchDepdDeprecations } from "./patches/plugins/patch-depd-deprecations
import { fixRequire } from "./patches/plugins/require.js";
import { shimRequireHook } from "./patches/plugins/require-hook.js";
import { patchRouteModules } from "./patches/plugins/route-module.js";
import { shimReact } from "./patches/plugins/shim-react.js";
import { setWranglerExternal } from "./patches/plugins/wrangler-external.js";
import { copyPackageCliFiles, needsExperimentalReact, normalizePath } from "./utils/index.js";

Expand Down Expand Up @@ -92,6 +93,7 @@ export async function bundleServer(buildOpts: BuildOptions, projectOpts: Project
conditions: getOpenNextConfig(buildOpts).cloudflare?.useWorkerdCondition === false ? [] : ["workerd"],
plugins: [
shimRequireHook(buildOpts),
shimReact(buildOpts),
inlineDynamicRequires(updater, buildOpts),
setWranglerExternal(),
fixRequire(updater),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import type { Plugin } from "esbuild";
export function shimRequireHook(options: BuildOptions): Plugin {
const emptyShimPath = join(options.outputDir, "cloudflare-templates/shims/empty.js");
return {
name: "replaceRelative",
name: "require-hook-shim",
setup(build) {
// Note: we (empty) shim require-hook modules as they generate problematic code that uses requires
// We (empty) shim require-hook modules as they generate problematic code that uses requires
build.onResolve(
{ filter: getCrossPlatformPathRegex(String.raw`^\./require-hook$`, { escape: false }) },
() => ({
Expand Down
37 changes: 37 additions & 0 deletions packages/cloudflare/src/cli/build/patches/plugins/shim-react.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { join } from "node:path";

import type { BuildOptions } from "@opennextjs/aws/build/helper.js";
import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
import type { Plugin } from "esbuild";

/**
* `react-dom/server.edge` requires:
* - `react-dom-server.edge.production.js`
* - `react-dom-server.browser.production.js`
* - `react-dom-server-legacy.browser.production.js`
*
* However only the first one is needed in the Cloudflare Workers environment.
* The other two can be shimmed to an empty module to reduce the bundle size.
*
* @param options Build options
* @returns An ESBuild plugin that shims unnecessary React modules
*/
export function shimReact(options: BuildOptions): Plugin {
const emptyShimPath = join(options.outputDir, "cloudflare-templates/shims/empty.js");
return {
name: "react-shim",
setup(build) {
build.onResolve(
{
filter: getCrossPlatformPathRegex(
String.raw`(react-dom-server\.browser\.production\.js|react-dom-server-legacy\.browser\.production\.js)$`,
{ escape: false }
),
},
() => ({
path: emptyShimPath,
})
);
},
};
}
Loading