Repository: remix-run/react-router
File: packages/react-router-dev/vite/plugin.ts (TypeScript)

Code before:
        let route = routes[key]!;
        if (isSpaMode && key !== "root") {
          // In SPA mode, we only pre-render to the root route and it's `HydrateFallback`.
          // Therefore, we can stub all other routes with an empty module as they
          // (and their deps) may not be compatible with server-side rendering.
          // This also helps keep the build fast.
          return `const route${index} = { default: () => null };`;
        } else {
          return `import * as route${index} from ${JSON.stringify(
            resolveFileUrl(
              ctx,
              resolveRelativeRouteFilePath(route, ctx.reactRouterConfig)
            )
          )};`;
        }
      })
      .join("\n")}
      export { default as assets } from ${JSON.stringify(
        `${virtual.serverManifest.id}${
          routeIds ? `?route-ids=${routeIds.join(",")}` : ""
        }`
      )};
      export const assetsBuildDirectory = ${JSON.stringify(
        path.relative(
          ctx.rootDirectory,
          getClientBuildDirectory(ctx.reactRouterConfig)
        )
      )};
      export const basename = ${JSON.stringify(ctx.reactRouterConfig.basename)};
      export const future = ${JSON.stringify(ctx.reactRouterConfig.future)};
      export const ssr = ${ctx.reactRouterConfig.ssr};
      export const isSpaMode = ${isSpaMode};
      export const prerender = ${JSON.stringify(prerenderPaths)};
      export const publicPath = ${JSON.stringify(ctx.publicPath)};
      export const entry = { module: entryServer };
      export const routes = {
        ${Object.keys(routes)
          .map((key, index) => {
            let route = routes[key]!;
            return `${JSON.stringify(key)}: {
          id: ${JSON.stringify(route.id)},
          parentId: ${JSON.stringify(route.parentId)},
          path: ${JSON.stringify(route.path)},
          index: ${JSON.stringify(route.index)},
          caseSensitive: ${JSON.stringify(route.caseSensitive)},
          module: route${index}
        }`;
          })
          .join(",\n  ")}
      };`;
  };

Review comment (refactor):
We can simplify this a bit - we don't even need to include the index route or any layouts below root - we only render the root route and it's `HydrateFallback` so that any application paths can hydrate using the same file (see https://github.com/remix-run/react-router/blob/fbea1eaa754522f8be0bcc5073df6bc2d9c34948/packages/react-router/lib/dom/ssr/components.tsx#L193)

Code after:
  let getServerEntry = async ({ routeIds }: { routeIds?: Array<string> }) => {
    invariant(viteConfig, "viteconfig required to generate the server entry");

    let routes = routeIds
      ? // For server bundle builds, the server build should only import the
        // routes for this bundle rather than importing all routes
        pick(ctx.reactRouterConfig.routes, routeIds)
      : // Otherwise, all routes are imported as usual
        ctx.reactRouterConfig.routes;

    let prerenderPaths = await getPrerenderPaths(
      ctx.reactRouterConfig.prerender,
      ctx.reactRouterConfig.ssr,
      routes
    );

    let isSpaMode = isSpaModeEnabled(ctx.reactRouterConfig);

    return `
    import * as entryServer from ${JSON.stringify(
      resolveFileUrl(ctx, ctx.entryServerFilePath)
    )};
    ${Object.keys(routes)
      .map((key, index) => {
        let route = routes[key]!;
        if (isSpaMode && key !== "root") {
          // In SPA mode, we only pre-render the root route and its `HydrateFallback`.
          // Therefore, we can stub all other routes with an empty module as they
          // (and their deps) may not be compatible with server-side rendering.
          // This also helps keep the build fast.
          return `const route${index} = { default: () => null };`;
        } else {
          return `import * as route${index} from ${JSON.stringify(
            resolveFileUrl(
              ctx,
              resolveRelativeRouteFilePath(route, ctx.reactRouterConfig)
            )
          )};`;
        }
      })
      .join("\n")}
      export { default as assets } from ${JSON.stringify(
        `${virtual.serverManifest.id}${
          routeIds ? `?route-ids=${routeIds.join(",")}` : ""
        }`
      )};
      export const assetsBuildDirectory = ${JSON.stringify(
        path.relative(
          ctx.rootDirectory,
          getClientBuildDirectory(ctx.reactRouterConfig)
        )