Skip to content

Commit eb59d5f

Browse files
authored
feat(react-router): don't bundle react-router in react-router/dom export (#13497)
1 parent 50a9f32 commit eb59d5f

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

.changeset/breezy-pets-hug.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
Don't bundle `react-router` in `react-router/dom` CJS export
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as React from "react";
2+
3+
import { render, screen } from "@testing-library/react";
4+
import { createMemoryRouter, useParams } from "react-router";
5+
import { RouterProvider } from "react-router/dom";
6+
7+
describe("react-router/dom", () => {
8+
function ShowParams() {
9+
return <pre data-testid="params">{JSON.stringify(useParams())}</pre>;
10+
}
11+
12+
describe("Does not bundle react-router causing duplicate context issues", () => {
13+
it("with route provider shows the url params", async () => {
14+
const router = createMemoryRouter(
15+
[
16+
{
17+
path: "/blog/:slug",
18+
element: <ShowParams />,
19+
},
20+
],
21+
{
22+
initialEntries: ["/blog/react-router"],
23+
}
24+
);
25+
26+
// When react-router was bundled in CJS scenarios, this `react-router/dom`
27+
// version of `RouterProvider` caused duplicate contexts and we would not
28+
// find the param values
29+
render(<RouterProvider router={router} />);
30+
31+
expect(await screen.findByTestId("params")).toMatchInlineSnapshot(`
32+
<pre
33+
data-testid="params"
34+
>
35+
{"slug":"react-router"}
36+
</pre>
37+
`);
38+
});
39+
});
40+
});

packages/react-router/tsup.config.ts

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const config = (enableDevWarnings: boolean) =>
1313
clean: false,
1414
entry,
1515
format: ["cjs"],
16+
// Don't bundle `react-router` in sub-exports (i.e., `react-router/dom`)
17+
external: ["react-router"],
1618
outDir: enableDevWarnings ? "dist/development" : "dist/production",
1719
dts: true,
1820
banner: {
@@ -28,6 +30,10 @@ const config = (enableDevWarnings: boolean) =>
2830
clean: false,
2931
entry,
3032
format: ["esm"],
33+
// We don't do the external thing for `react-router` here because it
34+
// doesn't get bundled by default in the ESM build, and when we tried it
35+
// in https://github.com/remix-run/react-router/pull/13497 it changed up
36+
// some chunk creation that we didn't want to risk having any side effects
3137
outDir: enableDevWarnings ? "dist/development" : "dist/production",
3238
dts: true,
3339
banner: {

0 commit comments

Comments
 (0)