Skip to content

Commit c37c53c

Browse files
Infer Vite Environment API usage in RSC Framework Mode (#14276)
1 parent 35069d5 commit c37c53c

File tree

11 files changed

+38
-56
lines changed

11 files changed

+38
-56
lines changed

integration/action-test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from "./helpers/create-fixture.js";
88
import type { Fixture, AppFixture } from "./helpers/create-fixture.js";
99
import { PlaywrightFixture, selectHtml } from "./helpers/playwright-fixture.js";
10-
import { type TemplateName, reactRouterConfig } from "./helpers/vite.js";
10+
import { type TemplateName } from "./helpers/vite.js";
1111

1212
const templateNames = [
1313
"vite-5-template",
@@ -31,9 +31,6 @@ test.describe("actions", () => {
3131
fixture = await createFixture({
3232
templateName,
3333
files: {
34-
"react-router.config.ts": reactRouterConfig({
35-
viteEnvironmentApi: templateName.includes("rsc"),
36-
}),
3734
"app/routes/urlencoded.tsx": js`
3835
import { Form, useActionData } from "react-router";
3936

integration/catch-boundary-data-test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from "./helpers/create-fixture.js";
88
import type { Fixture, AppFixture } from "./helpers/create-fixture.js";
99
import { PlaywrightFixture } from "./helpers/playwright-fixture.js";
10-
import { reactRouterConfig, type TemplateName } from "./helpers/vite.js";
10+
import { type TemplateName } from "./helpers/vite.js";
1111

1212
const templateNames = [
1313
"vite-5-template",
@@ -48,9 +48,6 @@ test.describe("ErrorBoundary (thrown responses)", () => {
4848
fixture = await createFixture({
4949
templateName,
5050
files: {
51-
"react-router.config.ts": reactRouterConfig({
52-
viteEnvironmentApi: templateName.includes("rsc"),
53-
}),
5451
"app/root.tsx": js`
5552
import {
5653
Links,

integration/client-data-test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ test.describe("Client Data", () => {
150150
files: {
151151
"react-router.config.ts": reactRouterConfig({
152152
splitRouteModules,
153-
viteEnvironmentApi: templateName.includes("rsc"),
154153
}),
155154
"app/root.tsx": js`
156155
import { Form, Outlet, Scripts } from "react-router"

integration/deduped-route-modules-test.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ import { test, expect } from "@playwright/test";
33
import { createFixture, createAppFixture } from "./helpers/create-fixture.js";
44
import type { Fixture, AppFixture } from "./helpers/create-fixture.js";
55
import { PlaywrightFixture } from "./helpers/playwright-fixture.js";
6-
import {
7-
type TemplateName,
8-
reactRouterConfig,
9-
viteConfig,
10-
} from "./helpers/vite.js";
6+
import { type TemplateName, viteConfig } from "./helpers/vite.js";
117

128
const templateNames = [
139
"vite-5-template",
@@ -30,9 +26,6 @@ test.describe("Deduped route modules", () => {
3026
"vite.config.js": await viteConfig.basic({
3127
templateName,
3228
}),
33-
"react-router.config.ts": reactRouterConfig({
34-
viteEnvironmentApi: templateName.includes("rsc"),
35-
}),
3629
"app/routes/client-first.a.tsx": `
3730
import { Link } from "react-router";
3831

integration/link-test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
createAppFixture,
88
} from "./helpers/create-fixture.js";
99
import type { Fixture, AppFixture } from "./helpers/create-fixture.js";
10-
import { reactRouterConfig, type TemplateName } from "./helpers/vite.js";
10+
import { type TemplateName } from "./helpers/vite.js";
1111
import { PlaywrightFixture } from "./helpers/playwright-fixture.js";
1212

1313
const templateNames = [
@@ -42,10 +42,6 @@ test.describe("route module link export", () => {
4242
fixture = await createFixture({
4343
templateName,
4444
files: {
45-
"react-router.config.ts": reactRouterConfig({
46-
viteEnvironmentApi: templateName.includes("rsc"),
47-
}),
48-
4945
"app/favicon.ico": js``,
5046

5147
"app/guitar.jpg": js``,

integration/mdx-test.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ import {
77
} from "./helpers/create-fixture.js";
88
import type { Fixture, AppFixture } from "./helpers/create-fixture.js";
99
import { PlaywrightFixture } from "./helpers/playwright-fixture.js";
10-
import {
11-
type TemplateName,
12-
reactRouterConfig,
13-
viteConfig,
14-
} from "./helpers/vite.js";
10+
import { type TemplateName, viteConfig } from "./helpers/vite.js";
1511

1612
const templateNames = [
1713
"vite-5-template",
@@ -32,9 +28,6 @@ test.describe("MDX", () => {
3228
templateName,
3329
mdx: true,
3430
}),
35-
"react-router.config.ts": reactRouterConfig({
36-
viteEnvironmentApi: templateName.includes("rsc"),
37-
}),
3831
"app/root.tsx": js`
3932
import { Outlet, Scripts } from "react-router"
4033

integration/vite-basename-test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ async function configFiles({
7676
}) {
7777
return {
7878
"react-router.config.ts": reactRouterConfig({
79-
viteEnvironmentApi: templateName.includes("rsc"),
8079
basename: basename !== "/" ? basename : undefined,
8180
}),
8281
"vite.config.ts": await viteConfig.basic({

integration/vite-hmr-hdr-rsc-test.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,14 @@ import path from "node:path";
33
import { expect } from "@playwright/test";
44

55
import type { Files, TemplateName } from "./helpers/vite.js";
6-
import {
7-
test,
8-
createEditor,
9-
viteConfig,
10-
reactRouterConfig,
11-
} from "./helpers/vite.js";
6+
import { test, createEditor, viteConfig } from "./helpers/vite.js";
127

138
const templateName = "rsc-vite-framework" as const satisfies TemplateName;
149

1510
test.describe("Vite HMR & HDR (RSC)", () => {
1611
test("vite dev", async ({ page, dev }) => {
1712
let files: Files = async ({ port }) => ({
1813
"vite.config.js": await viteConfig.basic({ port, templateName }),
19-
"react-router.config.ts": reactRouterConfig({
20-
viteEnvironmentApi: templateName.includes("rsc"),
21-
}),
2214
"app/routes/hmr/route.tsx": `
2315
// imports
2416
import { Mounted } from "./route.client";

integration/vite-hmr-hdr-test.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
EXPRESS_SERVER,
1111
viteConfig,
1212
viteMajorTemplates,
13-
reactRouterConfig,
1413
} from "./helpers/vite.js";
1514

1615
const templates = [
@@ -57,9 +56,6 @@ test.describe("Vite HMR & HDR", () => {
5756
test("vite dev", async ({ page, browserName, dev }) => {
5857
let files: Files = async ({ port }) => ({
5958
"vite.config.js": await viteConfig.basic({ port, templateName }),
60-
"react-router.config.ts": reactRouterConfig({
61-
viteEnvironmentApi: templateName.includes("rsc"),
62-
}),
6359
"app/routes/_index.tsx": indexRoute,
6460
});
6561
let { cwd, port } = await dev(files, templateName);
@@ -70,9 +66,6 @@ test.describe("Vite HMR & HDR", () => {
7066
test.skip(templateName.includes("rsc"), "RSC is not supported");
7167
let files: Files = async ({ port }) => ({
7268
"vite.config.js": await viteConfig.basic({ port, templateName }),
73-
"react-router.config.ts": reactRouterConfig({
74-
viteEnvironmentApi: templateName.includes("rsc"),
75-
}),
7669
"server.mjs": EXPRESS_SERVER({ port }),
7770
"app/routes/_index.tsx": indexRoute,
7871
});

packages/react-router-dev/vite/build.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,19 @@ export async function build(root: string, viteBuildOptions: ViteBuildOptions) {
4848
}
4949

5050
let config = configResult.value;
51-
let unstable_viteEnvironmentApi = config.future.unstable_viteEnvironmentApi;
52-
let viteMajor = parseInt(vite.version.split(".")[0], 10);
5351

54-
if (unstable_viteEnvironmentApi && viteMajor === 5) {
52+
let viteMajor = parseInt(vite.version.split(".")[0], 10);
53+
if (config.future.unstable_viteEnvironmentApi && viteMajor === 5) {
5554
throw new Error(
5655
"The future.unstable_viteEnvironmentApi option is not supported in Vite 5",
5756
);
5857
}
5958

60-
return await (unstable_viteEnvironmentApi
59+
const useViteEnvironmentApi =
60+
config.future.unstable_viteEnvironmentApi ||
61+
(await hasReactRouterRscPlugin({ root, viteBuildOptions }));
62+
63+
return await (useViteEnvironmentApi
6164
? viteAppBuild(root, viteBuildOptions)
6265
: viteBuild(root, viteBuildOptions));
6366
}
@@ -231,3 +234,27 @@ async function viteBuild(
231234
viteConfig,
232235
});
233236
}
237+
238+
async function hasReactRouterRscPlugin({
239+
root,
240+
viteBuildOptions: { config, logLevel, mode },
241+
}: {
242+
root: string;
243+
viteBuildOptions: ViteBuildOptions;
244+
}): Promise<boolean> {
245+
const vite = await import("vite");
246+
const viteConfig = await vite.resolveConfig(
247+
{
248+
configFile: config,
249+
logLevel,
250+
mode: mode ?? "production",
251+
root,
252+
},
253+
"build", // command
254+
"production", // default mode
255+
"production", // default NODE_ENV
256+
);
257+
return viteConfig.plugins.some(
258+
(plugin) => plugin?.name === "react-router/rsc",
259+
);
260+
}

0 commit comments

Comments
 (0)