Skip to content

Commit 4abec7a

Browse files
authored
Remove Vite server hooks in child compiler plugins (#13184)
1 parent 9f6a507 commit 4abec7a

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

.changeset/eleven-oranges-cheat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@react-router/dev": patch
3+
---
4+
5+
Fix conflicts with other Vite plugins that use the `configureServer` and/or `configurePreviewServer` hooks

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,10 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
13841384
}
13851385
}
13861386

1387+
const childCompilerPlugins = await asyncFlatten(
1388+
childCompilerConfigFile.config.plugins ?? []
1389+
);
1390+
13871391
viteChildCompiler = await vite.createServer({
13881392
...viteUserConfig,
13891393
// Ensure child compiler cannot overwrite the default cache directory
@@ -1397,8 +1401,7 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
13971401
configFile: false,
13981402
envFile: false,
13991403
plugins: [
1400-
...(childCompilerConfigFile.config.plugins ?? [])
1401-
.flat()
1404+
childCompilerPlugins
14021405
// Exclude this plugin from the child compiler to prevent an
14031406
// infinite loop (plugin creates a child compiler with the same
14041407
// plugin that creates another child compiler, repeat ad
@@ -1407,14 +1410,20 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
14071410
// production build because the child compiler is a Vite dev
14081411
// server and will generate incorrect manifests.
14091412
.filter(
1410-
(plugin) =>
1413+
(plugin): plugin is Vite.Plugin =>
14111414
typeof plugin === "object" &&
14121415
plugin !== null &&
14131416
"name" in plugin &&
14141417
plugin.name !== "react-router" &&
14151418
plugin.name !== "react-router:route-exports" &&
14161419
plugin.name !== "react-router:hmr-updates"
1417-
),
1420+
)
1421+
// Remove server hooks to avoid conflicts with main dev server
1422+
.map((plugin) => ({
1423+
...plugin,
1424+
configureServer: undefined,
1425+
configurePreviewServer: undefined,
1426+
})),
14181427
{
14191428
name: "react-router:override-optimize-deps",
14201429
config(userConfig) {
@@ -3509,3 +3518,17 @@ async function getEnvironmentsOptions(
35093518
function isNonNullable<T>(x: T): x is NonNullable<T> {
35103519
return x != null;
35113520
}
3521+
3522+
// Type and function copied from Vite
3523+
type AsyncFlatten<T extends unknown[]> = T extends (infer U)[]
3524+
? Exclude<Awaited<U>, U[]>[]
3525+
: never;
3526+
3527+
async function asyncFlatten<T extends unknown[]>(
3528+
arr: T
3529+
): Promise<AsyncFlatten<T>> {
3530+
do {
3531+
arr = (await Promise.all(arr)).flat(Infinity) as any;
3532+
} while (arr.some((v: any) => v?.then));
3533+
return arr as unknown[] as AsyncFlatten<T>;
3534+
}

0 commit comments

Comments
 (0)