Skip to content

feat(react-router): don't bundle react-router in react-router/dom export #13497

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

Merged
merged 8 commits into from
May 1, 2025
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
5 changes: 5 additions & 0 deletions .changeset/breezy-pets-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

Don't bundle `react-router` in `react-router/dom` CJS export
40 changes: 40 additions & 0 deletions packages/react-router/__tests__/dom/dom-export-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as React from "react";

import { render, screen } from "@testing-library/react";
import { createMemoryRouter, useParams } from "react-router";
import { RouterProvider } from "react-router/dom";

describe("react-router/dom", () => {
function ShowParams() {
return <pre data-testid="params">{JSON.stringify(useParams())}</pre>;
}

describe("Does not bundle react-router causing duplicate context issues", () => {
it("with route provider shows the url params", async () => {
const router = createMemoryRouter(
[
{
path: "/blog/:slug",
element: <ShowParams />,
},
],
{
initialEntries: ["/blog/react-router"],
}
);

// When react-router was bundled in CJS scenarios, this `react-router/dom`
// version of `RouterProvider` caused duplicate contexts and we would not
// find the param values
render(<RouterProvider router={router} />);

expect(await screen.findByTestId("params")).toMatchInlineSnapshot(`
<pre
data-testid="params"
>
{"slug":"react-router"}
</pre>
`);
});
});
});
6 changes: 6 additions & 0 deletions packages/react-router/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const config = (enableDevWarnings: boolean) =>
clean: false,
entry,
format: ["cjs"],
// Don't bundle `react-router` in sub-exports (i.e., `react-router/dom`)
external: ["react-router"],
outDir: enableDevWarnings ? "dist/development" : "dist/production",
dts: true,
banner: {
Expand All @@ -28,6 +30,10 @@ const config = (enableDevWarnings: boolean) =>
clean: false,
entry,
format: ["esm"],
// We don't do the external thing for `react-router` here because it
// doesn't get bundled by default in the ESM build, and when we tried it
// in https://github.com/remix-run/react-router/pull/13497 it changed up
// some chunk creation that we didn't want to risk having any side effects
outDir: enableDevWarnings ? "dist/development" : "dist/production",
dts: true,
banner: {
Expand Down