Skip to content

Commit

Permalink
Only generate types for route modules in a projects app directory
Browse files Browse the repository at this point in the history
  • Loading branch information
wilcoxmd committed Feb 11, 2025
1 parent 6b6b437 commit 635c44c
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions packages/react-router-dev/typegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createConfigLoader } from "../config/config";
import { generate } from "./generate";
import type { Context } from "./context";
import { getTypesDir, getTypesPath } from "./paths";
import type { RouteManifestEntry } from "../config/routes";

export async function run(rootDirectory: string) {
const ctx = await createContext({ rootDirectory, watch: false });
Expand Down Expand Up @@ -71,14 +72,27 @@ async function createContext({
};
}

function isRouteInAppDirectory(ctx: Context, route: RouteManifestEntry) {
const absoluteRoutePath = Path.isAbsolute(route.file)
? route.file
: Path.resolve(ctx.config.appDirectory, route.file);

return absoluteRoutePath.startsWith(ctx.config.appDirectory);
}

async function writeAll(ctx: Context): Promise<void> {
const typegenDir = getTypesDir(ctx);

fs.rmSync(typegenDir, { recursive: true, force: true });
Object.values(ctx.config.routes).forEach((route) => {
const typesPath = getTypesPath(ctx, route);
const content = generate(ctx, route);
fs.mkdirSync(Path.dirname(typesPath), { recursive: true });
fs.writeFileSync(typesPath, content);
});
Object.values(ctx.config.routes)
.filter((route) => {
// Only generate types for routes in the app directory
return isRouteInAppDirectory(ctx, route);
})
.forEach((route) => {
const typesPath = getTypesPath(ctx, route);
const content = generate(ctx, route);
fs.mkdirSync(Path.dirname(typesPath), { recursive: true });
fs.writeFileSync(typesPath, content);
});
}

0 comments on commit 635c44c

Please sign in to comment.